Skip to content

Commit 33f1e46

Browse files
authored
Delete the cluster at correct index to prevent intermittent test failures (#4055)
* Delete the cluster at correct index to prevent intermittent test failures
1 parent 3cf1547 commit 33f1e46

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

integration-tests/src/test/java/oracle/weblogic/kubernetes/ItKubernetesDomainEvents.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
import static oracle.weblogic.kubernetes.utils.CommonTestUtils.withLongRetryPolicy;
7979
import static oracle.weblogic.kubernetes.utils.ConfigMapUtils.createConfigMapForDomainCreation;
8080
import static oracle.weblogic.kubernetes.utils.DomainUtils.createDomainAndVerify;
81+
import static oracle.weblogic.kubernetes.utils.DomainUtils.deleteDomainResource;
82+
import static oracle.weblogic.kubernetes.utils.DomainUtils.removeClusterInDomainResource;
8183
import static oracle.weblogic.kubernetes.utils.DomainUtils.verifyDomainStatusConditionTypeDoesNotExist;
8284
import static oracle.weblogic.kubernetes.utils.ImageUtils.createBaseRepoSecret;
8385
import static oracle.weblogic.kubernetes.utils.JobUtils.createDomainJob;
@@ -292,15 +294,16 @@ void testDomainK8sEventsNonExistingManagedServer() {
292294
@Test
293295
@DisplayName("Test domain Failed event for non-existing cluster")
294296
void testDomainK8sEventsNonExistingCluster() {
297+
String nonExistingClusterName = "nonexisting-cluster";
295298
OffsetDateTime timestamp = now();
296299
createClusterAndVerify(createClusterResource(
297-
"nonexisting-cluster", "nonexisting-cluster", domainNamespace3, replicaCount));
300+
nonExistingClusterName, nonExistingClusterName, domainNamespace3, replicaCount));
298301
logger.info("patch the domain resource with new cluster");
299302
try {
300303
String patchStr
301304
= "["
302305
+ "{\"op\": \"add\",\"path\": \"/spec/clusters/-\", \"value\": "
303-
+ " {\"name\" : \"nonexisting-cluster\"}"
306+
+ " {\"name\" : \"" + nonExistingClusterName + "\"}"
304307
+ "}]";
305308
logger.info("Updating domain configuration using patch string: {0}\n", patchStr);
306309
V1Patch patch = new V1Patch(patchStr);
@@ -311,12 +314,7 @@ void testDomainK8sEventsNonExistingCluster() {
311314
} finally {
312315
//remove the cluster from domain resource
313316
timestamp = now();
314-
String patchStr = "[{\"op\": \"remove\",\"path\": \"/spec/clusters/1\"}]";
315-
logger.info("Updating domain configuration using patch string: {0}\n", patchStr);
316-
V1Patch patch = new V1Patch(patchStr);
317-
assertTrue(patchDomainCustomResource(domainUid, domainNamespace3, patch, V1Patch.PATCH_FORMAT_JSON_PATCH),
318-
"Failed to patch domain");
319-
317+
assertDoesNotThrow(() -> removeClusterInDomainResource(nonExistingClusterName, domainUid, domainNamespace3));
320318
// verify the Changed event is generated
321319
checkEvent(opNamespace, domainNamespace3, domainUid, DOMAIN_CHANGED, "Normal", timestamp);
322320
}
@@ -478,7 +476,7 @@ void testScaleDomainAndVerifyCompletedEvent() {
478476
createDomain(domainNamespace4, domainUid, pvName4, pvcName4);
479477
scaleDomainAndVerifyCompletedEvent(1, ScaleAction.scaleDown, true, domainNamespace4);
480478
scaleDomainAndVerifyCompletedEvent(2, ScaleAction.scaleUp, true, domainNamespace4);
481-
shutdownDomain(domainUid, domainNamespace4);
479+
deleteDomainResource(domainNamespace4, domainUid);
482480
}
483481

484482
/**
@@ -991,4 +989,5 @@ private void scaleDomainAndVerifyCompletedEvent(int replicaCount, ScaleAction te
991989
opNamespace, namespace, domainUid, DOMAIN_COMPLETED, "Normal", timestamp, countBefore);
992990
}
993991
}
992+
994993
}

integration-tests/src/test/java/oracle/weblogic/kubernetes/utils/DomainUtils.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
import java.util.Map;
1717
import java.util.Optional;
1818
import java.util.Properties;
19+
import java.util.concurrent.Callable;
1920
import java.util.regex.Matcher;
2021
import java.util.regex.Pattern;
2122
import javax.annotation.Nonnull;
2223

2324
import io.kubernetes.client.custom.V1Patch;
25+
import io.kubernetes.client.openapi.ApiException;
2426
import io.kubernetes.client.openapi.models.V1ConfigMap;
2527
import io.kubernetes.client.openapi.models.V1ConfigMapVolumeSource;
2628
import io.kubernetes.client.openapi.models.V1Container;
@@ -428,6 +430,45 @@ public static void deleteDomainResource(String domainNS, String domainUid) {
428430
domainNS);
429431
}
430432

433+
/**
434+
* Remove cluster reference from domain resource.
435+
*
436+
* @param clusterName name of the cluster to be removed from domain resource
437+
* @param domainUid name of the domain resource
438+
* @param namespace namespace in which domain resource exists
439+
* @throws ApiException throws when cluster interaction fails
440+
*/
441+
public static void removeClusterInDomainResource(String clusterName, String domainUid, String namespace)
442+
throws ApiException {
443+
LoggingFacade logger = getLogger();
444+
logger.info("Removing the cluster {0} from domain resource {1}", clusterName, domainUid);
445+
DomainResource domainCustomResource = getDomainCustomResource(domainUid, namespace);
446+
Optional<V1LocalObjectReference> cluster = domainCustomResource.getSpec()
447+
.getClusters().stream().filter(o -> o.getName().equals(clusterName)).findAny();
448+
int clusterIndex = -1;
449+
if (cluster.isPresent()) {
450+
clusterIndex = domainCustomResource.getSpec().getClusters().indexOf(cluster.get());
451+
logger.info("Cluster index is {0}", clusterIndex);
452+
} else {
453+
logger.warning("Cluster {0} not found in domain resource {1} in namespace {2}",
454+
clusterName, domainUid, namespace);
455+
}
456+
if (clusterIndex != -1) {
457+
String patchStr = "[{\"op\": \"remove\",\"path\": \"/spec/clusters/" + clusterIndex + "\"}]";
458+
logger.info("Updating domain configuration using patch string: {0}\n", patchStr);
459+
V1Patch patch = new V1Patch(patchStr);
460+
assertTrue(patchDomainCustomResource(domainUid, namespace, patch, V1Patch.PATCH_FORMAT_JSON_PATCH),
461+
"Failed to patch domain");
462+
Callable<Boolean> clusterNotFound = () -> !getDomainCustomResource(domainUid, namespace).getSpec()
463+
.getClusters().stream().anyMatch(c -> c.getName().equals(clusterName));
464+
testUntil(clusterNotFound, logger, "cluster {0} to be removed from domain resource in namespace {1}",
465+
clusterName, namespace);
466+
} else {
467+
logger.warning("Cluster {0} not found in domain resource {1} in namespace {2}",
468+
clusterName, domainUid, namespace);
469+
}
470+
}
471+
431472
/**
432473
* Patch a domain with auxiliary image and verify pods are rolling restarted.
433474
* @param oldImageName old auxiliary image name

0 commit comments

Comments
 (0)