forked from chainguard-dev/melange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_test.go
326 lines (309 loc) · 8.16 KB
/
test_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
325
326
// Copyright 2023 Chainguard, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package build
import (
"fmt"
"path/filepath"
"strings"
"testing"
"chainguard.dev/apko/pkg/build/types"
apko_types "chainguard.dev/apko/pkg/build/types"
"chainguard.dev/melange/pkg/config"
"chainguard.dev/melange/pkg/container"
"github.com/chainguard-dev/clog/slogtest"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
"github.com/yookoala/realpath"
)
const (
testImgRef = "testImageRef"
testPkgName = "testPkgName"
testWorkspaceDir = "/workspace"
etcResolveConf = "/etc/resolv.conf"
homeBuild = "/home/build"
)
var (
gid1000 = uint32(1000)
)
func defaultEnv(opts ...func(*apko_types.ImageConfiguration)) apko_types.ImageConfiguration {
env := apko_types.ImageConfiguration{
Accounts: types.ImageAccounts{
Groups: []types.Group{{GroupName: "build", GID: 1000, Members: []string{"build"}}},
Users: []apko_types.User{{UserName: "build", UID: 1000, GID: &gid1000}},
},
}
for _, opt := range opts {
opt(&env)
}
return env
}
func TestBuildWorkspaceConfig(t *testing.T) {
tmpDir := t.TempDir()
// realpath is used to get the real path of the temp dir
tmpDirReal, err := realpath.Realpath(tmpDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Just define the base stuff here that we can then
// modify in the tests.
baseTest := Test{
WorkspaceDir: testWorkspaceDir,
}
// Just define the base stuff here that we can then
// modify in the tests.
wantBase := container.Config{
Environment: map[string]string{"HOME": "/root"},
PackageName: testPkgName,
ImgRef: testImgRef,
WorkspaceDir: "/workspace",
Capabilities: container.Capabilities{Networking: true},
Mounts: []container.BindMount{
{Source: testWorkspaceDir, Destination: homeBuild},
{Source: etcResolveConf, Destination: etcResolveConf},
},
}
tests := []struct {
name string
env map[string]string
t *Test
wantErr string
want *container.Config
}{
{
name: "test - no cache dir",
t: &baseTest,
want: func() *container.Config {
want := wantBase
return &want
}(),
}, {
name: "test - with cache dir, exists",
t: func() *Test {
cacheT := baseTest
cacheT.CacheDir = tmpDir
return &cacheT
}(),
want: func() *container.Config {
want := wantBase
want.Mounts = append(want.Mounts, container.BindMount{Source: tmpDirReal, Destination: "/var/cache/melange"})
return &want
}(),
}, {
name: "test - with cache dir, exists, environment",
t: func() *Test {
cacheT := baseTest
cacheT.CacheDir = tmpDir
return &cacheT
}(),
env: map[string]string{"FOO": "bar", "BAZ": "zzz"},
want: func() *container.Config {
want := wantBase
want.Mounts = append(want.Mounts, container.BindMount{Source: tmpDirReal, Destination: "/var/cache/melange"})
want.Environment = map[string]string{"FOO": "bar", "BAZ": "zzz", "HOME": "/root"}
return &want
}(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := slogtest.Context(t)
got, gotErr := tt.t.buildWorkspaceConfig(ctx, testImgRef, testPkgName, apko_types.ImageConfiguration{Environment: tt.env})
if gotErr != nil {
if tt.wantErr == "" {
t.Fatalf("unexpected error: %v", gotErr)
}
if !strings.Contains(gotErr.Error(), tt.wantErr) {
t.Fatalf("expected error to contain %q, got %q", tt.wantErr, gotErr.Error())
}
} else {
if tt.wantErr != "" {
t.Fatalf("expected error %q, got nil", tt.wantErr)
}
if !cmp.Equal(tt.want, got) {
t.Errorf("%s", cmp.Diff(tt.want, got))
}
}
})
}
}
// TestConfigurationLoad is the main set of tests for loading a configuration
// file for tests. When in doubt, add your test here.
func TestConfigurationLoad(t *testing.T) {
tests := []struct {
name string
skipConfigCleanStep bool
requireErr require.ErrorAssertionFunc
expected *config.Configuration
}{
{
name: "range-subpackages",
requireErr: require.NoError,
expected: &config.Configuration{
Package: config.Package{
Name: "hello",
Version: "world",
Resources: &config.Resources{},
},
Test: &config.Test{
Environment: defaultEnv(),
Pipeline: []config.Pipeline{
{
Name: "hello",
Runs: "world",
},
},
},
Subpackages: []config.Subpackage{{
Name: "cats",
Test: &config.Test{
Environment: defaultEnv(),
Pipeline: []config.Pipeline{{
Runs: "cats are angry",
}},
},
}, {
Name: "dogs",
Test: &config.Test{
Environment: defaultEnv(),
Pipeline: []config.Pipeline{{
Runs: "dogs are loyal",
}},
},
}, {
Name: "turtles",
Test: &config.Test{
Environment: defaultEnv(),
Pipeline: []config.Pipeline{{
Runs: "turtles are slow",
}},
},
}, {
Name: "donatello",
Test: &config.Test{
Environment: defaultEnv(),
Pipeline: []config.Pipeline{
{
Runs: "donatello's color is purple",
},
{
Uses: "go/build",
With: map[string]string{"packages": "purple"},
},
},
},
}, {
Name: "leonardo",
Test: &config.Test{
Environment: defaultEnv(),
Pipeline: []config.Pipeline{
{
Runs: "leonardo's color is blue",
},
{
Uses: "go/build",
With: map[string]string{"packages": "blue"},
},
}},
}, {
Name: "michelangelo",
Test: &config.Test{
Environment: defaultEnv(),
Pipeline: []config.Pipeline{
{
Runs: "michelangelo's color is orange",
},
{
Uses: "go/build",
With: map[string]string{"packages": "orange"},
},
}},
}, {
Name: "raphael",
Test: &config.Test{
Environment: defaultEnv(),
Pipeline: []config.Pipeline{
{
Runs: "raphael's color is red",
},
{
Uses: "go/build",
With: map[string]string{"packages": "red"},
},
}},
}, {
Name: "simple",
Test: &config.Test{
Environment: defaultEnv(),
Pipeline: []config.Pipeline{
{
Runs: "simple-runs",
}, {
Uses: "simple-uses",
},
}},
}},
},
}, {
name: "py3-pandas",
requireErr: require.NoError,
expected: &config.Configuration{
Package: config.Package{
Name: "py3-pandas",
Version: "2.1.3",
Resources: &config.Resources{},
},
Test: &config.Test{
Environment: defaultEnv(func(env *apko_types.ImageConfiguration) {
env.Contents.Packages = []string{"busybox", "python-3"}
}),
Pipeline: []config.Pipeline{
{
Runs: "python3 ./py3-pandas-test.py\n",
}, {
Uses: "test-uses",
With: map[string]string{"test-with": "test-with-value"},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := slogtest.Context(t)
tctx := Test{
ConfigFile: filepath.Join("testdata", "test_configuration_load", fmt.Sprintf("%s.melange.yaml", tt.name)),
}
cfg, err := config.ParseConfiguration(ctx, tctx.ConfigFile)
tt.requireErr(t, err)
if !tt.skipConfigCleanStep {
cleanTestConfig(cfg)
}
if tt.expected == nil {
if cfg != nil {
t.Fatalf("actual didn't match expected (want nil, got config)")
}
} else {
if d := cmp.Diff(
*tt.expected,
*cfg,
cmpopts.IgnoreUnexported(config.Pipeline{}, config.Configuration{}),
); d != "" {
t.Fatalf("actual didn't match expected (-want, +got): %s", d)
}
}
})
}
}