|
| 1 | +// Copyright 2018, 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 static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.nio.file.StandardCopyOption; |
| 13 | +import java.nio.file.StandardOpenOption; |
| 14 | +import java.util.Map; |
| 15 | +import oracle.kubernetes.operator.utils.Domain; |
| 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.BeforeClass; |
| 21 | +import org.junit.FixMethodOrder; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runners.MethodSorters; |
| 24 | + |
| 25 | +/** |
| 26 | + * Multiple clusters in a domain tests |
| 27 | + * |
| 28 | + * <p>More than 1 cluster is created in a domain , like configured and dynamic |
| 29 | + */ |
| 30 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 31 | +public class ITMultipleClusters extends BaseTest { |
| 32 | + |
| 33 | + private static Operator operator1; |
| 34 | + private static final String TWO_CONFIGURED_CLUSTER_SCRIPT = |
| 35 | + "create-domain-two-configured-cluster.py"; |
| 36 | + private static final String TWO_MIXED_CLUSTER_SCRIPT = "create-domain-two-mixed-cluster.py"; |
| 37 | + private static String template; |
| 38 | + private static final String DOMAINUID = "twoconfigclustdomain"; |
| 39 | + |
| 40 | + /** |
| 41 | + * This method gets called only once before any of the test methods are executed. It does the |
| 42 | + * initialization of the integration test properties defined in OperatorIT.properties and setting |
| 43 | + * the resultRoot, pvRoot and projectRoot attributes. |
| 44 | + * |
| 45 | + * @throws Exception |
| 46 | + */ |
| 47 | + @BeforeClass |
| 48 | + public static void staticPrepare() throws Exception { |
| 49 | + // initialize test properties and create the directories |
| 50 | + initialize(APP_PROPS_FILE); |
| 51 | + template = |
| 52 | + BaseTest.getProjectRoot() + "/kubernetes/samples/scripts/common/domain-template.yaml"; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Releases k8s cluster lease, archives result, pv directories |
| 57 | + * |
| 58 | + * @throws 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 | + tearDown(new Object() {}.getClass().getEnclosingClass().getSimpleName()); |
| 66 | + logger.info("SUCCESS"); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Create 2 configured clusters in a domain each having 2 managed servers. Verify the managed |
| 71 | + * servers are running and verify the basic use cases. |
| 72 | + * |
| 73 | + * @throws Exception |
| 74 | + */ |
| 75 | + @Test |
| 76 | + public void testCreateDomainTwoConfiguredCluster() throws Exception { |
| 77 | + |
| 78 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 79 | + logTestBegin(testMethodName); |
| 80 | + |
| 81 | + logger.info("Creating Operator & waiting for the script to complete execution"); |
| 82 | + // create operator1 |
| 83 | + if (operator1 == null) { |
| 84 | + operator1 = TestUtils.createOperator(OPERATOR1_YAML); |
| 85 | + } |
| 86 | + Domain domain = null; |
| 87 | + boolean testCompletedSuccessfully = false; |
| 88 | + try { |
| 89 | + Map<String, Object> domainMap = TestUtils.loadYaml(DOMAINONPV_WLST_YAML); |
| 90 | + domainMap.put("domainUID", DOMAINUID); |
| 91 | + domainMap.put("clusterType", "CONFIGURED"); |
| 92 | + domainMap.put( |
| 93 | + "createDomainPyScript", |
| 94 | + "integration-tests/src/test/resources/domain-home-on-pv/" |
| 95 | + + TWO_CONFIGURED_CLUSTER_SCRIPT); |
| 96 | + addCluster2ToDomainTemplate(); |
| 97 | + domain = TestUtils.createDomain(domainMap); |
| 98 | + domain.verifyDomainCreated(); |
| 99 | + verifyServersStatus(domain); |
| 100 | + testBasicUseCases(domain); |
| 101 | + if (!SMOKETEST) { |
| 102 | + domain.testWlsLivenessProbe(); |
| 103 | + } |
| 104 | + testCompletedSuccessfully = true; |
| 105 | + } finally { |
| 106 | + if (domain != null && !SMOKETEST && (JENKINS || testCompletedSuccessfully)) { |
| 107 | + domain.destroy(); |
| 108 | + } |
| 109 | + restoreDomainTemplate(); |
| 110 | + } |
| 111 | + logger.info("SUCCESS - " + testMethodName); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Create 2 clusters(configured and dynamic) in a domain each having 2 managed servers. Verify the |
| 116 | + * managed servers are running and verify the basic use cases. |
| 117 | + * |
| 118 | + * @throws Exception |
| 119 | + */ |
| 120 | + @Test |
| 121 | + public void testCreateDomainTwoMixedCluster() throws Exception { |
| 122 | + String DOMAINUID = "twomixedclusterdomain"; |
| 123 | + String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName(); |
| 124 | + logTestBegin(testMethodName); |
| 125 | + String template = |
| 126 | + BaseTest.getProjectRoot() + "/kubernetes/samples/scripts/common/domain-template.yaml"; |
| 127 | + logger.info("Creating Operator & waiting for the script to complete execution"); |
| 128 | + if (operator1 == null) { |
| 129 | + operator1 = TestUtils.createOperator(OPERATOR1_YAML); |
| 130 | + } |
| 131 | + Domain domain = null; |
| 132 | + boolean testCompletedSuccessfully = false; |
| 133 | + try { |
| 134 | + Map<String, Object> domainMap = TestUtils.loadYaml(DOMAINONPV_WLST_YAML); |
| 135 | + domainMap.put("domainUID", DOMAINUID); |
| 136 | + domainMap.put( |
| 137 | + "createDomainPyScript", |
| 138 | + "integration-tests/src/test/resources/domain-home-on-pv/" + TWO_MIXED_CLUSTER_SCRIPT); |
| 139 | + addCluster2ToDomainTemplate(); |
| 140 | + domain = TestUtils.createDomain(domainMap); |
| 141 | + domain.verifyDomainCreated(); |
| 142 | + verifyServersStatus(domain); |
| 143 | + testBasicUseCases(domain); |
| 144 | + if (!SMOKETEST) { |
| 145 | + domain.testWlsLivenessProbe(); |
| 146 | + } |
| 147 | + testCompletedSuccessfully = true; |
| 148 | + } finally { |
| 149 | + if (domain != null && !SMOKETEST && (JENKINS || testCompletedSuccessfully)) { |
| 150 | + domain.destroy(); |
| 151 | + } |
| 152 | + restoreDomainTemplate(); |
| 153 | + } |
| 154 | + logger.info("SUCCESS - " + testMethodName); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Append a second cluster to the domain template |
| 159 | + * |
| 160 | + * @throws IOException when append fails |
| 161 | + */ |
| 162 | + private void addCluster2ToDomainTemplate() throws IOException { |
| 163 | + String add = |
| 164 | + " - clusterName: %CLUSTER_NAME%-2\n" |
| 165 | + + " serverStartState: \"RUNNING\"\n" |
| 166 | + + " replicas: %INITIAL_MANAGED_SERVER_REPLICAS%\n"; |
| 167 | + logger.info("Making a backup of the domain template file:" + template); |
| 168 | + if (!Files.exists(Paths.get(template + ".org"))) { |
| 169 | + Files.copy(Paths.get(template), Paths.get(template + ".org")); |
| 170 | + } |
| 171 | + Files.write(Paths.get(template), add.getBytes(), StandardOpenOption.APPEND); |
| 172 | + byte[] readAllBytes = Files.readAllBytes(Paths.get(template)); |
| 173 | + logger.info(new String(readAllBytes, StandardCharsets.UTF_8)); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Restore the domain template to original state when test is finished |
| 178 | + * |
| 179 | + * @throws IOException |
| 180 | + */ |
| 181 | + private void restoreDomainTemplate() throws IOException { |
| 182 | + Files.copy( |
| 183 | + Paths.get(template + ".org"), Paths.get(template), StandardCopyOption.REPLACE_EXISTING); |
| 184 | + Files.delete(Paths.get(template + ".org")); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Verifies all of the servers in the cluster are in Running status |
| 189 | + * |
| 190 | + * @param Domain |
| 191 | + */ |
| 192 | + private void verifyServersStatus(Domain domain) { |
| 193 | + K8sTestUtils testUtil = new K8sTestUtils(); |
| 194 | + String domain1LabelSelector = String.format("weblogic.domainUID in (%s)", DOMAINUID); |
| 195 | + String namespace = domain.getDomainNS(); |
| 196 | + String pods[] = { |
| 197 | + DOMAINUID + "-" + domain.getAdminServerName(), |
| 198 | + DOMAINUID + "-managed-server", |
| 199 | + DOMAINUID + "-managed-server1", |
| 200 | + DOMAINUID + "-managed-server2", |
| 201 | + DOMAINUID + "-new-managed-server1", |
| 202 | + DOMAINUID + "-new-managed-server2", |
| 203 | + }; |
| 204 | + for (String pod : pods) { |
| 205 | + assertTrue( |
| 206 | + pod + " Pod not running", testUtil.isPodRunning(namespace, domain1LabelSelector, pod)); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments