|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "maps" |
| 5 | + "testing" |
| 6 | + |
| 7 | + . "github.com/onsi/gomega" |
| 8 | +) |
| 9 | + |
| 10 | +func TestSettingAnnotations(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + value string |
| 14 | + fn func(map[string]string, string) map[string]string |
| 15 | + expectedAnnotations map[string]string |
| 16 | + suppliedAnnotations map[string]string |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "set empty CpuKey annotation", |
| 20 | + value: "", |
| 21 | + fn: SetCpuAnnotation, |
| 22 | + suppliedAnnotations: map[string]string{ |
| 23 | + CpuKey: "", |
| 24 | + }, |
| 25 | + expectedAnnotations: map[string]string{ |
| 26 | + CpuKey: "", |
| 27 | + }, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "replaces CpuKey annotation", |
| 31 | + value: "1", |
| 32 | + fn: SetCpuAnnotation, |
| 33 | + suppliedAnnotations: map[string]string{ |
| 34 | + CpuKey: "", |
| 35 | + }, |
| 36 | + expectedAnnotations: map[string]string{ |
| 37 | + CpuKey: "1", |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "adds CpuKey annotation", |
| 42 | + value: "1", |
| 43 | + fn: SetCpuAnnotation, |
| 44 | + suppliedAnnotations: map[string]string{}, |
| 45 | + expectedAnnotations: map[string]string{ |
| 46 | + CpuKey: "1", |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "replaces MemoryKey annotation", |
| 51 | + value: "4Gi", |
| 52 | + fn: SetMemoryAnnotation, |
| 53 | + suppliedAnnotations: map[string]string{ |
| 54 | + MemoryKey: "", |
| 55 | + }, |
| 56 | + expectedAnnotations: map[string]string{ |
| 57 | + MemoryKey: "4Gi", |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "adds MemoryKey annotation", |
| 62 | + value: "4Gi", |
| 63 | + fn: SetMemoryAnnotation, |
| 64 | + suppliedAnnotations: map[string]string{}, |
| 65 | + expectedAnnotations: map[string]string{ |
| 66 | + MemoryKey: "4Gi", |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "replaces GpuCount annotation", |
| 71 | + value: "1", |
| 72 | + fn: SetGpuCountAnnotation, |
| 73 | + suppliedAnnotations: map[string]string{ |
| 74 | + GpuCountKey: "", |
| 75 | + }, |
| 76 | + expectedAnnotations: map[string]string{ |
| 77 | + GpuCountKey: "1", |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "adds GpuCount annotation", |
| 82 | + value: "1", |
| 83 | + fn: SetGpuCountAnnotation, |
| 84 | + suppliedAnnotations: map[string]string{}, |
| 85 | + expectedAnnotations: map[string]string{ |
| 86 | + GpuCountKey: "1", |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "replaces GpuType annotation", |
| 91 | + value: "1", |
| 92 | + fn: SetGpuTypeAnnotation, |
| 93 | + suppliedAnnotations: map[string]string{ |
| 94 | + GpuTypeKey: "", |
| 95 | + }, |
| 96 | + expectedAnnotations: map[string]string{ |
| 97 | + GpuTypeKey: "nvidia.com/gpu", |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "adds GpuType annotation", |
| 102 | + value: "1", |
| 103 | + fn: SetGpuTypeAnnotation, |
| 104 | + suppliedAnnotations: map[string]string{}, |
| 105 | + expectedAnnotations: map[string]string{ |
| 106 | + GpuTypeKey: "nvidia.com/gpu", |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tc := range tests { |
| 112 | + t.Run(tc.name, func(t *testing.T) { |
| 113 | + observed := tc.fn(tc.suppliedAnnotations, tc.value) |
| 114 | + if !maps.Equal(observed, tc.expectedAnnotations) { |
| 115 | + t.Errorf("SetAnnotations() returned %v, expected %v", observed, tc.expectedAnnotations) |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestParsingAnnotations(t *testing.T) { |
| 122 | + tests := []struct { |
| 123 | + name string |
| 124 | + annotations map[string]string |
| 125 | + key string |
| 126 | + expectError bool |
| 127 | + expectedReturn string |
| 128 | + }{ |
| 129 | + { |
| 130 | + name: "Tests that key and value exists in annotation", |
| 131 | + annotations: map[string]string{ |
| 132 | + CpuKey: "1", |
| 133 | + }, |
| 134 | + key: CpuKey, |
| 135 | + expectError: false, |
| 136 | + expectedReturn: "1", |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "Tests that key and empty value exists in annotation", |
| 140 | + annotations: map[string]string{ |
| 141 | + CpuKey: "", |
| 142 | + }, |
| 143 | + key: CpuKey, |
| 144 | + expectError: false, |
| 145 | + expectedReturn: "", |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "Tests that empty string is returned if key doesn't exist", |
| 149 | + annotations: map[string]string{}, |
| 150 | + key: CpuKey, |
| 151 | + expectError: true, |
| 152 | + expectedReturn: "", |
| 153 | + }, |
| 154 | + } |
| 155 | + |
| 156 | + for _, tc := range tests { |
| 157 | + t.Run(tc.name, func(t *testing.T) { |
| 158 | + g := NewWithT(t) |
| 159 | + observed, err := ParseMachineSetAnnotationKey(tc.annotations, tc.key) |
| 160 | + |
| 161 | + if tc.expectError == true { |
| 162 | + g.Expect(err).To(HaveOccurred()) |
| 163 | + } else { |
| 164 | + g.Expect(err).ToNot(HaveOccurred()) |
| 165 | + } |
| 166 | + g.Expect(observed).To(Equal(tc.expectedReturn)) |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestHasScaleFromZero(t *testing.T) { |
| 172 | + tests := []struct { |
| 173 | + name string |
| 174 | + suppliedAnnotations map[string]string |
| 175 | + expectedReturn bool |
| 176 | + }{ |
| 177 | + { |
| 178 | + name: "Tests that ScaleFromZero is enabled", |
| 179 | + suppliedAnnotations: map[string]string{ |
| 180 | + CpuKey: "1", |
| 181 | + MemoryKey: "4Gi", |
| 182 | + }, |
| 183 | + expectedReturn: true, |
| 184 | + }, |
| 185 | + { |
| 186 | + name: "Tests that ScaleFromZero fails with one required value missing", |
| 187 | + suppliedAnnotations: map[string]string{ |
| 188 | + CpuKey: "1", |
| 189 | + MemoryKey: "", |
| 190 | + }, |
| 191 | + expectedReturn: false, |
| 192 | + }, |
| 193 | + { |
| 194 | + name: "Tests that ScaleFromZero fails with both required values missing", |
| 195 | + suppliedAnnotations: map[string]string{ |
| 196 | + CpuKey: "", |
| 197 | + MemoryKey: "", |
| 198 | + }, |
| 199 | + expectedReturn: false, |
| 200 | + }, |
| 201 | + } |
| 202 | + |
| 203 | + for _, tc := range tests { |
| 204 | + t.Run(tc.name, func(t *testing.T) { |
| 205 | + g := NewWithT(t) |
| 206 | + observed := HasScaleFromZeroAnnotationsEnabled(tc.suppliedAnnotations) |
| 207 | + |
| 208 | + g.Expect(observed).To(Equal(tc.expectedReturn)) |
| 209 | + }) |
| 210 | + } |
| 211 | +} |
0 commit comments