diff --git a/pkg/common/file.go b/pkg/common/file.go index 0af620eded..c4376468ef 100644 --- a/pkg/common/file.go +++ b/pkg/common/file.go @@ -60,8 +60,10 @@ func ReadConfig(filepath string) (*Config, error) { // an error if the file was unable to be written to. func WriteConfig(c *Config, filepath string) error { if c == nil { - c = new(Config) - c.yamlData = new(yaml.Node) + c = &Config{} + } + if c.yamlData == nil { + c.yamlData = &yaml.Node{} } setConfigOptions(c) diff --git a/pkg/common/file_test.go b/pkg/common/file_test.go new file mode 100644 index 0000000000..8053e9a540 --- /dev/null +++ b/pkg/common/file_test.go @@ -0,0 +1,110 @@ +/* +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 common_test + +import ( + "os" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "sigs.k8s.io/cri-tools/pkg/common" +) + +var _ = DescribeTable("ReadConfig", + func(content string, expectedConfig *common.Config, shouldFail bool) { + f, err := os.CreateTemp("", "crictl-read-config-") + defer os.RemoveAll(f.Name()) + Expect(err).NotTo(HaveOccurred()) + + _, err = f.WriteString(content) + Expect(err).NotTo(HaveOccurred()) + + readConfig, err := common.ReadConfig(f.Name()) + if shouldFail { + Expect(err).To(HaveOccurred()) + return + } else { + Expect(err).NotTo(HaveOccurred()) + } + + Expect(readConfig.RuntimeEndpoint).To(Equal(expectedConfig.RuntimeEndpoint)) + Expect(readConfig.ImageEndpoint).To(Equal(expectedConfig.ImageEndpoint)) + Expect(readConfig.Timeout).To(Equal(expectedConfig.Timeout)) + Expect(readConfig.Debug).To(Equal(expectedConfig.Debug)) + Expect(readConfig.PullImageOnCreate).To(Equal(expectedConfig.PullImageOnCreate)) + Expect(readConfig.DisablePullOnRun).To(Equal(expectedConfig.DisablePullOnRun)) + }, + + Entry("should succeed with config", ` +runtime-endpoint: "foo" +image-endpoint: "bar" +timeout: 10 +debug: true +pull-image-on-create: true +disable-pull-on-run: true +`, &common.Config{ + RuntimeEndpoint: "foo", + ImageEndpoint: "bar", + Timeout: 10, + Debug: true, + PullImageOnCreate: true, + DisablePullOnRun: true, + }, false), + + Entry("should fail with invalid config option", `runtime-endpoint-wrong: "foo"`, nil, true), + Entry("should fail with invalid 'timeout' value", `timeout: "foo"`, nil, true), + Entry("should fail with invalid 'debug' value", `debug: "foo"`, nil, true), + Entry("should fail with invalid 'pull-image-on-create' value", `pull-image-on-create: "foo"`, nil, true), + Entry("should fail with invalid 'disable-pull-on-run' value", `disable-pull-on-run: "foo"`, nil, true), +) + +var _ = DescribeTable("WriteConfig", + func(config *common.Config) { + f, err := os.CreateTemp("", "crictl-write-config-") + defer os.RemoveAll(f.Name()) + Expect(err).NotTo(HaveOccurred()) + + err = common.WriteConfig(config, f.Name()) + Expect(err).NotTo(HaveOccurred()) + + readConfig, err := common.ReadConfig(f.Name()) + Expect(err).NotTo(HaveOccurred()) + + if config == nil { + config = &common.Config{} + } + + Expect(readConfig.RuntimeEndpoint).To(Equal(config.RuntimeEndpoint)) + Expect(readConfig.ImageEndpoint).To(Equal(config.ImageEndpoint)) + Expect(readConfig.Timeout).To(Equal(config.Timeout)) + Expect(readConfig.Debug).To(Equal(config.Debug)) + Expect(readConfig.PullImageOnCreate).To(Equal(config.PullImageOnCreate)) + Expect(readConfig.DisablePullOnRun).To(Equal(config.DisablePullOnRun)) + }, + + Entry("should succeed with config", &common.Config{ + RuntimeEndpoint: "foo", + ImageEndpoint: "bar", + Timeout: 10, + Debug: true, + PullImageOnCreate: true, + DisablePullOnRun: true, + }), + + Entry("should succeed with nil config", nil), +) diff --git a/pkg/common/suite_test.go b/pkg/common/suite_test.go new file mode 100644 index 0000000000..b839c478e6 --- /dev/null +++ b/pkg/common/suite_test.go @@ -0,0 +1,30 @@ +/* +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 common_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestCommon(t *testing.T) { + t.Parallel() + RegisterFailHandler(Fail) + RunSpecs(t, "Common") +}