Skip to content

Commit 41599a4

Browse files
committed
integration test improvements
1 parent ca3c4b8 commit 41599a4

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

operator-framework/src/test/java/com/github/containersolutions/operator/ConcurrencyIT.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.util.List;
1717
import java.util.concurrent.TimeUnit;
18+
import java.util.stream.Collectors;
1819

1920
import static com.github.containersolutions.operator.IntegrationTestSupport.TEST_NAMESPACE;
2021
import static org.assertj.core.api.Assertions.assertThat;
@@ -44,7 +45,7 @@ public void cleanup() {
4445
@Test
4546
public void manyResourcesGetCreatedUpdatedAndDeleted() {
4647
integrationTest.teardownIfSuccess(() -> {
47-
log.info("Adding new resources.");
48+
log.info("Creating {} new resources", NUMBER_OF_RESOURCES_CREATED);
4849
for (int i = 0; i < NUMBER_OF_RESOURCES_CREATED; i++) {
4950
TestCustomResource tcr = integrationTest.createTestCustomResource(String.valueOf(i));
5051
integrationTest.getCrOperations().inNamespace(TEST_NAMESPACE).create(tcr);
@@ -59,7 +60,7 @@ public void manyResourcesGetCreatedUpdatedAndDeleted() {
5960
assertThat(items).hasSize(NUMBER_OF_RESOURCES_CREATED);
6061
});
6162

62-
log.info("Updating resources.");
63+
log.info("Updating {} resources", NUMBER_OF_RESOURCES_UPDATED);
6364
// update some resources
6465
for (int i = 0; i < NUMBER_OF_RESOURCES_UPDATED; i++) {
6566
TestCustomResource tcr = integrationTest.createTestCustomResource(String.valueOf(i));
@@ -69,7 +70,7 @@ public void manyResourcesGetCreatedUpdatedAndDeleted() {
6970
// sleep to make some variability to the test, so some updates are not executed before delete
7071
Thread.sleep(300);
7172

72-
log.info("Deleting resources.");
73+
log.info("Deleting {} resources", NUMBER_OF_RESOURCES_DELETED);
7374
for (int i = 0; i < NUMBER_OF_RESOURCES_DELETED; i++) {
7475
TestCustomResource tcr = integrationTest.createTestCustomResource(String.valueOf(i));
7576
integrationTest.getCrOperations().inNamespace(TEST_NAMESPACE).delete(tcr);
@@ -81,7 +82,9 @@ public void manyResourcesGetCreatedUpdatedAndDeleted() {
8182
.inNamespace(TEST_NAMESPACE)
8283
.withLabel("managedBy", TestCustomResourceController.class.getSimpleName())
8384
.list().getItems();
84-
assertThat(items).hasSize(NUMBER_OF_RESOURCES_CREATED - NUMBER_OF_RESOURCES_DELETED);
85+
//reducing configmaps to names only - better for debugging
86+
List<String> itemDescs = items.stream().map(configMap -> configMap.getMetadata().getName()).collect(Collectors.toList());
87+
assertThat(itemDescs).hasSize(NUMBER_OF_RESOURCES_CREATED - NUMBER_OF_RESOURCES_DELETED);
8588

8689
List<TestCustomResource> crs = integrationTest.getCrOperations()
8790
.inNamespace(TEST_NAMESPACE)

operator-framework/src/test/java/com/github/containersolutions/operator/IntegrationTestSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public void initialize(KubernetesClient k8sClient, ResourceController controller
4646
Class doneableClass = getCustomResourceDoneableClass(controller);
4747
Class customResourceClass = getCustomResourceClass(controller);
4848
crOperations = k8sClient.customResources(crd, customResourceClass, CustomResourceList.class, doneableClass);
49-
crOperations.inNamespace(TEST_NAMESPACE).delete(crOperations.list().getItems());
5049

5150
if (k8sClient.namespaces().withName(TEST_NAMESPACE).get() == null) {
5251
k8sClient.namespaces().create(new NamespaceBuilder()
@@ -68,6 +67,7 @@ public void cleanup() {
6867

6968
//we depend on the actual operator from the startup to handle the finalizers and clean up
7069
//resources from previous test runs
70+
crOperations.inNamespace(TEST_NAMESPACE).delete(crOperations.list().getItems());
7171

7272
await("all CRs cleaned up").atMost(60, TimeUnit.SECONDS)
7373
.untilAsserted(() -> {

operator-framework/src/test/java/com/github/containersolutions/operator/sample/TestCustomResourceController.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class TestCustomResourceController implements ResourceController<TestCust
2828

2929
private final KubernetesClient kubernetesClient;
3030
private final boolean updateStatus;
31-
private AtomicInteger numberOfExecutions = new AtomicInteger(0);
31+
private final AtomicInteger numberOfExecutions = new AtomicInteger(0);
3232

3333
public TestCustomResourceController(KubernetesClient kubernetesClient) {
3434
this(kubernetesClient, true);
@@ -41,10 +41,15 @@ public TestCustomResourceController(KubernetesClient kubernetesClient, boolean u
4141

4242
@Override
4343
public boolean deleteResource(TestCustomResource resource, Context<TestCustomResource> context) {
44-
kubernetesClient.configMaps().inNamespace(resource.getMetadata().getNamespace())
44+
Boolean delete = kubernetesClient.configMaps().inNamespace(resource.getMetadata().getNamespace())
4545
.withName(resource.getSpec().getConfigMapName()).delete();
46-
log.info("Deleting config map with name: {} for resource: {}", resource.getSpec().getConfigMapName(), resource.getMetadata().getName());
47-
return true;
46+
if (Boolean.TRUE.equals(delete)) {
47+
log.info("Deleted ConfigMap {} for resource: {}", resource.getSpec().getConfigMapName(), resource.getMetadata().getName());
48+
return true;
49+
} else {
50+
log.error("Failed to delete ConfigMap {} for resource: {}", resource.getSpec().getConfigMapName(), resource.getMetadata().getName());
51+
return false;
52+
}
4853
}
4954

5055
@Override

0 commit comments

Comments
 (0)