Skip to content

Commit 6f6637b

Browse files
committed
add utility methods to add init containers in the domain.yaml
1 parent 4bd129f commit 6f6637b

File tree

3 files changed

+288
-40
lines changed

3 files changed

+288
-40
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}

integration-tests/src/test/java/oracle/kubernetes/operator/ItPodsRestart.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.util.HashMap;
1313
import java.util.Map;
1414
import java.util.logging.Level;
15-
1615
import oracle.kubernetes.operator.utils.Domain;
1716
import oracle.kubernetes.operator.utils.DomainCrd;
1817
import oracle.kubernetes.operator.utils.ExecResult;
@@ -490,7 +489,7 @@ public void testClusterRestartVersion() throws Exception {
490489
* @throws Exception when domain.yaml cannot be read or modified to include the
491490
* restartVersion:v1.1
492491
*/
493-
// @Test
492+
@Test
494493
public void testMsRestartVersion() throws Exception {
495494
Assume.assumeFalse(QUICKTEST);
496495
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();

0 commit comments

Comments
 (0)