|
| 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 | +) |
0 commit comments