forked from kubernetes-sigs/cri-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
128 lines (103 loc) · 3.41 KB
/
config_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
/*
Copyright 2024 The Kubernetes Authors.
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 e2e
import (
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)
var _ = t.Describe("config", func() {
var configFile *os.File
BeforeEach(func() {
var err error
configFile, err = os.CreateTemp("", "crictl-*.yaml")
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(os.RemoveAll(configFile.Name())).NotTo(HaveOccurred())
})
listConfig := func() string {
res := t.Crictl("--config " + configFile.Name() + " config --list")
Expect(res).To(Exit(0))
return string(res.Out.Contents())
}
It("should succeed to list with empty config", func() {
cfg := listConfig()
Expect(cfg).To(ContainSubstring("runtime-endpoint"))
Expect(cfg).To(ContainSubstring("image-endpoint"))
Expect(cfg).To(MatchRegexp("timeout .* 0"))
Expect(cfg).To(MatchRegexp("debug .* false"))
Expect(cfg).To(MatchRegexp("pull-image-on-create .* false"))
Expect(cfg).To(MatchRegexp("disable-pull-on-run .* false"))
})
It("should succeed to set config values", func() {
t.CrictlExpectSuccess("--config "+configFile.Name()+" config --set runtime-endpoint=foo,timeout=10", "")
cfg := listConfig()
Expect(cfg).To(MatchRegexp("runtime-endpoint .* foo"))
Expect(cfg).To(MatchRegexp("timeout .* 10"))
})
It("should succeed to set config values and preserve comments", func() {
_, err := configFile.WriteString(`
runtime-endpoint: "foo"
image-endpoint: "bar" # an inline comment
timeout: 5
# comment below a newline
debug: true
`)
Expect(err).NotTo(HaveOccurred())
t.CrictlExpectSuccess("--config "+configFile.Name()+" config --set runtime-endpoint=bar,image-endpoint=baz,timeout=10,debug=false", "")
cfgContent, err := os.ReadFile(configFile.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(cfgContent)).To(Equal(
`runtime-endpoint: "bar"
image-endpoint: "baz" # an inline comment
timeout: 10
# comment below a newline
debug: false
pull-image-on-create: false
disable-pull-on-run: false
`))
})
It("should succeed to get the right value if duplicate entries are defined", func() {
_, err := configFile.WriteString(`
timeout: 20
timeout: 5
timeout: 10
`)
Expect(err).NotTo(HaveOccurred())
t.CrictlExpectSuccess("--config "+configFile.Name()+" config --get timeout", "10")
})
It("should succeed to set duplicate entries", func() {
_, err := configFile.WriteString(`
timeout: 20
timeout: 5
timeout: 10
`)
Expect(err).NotTo(HaveOccurred())
t.CrictlExpectSuccess("--config "+configFile.Name()+" config --set timeout=30", "")
cfgContent, err := os.ReadFile(configFile.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(cfgContent)).To(Equal(
`timeout: 30
timeout: 30
timeout: 30
runtime-endpoint: ""
image-endpoint: ""
debug: false
pull-image-on-create: false
disable-pull-on-run: false
`))
t.CrictlExpectSuccess("--config "+configFile.Name()+" config --get timeout", "30")
})
})