From bbd96d894d23efb560efca0f208e8a68e2905897 Mon Sep 17 00:00:00 2001 From: nueavv Date: Tue, 1 Oct 2024 00:42:18 +0900 Subject: [PATCH] Add unit tests for TaintsFromSDK function Signed-off-by: nueavv --- go.mod | 3 +- pkg/cloud/converters/eks_test.go | 207 +++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 pkg/cloud/converters/eks_test.go diff --git a/go.mod b/go.mod index 14518e3653..192c671d91 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace + github.com/stretchr/testify v1.9.0 github.com/zgalor/weberr v0.6.0 golang.org/x/crypto v0.22.0 golang.org/x/text v0.14.0 @@ -169,6 +170,7 @@ require ( github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.52.2 // indirect github.com/prometheus/procfs v0.13.0 // indirect @@ -185,7 +187,6 @@ require ( github.com/spf13/cast v1.6.0 // indirect github.com/spf13/viper v1.18.2 // indirect github.com/stoewer/go-strcase v1.2.0 // indirect - github.com/stretchr/testify v1.9.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/valyala/fastjson v1.6.4 // indirect github.com/vincent-petithory/dataurl v1.0.0 // indirect diff --git a/pkg/cloud/converters/eks_test.go b/pkg/cloud/converters/eks_test.go new file mode 100644 index 0000000000..cfc073db06 --- /dev/null +++ b/pkg/cloud/converters/eks_test.go @@ -0,0 +1,207 @@ +/* +Copyright 2020 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 converters + +import ( + "testing" + + "github.com/aws/aws-sdk-go/service/eks" + "github.com/stretchr/testify/assert" + + expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2" +) + +func TestTaintsFromSDK(t *testing.T) { + tests := []struct { + name string + input []*eks.Taint + expected expinfrav1.Taints + expectErr bool + }{ + { + name: "Single taint conversion", + input: []*eks.Taint{ + { + Key: awsString("key1"), + Value: awsString("value1"), + Effect: awsString(eks.TaintEffectNoSchedule), + }, + }, + expected: expinfrav1.Taints{ + { + Key: "key1", + Value: "value1", + Effect: expinfrav1.TaintEffectNoSchedule, + }, + }, + expectErr: false, + }, + { + name: "Multiple taints conversion", + input: []*eks.Taint{ + { + Key: awsString("key1"), + Value: awsString("value1"), + Effect: awsString(eks.TaintEffectNoExecute), + }, + { + Key: awsString("key2"), + Value: awsString("value2"), + Effect: awsString(eks.TaintEffectPreferNoSchedule), + }, + }, + expected: expinfrav1.Taints{ + { + Key: "key1", + Value: "value1", + Effect: expinfrav1.TaintEffectNoExecute, + }, + { + Key: "key2", + Value: "value2", + Effect: expinfrav1.TaintEffectPreferNoSchedule, + }, + }, + expectErr: false, + }, + { + name: "Unknown taint effect", + input: []*eks.Taint{ + { + Key: awsString("key1"), + Value: awsString("value1"), + Effect: awsString("UnknownEffect"), + }, + }, + expected: nil, + expectErr: true, + }, + { + name: "Nil taint value", + input: []*eks.Taint{ + { + Key: awsString("key1"), + Value: nil, + Effect: awsString(eks.TaintEffectNoSchedule), + }, + }, + expected: expinfrav1.Taints{ + { + Key: "key1", + Value: "", + Effect: expinfrav1.TaintEffectNoSchedule, + }, + }, + expectErr: false, + }, + { + name: "Empty input slice", + input: []*eks.Taint{}, + expected: expinfrav1.Taints{}, + expectErr: false, + }, + { + name: "Nil input", + input: nil, + expected: expinfrav1.Taints{}, + expectErr: false, + }, + { + name: "Mixed valid and invalid taints", + input: []*eks.Taint{ + { + Key: awsString("key1"), + Value: awsString("value1"), + Effect: awsString(eks.TaintEffectNoExecute), + }, + { + Key: awsString("key2"), + Value: awsString("value2"), + Effect: awsString("InvalidEffect"), + }, + }, + expected: nil, + expectErr: true, + }, + { + name: "Missing effect", + input: []*eks.Taint{ + { + Key: awsString("key1"), + Value: awsString("value1"), + // Effect is nil + }, + }, + expected: nil, + expectErr: true, + }, + { + name: "Empty key", + input: []*eks.Taint{ + { + Key: awsString(""), + Value: awsString("value1"), + Effect: awsString(eks.TaintEffectNoSchedule), + }, + }, + expected: expinfrav1.Taints{ + { + Key: "", + Value: "value1", + Effect: expinfrav1.TaintEffectNoSchedule, + }, + }, + expectErr: false, + }, + { + name: "Empty value", + input: []*eks.Taint{ + { + Key: awsString("key1"), + Value: awsString(""), + Effect: awsString(eks.TaintEffectNoSchedule), + }, + }, + expected: expinfrav1.Taints{ + { + Key: "key1", + Value: "", + Effect: expinfrav1.TaintEffectNoSchedule, + }, + }, + expectErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result, err := TaintsFromSDK(tt.input) + if tt.expectErr { + assert.Error(t, err) + assert.Nil(t, result, "Expected result to be nil on error") + } else { + assert.NoError(t, err) + assert.Equal(t, tt.expected, result, "Converted taints do not match expected") + } + }) + } +} + +// Helper function to create AWS String pointers +func awsString(value string) *string { + return &value +}