forked from containers/common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_local_test.go
602 lines (506 loc) · 17.1 KB
/
config_local_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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//go:build !remote
package config
import (
"fmt"
"os"
"path"
"runtime"
"strings"
"github.com/containers/common/libnetwork/types"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)
var _ = Describe("Config Local", func() {
It("should not fail on invalid NetworkConfigDir", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
tmpfile := path.Join(os.TempDir(), "wrong-file")
file, err := os.Create(tmpfile)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
file.Close()
defer os.Remove(tmpfile)
defConf.Network.NetworkConfigDir = tmpfile
defConf.Network.CNIPluginDirs.Set([]string{})
// When
err = defConf.Network.Validate()
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
It("should not fail on invalid CNIPluginDirs", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
validDirPath, err := os.MkdirTemp("", "config-empty")
if err != nil {
panic(err)
}
defer os.RemoveAll(validDirPath)
// Given
defConf.Network.NetworkConfigDir = validDirPath
defConf.Network.CNIPluginDirs.Set([]string{invalidPath})
// When
err = defConf.Network.Validate()
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
It("should fail on invalid subnet pool", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
validDirPath, err := os.MkdirTemp("", "config-empty")
if err != nil {
panic(err)
}
defer os.RemoveAll(validDirPath)
// Given
defConf.Network.NetworkConfigDir = validDirPath
defConf.Network.CNIPluginDirs.Set([]string{validDirPath})
net, _ := types.ParseCIDR("10.0.0.0/24")
defConf.Network.DefaultSubnetPools = []SubnetPool{
{Base: &net, Size: 16},
}
// When
err = defConf.Network.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
defConf.Network.DefaultSubnetPools = []SubnetPool{
{Base: &net, Size: 33},
}
// When
err = defConf.Network.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
})
It("parse network subnet pool", func() {
config, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
net1, _ := types.ParseCIDR("10.89.0.0/16")
net2, _ := types.ParseCIDR("10.90.0.0/15")
gomega.Expect(config.Network.DefaultSubnetPools).To(gomega.Equal(
[]SubnetPool{{
Base: &net1,
Size: 24,
}, {
Base: &net2,
Size: 24,
}},
))
})
It("parse dns port", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Network.DNSBindPort).To(gomega.Equal(uint16(0)))
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Network.DNSBindPort).To(gomega.Equal(uint16(1153)))
})
It("test firewall", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Network.FirewallDriver).To(gomega.Equal(string("")))
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Network.FirewallDriver).To(gomega.Equal("none"))
})
It("parse pasta_options", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Network.PastaOptions.Get()).To(gomega.BeEmpty())
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Network.PastaOptions.Get()).To(gomega.Equal([]string{"-t", "auto"}))
})
It("parse default_rootless_network_cmd", func() {
// Given
config, err := NewConfig("")
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Network.DefaultRootlessNetworkCmd).To(gomega.Equal("pasta"))
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Network.DefaultRootlessNetworkCmd).To(gomega.Equal("slirp4netns"))
})
It("should fail on invalid device mode", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.Devices.Set([]string{"/dev/null:/dev/null:abc"})
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
})
It("should fail on invalid first device", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.Devices.Set([]string{"wrong:/dev/null:rw"})
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
})
It("should fail on invalid second device", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.Devices.Set([]string{"/dev/null:wrong:rw"})
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
})
It("should fail on invalid device", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.Devices.Set([]string{invalidPath})
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
})
It("should fail on wrong invalid device specification", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.Devices.Set([]string{"::::"})
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
})
It("should fail on invalid interface_name", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.InterfaceName = "random"
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
})
It("should succeed on good interface_name", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.InterfaceName = "device"
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
It("should succeed on default interface_name", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.InterfaceName = ""
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
It("should fail on bad timezone", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.TZ = "foo"
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
})
It("should succeed on good timezone", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.TZ = "US/Eastern"
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
It("should succeed on local timezone", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.TZ = "local"
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
It("should fail on wrong DefaultUlimits", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.DefaultUlimits.Set([]string{invalidPath})
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
})
It("should return containers engine env", func() {
// Given
expectedEnv := []string{"super=duper", "foo=bar"}
// When
config, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Engine.Env.Get()).To(gomega.BeEquivalentTo(expectedEnv))
gomega.Expect(os.Getenv("super")).To(gomega.BeEquivalentTo("duper"))
gomega.Expect(os.Getenv("foo")).To(gomega.BeEquivalentTo("bar"))
})
It("should override cdi_spec_dirs if provided", func() {
// Given
config1, err := New(nil)
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config1.Engine.CdiSpecDirs.Get()).To(gomega.Equal([]string{"/etc/cdi"}))
// Given default just get default
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Engine.CdiSpecDirs.Get()).To(gomega.Equal([]string{"/etc/cdi"}))
// Given override just get override
config3, err := NewConfig("testdata/containers_override.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config3.Engine.CdiSpecDirs.Get()).To(gomega.Equal([]string{"/somepath"}))
// Given override just get override
config4, err := NewConfig("testdata/containers_override2.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config4.Engine.CdiSpecDirs.Get()).To(gomega.Equal([]string{"/somepath", "/some_other_path"}))
})
It("Expect Remote to be False", func() {
// Given
// When
config, err := New(nil)
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Engine.Remote).To(gomega.BeFalse())
})
It("verify getDefaultEnv", func() {
envs := []string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
}
// Given we do
oldContainersConf, envSet := os.LookupEnv(containersConfEnv)
os.Setenv(containersConfEnv, "/dev/null")
// When
config, err := Default()
// Undo that
if envSet {
os.Setenv(containersConfEnv, oldContainersConf)
} else {
os.Unsetenv(containersConfEnv)
}
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.GetDefaultEnv()).To(gomega.BeEquivalentTo(envs))
config.Containers.HTTPProxy = true
gomega.Expect(config.GetDefaultEnv()).To(gomega.BeEquivalentTo(envs))
os.Setenv("HTTP_PROXY", "localhost")
os.Setenv("FOO", "BAR")
newenvs := []string{"HTTP_PROXY=localhost"}
envs = append(newenvs, envs...)
gomega.Expect(config.GetDefaultEnv()).To(gomega.BeEquivalentTo(envs))
config.Containers.HTTPProxy = false
config.Containers.EnvHost = true
envString := strings.Join(config.GetDefaultEnv(), ",")
gomega.Expect(envString).To(gomega.ContainSubstring("FOO=BAR"))
gomega.Expect(envString).To(gomega.ContainSubstring("HTTP_PROXY=localhost"))
})
It("Default Umask", func() {
// Given
// When
config, err := New(nil)
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Containers.Umask).To(gomega.Equal("0022"))
})
It("Set Umask", func() {
// Given
// When
config, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Containers.Umask).To(gomega.Equal("0002"))
})
It("Should fail on bad Umask", func() {
defConf, err := defaultConfig()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(defConf).NotTo(gomega.BeNil())
// Given
defConf.Containers.Umask = "88888"
// When
err = defConf.Containers.Validate()
// Then
gomega.Expect(err).To(gomega.HaveOccurred())
})
It("default netns", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Containers.NetNS).To(gomega.Equal("private"))
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Containers.NetNS).To(gomega.Equal("bridge"))
})
It("should have a default secret driver", func() {
// Given
path := ""
// When
config, err := NewConfig(path)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
// Then
gomega.Expect(config.Secrets.Driver).To(gomega.Equal("file"))
})
It("should be possible to override the secret driver and options", func() {
// Given
path := "testdata/containers_override.conf"
// When
config, err := NewConfig(path)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
// Then
gomega.Expect(config.Secrets.Driver).To(gomega.Equal("pass"))
gomega.Expect(config.Secrets.Opts).To(gomega.Equal(map[string]string{
"key": "foo@bar",
"root": "/srv/password-store",
}))
})
It("Set machine image path", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Machine.Image).To(gomega.Equal(""))
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
path := "https://example.com/$OS/$ARCH/foobar.ami"
gomega.Expect(config2.Machine.Image).To(gomega.Equal(path))
val := fmt.Sprintf("https://example.com/%s/%s/foobar.ami", runtime.GOOS, runtime.GOARCH)
gomega.Expect(config2.Machine.URI()).To(gomega.BeEquivalentTo(val))
})
It("CompatAPIEnforceDockerHub", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Engine.CompatAPIEnforceDockerHub).To(gomega.BeTrue())
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Engine.CompatAPIEnforceDockerHub).To(gomega.BeFalse())
})
It("ComposeProviders", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Engine.ComposeProviders.Get()).To(gomega.Equal(getDefaultComposeProviders())) // no hard-coding to work on all platforms
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Engine.ComposeProviders.Get()).To(gomega.Equal([]string{"/some/thing/else", "/than/before"}))
})
It("AddCompression", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Engine.AddCompression.Get()).To(gomega.BeEmpty()) // no hard-coding to work on all platforms
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Engine.AddCompression.Get()).To(gomega.Equal([]string{"zstd", "zstd:chunked"}))
})
It("ComposeWarningLogs", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Engine.ComposeWarningLogs).To(gomega.BeTrue())
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Engine.ComposeWarningLogs).To(gomega.BeFalse())
})
It("Set machine disk", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Machine.DiskSize).To(gomega.Equal(uint64(100)))
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Machine.DiskSize).To(gomega.Equal(uint64(20)))
})
It("Set machine CPUs", func() {
// Given
cpus := runtime.NumCPU() / 2
if cpus == 0 {
cpus = 1
}
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Machine.CPUs).To(gomega.Equal(uint64(cpus)))
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Machine.CPUs).To(gomega.Equal(uint64(1)))
})
It("Set machine memory", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Machine.Memory).To(gomega.Equal(uint64(2048)))
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Machine.Memory).To(gomega.Equal(uint64(1024)))
})
It("Get Rosetta value", func() {
// Given
config, err := New(nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config.Machine.Rosetta).To(gomega.BeTrue())
// When
config2, err := NewConfig("testdata/containers_default.conf")
// Then
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(config2.Machine.Rosetta).To(gomega.BeFalse())
})
})