Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/limatmpl/abs_test.go
2611 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package limatmpl
5
6
import (
7
"os"
8
"path/filepath"
9
"runtime"
10
"strings"
11
"testing"
12
13
"gotest.tools/v3/assert"
14
)
15
16
type useAbsLocatorsTestCase struct {
17
description string
18
locator string
19
template string
20
expected string
21
}
22
23
var useAbsLocatorsTestCases = []useAbsLocatorsTestCase{
24
{
25
"Template without base or script file",
26
"template:foo",
27
`arch: aarch64`,
28
`arch: aarch64`,
29
},
30
{
31
"Single string base template",
32
"template:foo",
33
`base: bar.yaml`,
34
`base: template:bar.yaml`,
35
},
36
{
37
"Legacy template:// base template",
38
"template://foo",
39
`base: bar.yaml`,
40
`base: template:bar.yaml`,
41
},
42
{
43
"Flow style array of one base template",
44
"template:foo",
45
`base: [{url: bar.yaml, digest: deadbeef}]`,
46
// not sure why the quotes around the URL were added; maybe because we don't copy the style from the source
47
`base: [{url: 'template:bar.yaml', digest: deadbeef}]`,
48
},
49
{
50
"Flow style array of sequence of two base URLs",
51
"template:foo",
52
`base: [bar.yaml, baz.yaml]`,
53
`base: ['template:bar.yaml', 'template:baz.yaml']`,
54
},
55
{
56
"Flow style array of sequence of two base locator objects",
57
"template:foo",
58
`base: [{url: bar.yaml, digest: deadbeef}, {url: baz.yaml, digest: decafbad}]`,
59
`base: [{url: 'template:bar.yaml', digest: deadbeef}, {url: 'template:baz.yaml', digest: decafbad}]`,
60
},
61
{
62
"Block style array of one base template",
63
"template:foo",
64
`
65
base:
66
- bar.yaml
67
`,
68
`
69
base:
70
- template:bar.yaml`,
71
},
72
{
73
"Block style of four base templates",
74
"template:foo",
75
`
76
base:
77
- bar.yaml
78
- template:my
79
- https://example.com/my.yaml
80
- baz.yaml
81
`,
82
`
83
base:
84
- template:bar.yaml
85
- template:my
86
- https://example.com/my.yaml
87
- template:baz.yaml
88
`,
89
},
90
{
91
"Provisioning and probe scripts",
92
"template:experimental/foo",
93
`
94
provision:
95
- mode: user
96
file: userscript.sh
97
- mode: system
98
file:
99
url: systemscript.sh
100
digest: abc123
101
probes:
102
- file: probe.sh
103
- file:
104
url: probe.sh
105
digest: digest
106
`,
107
`
108
provision:
109
- mode: user
110
file: template:experimental/userscript.sh
111
- mode: system
112
file:
113
url: template:experimental/systemscript.sh
114
digest: abc123
115
probes:
116
- file: template:experimental/probe.sh
117
- file:
118
url: template:experimental/probe.sh
119
digest: digest
120
`,
121
},
122
}
123
124
func TestUseAbsLocators(t *testing.T) {
125
for _, tc := range useAbsLocatorsTestCases {
126
t.Run(tc.description, func(t *testing.T) { RunUseAbsLocatorTest(t, tc) })
127
}
128
}
129
130
func RunUseAbsLocatorTest(t *testing.T, tc useAbsLocatorsTestCase) {
131
tmpl := &Template{
132
Bytes: []byte(strings.TrimSpace(tc.template)),
133
Locator: tc.locator,
134
}
135
err := tmpl.UseAbsLocators()
136
assert.NilError(t, err, tc.description)
137
138
actual := strings.TrimSpace(string(tmpl.Bytes))
139
expected := strings.TrimSpace(tc.expected)
140
assert.Equal(t, actual, expected, tc.description)
141
}
142
143
func TestBasePath(t *testing.T) {
144
// On Windows the root will be something like "C:\"
145
root, err := filepath.Abs("/")
146
assert.NilError(t, err)
147
volume := filepath.VolumeName(root)
148
149
t.Run("", func(t *testing.T) {
150
actual, err := basePath("/foo")
151
assert.NilError(t, err)
152
assert.Equal(t, actual, root)
153
})
154
155
t.Run("", func(t *testing.T) {
156
actual, err := basePath("/foo/bar")
157
assert.NilError(t, err)
158
assert.Equal(t, actual, filepath.Clean(volume+"/foo"))
159
})
160
161
t.Run("", func(t *testing.T) {
162
actual, err := basePath("template:foo")
163
assert.NilError(t, err)
164
assert.Equal(t, actual, "template:")
165
})
166
167
t.Run("", func(t *testing.T) {
168
actual, err := basePath("template:foo/bar")
169
assert.NilError(t, err)
170
assert.Equal(t, actual, "template:foo")
171
})
172
173
t.Run("", func(t *testing.T) {
174
actual, err := basePath("http://host/foo")
175
assert.NilError(t, err)
176
assert.Equal(t, actual, "http://host")
177
})
178
179
t.Run("", func(t *testing.T) {
180
actual, err := basePath("http://host/foo/bar")
181
assert.NilError(t, err)
182
assert.Equal(t, actual, "http://host/foo")
183
})
184
185
t.Run("", func(t *testing.T) {
186
actual, err := basePath("file:///foo")
187
assert.NilError(t, err)
188
assert.Equal(t, actual, "file:///")
189
})
190
191
t.Run("", func(t *testing.T) {
192
actual, err := basePath("file:///foo/bar")
193
assert.NilError(t, err)
194
assert.Equal(t, actual, "file:///foo")
195
})
196
}
197
198
func TestAbsPath(t *testing.T) {
199
root, err := filepath.Abs("/")
200
assert.NilError(t, err)
201
volume := filepath.VolumeName(root)
202
203
t.Run("If the locator is already an absolute path, it is returned unchanged", func(t *testing.T) {
204
actual, err := absPath(volume+"/foo", volume+"/root")
205
assert.NilError(t, err)
206
assert.Equal(t, actual, filepath.Clean(volume+"/foo"))
207
})
208
209
t.Run("If the locator is a rooted path without volume name, then the volume will be added", func(t *testing.T) {
210
actual, err := absPath("/foo", volume+"/root")
211
assert.NilError(t, err)
212
assert.Equal(t, actual, filepath.Clean(volume+"/foo"))
213
})
214
215
t.Run("If the locator starts with ~/, then it will be expanded to an absolute path", func(t *testing.T) {
216
actual, err := absPath("~/foo", volume+"/root")
217
assert.NilError(t, err)
218
homeDir, err := os.UserHomeDir()
219
assert.NilError(t, err)
220
// homeDir already includes the volume
221
assert.Equal(t, actual, filepath.Join(homeDir, "foo"))
222
})
223
224
t.Run("", func(t *testing.T) {
225
actual, err := absPath("template:foo", volume+"/root")
226
assert.NilError(t, err)
227
assert.Equal(t, actual, "template:foo")
228
})
229
230
t.Run("", func(t *testing.T) {
231
actual, err := absPath("http://host/foo", volume+"/root")
232
assert.NilError(t, err)
233
assert.Equal(t, actual, "http://host/foo")
234
})
235
236
t.Run("", func(t *testing.T) {
237
actual, err := absPath("file:///foo", volume+"/root")
238
assert.NilError(t, err)
239
assert.Equal(t, actual, "file:///foo")
240
})
241
242
t.Run("Can't have relative path when reading from STDIN", func(t *testing.T) {
243
_, err = absPath("foo", "-")
244
assert.ErrorContains(t, err, "STDIN")
245
})
246
247
t.Run("Relative paths must be underneath the basePath", func(t *testing.T) {
248
_, err = absPath("../foo", volume+"/root")
249
assert.ErrorContains(t, err, "'../'")
250
})
251
252
t.Run("locator must not be empty", func(t *testing.T) {
253
_, err = absPath("", "foo")
254
assert.ErrorContains(t, err, "locator is empty")
255
})
256
257
t.Run("basePath must not be empty", func(t *testing.T) {
258
_, err = absPath("foo", "")
259
assert.ErrorContains(t, err, "basePath is empty")
260
})
261
262
t.Run("", func(t *testing.T) {
263
_, err = absPath("./foo", "")
264
assert.ErrorContains(t, err, "empty")
265
})
266
267
t.Run("", func(t *testing.T) {
268
actual, err := absPath("./foo", volume+"/root")
269
assert.NilError(t, err)
270
assert.Equal(t, actual, filepath.Clean(volume+"/root/foo"))
271
})
272
273
if runtime.GOOS == "windows" {
274
t.Run("Relative locators must not include volume names", func(t *testing.T) {
275
_, err := absPath(volume+"foo", volume+"/root")
276
assert.ErrorContains(t, err, "volume")
277
})
278
}
279
280
t.Run("", func(t *testing.T) {
281
actual, err := absPath("foo", "template:")
282
assert.NilError(t, err)
283
assert.Equal(t, actual, "template:foo")
284
})
285
286
t.Run("", func(t *testing.T) {
287
actual, err := absPath("bar", "template:foo")
288
assert.NilError(t, err)
289
assert.Equal(t, actual, "template:foo/bar")
290
})
291
292
t.Run("", func(t *testing.T) {
293
actual, err := absPath("foo", "http://host")
294
assert.NilError(t, err)
295
assert.Equal(t, actual, "http://host/foo")
296
})
297
298
t.Run("", func(t *testing.T) {
299
actual, err := absPath("bar", "http://host/foo")
300
assert.NilError(t, err)
301
assert.Equal(t, actual, "http://host/foo/bar")
302
})
303
304
t.Run("", func(t *testing.T) {
305
actual, err := absPath("foo", "file:///")
306
assert.NilError(t, err)
307
assert.Equal(t, actual, "file:///foo")
308
})
309
310
t.Run("", func(t *testing.T) {
311
actual, err := absPath("bar", "file:///foo")
312
assert.NilError(t, err)
313
assert.Equal(t, actual, "file:///foo/bar")
314
})
315
}
316
317