|
| 1 | +// Copyright (c) 2023, 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.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import oracle.weblogic.kubernetes.actions.impl.primitive.Command; |
| 11 | +import oracle.weblogic.kubernetes.actions.impl.primitive.CommandParams; |
| 12 | +import oracle.weblogic.kubernetes.annotations.IntegrationTest; |
| 13 | +import oracle.weblogic.kubernetes.annotations.Namespaces; |
| 14 | +import oracle.weblogic.kubernetes.logging.LoggingFacade; |
| 15 | +import oracle.weblogic.kubernetes.utils.ExecResult; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.Assumptions; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.DisplayName; |
| 20 | +import org.junit.jupiter.api.MethodOrderer; |
| 21 | +import org.junit.jupiter.api.Order; |
| 22 | +import org.junit.jupiter.api.Tag; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.TestMethodOrder; |
| 25 | +import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; |
| 26 | + |
| 27 | +import static oracle.weblogic.kubernetes.TestConstants.BASE_IMAGES_REPO_SECRET_NAME; |
| 28 | +import static oracle.weblogic.kubernetes.TestConstants.BUSYBOX_IMAGE; |
| 29 | +import static oracle.weblogic.kubernetes.TestConstants.BUSYBOX_TAG; |
| 30 | +import static oracle.weblogic.kubernetes.TestConstants.K8S_NODEPORT_HOST; |
| 31 | +import static oracle.weblogic.kubernetes.TestConstants.KIND_CLUSTER; |
| 32 | +import static oracle.weblogic.kubernetes.TestConstants.KIND_REPO; |
| 33 | +import static oracle.weblogic.kubernetes.TestConstants.OKD; |
| 34 | +import static oracle.weblogic.kubernetes.TestConstants.RESULTS_ROOT; |
| 35 | +import static oracle.weblogic.kubernetes.TestConstants.TEST_IMAGES_REPO_SECRET_NAME; |
| 36 | +import static oracle.weblogic.kubernetes.TestConstants.TRAEFIK_INGRESS_IMAGE_NAME; |
| 37 | +import static oracle.weblogic.kubernetes.TestConstants.TRAEFIK_INGRESS_IMAGE_REGISTRY; |
| 38 | +import static oracle.weblogic.kubernetes.TestConstants.TRAEFIK_INGRESS_IMAGE_TAG; |
| 39 | +import static oracle.weblogic.kubernetes.TestConstants.TRAEFIK_RELEASE_NAME; |
| 40 | +import static oracle.weblogic.kubernetes.TestConstants.WEBLOGIC_IMAGE_TAG; |
| 41 | +import static oracle.weblogic.kubernetes.TestConstants.WEBLOGIC_IMAGE_TO_USE_IN_SPEC; |
| 42 | +import static oracle.weblogic.kubernetes.actions.ActionConstants.WDT_DOWNLOAD_URL; |
| 43 | +import static oracle.weblogic.kubernetes.actions.ActionConstants.WIT_DOWNLOAD_URL; |
| 44 | +import static oracle.weblogic.kubernetes.actions.ActionConstants.WIT_JAVA_HOME; |
| 45 | +import static oracle.weblogic.kubernetes.actions.TestActions.imagePull; |
| 46 | +import static oracle.weblogic.kubernetes.actions.TestActions.imagePush; |
| 47 | +import static oracle.weblogic.kubernetes.actions.TestActions.imageTag; |
| 48 | +import static oracle.weblogic.kubernetes.utils.CommonTestUtils.getUniqueName; |
| 49 | +import static oracle.weblogic.kubernetes.utils.ImageUtils.createBaseRepoSecret; |
| 50 | +import static oracle.weblogic.kubernetes.utils.ImageUtils.createTestRepoSecret; |
| 51 | +import static oracle.weblogic.kubernetes.utils.ThreadSafeLogger.getLogger; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 53 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 54 | + |
| 55 | +/** |
| 56 | + * Test and verify model in image WLS domain sample in legacy mode. |
| 57 | + * Test creates the model-in-image domain image and use that image to start the domain. |
| 58 | + */ |
| 59 | +@DisplayName("test model in image WLS domain sample in legacy mode") |
| 60 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 61 | +@IntegrationTest |
| 62 | +@Tag("kind-sequential") |
| 63 | +@DisabledIfEnvironmentVariable(named = "SKIP_WLS_SAMPLES", matches = "true") |
| 64 | +class ItWlsMiiLegacySample { |
| 65 | + |
| 66 | + private static final String miiSampleScript = "../operator/integration-tests/model-in-image/run-test.sh"; |
| 67 | + private static final String DOMAIN_CREATION_IMAGE_NAME = "wdt-domain-image"; |
| 68 | + private static final String DOMAIN_CREATION_IMAGE_WLS_TAG = "WLS-LEGACY-v1"; |
| 69 | + private static final String MODEL_IMAGE_WLS_TAG = "WLS-LEGACY-v2"; |
| 70 | + private static String traefikNamespace = null; |
| 71 | + private static Map<String, String> envMap = null; |
| 72 | + private static LoggingFacade logger = null; |
| 73 | + |
| 74 | + private boolean previousTestSuccessful = true; |
| 75 | + |
| 76 | + /** |
| 77 | + * Create namespaces and set environment variables for the test. |
| 78 | + * @param namespaces list of namespaces created by the IntegrationTestWatcher by the |
| 79 | + * JUnit engine parameter resolution mechanism |
| 80 | + */ |
| 81 | + @BeforeAll |
| 82 | + public static void initAll(@Namespaces(3) List<String> namespaces) { |
| 83 | + logger = getLogger(); |
| 84 | + |
| 85 | + // get a new unique opNamespace |
| 86 | + logger.info("Creating unique namespace for Operator"); |
| 87 | + assertNotNull(namespaces.get(0), "Namespace list is null"); |
| 88 | + String opNamespace = namespaces.get(0); |
| 89 | + |
| 90 | + logger.info("Creating unique namespace for Domain"); |
| 91 | + assertNotNull(namespaces.get(1), "Namespace list is null"); |
| 92 | + String domainNamespace = namespaces.get(1); |
| 93 | + |
| 94 | + logger.info("Creating unique namespace for Traefik"); |
| 95 | + assertNotNull(namespaces.get(2), "Namespace list is null"); |
| 96 | + traefikNamespace = namespaces.get(2); |
| 97 | + |
| 98 | + String miiSampleWorkDir = |
| 99 | + RESULTS_ROOT + "/" + domainNamespace + "/model-in-image-sample-work-dir"; |
| 100 | + |
| 101 | + // env variables to override default values in sample scripts |
| 102 | + envMap = new HashMap<>(); |
| 103 | + envMap.put("OPER_NAMESPACE", opNamespace); |
| 104 | + envMap.put("DOMAIN_NAMESPACE", domainNamespace); |
| 105 | + envMap.put("DOMAIN_UID1", getUniqueName("sample-domain1-")); |
| 106 | + envMap.put("DOMAIN_UID2", getUniqueName("sample-domain2-")); |
| 107 | + envMap.put("TRAEFIK_NAMESPACE", traefikNamespace); |
| 108 | + envMap.put("TRAEFIK_HTTP_NODEPORT", "0"); // 0-->dynamically choose the np |
| 109 | + envMap.put("TRAEFIK_HTTPS_NODEPORT", "0"); // 0-->dynamically choose the np |
| 110 | + envMap.put("TRAEFIK_NAME", TRAEFIK_RELEASE_NAME + "-" + traefikNamespace.substring(3)); |
| 111 | + envMap.put("TRAEFIK_IMAGE_REGISTRY", TRAEFIK_INGRESS_IMAGE_REGISTRY); |
| 112 | + envMap.put("TRAEFIK_IMAGE_REPOSITORY", TRAEFIK_INGRESS_IMAGE_NAME); |
| 113 | + envMap.put("TRAEFIK_IMAGE_TAG", TRAEFIK_INGRESS_IMAGE_TAG); |
| 114 | + envMap.put("WORKDIR", miiSampleWorkDir); |
| 115 | + envMap.put("BASE_IMAGE_NAME", WEBLOGIC_IMAGE_TO_USE_IN_SPEC |
| 116 | + .substring(0, WEBLOGIC_IMAGE_TO_USE_IN_SPEC.lastIndexOf(":"))); |
| 117 | + envMap.put("BASE_IMAGE_TAG", WEBLOGIC_IMAGE_TAG); |
| 118 | + envMap.put("IMAGE_PULL_SECRET_NAME", BASE_IMAGES_REPO_SECRET_NAME); |
| 119 | + envMap.put("DOMAIN_IMAGE_PULL_SECRET_NAME", TEST_IMAGES_REPO_SECRET_NAME); |
| 120 | + envMap.put("K8S_NODEPORT_HOST", K8S_NODEPORT_HOST); |
| 121 | + envMap.put("OKD", "" + OKD); |
| 122 | + envMap.put("KIND_CLUSTER", "" + KIND_CLUSTER); |
| 123 | + |
| 124 | + // kind cluster uses openjdk which is not supported by image tool |
| 125 | + if (WIT_JAVA_HOME != null) { |
| 126 | + envMap.put("JAVA_HOME", WIT_JAVA_HOME); |
| 127 | + } |
| 128 | + |
| 129 | + if (WIT_DOWNLOAD_URL != null) { |
| 130 | + envMap.put("WIT_INSTALLER_URL", WIT_DOWNLOAD_URL); |
| 131 | + } |
| 132 | + |
| 133 | + if (WDT_DOWNLOAD_URL != null) { |
| 134 | + envMap.put("WDT_INSTALLER_URL", WDT_DOWNLOAD_URL); |
| 135 | + } |
| 136 | + logger.info("Environment variables to the script {0}", envMap); |
| 137 | + |
| 138 | + logger.info("Setting up image registry secrets"); |
| 139 | + // Create the repo secret to pull the domain image |
| 140 | + // this secret is used only for non-kind cluster |
| 141 | + createTestRepoSecret(domainNamespace); |
| 142 | + logger.info("Registry secret {0} created for domain image successfully in namespace {1}", |
| 143 | + TEST_IMAGES_REPO_SECRET_NAME, domainNamespace); |
| 144 | + // Create the repo secret to pull the base image |
| 145 | + // this secret is used only for non-kind cluster |
| 146 | + createBaseRepoSecret(domainNamespace); |
| 147 | + logger.info("Registry secret {0} for base image created successfully in namespace {1}", |
| 148 | + BASE_IMAGES_REPO_SECRET_NAME, domainNamespace); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Test model in image sample install operator use case. |
| 153 | + */ |
| 154 | + @Test |
| 155 | + @Order(1) |
| 156 | + public void testInstallOperator() { |
| 157 | + execTestScriptAndAssertSuccess("-oper", "Failed to run -oper"); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Test model in image sample install Traefik use case. |
| 162 | + */ |
| 163 | + @Test |
| 164 | + @Order(2) |
| 165 | + public void testInstallTraefik() { |
| 166 | + execTestScriptAndAssertSuccess("-traefik", "Failed to run -traefik"); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Test model in image sample building image use case. |
| 171 | + */ |
| 172 | + @Test |
| 173 | + @Order(3) |
| 174 | + public void testInitialImage() { |
| 175 | + imagePull(BUSYBOX_IMAGE + ":" + BUSYBOX_TAG); |
| 176 | + imageTag(BUSYBOX_IMAGE + ":" + BUSYBOX_TAG, "busybox"); |
| 177 | + execTestScriptAndAssertSuccess("-initial-image", "Failed to run -initial-image"); |
| 178 | + |
| 179 | + // load the image to kind if using kind cluster |
| 180 | + if (KIND_REPO != null) { |
| 181 | + String imageCreated = DOMAIN_CREATION_IMAGE_NAME + ":" + DOMAIN_CREATION_IMAGE_WLS_TAG; |
| 182 | + logger.info("loading image {0} to kind", imageCreated); |
| 183 | + imagePush(imageCreated); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Test model in image sample create domain use case. |
| 189 | + */ |
| 190 | + @Test |
| 191 | + @Order(4) |
| 192 | + public void testInitialMain() { |
| 193 | + // load the base image to kind if using kind cluster |
| 194 | + if (KIND_REPO != null) { |
| 195 | + logger.info("loading image {0} to kind", WEBLOGIC_IMAGE_TO_USE_IN_SPEC); |
| 196 | + imagePush(WEBLOGIC_IMAGE_TO_USE_IN_SPEC); |
| 197 | + } |
| 198 | + |
| 199 | + execTestScriptAndAssertSuccess("-initial-main", "Failed to run -initial-main"); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Test model in image sample update domain use case 1. |
| 204 | + */ |
| 205 | + @Test |
| 206 | + @Order(5) |
| 207 | + public void testUpate1() { |
| 208 | + execTestScriptAndAssertSuccess("-update1", "Failed to run -update1"); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Test model in image sample update domain use case 2. |
| 213 | + */ |
| 214 | + @Test |
| 215 | + @Order(6) |
| 216 | + public void testUpate2() { |
| 217 | + execTestScriptAndAssertSuccess("-update2", "Failed to run -update2"); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Test model in image sample update domain use case 3. |
| 222 | + */ |
| 223 | + @Test |
| 224 | + @Order(7) |
| 225 | + public void testUpate3() { |
| 226 | + execTestScriptAndAssertSuccess("-update3-image", "Failed to run -update3-image"); |
| 227 | + |
| 228 | + // load the image to kind if using kind cluster |
| 229 | + if (KIND_REPO != null) { |
| 230 | + String imageUpdated = DOMAIN_CREATION_IMAGE_NAME + ":" + MODEL_IMAGE_WLS_TAG; |
| 231 | + logger.info("loading image {0} to kind", imageUpdated); |
| 232 | + imagePush(imageUpdated); |
| 233 | + } |
| 234 | + |
| 235 | + execTestScriptAndAssertSuccess("-update3-main", "Failed to run -update3-main"); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Test model in image sample update domain use case 4. |
| 240 | + */ |
| 241 | + @Test |
| 242 | + @Order(7) |
| 243 | + public void testUpate4() { |
| 244 | + execTestScriptAndAssertSuccess("-update4", "Failed to run -update4"); |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * Run script run-test.sh. |
| 249 | + * @param arg arguments to execute script |
| 250 | + * @param errString a string of detailed error |
| 251 | + */ |
| 252 | + private void execTestScriptAndAssertSuccess(String arg, |
| 253 | + String errString) { |
| 254 | + |
| 255 | + Assumptions.assumeTrue(previousTestSuccessful); |
| 256 | + previousTestSuccessful = false; |
| 257 | + |
| 258 | + String command = miiSampleScript |
| 259 | + + " -legacy " |
| 260 | + + arg; |
| 261 | + |
| 262 | + ExecResult result = Command.withParams( |
| 263 | + new CommandParams() |
| 264 | + .command(command) |
| 265 | + .env(envMap) |
| 266 | + .redirect(true) |
| 267 | + ).executeAndReturnResult(); |
| 268 | + |
| 269 | + boolean success = |
| 270 | + result != null |
| 271 | + && result.exitValue() == 0 |
| 272 | + && result.stdout() != null |
| 273 | + && result.stdout().contains("Finished without errors"); |
| 274 | + |
| 275 | + String outStr = errString; |
| 276 | + outStr += ", command=\n{\n" + command + "\n}\n"; |
| 277 | + outStr += ", stderr=\n{\n" + (result != null ? result.stderr() : "") + "\n}\n"; |
| 278 | + outStr += ", stdout=\n{\n" + (result != null ? result.stdout() : "") + "\n}\n"; |
| 279 | + |
| 280 | + assertTrue(success, outStr); |
| 281 | + |
| 282 | + previousTestSuccessful = true; |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * Uninstall Traefik. |
| 287 | + */ |
| 288 | + @AfterAll |
| 289 | + public static void tearDownAll() { |
| 290 | + logger = getLogger(); |
| 291 | + // uninstall traefik |
| 292 | + if (traefikNamespace != null) { |
| 293 | + logger.info("Uninstall Traefik"); |
| 294 | + Command.withParams(new CommandParams() |
| 295 | + .command("helm uninstall traefik-operator -n " + traefikNamespace) |
| 296 | + .redirect(true)).execute(); |
| 297 | + } |
| 298 | + |
| 299 | + } |
| 300 | +} |
0 commit comments