|
| 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.Operator; |
| 17 | +import oracle.kubernetes.operator.utils.TestUtils; |
| 18 | +import org.junit.AfterClass; |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Assume; |
| 21 | +import org.junit.BeforeClass; |
| 22 | +import org.junit.FixMethodOrder; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runners.MethodSorters; |
| 25 | + |
| 26 | +/** Integration tests for testing the init container for weblogic server pods */ |
| 27 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 28 | +public class ItInitContainers extends BaseTest { |
| 29 | + |
| 30 | + private static Domain domain = null; |
| 31 | + private static Operator operator; |
| 32 | + private static String domainUid = "domaininitcont"; |
| 33 | + private static String initContainerTmpDir = ""; |
| 34 | + private static String originalYaml; |
| 35 | + |
| 36 | + /** |
| 37 | + * This method gets called only once before any of the test methods are executed. It does the |
| 38 | + * initialization of the integration test properties defined in OperatorIT.properties and setting |
| 39 | + * the resultRoot, pvRoot and projectRoot attributes. Create Operator1 and domainOnPVUsingWLST |
| 40 | + * |
| 41 | + * @throws Exception exception |
| 42 | + */ |
| 43 | + @BeforeClass |
| 44 | + public static void staticPrepare() throws Exception { |
| 45 | + logger.info("staticPrepare------Begin"); |
| 46 | + // initialize test properties and create the directories |
| 47 | + if (!QUICKTEST) { |
| 48 | + initialize(APP_PROPS_FILE); |
| 49 | + |
| 50 | + logger.info("Checking if operator and domain are running, if not creating"); |
| 51 | + if (operator == null) { |
| 52 | + operator = TestUtils.createOperator(OPERATOR1_YAML); |
| 53 | + } |
| 54 | + initContainerTmpDir = BaseTest.getResultDir() + "/initconttemp"; |
| 55 | + Files.createDirectories(Paths.get(initContainerTmpDir)); |
| 56 | + |
| 57 | + domain = createInitContdomain(); |
| 58 | + originalYaml = |
| 59 | + BaseTest.getUserProjectsDir() |
| 60 | + + "/weblogic-domains/" |
| 61 | + + domain.getDomainUid() |
| 62 | + + "/domain.yaml"; |
| 63 | + Assert.assertNotNull(domain); |
| 64 | + } |
| 65 | + logger.info("staticPrepare------End"); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Releases k8s cluster lease, archives result, pv directories. |
| 70 | + * |
| 71 | + * @throws Exception exception |
| 72 | + */ |
| 73 | + @AfterClass |
| 74 | + public static void staticUnPrepare() throws Exception { |
| 75 | + if (!QUICKTEST) { |
| 76 | + logger.info("staticUnPrepare------Begin"); |
| 77 | + if (domain != null) { |
| 78 | + destroyInitContdomain(); |
| 79 | + } |
| 80 | + if (operator != null) { |
| 81 | + operator.destroy(); |
| 82 | + } |
| 83 | + tearDown(new Object() {}.getClass().getEnclosingClass().getSimpleName()); |
| 84 | + logger.info("staticUnPrepare------End"); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * creates the init container domain on PV |
| 90 | + * |
| 91 | + * @return created domain Domain |
| 92 | + * @throws Exception when domain creation fails |
| 93 | + */ |
| 94 | + private static Domain createInitContdomain() throws Exception { |
| 95 | + Map<String, Object> domainMap = TestUtils.loadYaml(DOMAINONPV_WLST_YAML); |
| 96 | + domainMap.put("domainUID", domainUid); |
| 97 | + domainUid = (String) domainMap.get("domainUID"); |
| 98 | + logger.info("Creating and verifying the domain creation with domainUid: " + domainUid); |
| 99 | + domain = TestUtils.createDomain(domainMap); |
| 100 | + domain.verifyDomainCreated(); |
| 101 | + return domain; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * destroys the running domain |
| 106 | + * |
| 107 | + * @throws Exception when domain destruction fails |
| 108 | + */ |
| 109 | + private static void destroyInitContdomain() throws Exception { |
| 110 | + if (domain != null) { |
| 111 | + domain.destroy(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Add initContainers at domain spec level and verify the admin server pod goes through Init state |
| 117 | + * before starting the admin server pod |
| 118 | + * |
| 119 | + * @throws Exception when domain.yaml cannot be read or modified to include the initContainers or |
| 120 | + * weblogic server pod doesn't go through initialization and ready state |
| 121 | + */ |
| 122 | + @Test |
| 123 | + public void testDomainInitContainer() throws Exception { |
| 124 | + Assume.assumeFalse(QUICKTEST); |
| 125 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 126 | + logTestBegin(testMethodName); |
| 127 | + String pods[] = {domainUid + "-" + domain.getAdminServerName(), domainUid + "-managed-server1"}; |
| 128 | + |
| 129 | + // Modify the original domain yaml to include restartVersion in admin server node |
| 130 | + DomainCrd crd = new DomainCrd(originalYaml); |
| 131 | + crd.addInitContNode("spec", null, null, "busybox", "sleep"); |
| 132 | + String modYaml = crd.getYamlTree(); |
| 133 | + logger.info(modYaml); |
| 134 | + testInitContainer(modYaml); |
| 135 | + for (String pod : pods) { |
| 136 | + logger.info("Verifying if the pods are recreated with initialization"); |
| 137 | + verifyPodInitialized(pod); |
| 138 | + } |
| 139 | + logger.log(Level.INFO, "SUCCESS - {0}", testMethodName); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Add initContainers to adminServer and verify the admin server pod goes through Init state |
| 144 | + * before starting the admin server pod |
| 145 | + * |
| 146 | + * @throws Exception when domain.yaml cannot be read or modified to include the initContainers or |
| 147 | + * weblogic server pod doesn't go through initialization and ready state |
| 148 | + */ |
| 149 | + @Test |
| 150 | + public void testAdminServerInitContainer() throws Exception { |
| 151 | + Assume.assumeFalse(QUICKTEST); |
| 152 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 153 | + logTestBegin(testMethodName); |
| 154 | + String adminPodName = domainUid + "-" + domain.getAdminServerName(); |
| 155 | + |
| 156 | + // Modify the original domain yaml to include restartVersion in admin server node |
| 157 | + DomainCrd crd = new DomainCrd(originalYaml); |
| 158 | + crd.addInitContNode("adminServer", null, null, "busybox", "sleep"); |
| 159 | + String modYaml = crd.getYamlTree(); |
| 160 | + logger.info(modYaml); |
| 161 | + testInitContainer(modYaml); |
| 162 | + logger.info("Verifying if the admin server pod is recreated with initialization"); |
| 163 | + verifyPodInitialized(adminPodName); |
| 164 | + logger.log(Level.INFO, "SUCCESS - {0}", testMethodName); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Add initContainers to adminServer and verify the admin server pod goes through Init state |
| 169 | + * before starting the admin server pod |
| 170 | + * |
| 171 | + * @throws Exception when domain.yaml cannot be read or modified to include the initContainers or |
| 172 | + * weblogic server pod doesn't go through initialization and ready state |
| 173 | + */ |
| 174 | + @Test |
| 175 | + public void testClusterInitContainer() throws Exception { |
| 176 | + Assume.assumeFalse(QUICKTEST); |
| 177 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 178 | + logTestBegin(testMethodName); |
| 179 | + String adminPodName = domainUid + "-" + domain.getAdminServerName(); |
| 180 | + String ms2PodName = domainUid + "-managed-server2"; |
| 181 | + |
| 182 | + // Modify the original domain yaml to include restartVersion in admin server node |
| 183 | + DomainCrd crd = new DomainCrd(originalYaml); |
| 184 | + crd.addInitContNode("clusters", "cluster-1", null, "busybox", "sleep"); |
| 185 | + String modYaml = crd.getYamlTree(); |
| 186 | + logger.info(modYaml); |
| 187 | + testInitContainer(modYaml); |
| 188 | + TestUtils.checkPodReady(adminPodName, domain.getDomainNs()); |
| 189 | + logger.info("Verifying if the managed server pods are recreated with initialization"); |
| 190 | + verifyPodInitialized(ms2PodName); |
| 191 | + logger.log(Level.INFO, "SUCCESS - {0}", testMethodName); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Add initContainers to adminServer and verify the admin server pod goes through Init state |
| 196 | + * before starting the admin server pod |
| 197 | + * |
| 198 | + * @throws Exception when domain.yaml cannot be read or modified to include the initContainers or |
| 199 | + * weblogic server pod doesn't go through initialization and ready state |
| 200 | + */ |
| 201 | + @Test |
| 202 | + public void testMSInitContainer() throws Exception { |
| 203 | + Assume.assumeFalse(QUICKTEST); |
| 204 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 205 | + logTestBegin(testMethodName); |
| 206 | + String adminPodName = domainUid + "-" + domain.getAdminServerName(); |
| 207 | + String ms1PodName = domainUid + "-managed-server1"; |
| 208 | + |
| 209 | + // Modify the original domain yaml to include restartVersion in admin server node |
| 210 | + DomainCrd crd = new DomainCrd(originalYaml); |
| 211 | + crd.addInitContNode("managedServers", "cluster-1", "managed-server1", "busybox", "sleep"); |
| 212 | + String modYaml = crd.getYamlTree(); |
| 213 | + logger.info(modYaml); |
| 214 | + testInitContainer(modYaml); |
| 215 | + TestUtils.checkPodReady(adminPodName, domain.getDomainNs()); |
| 216 | + logger.info("Verifying if the managed server pod is recreated with initialization"); |
| 217 | + verifyPodInitialized(ms1PodName); |
| 218 | + logger.log(Level.INFO, "SUCCESS - {0}", testMethodName); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Add initContainers which fails to run to completion and verify the weblogic server pods are not |
| 223 | + * started as result of it. |
| 224 | + * |
| 225 | + * @throws Exception when domain.yaml cannot be read or modified to include the initContainers or |
| 226 | + * weblogic server pod doesn't go through initialization and ready state |
| 227 | + */ |
| 228 | + @Test |
| 229 | + public void testDomainInitContainerNegative() throws Exception { |
| 230 | + Assume.assumeFalse(QUICKTEST); |
| 231 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 232 | + logTestBegin(testMethodName); |
| 233 | + String adminPodName = domainUid + "-" + domain.getAdminServerName(); |
| 234 | + |
| 235 | + // Modify the original domain yaml to include restartVersion in admin server node |
| 236 | + DomainCrd crd = new DomainCrd(originalYaml); |
| 237 | + crd.addInitContNode("spec", null, null, "busybox", "sleep"); |
| 238 | + String modYaml = crd.getYamlTree(); |
| 239 | + modYaml = modYaml.replaceAll("sleep", "foo"); |
| 240 | + logger.info(modYaml); |
| 241 | + testInitContainer(modYaml); |
| 242 | + String cmd = "kubectl get pod " + adminPodName + " -n " + domain.getDomainNs(); |
| 243 | + TestUtils.checkCmdInLoop(cmd, "Init:CrashLoopBackOff", adminPodName); |
| 244 | + logger.log(Level.INFO, "SUCCESS - {0}", testMethodName); |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * Add initContainers at domain and admin server level and verify init container runs at both |
| 249 | + * level when the names are different |
| 250 | + * |
| 251 | + * @throws Exception when domain.yaml cannot be read or modified to include the initContainers or |
| 252 | + * weblogic server pod doesn't go through initialization and ready state |
| 253 | + */ |
| 254 | + @Test |
| 255 | + public void testInitContainerDiffLevelDiffName() throws Exception { |
| 256 | + Assume.assumeFalse(QUICKTEST); |
| 257 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 258 | + logTestBegin(testMethodName); |
| 259 | + String pods[] = {domainUid + "-" + domain.getAdminServerName(), domainUid + "-managed-server2"}; |
| 260 | + |
| 261 | + // Modify the original domain yaml to include restartVersion in admin server node |
| 262 | + DomainCrd crd = new DomainCrd(originalYaml); |
| 263 | + crd.addInitContNode("spec", null, null, "busybox1", "sleep"); |
| 264 | + crd.addInitContNode("adminServer", null, null, "busybox2", "sleep"); |
| 265 | + String modYaml = crd.getYamlTree(); |
| 266 | + logger.info(modYaml); |
| 267 | + testInitContainer(modYaml); |
| 268 | + String cmd = "kubectl get pod " + pods[0] + " -n " + domain.getDomainNs(); |
| 269 | + |
| 270 | + TestUtils.checkCmdInLoop(cmd, "Init:0/2", pods[0]); |
| 271 | + TestUtils.checkCmdInLoop(cmd, "Init:1/2", pods[0]); |
| 272 | + TestUtils.checkPodReady(pods[0], domain.getDomainNs()); |
| 273 | + |
| 274 | + logger.info("Verifying if the pods are recreated with initialization"); |
| 275 | + verifyPodInitialized(pods[1]); |
| 276 | + logger.log(Level.INFO, "SUCCESS - {0}", testMethodName); |
| 277 | + } |
| 278 | + |
| 279 | + /** |
| 280 | + * Add initContainers at domain and admin server level and verify init container is not run at |
| 281 | + * both level when the names are same |
| 282 | + * |
| 283 | + * @throws Exception when domain.yaml cannot be read or modified to include the initContainers or |
| 284 | + * weblogic server pod doesn't go through initialization and ready state |
| 285 | + */ |
| 286 | + @Test |
| 287 | + public void testInitContainerDiffLevelSameName() throws Exception { |
| 288 | + Assume.assumeFalse(QUICKTEST); |
| 289 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 290 | + logTestBegin(testMethodName); |
| 291 | + String pods[] = {domainUid + "-" + domain.getAdminServerName(), domainUid + "-managed-server2"}; |
| 292 | + |
| 293 | + // Modify the original domain yaml to include restartVersion in admin server node |
| 294 | + DomainCrd crd = new DomainCrd(originalYaml); |
| 295 | + crd.addInitContNode("spec", null, null, "busybox", "foo"); |
| 296 | + crd.addInitContNode("adminServer", null, null, "busybox", "sleep"); |
| 297 | + crd.addInitContNode("clusters", "cluster-1", null, "busybox", "sleep"); |
| 298 | + String modYaml = crd.getYamlTree(); |
| 299 | + logger.info(modYaml); |
| 300 | + testInitContainer(modYaml); |
| 301 | + for (String pod : pods) { |
| 302 | + logger.info("Verifying if the pods are recreated with initialization"); |
| 303 | + verifyPodInitialized(pod); |
| 304 | + } |
| 305 | + logger.log(Level.INFO, "SUCCESS - {0}", testMethodName); |
| 306 | + } |
| 307 | + /** |
| 308 | + * Add multiple initContainers at domain level and verify all of the init containers are run |
| 309 | + * |
| 310 | + * @throws Exception when domain.yaml cannot be read or modified to include the initContainers or |
| 311 | + * weblogic server pod doesn't go through initialization and ready state |
| 312 | + */ |
| 313 | + @Test |
| 314 | + public void testInitContainerMultiple() throws Exception { |
| 315 | + Assume.assumeFalse(QUICKTEST); |
| 316 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 317 | + logTestBegin(testMethodName); |
| 318 | + String pods[] = {domainUid + "-" + domain.getAdminServerName(), domainUid + "-managed-server1"}; |
| 319 | + |
| 320 | + // Modify the original domain yaml to include restartVersion in admin server node |
| 321 | + DomainCrd crd = new DomainCrd(originalYaml); |
| 322 | + crd.addInitContNode("spec", null, null, "busybox1", "sleep"); |
| 323 | + crd.addInitContNode("spec", null, null, "busybox2", "sleep"); |
| 324 | + String modYaml = crd.getYamlTree(); |
| 325 | + logger.info(modYaml); |
| 326 | + testInitContainer(modYaml); |
| 327 | + String cmd; |
| 328 | + for (String pod : pods) { |
| 329 | + cmd = "kubectl get pod " + pod + " -n " + domain.getDomainNs(); |
| 330 | + TestUtils.checkCmdInLoop(cmd, "Init:0/2", pod); |
| 331 | + TestUtils.checkCmdInLoop(cmd, "Init:1/2", pod); |
| 332 | + TestUtils.checkPodReady(pod, domain.getDomainNs()); |
| 333 | + } |
| 334 | + logger.log(Level.INFO, "SUCCESS - {0}", testMethodName); |
| 335 | + } |
| 336 | + |
| 337 | + /** |
| 338 | + * Add initContainers to adminServer and verify the admin server pod goes through Init state |
| 339 | + * before starting the admin server pod |
| 340 | + * |
| 341 | + * @throws Exception when domain.yaml cannot be read or modified to include the initContainers or |
| 342 | + * weblogic server pod doesn't go through initialization and ready state |
| 343 | + */ |
| 344 | + private void testInitContainer(String modYaml) throws Exception { |
| 345 | + // Write the modified yaml to a new file |
| 346 | + Path path = Paths.get(initContainerTmpDir, "domain.yaml"); |
| 347 | + logger.log(Level.INFO, "Path of the modified domain.yaml :{0}", path.toString()); |
| 348 | + Charset charset = StandardCharsets.UTF_8; |
| 349 | + Files.write(path, modYaml.getBytes(charset)); |
| 350 | + |
| 351 | + destroyInitContdomain(); |
| 352 | + |
| 353 | + // Apply the new yaml to update the domain |
| 354 | + logger.log(Level.INFO, "kubectl apply -f {0}", path.toString()); |
| 355 | + ExecResult exec = TestUtils.exec("kubectl apply -f " + path.toString()); |
| 356 | + logger.info(exec.stdout()); |
| 357 | + } |
| 358 | + |
| 359 | + /** |
| 360 | + * Utility method to check if a pod goes through initialization and Ready. |
| 361 | + * |
| 362 | + * @param podName - String name of the pod to check the status for |
| 363 | + * @throws Exception when pod doesn't go through the initialization and ready state |
| 364 | + */ |
| 365 | + private void verifyPodInitialized(String podName) throws Exception { |
| 366 | + TestUtils.checkPodInitializing(podName, domain.getDomainNs()); |
| 367 | + TestUtils.checkPodReady(podName, domain.getDomainNs()); |
| 368 | + } |
| 369 | +} |
0 commit comments