Skip to content

Commit 4b94622

Browse files
authored
Merge pull request #1646 from saschagrunert/file-tests
Add config file unit tests
2 parents 259f32b + 60d6117 commit 4b94622

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

pkg/common/file.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ func ReadConfig(filepath string) (*Config, error) {
6060
// an error if the file was unable to be written to.
6161
func WriteConfig(c *Config, filepath string) error {
6262
if c == nil {
63-
c = new(Config)
64-
c.yamlData = new(yaml.Node)
63+
c = &Config{}
64+
}
65+
if c.yamlData == nil {
66+
c.yamlData = &yaml.Node{}
6567
}
6668

6769
setConfigOptions(c)

pkg/common/file_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package common_test
18+
19+
import (
20+
"os"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
25+
"sigs.k8s.io/cri-tools/pkg/common"
26+
)
27+
28+
var _ = DescribeTable("ReadConfig",
29+
func(content string, expectedConfig *common.Config, shouldFail bool) {
30+
f, err := os.CreateTemp("", "crictl-read-config-")
31+
defer os.RemoveAll(f.Name())
32+
Expect(err).NotTo(HaveOccurred())
33+
34+
_, err = f.WriteString(content)
35+
Expect(err).NotTo(HaveOccurred())
36+
37+
readConfig, err := common.ReadConfig(f.Name())
38+
if shouldFail {
39+
Expect(err).To(HaveOccurred())
40+
return
41+
} else {
42+
Expect(err).NotTo(HaveOccurred())
43+
}
44+
45+
Expect(readConfig.RuntimeEndpoint).To(Equal(expectedConfig.RuntimeEndpoint))
46+
Expect(readConfig.ImageEndpoint).To(Equal(expectedConfig.ImageEndpoint))
47+
Expect(readConfig.Timeout).To(Equal(expectedConfig.Timeout))
48+
Expect(readConfig.Debug).To(Equal(expectedConfig.Debug))
49+
Expect(readConfig.PullImageOnCreate).To(Equal(expectedConfig.PullImageOnCreate))
50+
Expect(readConfig.DisablePullOnRun).To(Equal(expectedConfig.DisablePullOnRun))
51+
},
52+
53+
Entry("should succeed with config", `
54+
runtime-endpoint: "foo"
55+
image-endpoint: "bar"
56+
timeout: 10
57+
debug: true
58+
pull-image-on-create: true
59+
disable-pull-on-run: true
60+
`, &common.Config{
61+
RuntimeEndpoint: "foo",
62+
ImageEndpoint: "bar",
63+
Timeout: 10,
64+
Debug: true,
65+
PullImageOnCreate: true,
66+
DisablePullOnRun: true,
67+
}, false),
68+
69+
Entry("should fail with invalid config option", `runtime-endpoint-wrong: "foo"`, nil, true),
70+
Entry("should fail with invalid 'timeout' value", `timeout: "foo"`, nil, true),
71+
Entry("should fail with invalid 'debug' value", `debug: "foo"`, nil, true),
72+
Entry("should fail with invalid 'pull-image-on-create' value", `pull-image-on-create: "foo"`, nil, true),
73+
Entry("should fail with invalid 'disable-pull-on-run' value", `disable-pull-on-run: "foo"`, nil, true),
74+
)
75+
76+
var _ = DescribeTable("WriteConfig",
77+
func(config *common.Config) {
78+
f, err := os.CreateTemp("", "crictl-write-config-")
79+
defer os.RemoveAll(f.Name())
80+
Expect(err).NotTo(HaveOccurred())
81+
82+
err = common.WriteConfig(config, f.Name())
83+
Expect(err).NotTo(HaveOccurred())
84+
85+
readConfig, err := common.ReadConfig(f.Name())
86+
Expect(err).NotTo(HaveOccurred())
87+
88+
if config == nil {
89+
config = &common.Config{}
90+
}
91+
92+
Expect(readConfig.RuntimeEndpoint).To(Equal(config.RuntimeEndpoint))
93+
Expect(readConfig.ImageEndpoint).To(Equal(config.ImageEndpoint))
94+
Expect(readConfig.Timeout).To(Equal(config.Timeout))
95+
Expect(readConfig.Debug).To(Equal(config.Debug))
96+
Expect(readConfig.PullImageOnCreate).To(Equal(config.PullImageOnCreate))
97+
Expect(readConfig.DisablePullOnRun).To(Equal(config.DisablePullOnRun))
98+
},
99+
100+
Entry("should succeed with config", &common.Config{
101+
RuntimeEndpoint: "foo",
102+
ImageEndpoint: "bar",
103+
Timeout: 10,
104+
Debug: true,
105+
PullImageOnCreate: true,
106+
DisablePullOnRun: true,
107+
}),
108+
109+
Entry("should succeed with nil config", nil),
110+
)

pkg/common/suite_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package common_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestCommon(t *testing.T) {
27+
t.Parallel()
28+
RegisterFailHandler(Fail)
29+
RunSpecs(t, "Common")
30+
}

0 commit comments

Comments
 (0)