|
35 | 35 |
|
36 | 36 | import io.fabric8.kubernetes.api.model.GenericKubernetesResource; |
37 | 37 | import io.fabric8.kubernetes.api.model.HasMetadata; |
| 38 | +import io.fabric8.kubernetes.api.model.ObjectMeta; |
38 | 39 | import io.javaoperatorsdk.operator.api.reconciler.Context; |
39 | 40 | import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.DefaultManagedDependentResourceContext; |
40 | 41 | import io.javaoperatorsdk.operator.processing.dependent.BulkDependentResource; |
|
47 | 48 | import edu.umd.cs.findbugs.annotations.NonNull; |
48 | 49 |
|
49 | 50 | import static org.assertj.core.api.Assertions.assertThat; |
| 51 | +import static org.assertj.core.api.Assertions.entry; |
50 | 52 | import static org.mockito.Mockito.doReturn; |
51 | 53 | import static org.mockito.Mockito.mock; |
52 | 54 |
|
@@ -195,75 +197,84 @@ record ConditionStruct(ConditionType type, |
195 | 197 | private static List<DynamicTest> testsForDir(List<DesiredFn<KafkaProxy, ?>> dependentResources, |
196 | 198 | Path testDir) |
197 | 199 | throws IOException { |
198 | | - var unusedFiles = childFilesMatching(testDir, "*"); |
199 | | - Path input = testDir.resolve("in-KafkaProxy.yaml"); |
200 | | - KafkaProxy kafkaProxy = kafkaProxyFromFile(input); |
201 | | - assertMinimalMetadataPresent(kafkaProxy); |
202 | | - unusedFiles.remove(input); |
203 | | - unusedFiles.remove(testDir.resolve("operator-config.yaml")); |
204 | | - unusedFiles.removeAll(childFilesMatching(testDir, "in-*")); |
205 | | - |
206 | | - Context<KafkaProxy> context; |
207 | 200 | try { |
208 | | - context = buildContext(kafkaProxy, testDir); |
209 | | - } |
210 | | - catch (IOException e) { |
211 | | - throw new UncheckedIOException(e); |
212 | | - } |
213 | | - |
214 | | - List<DynamicTest> tests = new ArrayList<>(); |
215 | | - |
216 | | - var dr = dependentResources.stream() |
217 | | - .flatMap(r -> r.invokeDesired(kafkaProxy, context).values().stream().map(x -> Map.entry(r.resourceType(), x))) |
218 | | - .collect(Collectors.groupingBy(Map.Entry::getKey)) |
219 | | - .entrySet() |
220 | | - .stream() |
221 | | - .collect(Collectors.toMap(Map.Entry::getKey, |
222 | | - e -> e.getValue().stream().map(Map.Entry::getValue).collect(Collectors.toCollection(() -> new TreeSet<>( |
223 | | - Comparator.comparing(hasMetadata -> hasMetadata.getMetadata().getName())))))); |
224 | | - for (var entry : dr.entrySet()) { |
225 | | - var resourceType = entry.getKey(); |
226 | | - var actualResources = entry.getValue(); |
227 | | - for (var actualResource : actualResources) { |
228 | | - String kind = resourceType.getSimpleName(); |
229 | | - String name = actualResource.getMetadata().getName(); |
230 | | - var expectedFile = testDir.resolve("out-" + kind + "-" + name + ".yaml"); |
231 | | - tests.add(DynamicTest.dynamicTest(kind + " '" + name + "' should have the same content as " + testDir.relativize(expectedFile), |
232 | | - () -> { |
233 | | - assertThat(Files.exists(expectedFile)).isTrue(); |
234 | | - var expected = loadExpected(expectedFile, resourceType); |
235 | | - assertSameYaml(actualResource, expected); |
236 | | - unusedFiles.remove(expectedFile); |
237 | | - })); |
| 201 | + var unusedFiles = childFilesMatching(testDir, "*"); |
| 202 | + String inFileName = "in-KafkaProxy.yaml"; |
| 203 | + Path input = testDir.resolve(inFileName); |
| 204 | + KafkaProxy kafkaProxy = kafkaProxyFromFile(input); |
| 205 | + assertMinimalMetadata(kafkaProxy.getMetadata(), inFileName); |
| 206 | + |
| 207 | + unusedFiles.remove(input); |
| 208 | + unusedFiles.remove(testDir.resolve("operator-config.yaml")); |
| 209 | + unusedFiles.removeAll(childFilesMatching(testDir, "in-*")); |
| 210 | + |
| 211 | + Context<KafkaProxy> context; |
| 212 | + try { |
| 213 | + context = buildContext(kafkaProxy, testDir); |
238 | 214 | } |
239 | | - for (var cluster : kafkaProxy.getSpec().getClusters()) { |
240 | | - ClusterCondition actualClusterCondition = SharedKafkaProxyContext.clusterCondition(context, cluster); |
241 | | - if (actualClusterCondition.type() == ConditionType.Accepted && actualClusterCondition.status().equals(Conditions.Status.TRUE)) { |
242 | | - continue; |
243 | | - } |
244 | | - else { |
245 | | - var expectedFile = testDir.resolve("cond-" + actualClusterCondition.type() + "-" + actualClusterCondition.cluster() + ".yaml"); |
246 | | - tests.add(DynamicTest.dynamicTest( |
247 | | - "Condition " + actualClusterCondition.type() + " for cluster " + actualClusterCondition.cluster() + " matches contents of expected file " |
248 | | - + expectedFile, |
| 215 | + catch (IOException e) { |
| 216 | + throw new UncheckedIOException(e); |
| 217 | + } |
| 218 | + |
| 219 | + List<DynamicTest> tests = new ArrayList<>(); |
| 220 | + |
| 221 | + var dr = dependentResources.stream() |
| 222 | + .flatMap(r -> r.invokeDesired(kafkaProxy, context).values().stream().map(x -> Map.entry(r.resourceType(), x))) |
| 223 | + .collect(Collectors.groupingBy(Map.Entry::getKey)) |
| 224 | + .entrySet() |
| 225 | + .stream() |
| 226 | + .collect(Collectors.toMap(Map.Entry::getKey, |
| 227 | + e -> e.getValue().stream().map(Map.Entry::getValue).collect(Collectors.toCollection(() -> new TreeSet<>( |
| 228 | + Comparator.comparing(hasMetadata -> hasMetadata.getMetadata().getName())))))); |
| 229 | + for (var entry : dr.entrySet()) { |
| 230 | + var resourceType = entry.getKey(); |
| 231 | + var actualResources = entry.getValue(); |
| 232 | + for (var actualResource : actualResources) { |
| 233 | + String kind = resourceType.getSimpleName(); |
| 234 | + String name = actualResource.getMetadata().getName(); |
| 235 | + var expectedFile = testDir.resolve("out-" + kind + "-" + name + ".yaml"); |
| 236 | + tests.add(DynamicTest.dynamicTest(kind + " '" + name + "' should have the same content as " + testDir.relativize(expectedFile), |
249 | 237 | () -> { |
250 | | - var expected = loadExpected(expectedFile, ClusterCondition.class); |
251 | | - assertSameYaml(actualClusterCondition, expected); |
| 238 | + assertThat(Files.exists(expectedFile)).isTrue(); |
| 239 | + var expected = loadExpected(expectedFile, resourceType); |
| 240 | + assertSameYaml(actualResource, expected); |
252 | 241 | unusedFiles.remove(expectedFile); |
253 | 242 | })); |
254 | 243 | } |
| 244 | + for (var cluster : kafkaProxy.getSpec().getClusters()) { |
| 245 | + ClusterCondition actualClusterCondition = SharedKafkaProxyContext.clusterCondition(context, cluster); |
| 246 | + if (actualClusterCondition.type() == ConditionType.Accepted && actualClusterCondition.status().equals(Conditions.Status.TRUE)) { |
| 247 | + continue; |
| 248 | + } |
| 249 | + else { |
| 250 | + var expectedFile = testDir.resolve("cond-" + actualClusterCondition.type() + "-" + actualClusterCondition.cluster() + ".yaml"); |
| 251 | + tests.add(DynamicTest.dynamicTest( |
| 252 | + "Condition " + actualClusterCondition.type() + " for cluster " + actualClusterCondition.cluster() + " matches contents of expected file " |
| 253 | + + expectedFile, |
| 254 | + () -> { |
| 255 | + var expected = loadExpected(expectedFile, ClusterCondition.class); |
| 256 | + assertSameYaml(actualClusterCondition, expected); |
| 257 | + unusedFiles.remove(expectedFile); |
| 258 | + })); |
| 259 | + } |
| 260 | + } |
255 | 261 | } |
256 | | - } |
257 | 262 |
|
258 | | - tests.add(DynamicTest.dynamicTest("There should be no unused files in " + testDir, |
259 | | - () -> assertThat(unusedFiles).isEmpty())); |
260 | | - return tests; |
| 263 | + tests.add(DynamicTest.dynamicTest("There should be no unused files in " + testDir, |
| 264 | + () -> assertThat(unusedFiles).isEmpty())); |
| 265 | + return tests; |
| 266 | + } |
| 267 | + catch (AssertionError e) { |
| 268 | + return List.of(DynamicTest.dynamicTest("failed to initialize test", () -> { |
| 269 | + throw e; |
| 270 | + })); |
| 271 | + } |
261 | 272 | } |
262 | 273 |
|
263 | | - // sanity check since we can omit fields that the k8s API will ensure are present in reality |
264 | | - private static void assertMinimalMetadataPresent(HasMetadata resource) { |
265 | | - assertThat(resource.getMetadata().getName()).isNotNull().isNotEmpty(); |
266 | | - assertThat(resource.getMetadata().getNamespace()).isNotNull().isNotEmpty(); |
| 274 | + private static void assertMinimalMetadata(ObjectMeta metadata, String inFileName) { |
| 275 | + // sanity check since we can omit fields that the k8s API will ensure are present in reality |
| 276 | + assertThat(metadata.getName()).describedAs("metadata.name in " + inFileName).isNotNull().isNotEmpty(); |
| 277 | + assertThat(metadata.getNamespace()).describedAs("metadata.namespace in " + inFileName).isNotNull().isNotEmpty(); |
267 | 278 | } |
268 | 279 |
|
269 | 280 | private static <T> void assertSameYaml(T actualResource, T expected) throws JsonProcessingException { |
@@ -305,10 +316,11 @@ private static Context<KafkaProxy> buildContext(KafkaProxy kafkaProxy, Path test |
305 | 316 | var runtimeDecl = Files.exists(configFile) ? configFromFile(configFile) : new RuntimeDecl(List.of()); |
306 | 317 | Set<GenericKubernetesResource> filterInstances = new HashSet<>(); |
307 | 318 | for (var filterApi : runtimeDecl.filterApis()) { |
308 | | - try (var dirStream = Files.newDirectoryStream(testDir, "in-" + filterApi.kind() + "-*.yaml")) { |
| 319 | + String fileName = "in-" + filterApi.kind() + "-*.yaml"; |
| 320 | + try (var dirStream = Files.newDirectoryStream(testDir, fileName)) { |
309 | 321 | for (Path p : dirStream) { |
310 | 322 | GenericKubernetesResource resource = YAML_MAPPER.readValue(p.toFile(), GenericKubernetesResource.class); |
311 | | - assertMinimalMetadataPresent(resource); |
| 323 | + assertMinimalMetadata(resource.getMetadata(), fileName); |
312 | 324 | filterInstances.add(resource); |
313 | 325 | } |
314 | 326 | } |
|
0 commit comments