|
| 1 | +// Copyright (c) 2021, Oracle and/or its affiliates. |
| 2 | +// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 3 | + |
| 4 | +package oracle.weblogic.kubernetes; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import oracle.weblogic.kubernetes.actions.impl.primitive.Slammer; |
| 9 | +import oracle.weblogic.kubernetes.actions.impl.primitive.SlammerParams; |
| 10 | +import oracle.weblogic.kubernetes.annotations.IntegrationTest; |
| 11 | +import oracle.weblogic.kubernetes.annotations.Namespaces; |
| 12 | +import oracle.weblogic.kubernetes.logging.LoggingFacade; |
| 13 | +import oracle.weblogic.kubernetes.utils.SlammerUtils; |
| 14 | +import org.awaitility.core.ConditionFactory; |
| 15 | +import org.junit.jupiter.api.BeforeAll; |
| 16 | +import org.junit.jupiter.api.DisplayName; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 20 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 21 | +import static oracle.weblogic.kubernetes.TestConstants.ADMIN_SERVER_NAME_BASE; |
| 22 | +import static oracle.weblogic.kubernetes.TestConstants.MANAGED_SERVER_NAME_BASE; |
| 23 | +import static oracle.weblogic.kubernetes.TestConstants.MII_BASIC_IMAGE_NAME; |
| 24 | +import static oracle.weblogic.kubernetes.TestConstants.MII_BASIC_IMAGE_TAG; |
| 25 | +import static oracle.weblogic.kubernetes.actions.TestActions.scaleCluster; |
| 26 | +import static oracle.weblogic.kubernetes.utils.CommonMiiTestUtils.createMiiDomainAndVerify; |
| 27 | +import static oracle.weblogic.kubernetes.utils.CommonTestUtils.checkPodReadyAndServiceExists; |
| 28 | +import static oracle.weblogic.kubernetes.utils.OperatorUtils.installAndVerifyOperator; |
| 29 | +import static oracle.weblogic.kubernetes.utils.ThreadSafeLogger.getLogger; |
| 30 | +import static org.awaitility.Awaitility.with; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 34 | + |
| 35 | + |
| 36 | +// Test resilience using slammer |
| 37 | +@DisplayName("Test resilience using slammer") |
| 38 | +@IntegrationTest |
| 39 | +class ItResilience { |
| 40 | + private static String opNamespace = null; |
| 41 | + private static String domainNamespace = null; |
| 42 | + private static String domainUid = "domain1"; |
| 43 | + private static ConditionFactory withStandardRetryPolicy = null; |
| 44 | + |
| 45 | + private static String adminServerPodName = String.format("%s-%s", domainUid, ADMIN_SERVER_NAME_BASE); |
| 46 | + private static String managedServerPrefix = String.format("%s-%s", domainUid, MANAGED_SERVER_NAME_BASE); |
| 47 | + private static int replicaCount = 2; |
| 48 | + private static LoggingFacade logger = null; |
| 49 | + private static String ingressHost = null; //only used for OKD |
| 50 | + |
| 51 | + /** |
| 52 | + * Perform initialization for all the tests in this class. |
| 53 | + * Set up the necessary namespaces, install the operator in the first namespace, and |
| 54 | + * create a domain in the second namespace using the pre-created basic MII image. |
| 55 | + * |
| 56 | + * @param namespaces list of namespaces created by the IntegrationTestWatcher by the |
| 57 | + * JUnit engine parameter resolution mechanism |
| 58 | + */ |
| 59 | + @BeforeAll |
| 60 | + public static void initAll(@Namespaces(2) List<String> namespaces) { |
| 61 | + logger = getLogger(); |
| 62 | + // create standard, reusable retry/backoff policy |
| 63 | + withStandardRetryPolicy = with().pollDelay(2, SECONDS) |
| 64 | + .and().with().pollInterval(10, SECONDS) |
| 65 | + .atMost(6, MINUTES).await(); |
| 66 | + |
| 67 | + //install slammer |
| 68 | + Slammer.installSlammer(); |
| 69 | + |
| 70 | + //check if slammer is up |
| 71 | + assertTrue(Slammer.list("network"), "Can't reach slammer"); |
| 72 | + // get namespaces |
| 73 | + assertNotNull(namespaces.get(0), "Namespace namespaces.get(0) is null"); |
| 74 | + opNamespace = namespaces.get(0); |
| 75 | + |
| 76 | + assertNotNull(namespaces.get(1), "Namespace namespaces.get(1) is null"); |
| 77 | + domainNamespace = namespaces.get(1); |
| 78 | + |
| 79 | + // install the operator |
| 80 | + logger.info("Install an operator in namespace {0}, managing namespace {1}", |
| 81 | + opNamespace, domainNamespace); |
| 82 | + installAndVerifyOperator(opNamespace, domainNamespace); |
| 83 | + |
| 84 | + // create a domain resource |
| 85 | + logger.info("Create model-in-image domain {0} in namespace {1}, and wait until it comes up", |
| 86 | + domainUid, domainNamespace); |
| 87 | + createMiiDomainAndVerify( |
| 88 | + domainNamespace, |
| 89 | + domainUid, |
| 90 | + MII_BASIC_IMAGE_NAME + ":" + MII_BASIC_IMAGE_TAG, |
| 91 | + adminServerPodName, |
| 92 | + managedServerPrefix, |
| 93 | + replicaCount); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * verify the cluster is scaled up. |
| 98 | + */ |
| 99 | + @Test |
| 100 | + @DisplayName("increase replica count for the domain, and verify cluster is scaled up") |
| 101 | + void testNetworkDelayVerifyScaling() { |
| 102 | + |
| 103 | + try { |
| 104 | + //check if slammer is up |
| 105 | + assertTrue(Slammer.list("network"), "Can't reach slammer"); |
| 106 | + |
| 107 | + // check new server is started and existing servers are running |
| 108 | + logger.info("Check admin service and pod {0} is created in namespace {1}", |
| 109 | + adminServerPodName, domainNamespace); |
| 110 | + checkPodReadyAndServiceExists(adminServerPodName, domainUid, domainNamespace); |
| 111 | + |
| 112 | + // check managed server services and pods are ready |
| 113 | + for (int i = 1; i <= replicaCount; i++) { |
| 114 | + logger.info("Wait for managed server pod {0} to be ready in namespace {1}", |
| 115 | + managedServerPrefix + i, domainNamespace); |
| 116 | + checkPodReadyAndServiceExists(managedServerPrefix + i, domainUid, domainNamespace); |
| 117 | + } |
| 118 | + |
| 119 | + Thread t2 = new ScalingUpThread(5); |
| 120 | + SlammerParams params = new SlammerParams().delay("9"); |
| 121 | + Thread t1 = new SlammerThread(params); |
| 122 | + t1.start(); |
| 123 | + t2.start(); |
| 124 | + |
| 125 | + assertDoesNotThrow(() -> t2.join(100 * 1000), "failed to join thread"); |
| 126 | + assertDoesNotThrow(() -> t1.join(100 * 1000), "failed to join thread"); |
| 127 | + // check managed server services and pods are ready |
| 128 | + for (int i = 1; i <= 5; i++) { |
| 129 | + logger.info("Wait for managed server pod {0} to be ready in namespace {1}", |
| 130 | + managedServerPrefix + i, domainNamespace); |
| 131 | + checkPodReadyAndServiceExists(managedServerPrefix + i, domainUid, domainNamespace); |
| 132 | + } |
| 133 | + } finally { |
| 134 | + SlammerUtils.deleteNetworkLatencyDelay(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private void runScaleOperation(int replicaCount) { |
| 139 | + // scale up the domain by increasing replica count |
| 140 | + boolean scalingSuccess = assertDoesNotThrow(() -> |
| 141 | + scaleCluster(domainUid, domainNamespace, "cluster-1", replicaCount), |
| 142 | + String.format("Scaling the cluster cluster-1 of domain %s in namespace %s failed", domainUid, domainNamespace)); |
| 143 | + assertTrue(scalingSuccess, |
| 144 | + String.format("Cluster scaling failed for domain %s in namespace %s", domainUid, domainNamespace)); |
| 145 | + } |
| 146 | + |
| 147 | + class ScalingUpThread extends Thread { |
| 148 | + int replicaCount; |
| 149 | + |
| 150 | + ScalingUpThread(int replicaCount) { |
| 151 | + this.replicaCount = replicaCount; |
| 152 | + } |
| 153 | + |
| 154 | + public void run() { |
| 155 | + logger.info("Started Scaling thread"); |
| 156 | + runScaleOperation(replicaCount); |
| 157 | + logger.info("Finished Scaling thread"); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + class SlammerThread extends Thread { |
| 162 | + SlammerParams params; |
| 163 | + |
| 164 | + SlammerThread(SlammerParams params) { |
| 165 | + this.params = params; |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + public void run() { |
| 170 | + logger.info("Started Slammer thread"); |
| 171 | + logger.info("Adding Network delay for " + params.getDelay()); |
| 172 | + Slammer.list("network"); |
| 173 | + assertTrue(SlammerUtils.addNetworkLatencyDelay(params.getDelay()), "addNetworkLatencyDelay failed"); |
| 174 | + Slammer.list("network"); |
| 175 | + logger.info("Finished Slammer thread"); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments