Skip to content

Commit 52debdf

Browse files
committed
Completed JIRA OWLS-73084 - QA - Create a generic utility to build the war file for web app in a server pod and deploy it from the pod
1 parent ff8929a commit 52debdf

File tree

10 files changed

+372
-15
lines changed

10 files changed

+372
-15
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class BaseTest {
5959
private static String leaseId = "";
6060
private static String branchName = "";
6161
private static String appLocationInPod = "/u01/oracle/apps";
62+
private static String appLocationOnHost;
6263
private static Properties appProps;
6364

6465
public static boolean QUICKTEST;
@@ -182,6 +183,8 @@ public static void initialize(String appPropsFile) throws Exception {
182183
}
183184
}
184185

186+
appLocationOnHost = getProjectRoot() + "/integration-tests/src/test/resources/apps";
187+
185188
logger.info("appProps = " + appProps);
186189
logger.info("maxIterationPod = " + appProps.getProperty("maxIterationsPod"));
187190
logger.info(
@@ -544,6 +547,14 @@ public static String getBranchName() {
544547
return branchName;
545548
}
546549

550+
public static String getAppLocationInPod() {
551+
return appLocationInPod;
552+
}
553+
554+
public static String getAppLocationOnHost() {
555+
return appLocationOnHost;
556+
}
557+
547558
private void copyScalingScriptToPod(
548559
String dirPathToCreate, String domainUID, String podName, String domainNS) throws Exception {
549560

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
*/
2929
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
3030
public class ITSessionMigration extends BaseTest {
31-
private static final String TESTAPPNAME = "httpsessionreplication";
31+
private static final String testAppName = "httpsessionreptestapp";
32+
private static final String scriptName = "buildDeployWebAppInPod.sh";
33+
3234
private static Map<String, String> httpAttrMap;
3335

3436
private static String httpHeaderFile;
@@ -52,7 +54,7 @@ public static void staticPrepare() throws Exception {
5254
// initialize test properties and create the directories
5355
initialize(APP_PROPS_FILE);
5456

55-
// create operator1
57+
// Create operator1
5658
if (operator == null) {
5759
logger.info("Creating Operator & waiting for the script to complete execution");
5860
operator = TestUtils.createOperator(OPERATOR1_YAML);
@@ -63,15 +65,6 @@ public static void staticPrepare() throws Exception {
6365
logger.info("Creating WLS Domain & waiting for the script to complete execution");
6466
domain = TestUtils.createDomain(DOMAINONPV_WLST_YAML);
6567
domain.verifyDomainCreated();
66-
67-
domain.deployWebAppViaREST(
68-
TESTAPPNAME,
69-
BaseTest.getProjectRoot() + "/src/integration-tests/apps/httpsessionreplication.war",
70-
BaseTest.getUsername(),
71-
BaseTest.getPassword());
72-
73-
// wait some time for deployment gets ready
74-
Thread.sleep(10 * 1000);
7568
}
7669

7770
httpHeaderFile = BaseTest.getResultDir() + "/headers";
@@ -82,6 +75,13 @@ public static void staticPrepare() throws Exception {
8275
httpAttrMap.put("primary", "(.*)primary>(.*)</primary(.*)");
8376
httpAttrMap.put("secondary", "(.*)secondary>(.*)</secondary(.*)");
8477
httpAttrMap.put("count", "(.*)countattribute>(.*)</countattribute(.*)");
78+
79+
// Build WAR in the admin pod and deploy it from the admin pod to a weblogic target
80+
domain.buildWarDeployAppInPod(
81+
testAppName, scriptName, BaseTest.getUsername(), BaseTest.getPassword());
82+
83+
// Wait some time for deployment gets ready
84+
Thread.sleep(10 * 1000);
8585
}
8686
}
8787

@@ -119,7 +119,7 @@ public void testRepickPrimary() throws Exception {
119119
String domainNS = domainMap.get("namespace").toString();
120120
String domainUid = domain.getDomainUid();
121121

122-
String testAppPath = TESTAPPNAME + "/CounterServlet";
122+
String testAppPath = testAppName + "/CounterServlet";
123123
String sessCreateTime = "sessioncreatetime";
124124
String primaryServ = "primary";
125125

@@ -150,7 +150,7 @@ public void testRepickPrimary() throws Exception {
150150

151151
// Restore test env
152152
// Wait some time for ms pod to become ready
153-
Thread.sleep(10 * 1000);
153+
Thread.sleep(30 * 1000);
154154
TestUtils.checkPodReady(domainUid + "-" + primaryServName1, domainNS);
155155

156156
logger.info(
@@ -174,7 +174,7 @@ public void testHttpSessionMigr() throws Exception {
174174
String domainUid = domain.getDomainUid();
175175

176176
int counterNum = 4;
177-
String testAppPath = TESTAPPNAME + "/CounterServlet";
177+
String testAppPath = testAppName + "/CounterServlet";
178178
String webServiceSetUrl = testAppPath + "?setCounter=" + counterNum;
179179
String webServiceGetUrl = testAppPath + "?getCounter";
180180

@@ -210,7 +210,7 @@ public void testHttpSessionMigr() throws Exception {
210210

211211
// Restore test env
212212
// Wait some time for ms pod to become ready
213-
Thread.sleep(10 * 1000);
213+
Thread.sleep(30 * 1000);
214214
TestUtils.checkPodReady(domainUid + "-" + primaryServName1, domainNS);
215215

216216
logger.info("SUCCESS - " + testMethodName + ". HTTP session state is migrated!");

integration-tests/src/test/java/oracle/kubernetes/operator/utils/Domain.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,4 +1495,108 @@ public void restartManagedServerUsingServerStartPolicy(String msName) throws Exc
14951495
TestUtils.checkPodCreated(domainUid + "-" + msName, domainNS);
14961496
TestUtils.checkPodReady(domainUid + "-" + msName, domainNS);
14971497
}
1498+
1499+
/**
1500+
* Run the shell script to build .war file and deploy the App in the admin pod
1501+
*
1502+
* @param webappName - Web App Name to be deployed
1503+
* @param scriptName - a shell script to build .war file and deploy the App in the admin pod
1504+
* @param username - weblogic user name
1505+
* @param password - weblogc password
1506+
* @throws Exception
1507+
*/
1508+
private void callShellScriptToBuildWarDeployAppInPod(
1509+
String webappName, String scriptName, String username, String password) throws Exception {
1510+
1511+
String nodeHost = getHostNameForCurl();
1512+
String nodePort = getNodePort();
1513+
String appLocationInPod = BaseTest.getAppLocationInPod();
1514+
1515+
StringBuffer cmdKubectlSh = new StringBuffer("kubectl -n ");
1516+
cmdKubectlSh
1517+
.append(domainNS)
1518+
.append(" exec -it ")
1519+
.append(domainUid)
1520+
.append("-")
1521+
.append(adminServerName)
1522+
.append(" -- bash -c 'chmod +x -R ")
1523+
.append(appLocationInPod)
1524+
.append(" && sh ")
1525+
.append(appLocationInPod)
1526+
.append("/")
1527+
.append(scriptName)
1528+
.append(" ")
1529+
.append(nodeHost)
1530+
.append(" ")
1531+
.append(nodePort)
1532+
.append(" ")
1533+
.append(username)
1534+
.append(" ")
1535+
.append(password)
1536+
.append(" ")
1537+
.append(appLocationInPod)
1538+
.append("/")
1539+
.append(webappName)
1540+
.append(" ")
1541+
.append(webappName)
1542+
.append(" ")
1543+
.append(clusterName)
1544+
.append("'");
1545+
1546+
logger.info("Command to exec script file: " + cmdKubectlSh);
1547+
ExecResult result = ExecCommand.exec(cmdKubectlSh.toString());
1548+
1549+
String resultStr =
1550+
"Command= '"
1551+
+ cmdKubectlSh
1552+
+ "'"
1553+
+ ", exitValue="
1554+
+ result.exitValue()
1555+
+ ", stdout='"
1556+
+ result.stdout()
1557+
+ "'"
1558+
+ ", stderr='"
1559+
+ result.stderr()
1560+
+ "'";
1561+
1562+
if (!resultStr.contains("Unable to use a TTY") && result.exitValue() != 0) {
1563+
throw new RuntimeException("FAILURE: webapp deploy failed - " + resultStr);
1564+
}
1565+
}
1566+
1567+
/**
1568+
* Create dir to save Web App files Copy the shell script file and all App files over to the admin
1569+
* pod Run the shell script to build .war file and deploy the App in the admin pod
1570+
*
1571+
* @param webappName - Web App Name to be deployed
1572+
* @param scriptName - a shell script to build .war file and deploy the App in the admin pod
1573+
* @param username - weblogic user name
1574+
* @param password - weblogc password
1575+
* @throws Exception
1576+
*/
1577+
public void buildWarDeployAppInPod(
1578+
String webappName, String scriptName, String username, String password) throws Exception {
1579+
String adminServerPod = domainUid + "-" + adminServerName;
1580+
// String scriptName = "buildDeployWebAppInPod.sh";
1581+
1582+
String appLocationOnHost = BaseTest.getAppLocationOnHost() + "/" + webappName;
1583+
String appLocationInPod = BaseTest.getAppLocationInPod() + "/" + webappName;
1584+
String scriptPathOnHost = BaseTest.getAppLocationOnHost() + "/" + scriptName;
1585+
String scriptPathInPod = BaseTest.getAppLocationInPod() + "/" + scriptName;
1586+
1587+
StringBuffer mkdirCmd = new StringBuffer(" -- bash -c 'mkdir -p ");
1588+
mkdirCmd.append(appLocationInPod).append("/WEB-INF'");
1589+
1590+
// Create app dir in the pod
1591+
TestUtils.kubectlexec(adminServerPod, domainNS, mkdirCmd.toString());
1592+
1593+
// Copy shell script to the pod
1594+
TestUtils.copyFileViaCat(scriptPathOnHost, scriptPathInPod, adminServerPod, domainNS);
1595+
1596+
// Copy all App files to the admin pod
1597+
TestUtils.copyAppFilesToPod(appLocationOnHost, appLocationInPod, adminServerPod, domainNS);
1598+
1599+
// Run the script to build .war file and deploy the App in the pod
1600+
callShellScriptToBuildWarDeployAppInPod(webappName, scriptName, username, password);
1601+
}
14981602
}

integration-tests/src/test/java/oracle/kubernetes/operator/utils/TestUtils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,43 @@ public static ExecResult kubectlexecNoCheck(String podName, String namespace, St
422422
return ExecCommand.exec(cmdKubectlSh.toString());
423423
}
424424

425+
public static void copyAppFilesToPod(
426+
String appLocationOnHost, String appLocationInPod, String podName, String namespace)
427+
throws Exception {
428+
File appFileRoot = new File(appLocationOnHost);
429+
File[] appFileList = appFileRoot.listFiles();
430+
String fileLocationInPod = appLocationInPod;
431+
432+
if (appFileList == null) return;
433+
434+
for (File file : appFileList) {
435+
if (file.isDirectory()) {
436+
// Find dir recursively
437+
copyAppFilesToPod(file.getAbsolutePath(), appLocationInPod, podName, namespace);
438+
} else {
439+
logger.info("Copy file: " + file.getAbsoluteFile().toString() + " to the pod: " + podName);
440+
441+
String fileParent = file.getParentFile().getName();
442+
logger.fine("file Parent: " + fileParent);
443+
444+
if (!appLocationInPod.contains(fileParent)) {
445+
// Copy files in child dir of appLocationInPod
446+
fileLocationInPod = appLocationInPod + "/" + fileParent;
447+
}
448+
449+
StringBuffer copyFileCmd = new StringBuffer(" -- bash -c 'cat > ");
450+
copyFileCmd
451+
.append(fileLocationInPod)
452+
.append("/")
453+
.append(file.getName())
454+
.append("' < ")
455+
.append(file.getAbsoluteFile().toString());
456+
457+
kubectlexecNoCheck(podName, namespace, copyFileCmd.toString());
458+
}
459+
}
460+
}
461+
425462
public static void kubectlexec(String podName, String namespace, String scriptPath)
426463
throws Exception {
427464

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
How to use the shell script in this dir to build the archive file in the admin pod and deploy it to a weblogic target from the admin pod:
2+
3+
Web App
4+
1) Create a directory structure for packaging WAR
5+
6+
integration-tests/src/test/resources/apps/
7+
testAppName/
8+
foo.java
9+
WEB-INF/
10+
weblogic.xml
11+
web.xml
12+
13+
2) In the standalone client java file, call:
14+
15+
String scriptName = "buildDeployWebAppInPod.sh";
16+
domain.buildWarDeployAppInPod(testAppName, scriptName, BaseTest.getUsername(), BaseTest.getPassword());
17+
Create dir in the admon pod to save Web App files
18+
Copy the shell script file and all App files over to the admin pod
19+
Run the shell script to build WAR and deploy the App from the admin pod to the given webligic target
20+
21+
3) testAppName.war file is created at "/u01/oracle/apps/testAppName" in the admin pod
22+
23+
sh buildDeployWebAppInPod.sh node-hostname node-port username password dir-in-pod-to-save-app-files appname deploy-target
24+
Create directories to save the binaries
25+
Complie java files
26+
Create WAR
27+
Deploy the App from the admin pod to the given webligic target
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/sh
2+
3+
usage()
4+
{
5+
printf "\n"
6+
echo 1>&2 "Usage: sh $0 node-hostname node-port username password dir-in-pod-to-save-app-files appname deploy-target"
7+
printf "\n"
8+
}
9+
10+
11+
##### Main
12+
13+
if [ $1 = "-h" ] || [ $# -eq 0 ]; then
14+
usage
15+
exit 0
16+
fi
17+
18+
HOST=$1
19+
PORT=$2
20+
USER=${3:-weblogic}
21+
PASSWORD=${4:-welcome1}
22+
APP_DIR_INPOD=$5
23+
APP_NAME=$6
24+
DEPLOY_TARGET=$7
25+
WAR_FILE_NAME=${APP_NAME}.war
26+
27+
echo "App location in the pod: ${APP_NAME}"
28+
echo "App name: ${APP_NAME}"
29+
echo "Deploy the app to: ${DEPLOY_TARGET}"
30+
31+
source $ORACLE_HOME/wlserver/server/bin/setWLSEnv.sh
32+
33+
cd ${APP_DIR_INPOD}
34+
35+
echo -e "mkdir -p stagedir/WEB-INF/classes\n"
36+
mkdir -p stagedir/WEB-INF/classes
37+
38+
echo -e "cp -r WEB-INF/* stagedir/WEB-INF/\n"
39+
cp -r WEB-INF/* stagedir/WEB-INF/
40+
41+
echo -e "javac -d stagedir/WEB-INF/classes *.java\n"
42+
javac -d stagedir/WEB-INF/classes *.java
43+
44+
echo -e "jar -cvf ${WAR_FILE_NAME} -C stagedir .\n"
45+
jar -cvf ${WAR_FILE_NAME} -C stagedir .
46+
47+
echo "Deploy ${APP_NAME} using cmd:"
48+
echo -e "curl --noproxy '*' --silent --user ${USER}:${PASSWORD} -H X-Requested-By:MyClient -H Accept:application/json -H Content-Type:multipart/form-data -F "model={ name: '${APP_NAME}', targets: [ { identity: [ clusters, '${DEPLOY_TARGET}' ] } ] }" -F "sourcePath=@${APP_DIR_INPOD}/${WAR_FILE_NAME}" -H "Prefer:respond-async" -X POST http://${HOST}:${PORT}/management/weblogic/latest/edit/appDeployments\n"
49+
curl --noproxy '*' --silent --user ${USER}:${PASSWORD} -H X-Requested-By:MyClient -H Accept:application/json -H Content-Type:multipart/form-data -F "model={ name: '${APP_NAME}', targets: [ { identity: [ clusters, '${DEPLOY_TARGET}' ] } ] }" -F "sourcePath=@${APP_DIR_INPOD}/${WAR_FILE_NAME}" -H "Prefer:respond-async" -X POST http://${HOST}:${PORT}/management/weblogic/latest/edit/appDeployments
50+
51+
exit 0

0 commit comments

Comments
 (0)