|
| 1 | +// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Universal Permissive License v 1.0 as shown at |
| 3 | +// http://oss.oracle.com/licenses/upl. |
| 4 | + |
| 5 | +package oracle.kubernetes.operator; |
| 6 | + |
| 7 | +import java.nio.charset.Charset; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.logging.Level; |
| 15 | + |
| 16 | +import org.junit.AfterClass; |
| 17 | +import org.junit.BeforeClass; |
| 18 | +import org.junit.FixMethodOrder; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runners.MethodSorters; |
| 21 | + |
| 22 | +import oracle.kubernetes.operator.utils.Domain; |
| 23 | +import oracle.kubernetes.operator.utils.DomainCrd; |
| 24 | +import oracle.kubernetes.operator.utils.ExecResult; |
| 25 | +import oracle.kubernetes.operator.utils.Operator; |
| 26 | +import oracle.kubernetes.operator.utils.TestUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * Simple JUnit test file used for testing domain pod templates. |
| 30 | + * |
| 31 | + * <p> |
| 32 | + * This test is used for creating Operator(s) and domain which uses pod templates. |
| 33 | + */ |
| 34 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 35 | +public class ItPodTemplates extends BaseTest { |
| 36 | + |
| 37 | + private static Operator operator1; |
| 38 | + |
| 39 | + /** |
| 40 | + * This method gets called only once before any of the test methods are |
| 41 | + * executed. It does the initialization of the integration test properties |
| 42 | + * defined in OperatorIT.properties and setting the resultRoot, pvRoot and |
| 43 | + * projectRoot attributes. |
| 44 | + * |
| 45 | + * @throws Exception |
| 46 | + * exception |
| 47 | + */ |
| 48 | + @BeforeClass |
| 49 | + public static void staticPrepare() throws Exception { |
| 50 | + // initialize test properties and create the directories |
| 51 | + initialize(APP_PROPS_FILE); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Releases k8s cluster lease, archives result, pv directories. |
| 56 | + * |
| 57 | + * @throws Exception |
| 58 | + * exception |
| 59 | + */ |
| 60 | + @AfterClass |
| 61 | + public static void staticUnPrepare() throws Exception { |
| 62 | + logger.info("+++++++++++++++++++++++++++++++++---------------------------------+"); |
| 63 | + logger.info("BEGIN"); |
| 64 | + logger.info("Run once, release cluster lease"); |
| 65 | + |
| 66 | + tearDown(new Object() { |
| 67 | + }.getClass().getEnclosingClass().getSimpleName()); |
| 68 | + |
| 69 | + logger.info("SUCCESS"); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Test pod templates using all the variables $(SERVER_NAME), $(DOMAIN_NAME), $(DOMAIN_UID), |
| 74 | + * $(DOMAIN_HOME), $(LOG_HOME) and $(CLUSTER_NAME) in serverPod. Make sure the domain comes up |
| 75 | + * successfully. |
| 76 | + * @throws Exception when the domain crd creation fails or when updating the serverPod with variables |
| 77 | + * |
| 78 | + */ |
| 79 | + @Test |
| 80 | + public void testPodTemplateUsingVariables() throws Exception { |
| 81 | + String testMethodName = new Object() { |
| 82 | + }.getClass().getEnclosingMethod().getName(); |
| 83 | + logTestBegin(testMethodName); |
| 84 | + logger.info("Creating Operator & waiting for the script to complete execution"); |
| 85 | + // create operator1 |
| 86 | + if (operator1 == null) { |
| 87 | + operator1 = TestUtils.createOperator(OPERATOR1_YAML); |
| 88 | + } |
| 89 | + Domain domain = null; |
| 90 | + boolean testCompletedSuccessfully = false; |
| 91 | + try { |
| 92 | + Map<String, Object> domainMap = TestUtils.loadYaml(DOMAINONPV_WLST_YAML); |
| 93 | + domainMap.put("domainUID", "podtemplatedomain"); |
| 94 | + domainMap.put("adminNodePort", new Integer("30713")); |
| 95 | + // just create domain yaml, dont apply |
| 96 | + domain = TestUtils.createDomain(domainMap, false); |
| 97 | + String originalYaml = BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domain.getDomainUid() |
| 98 | + + "/domain.yaml"; |
| 99 | + |
| 100 | + // Modify the original domain yaml to include labels in serverPod |
| 101 | + // node |
| 102 | + DomainCrd crd = new DomainCrd(originalYaml); |
| 103 | + // add labels to serverPod |
| 104 | + Map<String, String> labelKeyValue = new HashMap(); |
| 105 | + labelKeyValue.put("servername", "$(SERVER_NAME)"); |
| 106 | + labelKeyValue.put("domainname", "$(DOMAIN_NAME)"); |
| 107 | + labelKeyValue.put("domainuid", "$(DOMAIN_UID)"); |
| 108 | + crd.addObjectNodeToServerPod("labels",labelKeyValue); |
| 109 | + |
| 110 | + //add annotations to serverPod as DOMAIN_HOME and LOG_HOME contains "/" which is not allowed in labels |
| 111 | + Map<String, String> envKeyValue = new HashMap(); |
| 112 | + envKeyValue.put("domainhome", "$(DOMAIN_HOME)"); |
| 113 | + envKeyValue.put("loghome", "$(LOG_HOME)"); |
| 114 | + crd.addObjectNodeToServerPod("annotations",envKeyValue); |
| 115 | + |
| 116 | + //add label to cluster serverPod for CLUSTER_NAME |
| 117 | + Map<String, String> clusterLabelKeyValue = new HashMap(); |
| 118 | + clusterLabelKeyValue.put("clustername", "$(CLUSTER_NAME)"); |
| 119 | + crd.addObjectNodeToClusterServerPod(domain.getClusterName(), "labels", clusterLabelKeyValue); |
| 120 | + |
| 121 | + String modYaml = crd.getYamlTree(); |
| 122 | + logger.info(modYaml); |
| 123 | + |
| 124 | + // Write the modified yaml to a new file |
| 125 | + Path path = Paths.get(BaseTest.getUserProjectsDir() + "/weblogic-domains/" + domain.getDomainUid(), |
| 126 | + "domain.modified.yaml"); |
| 127 | + logger.log(Level.INFO, "Path of the modified domain.yaml :{0}", path.toString()); |
| 128 | + Charset charset = StandardCharsets.UTF_8; |
| 129 | + Files.write(path, modYaml.getBytes(charset)); |
| 130 | + |
| 131 | + // Apply the new yaml to update the domain |
| 132 | + logger.log(Level.INFO, "kubectl apply -f {0}", path.toString()); |
| 133 | + ExecResult exec = TestUtils.exec("kubectl apply -f " + path.toString()); |
| 134 | + logger.info(exec.stdout()); |
| 135 | + |
| 136 | + domain.verifyDomainCreated(); |
| 137 | + testCompletedSuccessfully = true; |
| 138 | + } finally { |
| 139 | + if (domain != null && !SMOKETEST && (JENKINS || testCompletedSuccessfully)) { |
| 140 | + domain.shutdownUsingServerStartPolicy(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + logger.info("SUCCESS - " + testMethodName); |
| 145 | + } |
| 146 | +} |
0 commit comments