|
| 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 converters |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/aws/aws-sdk-go/service/eks" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + |
| 25 | + expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2" |
| 26 | +) |
| 27 | + |
| 28 | +func TestTaintsFromSDK(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + input []*eks.Taint |
| 32 | + expected expinfrav1.Taints |
| 33 | + expectErr bool |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "Single taint conversion", |
| 37 | + input: []*eks.Taint{ |
| 38 | + { |
| 39 | + Key: awsString("key1"), |
| 40 | + Value: awsString("value1"), |
| 41 | + Effect: awsString(eks.TaintEffectNoSchedule), |
| 42 | + }, |
| 43 | + }, |
| 44 | + expected: expinfrav1.Taints{ |
| 45 | + { |
| 46 | + Key: "key1", |
| 47 | + Value: "value1", |
| 48 | + Effect: expinfrav1.TaintEffectNoSchedule, |
| 49 | + }, |
| 50 | + }, |
| 51 | + expectErr: false, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "Multiple taints conversion", |
| 55 | + input: []*eks.Taint{ |
| 56 | + { |
| 57 | + Key: awsString("key1"), |
| 58 | + Value: awsString("value1"), |
| 59 | + Effect: awsString(eks.TaintEffectNoExecute), |
| 60 | + }, |
| 61 | + { |
| 62 | + Key: awsString("key2"), |
| 63 | + Value: awsString("value2"), |
| 64 | + Effect: awsString(eks.TaintEffectPreferNoSchedule), |
| 65 | + }, |
| 66 | + }, |
| 67 | + expected: expinfrav1.Taints{ |
| 68 | + { |
| 69 | + Key: "key1", |
| 70 | + Value: "value1", |
| 71 | + Effect: expinfrav1.TaintEffectNoExecute, |
| 72 | + }, |
| 73 | + { |
| 74 | + Key: "key2", |
| 75 | + Value: "value2", |
| 76 | + Effect: expinfrav1.TaintEffectPreferNoSchedule, |
| 77 | + }, |
| 78 | + }, |
| 79 | + expectErr: false, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "Unknown taint effect", |
| 83 | + input: []*eks.Taint{ |
| 84 | + { |
| 85 | + Key: awsString("key1"), |
| 86 | + Value: awsString("value1"), |
| 87 | + Effect: awsString("UnknownEffect"), |
| 88 | + }, |
| 89 | + }, |
| 90 | + expected: nil, |
| 91 | + expectErr: true, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "Nil taint value", |
| 95 | + input: []*eks.Taint{ |
| 96 | + { |
| 97 | + Key: awsString("key1"), |
| 98 | + Value: nil, |
| 99 | + Effect: awsString(eks.TaintEffectNoSchedule), |
| 100 | + }, |
| 101 | + }, |
| 102 | + expected: expinfrav1.Taints{ |
| 103 | + { |
| 104 | + Key: "key1", |
| 105 | + Value: "", |
| 106 | + Effect: expinfrav1.TaintEffectNoSchedule, |
| 107 | + }, |
| 108 | + }, |
| 109 | + expectErr: false, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "Empty input slice", |
| 113 | + input: []*eks.Taint{}, |
| 114 | + expected: expinfrav1.Taints{}, |
| 115 | + expectErr: false, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "Nil input", |
| 119 | + input: nil, |
| 120 | + expected: expinfrav1.Taints{}, |
| 121 | + expectErr: false, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "Mixed valid and invalid taints", |
| 125 | + input: []*eks.Taint{ |
| 126 | + { |
| 127 | + Key: awsString("key1"), |
| 128 | + Value: awsString("value1"), |
| 129 | + Effect: awsString(eks.TaintEffectNoExecute), |
| 130 | + }, |
| 131 | + { |
| 132 | + Key: awsString("key2"), |
| 133 | + Value: awsString("value2"), |
| 134 | + Effect: awsString("InvalidEffect"), |
| 135 | + }, |
| 136 | + }, |
| 137 | + expected: nil, |
| 138 | + expectErr: true, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "Empty key", |
| 142 | + input: []*eks.Taint{ |
| 143 | + { |
| 144 | + Key: awsString(""), |
| 145 | + Value: awsString("value1"), |
| 146 | + Effect: awsString(eks.TaintEffectNoSchedule), |
| 147 | + }, |
| 148 | + }, |
| 149 | + expected: expinfrav1.Taints{ |
| 150 | + { |
| 151 | + Key: "", |
| 152 | + Value: "value1", |
| 153 | + Effect: expinfrav1.TaintEffectNoSchedule, |
| 154 | + }, |
| 155 | + }, |
| 156 | + expectErr: false, |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "Empty value", |
| 160 | + input: []*eks.Taint{ |
| 161 | + { |
| 162 | + Key: awsString("key1"), |
| 163 | + Value: awsString(""), |
| 164 | + Effect: awsString(eks.TaintEffectNoSchedule), |
| 165 | + }, |
| 166 | + }, |
| 167 | + expected: expinfrav1.Taints{ |
| 168 | + { |
| 169 | + Key: "key1", |
| 170 | + Value: "", |
| 171 | + Effect: expinfrav1.TaintEffectNoSchedule, |
| 172 | + }, |
| 173 | + }, |
| 174 | + expectErr: false, |
| 175 | + }, |
| 176 | + } |
| 177 | + |
| 178 | + for _, tt := range tests { |
| 179 | + t.Run(tt.name, func(t *testing.T) { |
| 180 | + result, err := TaintsFromSDK(tt.input) |
| 181 | + if tt.expectErr { |
| 182 | + assert.Error(t, err) |
| 183 | + assert.Nil(t, result, "Expected result to be nil on error") |
| 184 | + } else { |
| 185 | + assert.NoError(t, err) |
| 186 | + assert.Equal(t, tt.expected, result, "Converted taints do not match expected") |
| 187 | + } |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// Helper function to create AWS String pointers. |
| 193 | +func awsString(value string) *string { |
| 194 | + return &value |
| 195 | +} |
0 commit comments