-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathhg_test.go
154 lines (127 loc) · 3.93 KB
/
hg_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package e2e
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"`
}
func loadHgAssetInfo(t *testing.T, filename string) HgAssetInfo {
t.Helper()
f, err := os.Open(filename)
require.NoError(t, err, "could not open file %s", filename)
var info HgAssetInfo
require.NoError(t, json.NewDecoder(f).Decode(&info), "could not parse file %s", filename)
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")
require.NoError(t, err)
defer os.RemoveAll(hgSrcPath)
out, err := exec.Command("tar", "xzvf", "assets/hg-repos/asset.tgz", "-C", hgSrcPath).CombinedOutput()
require.NoError(t, err, "Unpacking hg-repos asset, output: '%s'", 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)
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 VendirOutput
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 VendirOutput
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()
require.NoError(t, err, "Unpacking hg-repos asset, output: '%s'", 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 VendirOutput
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))
})
}