Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#1646 from saschagrunert/file-tests
Browse files Browse the repository at this point in the history
Add config file unit tests
  • Loading branch information
k8s-ci-robot authored Oct 23, 2024
2 parents 259f32b + 60d6117 commit 4b94622
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/common/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
110 changes: 110 additions & 0 deletions pkg/common/file_test.go
Original file line number Diff line number Diff line change
@@ -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),
)
30 changes: 30 additions & 0 deletions pkg/common/suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 4b94622

Please sign in to comment.