|
4 | 4 | import com.github.containersolutions.operator.sample.TestCustomResourceController;
|
5 | 5 | import io.fabric8.kubernetes.api.model.ConfigMap;
|
6 | 6 | import org.awaitility.Awaitility;
|
7 |
| -import org.junit.jupiter.api.*; |
| 7 | +import org.junit.jupiter.api.BeforeAll; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.TestInstance; |
8 | 11 | import org.slf4j.Logger;
|
9 | 12 | import org.slf4j.LoggerFactory;
|
10 | 13 |
|
@@ -34,58 +37,55 @@ public void cleanup() {
|
34 | 37 | integrationTest.cleanup();
|
35 | 38 | }
|
36 | 39 |
|
37 |
| - @AfterAll |
38 |
| - public void teardown() { |
39 |
| - integrationTest.teardown(); |
40 |
| - } |
41 |
| - |
42 | 40 | @Test
|
43 |
| - public void manyResourcesGetCreatedUpdatedAndDeleted() throws InterruptedException { |
44 |
| - log.info("Adding new resources."); |
45 |
| - for (int i = 0; i < NUMBER_OF_RESOURCES_CREATED; i++) { |
46 |
| - TestCustomResource tcr = integrationTest.createTestCustomResource(String.valueOf(i)); |
47 |
| - integrationTest.getCrOperations().inNamespace(TEST_NAMESPACE).create(tcr); |
48 |
| - } |
49 |
| - |
50 |
| - Awaitility.await().atMost(1, TimeUnit.MINUTES) |
51 |
| - .untilAsserted(() -> { |
52 |
| - List<ConfigMap> items = integrationTest.getK8sClient().configMaps() |
53 |
| - .inNamespace(TEST_NAMESPACE) |
54 |
| - .withLabel("managedBy", TestCustomResourceController.class.getSimpleName()) |
55 |
| - .list().getItems(); |
56 |
| - assertThat(items).hasSize(NUMBER_OF_RESOURCES_CREATED); |
57 |
| - }); |
58 |
| - |
59 |
| - log.info("Updating resources."); |
60 |
| - // update some resources |
61 |
| - for (int i = 0; i < NUMBER_OF_RESOURCES_UPDATED; i++) { |
62 |
| - TestCustomResource tcr = integrationTest.createTestCustomResource(String.valueOf(i)); |
63 |
| - tcr.getSpec().setValue(i + UPDATED_SUFFIX); |
64 |
| - integrationTest.getCrOperations().inNamespace(TEST_NAMESPACE).createOrReplace(tcr); |
65 |
| - } |
66 |
| - // sleep to make some variability to the test, so some updates are not executed before delete |
67 |
| - Thread.sleep(300); |
68 |
| - |
69 |
| - log.info("Deleting resources."); |
70 |
| - // deleting some resources |
71 |
| - for (int i = 0; i < NUMBER_OF_RESOURCES_DELETED; i++) { |
72 |
| - TestCustomResource tcr = integrationTest.createTestCustomResource(String.valueOf(i)); |
73 |
| - integrationTest.getCrOperations().inNamespace(TEST_NAMESPACE).delete(tcr); |
74 |
| - } |
75 |
| - |
76 |
| - Awaitility.await().atMost(1, TimeUnit.MINUTES) |
77 |
| - .untilAsserted(() -> { |
78 |
| - List<ConfigMap> items = integrationTest.getK8sClient().configMaps() |
79 |
| - .inNamespace(TEST_NAMESPACE) |
80 |
| - .withLabel("managedBy", TestCustomResourceController.class.getSimpleName()) |
81 |
| - .list().getItems(); |
82 |
| - assertThat(items).hasSize(NUMBER_OF_RESOURCES_CREATED - NUMBER_OF_RESOURCES_DELETED); |
83 |
| - |
84 |
| - List<TestCustomResource> crs = integrationTest.getCrOperations() |
85 |
| - .inNamespace(TEST_NAMESPACE) |
86 |
| - .list().getItems(); |
87 |
| - assertThat(crs).hasSize(NUMBER_OF_RESOURCES_CREATED - NUMBER_OF_RESOURCES_DELETED); |
88 |
| - }); |
| 41 | + public void manyResourcesGetCreatedUpdatedAndDeleted() throws Exception { |
| 42 | + integrationTest.teardownIfSuccess(() -> { |
| 43 | + log.info("Adding new resources."); |
| 44 | + for (int i = 0; i < NUMBER_OF_RESOURCES_CREATED; i++) { |
| 45 | + TestCustomResource tcr = integrationTest.createTestCustomResource(String.valueOf(i)); |
| 46 | + integrationTest.getCrOperations().inNamespace(TEST_NAMESPACE).create(tcr); |
| 47 | + } |
| 48 | + |
| 49 | + Awaitility.await().atMost(1, TimeUnit.MINUTES) |
| 50 | + .untilAsserted(() -> { |
| 51 | + List<ConfigMap> items = integrationTest.getK8sClient().configMaps() |
| 52 | + .inNamespace(TEST_NAMESPACE) |
| 53 | + .withLabel("managedBy", TestCustomResourceController.class.getSimpleName()) |
| 54 | + .list().getItems(); |
| 55 | + assertThat(items).hasSize(NUMBER_OF_RESOURCES_CREATED); |
| 56 | + }); |
| 57 | + |
| 58 | + log.info("Updating resources."); |
| 59 | + // update some resources |
| 60 | + for (int i = 0; i < NUMBER_OF_RESOURCES_UPDATED; i++) { |
| 61 | + TestCustomResource tcr = integrationTest.createTestCustomResource(String.valueOf(i)); |
| 62 | + tcr.getSpec().setValue(i + UPDATED_SUFFIX); |
| 63 | + integrationTest.getCrOperations().inNamespace(TEST_NAMESPACE).createOrReplace(tcr); |
| 64 | + } |
| 65 | + // sleep to make some variability to the test, so some updates are not executed before delete |
| 66 | + Thread.sleep(300); |
| 67 | + |
| 68 | + log.info("Deleting resources."); |
| 69 | + // deleting some resources |
| 70 | + for (int i = 0; i < NUMBER_OF_RESOURCES_DELETED; i++) { |
| 71 | + TestCustomResource tcr = integrationTest.createTestCustomResource(String.valueOf(i)); |
| 72 | + integrationTest.getCrOperations().inNamespace(TEST_NAMESPACE).delete(tcr); |
| 73 | + } |
| 74 | + |
| 75 | + Awaitility.await().atMost(1, TimeUnit.MINUTES) |
| 76 | + .untilAsserted(() -> { |
| 77 | + List<ConfigMap> items = integrationTest.getK8sClient().configMaps() |
| 78 | + .inNamespace(TEST_NAMESPACE) |
| 79 | + .withLabel("managedBy", TestCustomResourceController.class.getSimpleName()) |
| 80 | + .list().getItems(); |
| 81 | + assertThat(items).hasSize(NUMBER_OF_RESOURCES_CREATED - NUMBER_OF_RESOURCES_DELETED); |
| 82 | + |
| 83 | + List<TestCustomResource> crs = integrationTest.getCrOperations() |
| 84 | + .inNamespace(TEST_NAMESPACE) |
| 85 | + .list().getItems(); |
| 86 | + assertThat(crs).hasSize(NUMBER_OF_RESOURCES_CREATED - NUMBER_OF_RESOURCES_DELETED); |
| 87 | + }); |
| 88 | + }); |
89 | 89 | }
|
90 | 90 |
|
91 | 91 |
|
|
0 commit comments