From 68526a8c4bc0b4c4b2d388010c6ab1af56b2c962 Mon Sep 17 00:00:00 2001 From: ditsuke Date: Wed, 16 Mar 2022 14:12:38 +0530 Subject: [PATCH 1/2] Add tests for the services/govmomi/extra package Adds Ginkgo tests go the extra package, testing public methods under the package. Signed-off-by: ditsuke --- .../govmomi/extra/extra_suite_test.go | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 pkg/services/govmomi/extra/extra_suite_test.go diff --git a/pkg/services/govmomi/extra/extra_suite_test.go b/pkg/services/govmomi/extra/extra_suite_test.go new file mode 100644 index 0000000000..11c5ff558e --- /dev/null +++ b/pkg/services/govmomi/extra/extra_suite_test.go @@ -0,0 +1,151 @@ +/* +Copyright 2022 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 extra + +import ( + "encoding/base64" + "fmt" + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/vmware/govmomi/vim25/types" +) + +func TestExtra(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Extra Suite") +} + +type ConfigInitFn func(*Config, string) error + +var _ = Describe("Config_SetCustomVMXKeys", func() { + Context("we try to set custom keys in the config", func() { + config := getMockConfig() + customConfigKeys := map[string]string{ + "customKey1": "customVal1", + "customKey2": "customKey2", + } + + var configKeys []interface{} + for k, v := range customConfigKeys { + configKeys = append(configKeys, &types.OptionValue{ + Key: k, + Value: v, + }) + } + + It("adds the keys to the config", func() { + err := config.SetCustomVMXKeys(customConfigKeys) + + Expect(err).ToNot(HaveOccurred()) + Expect(*config).To(ConsistOf(configKeys...)) + }) + }) +}) + +var _ = Describe("Config_SetCloudInitUserData", func() { + ConfigInitFnTester( + func(config *Config, s string) error { + return config.SetCloudInitUserData([]byte(s)) + }, + "SetCloudInitUserData", + "guestinfo.userdata", + "guestinfo.userdata.encoding", + ) +}) + +var _ = Describe("Config_SetCloudInitMetadata", func() { + ConfigInitFnTester(func(config *Config, s string) error { + return config.SetCloudInitMetadata([]byte(s)) + }, + "SetCloudInitMetadata", + "guestinfo.metadata", + "guestinfo.metadata.encoding", + ) +}) + +func getMockConfig() *Config { + return new(Config) +} + +func base64Encode(s string) string { + return base64.StdEncoding.EncodeToString([]byte(s)) +} + +// ConfigInitFnTester is a common testing method for config.SetCloudInitUserData and config.SetCloudInitMetadata. +func ConfigInitFnTester(method ConfigInitFn, methodName string, dataKey string, encodingKey string) { + const sampleData = "some sample data, " + var expectedData = base64Encode(sampleData) + + Context(fmt.Sprintf("we call %q with some non-encoded sample data", methodName), func() { + config := getMockConfig() + err := method(config, sampleData) + + It("must set 2 keys in the config", func() { + Expect(err).ToNot(HaveOccurred()) + Expect(len(*config)).To(Equal(2)) + }) + + It(fmt.Sprintf("must set data as a base64 encoded string with the key %q", dataKey), func() { + Expect(*config).To(ContainElement(&types.OptionValue{ + Key: dataKey, + Value: expectedData, + })) + }) + + It("must set a key to indicate base64 encoding of the data", func() { + Expect(*config).To(ContainElement(&types.OptionValue{ + Key: encodingKey, + Value: "base64", + })) + }) + }) + + Context(fmt.Sprintf("We call %q with some pre-encoded data (single pass)", methodName), func() { + config := getMockConfig() + preEncodedData := base64Encode(sampleData) + err := method(config, preEncodedData) + + It("does not encode the data further on storing", func() { + Expect(err).ToNot(HaveOccurred()) + Expect(*config).To(ContainElement(&types.OptionValue{ + Key: dataKey, + Value: preEncodedData, + })) + }) + }) + + Context(fmt.Sprintf("We call %q with some pre-encoded data (multiple passes)", methodName), func() { + config := getMockConfig() + const passCount = 5 + multiPassEncodedData := sampleData + for i := 0; i < passCount; i++ { + multiPassEncodedData = base64Encode(multiPassEncodedData) + } + + err := method(config, multiPassEncodedData) + + It("saves data with a single pass of encoding", func() { + Expect(err).ToNot(HaveOccurred()) + Expect(*config).To(ContainElement(&types.OptionValue{ + Key: dataKey, + Value: expectedData, + })) + }) + }) +} From 1694b229baa3fc4e88ca102d3fc3f432890524f7 Mon Sep 17 00:00:00 2001 From: ditsuke Date: Wed, 30 Mar 2022 22:30:41 +0530 Subject: [PATCH 2/2] Implement suggestions to improve code quality Implements various suggestions by a reviewer to improve code quality and readability. --- .../govmomi/extra/extra_suite_test.go | 44 ++++++++----------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/pkg/services/govmomi/extra/extra_suite_test.go b/pkg/services/govmomi/extra/extra_suite_test.go index 11c5ff558e..201496d2ce 100644 --- a/pkg/services/govmomi/extra/extra_suite_test.go +++ b/pkg/services/govmomi/extra/extra_suite_test.go @@ -35,25 +35,23 @@ type ConfigInitFn func(*Config, string) error var _ = Describe("Config_SetCustomVMXKeys", func() { Context("we try to set custom keys in the config", func() { - config := getMockConfig() + var config Config customConfigKeys := map[string]string{ "customKey1": "customVal1", "customKey2": "customKey2", } - var configKeys []interface{} - for k, v := range customConfigKeys { - configKeys = append(configKeys, &types.OptionValue{ - Key: k, - Value: v, - }) - } - It("adds the keys to the config", func() { err := config.SetCustomVMXKeys(customConfigKeys) Expect(err).ToNot(HaveOccurred()) - Expect(*config).To(ConsistOf(configKeys...)) + + for k, v := range customConfigKeys { + Expect(config).To(ContainElement(&types.OptionValue{ + Key: k, + Value: v, + })) + } }) }) }) @@ -79,10 +77,6 @@ var _ = Describe("Config_SetCloudInitMetadata", func() { ) }) -func getMockConfig() *Config { - return new(Config) -} - func base64Encode(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } @@ -93,23 +87,23 @@ func ConfigInitFnTester(method ConfigInitFn, methodName string, dataKey string, var expectedData = base64Encode(sampleData) Context(fmt.Sprintf("we call %q with some non-encoded sample data", methodName), func() { - config := getMockConfig() - err := method(config, sampleData) + var config Config + err := method(&config, sampleData) It("must set 2 keys in the config", func() { Expect(err).ToNot(HaveOccurred()) - Expect(len(*config)).To(Equal(2)) + Expect(len(config)).To(Equal(2)) }) It(fmt.Sprintf("must set data as a base64 encoded string with the key %q", dataKey), func() { - Expect(*config).To(ContainElement(&types.OptionValue{ + Expect(config).To(ContainElement(&types.OptionValue{ Key: dataKey, Value: expectedData, })) }) It("must set a key to indicate base64 encoding of the data", func() { - Expect(*config).To(ContainElement(&types.OptionValue{ + Expect(config).To(ContainElement(&types.OptionValue{ Key: encodingKey, Value: "base64", })) @@ -117,13 +111,13 @@ func ConfigInitFnTester(method ConfigInitFn, methodName string, dataKey string, }) Context(fmt.Sprintf("We call %q with some pre-encoded data (single pass)", methodName), func() { - config := getMockConfig() + var config Config preEncodedData := base64Encode(sampleData) - err := method(config, preEncodedData) + err := method(&config, preEncodedData) It("does not encode the data further on storing", func() { Expect(err).ToNot(HaveOccurred()) - Expect(*config).To(ContainElement(&types.OptionValue{ + Expect(config).To(ContainElement(&types.OptionValue{ Key: dataKey, Value: preEncodedData, })) @@ -131,18 +125,18 @@ func ConfigInitFnTester(method ConfigInitFn, methodName string, dataKey string, }) Context(fmt.Sprintf("We call %q with some pre-encoded data (multiple passes)", methodName), func() { - config := getMockConfig() + var config Config const passCount = 5 multiPassEncodedData := sampleData for i := 0; i < passCount; i++ { multiPassEncodedData = base64Encode(multiPassEncodedData) } - err := method(config, multiPassEncodedData) + err := method(&config, multiPassEncodedData) It("saves data with a single pass of encoding", func() { Expect(err).ToNot(HaveOccurred()) - Expect(*config).To(ContainElement(&types.OptionValue{ + Expect(config).To(ContainElement(&types.OptionValue{ Key: dataKey, Value: expectedData, }))