-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_test.go
324 lines (278 loc) · 7.97 KB
/
port_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package clade_test
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/lesomnus/clade"
"github.com/lesomnus/pl"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
)
func TestPortUnmarshalYAML(t *testing.T) {
t.Run(".name", func(t *testing.T) {
require := require.New(t)
var port clade.Port
err := yaml.Unmarshal([]byte("name: cr.io/repo/name"), &port)
require.NoError(err)
require.Equal("cr.io/repo/name", port.Name.String())
})
t.Run(".images", func(t *testing.T) {
t.Run("can have empty value", func(t *testing.T) {
require := require.New(t)
var port clade.Port
err := yaml.Unmarshal([]byte(`
name: cr.io/repo/name
images:
-
`), &port)
require.NoError(err)
require.Len(port.Images, 1)
require.Nil(port.Images[0])
})
t.Run("`.skip` is true if `$.skip` is true", func(t *testing.T) {
require := require.New(t)
var port clade.Port
err := yaml.Unmarshal([]byte(`
name: cr.io/repo/name
images:
- skip: false
`), &port)
require.NoError(err)
require.Len(port.Images, 1)
require.False(port.Images[0].Skip)
err = yaml.Unmarshal([]byte(`
name: cr.io/repo/name
skip: true
images:
- skip: false
`), &port)
require.NoError(err)
require.Len(port.Images, 1)
require.True(port.Images[0].Skip)
})
t.Run("`.platform` will be set by `$.platform` if it is empty", func(t *testing.T) {
require := require.New(t)
var port clade.Port
err := yaml.Unmarshal([]byte(`
name: cr.io/repo/name
images:
- platform: foo
`), &port)
require.NoError(err)
require.Len(port.Images, 1)
require.NotNil(port.Images[0].Platform)
require.Equal("foo", port.Images[0].Platform.Lhs.Var)
err = yaml.Unmarshal([]byte(`
name: cr.io/repo/name
platform: bar
images:
- name: cr.io/repo/baz
`), &port)
require.NoError(err)
require.Len(port.Images, 1)
require.Equal("bar", port.Images[0].Platform.Lhs.Var)
})
t.Run("`.args` is merged from `$.args`", func(t *testing.T) {
require := require.New(t)
var port clade.Port
err := yaml.Unmarshal([]byte(`
name: cr.io/repo/name
args:
keyA: valA
keyB: valB
images:
- args:
keyB: valFoo
keyC: valC
`), &port)
require.NoError(err)
require.Len(port.Images, 1)
require.Len(port.Images[0].Args, 3)
require.Contains(port.Images[0].Args, "keyA")
require.Contains(port.Images[0].Args, "keyB")
require.Contains(port.Images[0].Args, "keyC")
require.Equal(*(*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "valA")))), port.Images[0].Args["keyA"])
require.Equal(*(*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "valFoo")))), port.Images[0].Args["keyB"], "no overwrite")
require.Equal(*(*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "valC")))), port.Images[0].Args["keyC"])
})
})
t.Run("fails if", func(t *testing.T) {
tcs := []struct {
desc string
data string
msgs []string
}{
{
desc: "not a map",
data: "string",
msgs: []string{"string"},
},
{
desc: "name is invalid",
data: `{"name": "not_canonical"}`,
msgs: []string{"canonical"},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
require := require.New(t)
var actual clade.Port
err := yaml.Unmarshal([]byte(tc.data), &actual)
for _, msg := range tc.msgs {
require.ErrorContains(err, msg)
}
})
}
})
}
func TestDeduplicateBySemver(t *testing.T) {
type Pair struct {
lhs []string
rhs []string
}
tcs := []struct {
given Pair
expected Pair
}{
{
given: Pair{
lhs: []string{"1.0.0", "1.0", "1"},
rhs: []string{"1.0.0", "1.0", "1"},
},
expected: Pair{
lhs: []string{"1.0.0", "1.0", "1"},
rhs: []string{},
},
},
{
given: Pair{
lhs: []string{"1.0.1", "1.0", "1"},
rhs: []string{"1.0.0", "1.0", "1"},
},
expected: Pair{
lhs: []string{"1.0.1", "1.0", "1"},
rhs: []string{"1.0.0"},
},
},
{
given: Pair{
lhs: []string{"1.0.0", "1.0", "1"},
rhs: []string{"1.0.1", "1.0", "1"},
},
expected: Pair{
lhs: []string{"1.0.0"},
rhs: []string{"1.0.1", "1.0", "1"},
},
},
{
given: Pair{
lhs: []string{"1.1.1", "1.1", "1"},
rhs: []string{"1.0.2", "1.0", "1"},
},
expected: Pair{
lhs: []string{"1.1.1", "1.1", "1"},
rhs: []string{"1.0.2", "1.0"},
},
},
}
for _, tc := range tcs {
actual := Pair{
lhs: slices.Clone(tc.given.lhs),
rhs: slices.Clone(tc.given.rhs),
}
err := clade.DeduplicateBySemver(&actual.lhs, &actual.rhs)
require.NoError(t, err)
require.ElementsMatch(t, tc.expected.lhs, actual.lhs)
require.ElementsMatch(t, tc.expected.rhs, actual.rhs)
}
}
func TestReadPort(t *testing.T) {
require := require.New(t)
data := `
name: cr.io/foo/bar
args:
YEAR: 1995
images:
- tags: [a, b, c]
from: hub.io/foo/bar:baz
- tags: [inglourious]
from: hub.io/foo/bar:basterds
args:
YEAR: 2009
VILLAIN: Hans Landa
dockerfile: 35mm Nitrate Film
context: Le Gamaar cinema
`
dir := os.TempDir()
tmp, err := os.CreateTemp(dir, "")
require.NoError(err)
defer os.Remove(tmp.Name())
_, err = tmp.Write([]byte(data))
require.NoError(err)
port, err := clade.ReadPort(tmp.Name())
require.NoError(err)
require.Len(port.Images, 2)
require.Equal("hub.io/foo/bar", port.Images[0].From.Primary.String())
require.Equal((*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "baz")))), port.Images[0].From.Primary.Tag)
require.Equal("hub.io/foo/bar", port.Images[1].From.Primary.String())
require.Equal((*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "basterds")))), port.Images[1].From.Primary.Tag)
require.Equal(filepath.Join(dir, "Dockerfile"), port.Images[0].Dockerfile, "default dockerfile is {path}/Dockerfile")
require.Equal(filepath.Join(dir, "."), port.Images[0].ContextPath, "default context is {path}")
require.Contains(port.Images[0].Args, "YEAR", "root args are inherited")
require.Equal(*(*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "1995")))), port.Images[0].Args["YEAR"], "root args are inherited")
require.Equal(port.Images[1].Dockerfile, filepath.Join(dir, "35mm Nitrate Film"))
require.Equal(port.Images[1].ContextPath, filepath.Join(dir, "Le Gamaar cinema"))
require.Contains(port.Images[1].Args, "YEAR")
require.Equal(*(*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "2009")))), port.Images[1].Args["YEAR"])
require.Contains(port.Images[1].Args, "VILLAIN")
require.Equal(*(*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "Hans Landa")))), port.Images[1].Args["VILLAIN"])
}
func TestReadPortsFromFs(t *testing.T) {
ctx := context.Background()
t.Run("reads port.yaml for each directory", func(t *testing.T) {
require := require.New(t)
tmp := t.TempDir()
// Make valid port directory.
err := os.Mkdir(filepath.Join(tmp, "foo"), 0755)
require.NoError(err)
err = os.WriteFile(
filepath.Join(tmp, "foo", "port.yaml"),
[]byte(`name: ghcr.io/repo/name`),
0644)
require.NoError(err)
// Directory without port.yaml.
err = os.Mkdir(filepath.Join(tmp, "bar"), 0755)
require.NoError(err)
err = os.WriteFile(filepath.Join(tmp, "bar", "secrets"), []byte("Frank Moses"), 0644)
require.NoError(err)
// A file.
err = os.WriteFile(filepath.Join(tmp, "baz"), []byte("Sarah Ross"), 0644)
require.NoError(err)
ports, err := clade.ReadPortsFromFs(ctx, tmp)
require.NoError(err)
require.Len(ports, 1)
})
t.Run("fails if directory not exists", func(t *testing.T) {
require := require.New(t)
_, err := clade.ReadPortsFromFs(ctx, "/not exists")
require.ErrorIs(err, os.ErrNotExist)
})
t.Run("fails if port.yaml is invalid", func(t *testing.T) {
require := require.New(t)
tmp := t.TempDir()
// Make valid port directory.
err := os.Mkdir(filepath.Join(tmp, "foo"), 0755)
require.NoError(err)
err = os.WriteFile(
filepath.Join(tmp, "foo", "port.yaml"),
[]byte(`name: invalid name`),
0644)
require.NoError(err)
_, err = clade.ReadPortsFromFs(ctx, tmp)
require.ErrorContains(err, filepath.Join(tmp, "foo", "port.yaml"))
require.ErrorContains(err, "name")
require.ErrorContains(err, "invalid")
})
}