|
| 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 | +package oracle.kubernetes.operator; |
| 5 | + |
| 6 | +import java.nio.charset.Charset; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.logging.Level; |
| 13 | +import oracle.kubernetes.operator.utils.Domain; |
| 14 | +import oracle.kubernetes.operator.utils.DomainCrd; |
| 15 | +import oracle.kubernetes.operator.utils.ExecResult; |
| 16 | +import oracle.kubernetes.operator.utils.K8sTestUtils; |
| 17 | +import oracle.kubernetes.operator.utils.Operator; |
| 18 | +import oracle.kubernetes.operator.utils.TestUtils; |
| 19 | +import org.junit.AfterClass; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Assume; |
| 22 | +import org.junit.BeforeClass; |
| 23 | +import org.junit.FixMethodOrder; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runners.MethodSorters; |
| 26 | + |
| 27 | +/** |
| 28 | + * Simple JUnit test file used for testing Operator. |
| 29 | + * |
| 30 | + * <p>This test is used for testing pods being restarted by some properties change. |
| 31 | + */ |
| 32 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 33 | +public class ItInitContainers extends BaseTest { |
| 34 | + |
| 35 | + private static Domain domain = null; |
| 36 | + private static Operator operator; |
| 37 | + private static String domainUid = "domaininitcont"; |
| 38 | + private static String initContainerTmpDir = ""; |
| 39 | + private static String originalYaml; |
| 40 | + |
| 41 | + /** |
| 42 | + * This method gets called only once before any of the test methods are executed. It does the |
| 43 | + * initialization of the integration test properties defined in OperatorIT.properties and setting |
| 44 | + * the resultRoot, pvRoot and projectRoot attributes. Create Operator1 and domainOnPVUsingWLST |
| 45 | + * |
| 46 | + * @throws Exception exception |
| 47 | + */ |
| 48 | + @BeforeClass |
| 49 | + public static void staticPrepare() throws Exception { |
| 50 | + logger.info("staticPrepare------Begin"); |
| 51 | + // initialize test properties and create the directories |
| 52 | + if (!QUICKTEST) { |
| 53 | + initialize(APP_PROPS_FILE); |
| 54 | + |
| 55 | + logger.info("Checking if operator and domain are running, if not creating"); |
| 56 | + if (operator == null) { |
| 57 | + operator = TestUtils.createOperator(OPERATOR1_YAML); |
| 58 | + } |
| 59 | + initContainerTmpDir = BaseTest.getResultDir() + "/initconttemp"; |
| 60 | + Files.createDirectories(Paths.get(initContainerTmpDir)); |
| 61 | + |
| 62 | + domain = createInitContdomain(); |
| 63 | + originalYaml = |
| 64 | + BaseTest.getUserProjectsDir() |
| 65 | + + "/weblogic-domains/" |
| 66 | + + domain.getDomainUid() |
| 67 | + + "/domain.yaml"; |
| 68 | + Assert.assertNotNull(domain); |
| 69 | + } |
| 70 | + logger.info("staticPrepare------End"); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Releases k8s cluster lease, archives result, pv directories. |
| 75 | + * |
| 76 | + * @throws Exception exception |
| 77 | + */ |
| 78 | + @AfterClass |
| 79 | + public static void staticUnPrepare() throws Exception { |
| 80 | + if (!QUICKTEST) { |
| 81 | + logger.info("staticUnPrepare------Begin"); |
| 82 | + if (domain != null) { |
| 83 | + destroyInitContdomain(); |
| 84 | + } |
| 85 | + if (operator != null) { |
| 86 | + operator.destroy(); |
| 87 | + } |
| 88 | + tearDown(new Object() {}.getClass().getEnclosingClass().getSimpleName()); |
| 89 | + logger.info("staticUnPrepare------End"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static Domain createInitContdomain() throws Exception { |
| 94 | + Map<String, Object> domainMap = TestUtils.loadYaml(DOMAINONPV_WLST_YAML); |
| 95 | + domainMap.put("domainUID", domainUid); |
| 96 | + domainUid = (String) domainMap.get("domainUID"); |
| 97 | + logger.info("Creating and verifying the domain creation with domainUid: " + domainUid); |
| 98 | + domain = TestUtils.createDomain(domainMap); |
| 99 | + domain.verifyDomainCreated(); |
| 100 | + return domain; |
| 101 | + } |
| 102 | + |
| 103 | + private static void destroyInitContdomain() throws Exception { |
| 104 | + if (domain != null) { |
| 105 | + domain.destroy(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Add restartVersion:v1.1 at adminServer level and verify the admin pod is Terminated and |
| 111 | + * recreated |
| 112 | + * |
| 113 | + * @throws Exception when domain.yaml cannot be read or modified to include the |
| 114 | + * restartVersion:v1.1 |
| 115 | + */ |
| 116 | + @Test |
| 117 | + public void testAdminServerInitContainer() throws Exception { |
| 118 | + Assume.assumeFalse(QUICKTEST); |
| 119 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 120 | + logTestBegin(testMethodName); |
| 121 | + String podName = domainUid + "-" + domain.getAdminServerName(); |
| 122 | + |
| 123 | + // Modify the original domain yaml to include restartVersion in admin server node |
| 124 | + DomainCrd crd = new DomainCrd(originalYaml); |
| 125 | + crd.addInitContNode("spec", null, null); |
| 126 | + String modYaml = crd.getYamlTree(); |
| 127 | + logger.info(modYaml); |
| 128 | + |
| 129 | + // Write the modified yaml to a new file |
| 130 | + Path path = Paths.get(initContainerTmpDir, "domain.yaml"); |
| 131 | + logger.log(Level.INFO, "Path of the modified domain.yaml :{0}", path.toString()); |
| 132 | + Charset charset = StandardCharsets.UTF_8; |
| 133 | + Files.write(path, modYaml.getBytes(charset)); |
| 134 | + |
| 135 | + destroyInitContdomain(); |
| 136 | + |
| 137 | + // Apply the new yaml to update the domain |
| 138 | + logger.log(Level.INFO, "kubectl apply -f {0}", path.toString()); |
| 139 | + ExecResult exec = TestUtils.exec("kubectl apply -f " + path.toString()); |
| 140 | + logger.info(exec.stdout()); |
| 141 | + |
| 142 | + logger.info("Verifying if the admin server pod is recreated"); |
| 143 | + // domain.verifyAdminServerRestarted(); |
| 144 | + for (int i = 0; i < 30; i++) { |
| 145 | + Thread.sleep(1000 * 10); |
| 146 | + TestUtils.exec("kubectl get all --all-namespaces", true); |
| 147 | + } |
| 148 | + |
| 149 | + logger.log(Level.INFO, "SUCCESS - {0}", testMethodName); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Utility method to check if a pod is in Terminating or Running status. |
| 154 | + * |
| 155 | + * @param podName - String name of the pod to check the status for |
| 156 | + * @param podStatusExpected - String the expected status of Terminating || RUnning |
| 157 | + * @throws InterruptedException when thread is interrupted |
| 158 | + */ |
| 159 | + private void verifyPodStatus(String podName, String podStatusExpected) |
| 160 | + throws InterruptedException { |
| 161 | + K8sTestUtils testUtil = new K8sTestUtils(); |
| 162 | + String domain1LabelSelector = String.format("weblogic.domainUID in (%s)", domainUid); |
| 163 | + String namespace = domain.getDomainNs(); |
| 164 | + boolean gotExpected = false; |
| 165 | + for (int i = 0; i < BaseTest.getMaxIterationsPod(); i++) { |
| 166 | + if (podStatusExpected.equals("Terminating")) { |
| 167 | + if (testUtil.isPodTerminating(namespace, domain1LabelSelector, podName)) { |
| 168 | + gotExpected = true; |
| 169 | + break; |
| 170 | + } |
| 171 | + } else if (podStatusExpected.equals("Running")) { |
| 172 | + if (testUtil.isPodRunning(namespace, domain1LabelSelector, podName)) { |
| 173 | + gotExpected = true; |
| 174 | + break; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + Thread.sleep(BaseTest.getWaitTimePod() * 1000); |
| 179 | + } |
| 180 | + Assert.assertTrue("Didn't get the expected pod status", gotExpected); |
| 181 | + } |
| 182 | +} |
0 commit comments