|
1 | 1 | package hypershift
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "testing" |
| 5 | + |
4 | 6 | . "github.com/onsi/gomega"
|
| 7 | + corev1 "k8s.io/api/core/v1" |
5 | 8 | "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
6 | 9 | "k8s.io/apimachinery/pkg/runtime"
|
7 | 10 | "k8s.io/apimachinery/pkg/util/yaml"
|
8 |
| - "testing" |
9 | 11 | )
|
10 | 12 |
|
11 | 13 | func TestParseHostedControlPlane(t *testing.T) {
|
@@ -103,3 +105,110 @@ spec:
|
103 | 105 | g.Expect(actualOutput).To(Equal(tc.expectedOutput))
|
104 | 106 | }
|
105 | 107 | }
|
| 108 | + |
| 109 | +func TestTolerationsToStringSliceYaml(t *testing.T) { |
| 110 | + g := NewGomegaWithT(t) |
| 111 | + |
| 112 | + testCases := []struct { |
| 113 | + name string |
| 114 | + tolerations []corev1.Toleration |
| 115 | + expected []string |
| 116 | + }{ |
| 117 | + { |
| 118 | + name: "operator Exists with no key or value should generate valid YAML", |
| 119 | + tolerations: []corev1.Toleration{ |
| 120 | + { |
| 121 | + Operator: corev1.TolerationOpExists, |
| 122 | + }, |
| 123 | + }, |
| 124 | + expected: []string{ |
| 125 | + "- operator: Exists", |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "operator Equal with key and value should include all fields", |
| 130 | + tolerations: []corev1.Toleration{ |
| 131 | + { |
| 132 | + Key: "node-role.kubernetes.io/master", |
| 133 | + Operator: corev1.TolerationOpEqual, |
| 134 | + Value: "true", |
| 135 | + Effect: corev1.TaintEffectNoSchedule, |
| 136 | + }, |
| 137 | + }, |
| 138 | + expected: []string{ |
| 139 | + "- key: node-role.kubernetes.io/master", |
| 140 | + " operator: Equal", |
| 141 | + " value: \"true\"", |
| 142 | + " effect: NoSchedule", |
| 143 | + }, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "empty string values should be filtered out", |
| 147 | + tolerations: []corev1.Toleration{ |
| 148 | + { |
| 149 | + Key: "test-key", |
| 150 | + Operator: corev1.TolerationOpEqual, |
| 151 | + Value: "", |
| 152 | + Effect: corev1.TaintEffectNoSchedule, |
| 153 | + }, |
| 154 | + }, |
| 155 | + expected: []string{ |
| 156 | + "- key: test-key", |
| 157 | + " operator: Equal", |
| 158 | + " effect: NoSchedule", |
| 159 | + }, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "operator Exists with key should not include null value", |
| 163 | + tolerations: []corev1.Toleration{ |
| 164 | + { |
| 165 | + Key: "node.kubernetes.io/unreachable", |
| 166 | + Operator: corev1.TolerationOpExists, |
| 167 | + Effect: corev1.TaintEffectNoExecute, |
| 168 | + }, |
| 169 | + }, |
| 170 | + expected: []string{ |
| 171 | + "- key: node.kubernetes.io/unreachable", |
| 172 | + " operator: Exists", |
| 173 | + " effect: NoExecute", |
| 174 | + }, |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "multiple tolerations should generate proper YAML array", |
| 178 | + tolerations: []corev1.Toleration{ |
| 179 | + { |
| 180 | + Key: "node-role.kubernetes.io/master", |
| 181 | + Operator: corev1.TolerationOpEqual, |
| 182 | + Value: "true", |
| 183 | + Effect: corev1.TaintEffectNoSchedule, |
| 184 | + }, |
| 185 | + { |
| 186 | + Operator: corev1.TolerationOpExists, |
| 187 | + }, |
| 188 | + { |
| 189 | + Key: "node.kubernetes.io/unreachable", |
| 190 | + Operator: corev1.TolerationOpExists, |
| 191 | + Effect: corev1.TaintEffectNoExecute, |
| 192 | + }, |
| 193 | + }, |
| 194 | + expected: []string{ |
| 195 | + "- key: node-role.kubernetes.io/master", |
| 196 | + " operator: Equal", |
| 197 | + " value: \"true\"", |
| 198 | + " effect: NoSchedule", |
| 199 | + "- operator: Exists", |
| 200 | + "- key: node.kubernetes.io/unreachable", |
| 201 | + " operator: Exists", |
| 202 | + " effect: NoExecute", |
| 203 | + }, |
| 204 | + }, |
| 205 | + } |
| 206 | + |
| 207 | + for _, tc := range testCases { |
| 208 | + t.Run(tc.name, func(t *testing.T) { |
| 209 | + result, err := tolerationsToStringSliceYaml(tc.tolerations) |
| 210 | + g.Expect(err).NotTo(HaveOccurred()) |
| 211 | + g.Expect(result).To(Equal(tc.expected), "Expected exact YAML format match") |
| 212 | + }) |
| 213 | + } |
| 214 | +} |
0 commit comments