Skip to content

Commit

Permalink
hg-cache: add a e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: Christophe de Vienne <[email protected]>
  • Loading branch information
cdevienne committed May 7, 2024
1 parent 5281e64 commit 3d1ecf9
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/e2e/assets/hg-repos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
``build.sh`` assemble a mercurial repository and more, suitable for testing the hg
fetcher cache feature:

- A base repository (repo)
- A bundle with an extra changeset
- A json file with the changeset ids we need in the test, and the bundle filename
Binary file added test/e2e/assets/hg-repos/asset.tgz
Binary file not shown.
44 changes: 44 additions & 0 deletions test/e2e/assets/hg-repos/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

rm -rf build

mkdir build
cd build

mkdir repo
cd repo

hg init .

echo "content1" > file1.txt

hg add file1.txt
hg commit -m "Added file1"
CSET1_ID=$(hg id --id)

hg tag first-tag
hg phase -p

hg topic "wip"
echo "content2" > file1.txt
hg commit -m "extra cset"
CSETX_ID=$(hg id --id)

hg strip -r .

BUNDLE=$(basename .hg/strip-backup/*-backup.hg)
mv .hg/strip-backup/$BUNDLE ..

hg checkout 00000

cd ..

cat > info.json <<EOF
{
"initial-changeset": "$CSET1_ID",
"extra-bundle": "$BUNDLE",
"extra-changeset": "$CSETX_ID"
}
EOF

tar caf ../asset.tgz .
167 changes: 167 additions & 0 deletions test/e2e/hg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package e2e

Check failure on line 1 in test/e2e/hg_test.go

View workflow job for this annotation

GitHub Actions / lint

Missed header for check (goheader)

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type HgAssetInfo struct {
InitialChangeset string `json:"initial-changeset"`
ExtraBundle string `json:"extra-bundle"`
ExtraChangeset string `json:"extra-changeset"`
}

type hgVendirOutput struct {
Tables string
Blocks []string
Lines []string
}

func loadHgAssetInfo(t *testing.T, filename string) HgAssetInfo {
t.Helper()
f, err := os.Open(filename)
if err != nil {
t.Fatalf("could not open file %s: %s", filename, err)
}
var info HgAssetInfo
if err := json.NewDecoder(f).Decode(&info); err != nil {
t.Fatalf("could not parse file %s: %s", filename, err)
}
return info
}

func TestHgCache(t *testing.T) {
env := BuildEnv(t)
logger := Logger{}
vendir := Vendir{t, env.BinaryPath, logger}

_ = Vendir{t, env.BinaryPath, logger}

hgSrcPath, err := os.MkdirTemp("", "vendir-e2e-hg-cache")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(hgSrcPath)

out, err := exec.Command("tar", "xzvf", "assets/hg-repos/asset.tgz", "-C", hgSrcPath).CombinedOutput()
if err != nil {
t.Fatalf("Unpacking hg-repos asset: %s (output: '%s')", err, out)
}

info := loadHgAssetInfo(t, path.Join(hgSrcPath, "info.json"))

yamlConfig := func(ref string) io.Reader {
return strings.NewReader(fmt.Sprintf(`
apiVersion: vendir.k14s.io/v1alpha1
kind: Config
directories:
- path: vendor
contents:
- path: test
hg:
url: "%s/repo"
ref: "%s"
`, hgSrcPath, ref))
}

cachePath, err := os.MkdirTemp("", "vendir-e2e-hg-cache-vendir-cache")
require.NoError(t, err)
defer os.RemoveAll(cachePath)

dstPath, err := os.MkdirTemp("", "vendir-e2e-hg-cache-dst")
require.NoError(t, err)
defer os.RemoveAll(dstPath)

var stdout bytes.Buffer
stdoutDec := json.NewDecoder(&stdout)
//defer os.RemoveAll(dstPath)
logger.Section("initial clone", func() {
stdout.Truncate(0)
vendir.RunWithOpts(
[]string{"sync", "-f", "-", "--json"},
RunOpts{
Dir: dstPath,
StdinReader: yamlConfig(info.InitialChangeset),
StdoutWriter: &stdout,
Env: []string{"VENDIR_CACHE_DIR=" + cachePath},
})

var out hgVendirOutput
require.NoError(t, stdoutDec.Decode(&out))

assert.Contains(t, out.Lines[1], "init")
assert.Contains(t, out.Lines[3], "pull")

b, err := os.ReadFile(filepath.Join(dstPath, "vendor", "test", "file1.txt"))
assert.NoError(t, err)
assert.Equal(t, "content1\n", string(b))
})

logger.Section("sync from cache only", func() {
stdout.Truncate(0)
vendir.RunWithOpts(
[]string{"sync", "-f", "-", "--json"},
RunOpts{
Dir: dstPath,
StdinReader: yamlConfig(info.InitialChangeset),
StdoutWriter: &stdout,
Env: []string{"VENDIR_CACHE_DIR=" + cachePath},
})

var out hgVendirOutput
require.NoError(t, stdoutDec.Decode(&out))

for _, line := range out.Lines {
assert.NotContains(t, line, "init")
assert.NotContains(t, line, "pull")
}

b, err := os.ReadFile(filepath.Join(dstPath, "vendor", "test", "file1.txt"))
assert.NoError(t, err)
assert.Equal(t, "content1\n", string(b))
})

out, err = exec.Command(
"hg", "unbundle",
"--repository", filepath.Join(hgSrcPath, "/repo"),
filepath.Join(hgSrcPath, info.ExtraBundle),
).CombinedOutput()
if err != nil {
t.Fatalf("Unpacking hg-repos asset: %s (output: '%s')", err, out)
}

logger.Section("sync from cache + pull", func() {
stdout.Truncate(0)
vendir.RunWithOpts(
[]string{"sync", "-f", "-", "--json"},
RunOpts{
Dir: dstPath,
StdinReader: yamlConfig(info.ExtraChangeset),
StdoutWriter: &stdout,
Env: []string{"VENDIR_CACHE_DIR=" + cachePath},
})

var out hgVendirOutput
require.NoError(t, stdoutDec.Decode(&out))

assert.Contains(t, out.Lines[3], "pull")
for _, line := range out.Lines {
assert.NotContains(t, line, "init")
}

b, err := os.ReadFile(filepath.Join(dstPath, "vendor", "test", "file1.txt"))
assert.NoError(t, err)
assert.Equal(t, "content2\n", string(b))
})
}

0 comments on commit 3d1ecf9

Please sign in to comment.