Skip to content

Commit 419c3fe

Browse files
authored
Additional unit tests (#206)
* added a few unit tests * fix regex in Jenkinsfile changelog checks * moved DB image name to pom.xml and use from ENV
1 parent 8052b5a commit 419c3fe

File tree

10 files changed

+125
-50
lines changed

10 files changed

+125
-50
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
!.idea/codeStyles/codeStyleConfig.xml
77
**/target
88
**/.DS_Store
9+
.mvn/maven.config

Jenkinsfile

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pipeline {
1414
}
1515

1616
environment {
17+
// variables for SystemTest stages (integration tests)
1718
STAGING_DIR = "/scratch/artifacts/imagetool"
19+
DB_IMAGE = "phx.ocir.io/weblogick8s/database/enterprise:12.2.0.1-slim"
1820
}
1921

2022
stages {
@@ -34,9 +36,7 @@ pipeline {
3436
}
3537
stage ('Test') {
3638
when {
37-
not {
38-
changelog '\\[skip-ci\\]'
39-
}
39+
not { changelog '\\[skip-ci\\].*' }
4040
}
4141
steps {
4242
sh 'mvn test'
@@ -51,9 +51,8 @@ pipeline {
5151
when {
5252
allOf {
5353
changeRequest target: 'master'
54-
not {
55-
changelog '\\[skip-ci\\]'
56-
}
54+
not { changelog '\\[skip-ci\\].*' }
55+
not { changelog '\\[full-mats\\].*' }
5756
}
5857
}
5958
steps {
@@ -74,10 +73,14 @@ pipeline {
7473
when {
7574
anyOf {
7675
triggeredBy 'TimerTrigger'
77-
tag "release-*"
76+
tag 'release-*'
77+
changelog '\\[full-mats\\].*'
7878
}
7979
}
8080
steps {
81+
script {
82+
docker.image("${env.DB_IMAGE}").pull()
83+
}
8184
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'otn-cred', passwordVariable: 'ORACLE_SUPPORT_PASSWORD', usernameVariable: 'ORACLE_SUPPORT_USERNAME']]) {
8285
sh '''
8386
cd tests

imagetool/src/main/java/com/oracle/weblogic/imagetool/aru/AruUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.w3c.dom.NodeList;
2828

2929
import static com.oracle.weblogic.imagetool.util.Constants.ARU_LANG_URL;
30+
import static com.oracle.weblogic.imagetool.util.Constants.ARU_REST_URL;
3031
import static com.oracle.weblogic.imagetool.util.Constants.CONFLICTCHECKER_URL;
3132
import static com.oracle.weblogic.imagetool.util.Constants.GET_LSINVENTORY_URL;
3233
import static com.oracle.weblogic.imagetool.util.Constants.RECOMMENDED_PATCHES_URL;
@@ -38,7 +39,7 @@ public class AruUtil {
3839

3940
private static AruUtil instance;
4041

41-
private static final String BUG_SEARCH_URL = "https://updates.oracle.com/Orion/Services/search?bug=%s";
42+
private static final String BUG_SEARCH_URL = ARU_REST_URL + "/search?bug=%s";
4243

4344
/**
4445
* Get ARU HTTP helper instance.
@@ -350,7 +351,7 @@ public static boolean checkCredentials(String username, String password) {
350351
return aruHttpHelper.success();
351352
}
352353

353-
private void verifyResponse(Document response) throws AruException, XPathExpressionException {
354+
void verifyResponse(Document response) throws AruException, XPathExpressionException {
354355
NodeList nodeList = XPathUtil.nodelist(response, "/results/error");
355356
if (nodeList.getLength() > 0) {
356357
String errorMessage = XPathUtil.string(response, "/results/error/message");

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
public final class Constants {
77

8-
public static final String REL_URL = "https://updates.oracle.com/Orion/Services/metadata?table=aru_releases";
9-
public static final String RECOMMENDED_PATCHES_URL = "https://updates.oracle.com/Orion/Services/search?patch_type=all&life_cycle=Recommended&product=%s&release=%s";
10-
public static final String ARU_LANG_URL = "https://updates.oracle.com/Orion/Services/metadata?table=aru_languages";
11-
public static final String CONFLICTCHECKER_URL = "https://updates.oracle.com/Orion/Services/conflict_checks";
12-
public static final String GET_LSINVENTORY_URL = "https://updates.oracle.com/Orion/Services/get_inventory_upi";
8+
public static final String ARU_REST_URL = "https://updates.oracle.com/Orion/Services";
9+
public static final String REL_URL = ARU_REST_URL + "/metadata?table=aru_releases";
10+
public static final String RECOMMENDED_PATCHES_URL = ARU_REST_URL
11+
+ "/search?patch_type=all&life_cycle=Recommended&product=%s&release=%s";
12+
public static final String ARU_LANG_URL = ARU_REST_URL + "/metadata?table=aru_languages";
13+
public static final String CONFLICTCHECKER_URL = ARU_REST_URL + "/conflict_checks";
14+
public static final String GET_LSINVENTORY_URL = ARU_REST_URL + "/get_inventory_upi";
1315
public static final String CACHE_DIR_KEY = "cache.dir";
1416
public static final String DEFAULT_WLS_VERSION = "12.2.1.3.0";
1517
public static final String DEFAULT_JDK_VERSION = "8u202";

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/HttpUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static Document getXMLContent(String url, String username, String passwor
103103
String xmlString = Executor.newInstance(getOraClient(username, password))
104104
.execute(Request.Get(url).connectTimeout(30000).socketTimeout(30000))
105105
.returnContent().asString();
106-
logger.exiting();
106+
logger.exiting(xmlString);
107107
return parseXmlString(xmlString);
108108
}
109109

imagetool/src/test/java/com/oracle/weblogic/imagetool/aru/AruUtilTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import java.util.List;
1111
import java.util.logging.Level;
1212
import java.util.stream.Collectors;
13+
import javax.xml.xpath.XPathExpressionException;
1314

15+
import com.oracle.weblogic.imagetool.installer.FmwInstallerType;
1416
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
1517
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
1618
import com.oracle.weblogic.imagetool.util.HttpUtil;
@@ -62,17 +64,20 @@ Document getAllReleases(String userId, String password) {
6264

6365
@Override
6466
Document getRecommendedPatchesMetadata(AruProduct product, String releaseNumber, String userId,
65-
String password) {
67+
String password) throws XPathExpressionException, AruException {
68+
Document result;
6669
try {
6770
if (releaseNumber.equals("336")) {
68-
return getResource("/recommended-patches.xml");
71+
result = getResource("/recommended-patches.xml");
6972
} else {
70-
return getResource("/no-patches.xml");
73+
result = getResource("/no-patches.xml");
7174
}
7275
} catch (IOException e) {
7376
e.printStackTrace();
7477
throw new RuntimeException("failed to load resources XML", e);
7578
}
79+
verifyResponse(result);
80+
return result;
7681
}
7782

7883
private Document getResource(String path) throws IOException {
@@ -95,6 +100,11 @@ void testRecommendedPatches() throws Exception {
95100
assertTrue(bugs.contains("31384951"));
96101
assertTrue(bugs.contains("28512225"));
97102
assertTrue(bugs.contains("28278427"));
103+
104+
// if no recommended patches are found, method should return an empty list (test data does not have 12.2.1.4)
105+
recommendedPatches =
106+
AruUtil.rest().getRecommendedPatches(FmwInstallerType.WLS, "12.2.1.4.0", "x", "x");
107+
assertTrue(recommendedPatches.isEmpty());
98108
}
99109

100110
@Test
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) 2020, Oracle Corporation and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cli.cache;
5+
6+
import java.io.PrintWriter;
7+
import java.io.StringWriter;
8+
import java.util.logging.Level;
9+
10+
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
11+
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
12+
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
13+
import org.junit.jupiter.api.AfterAll;
14+
import org.junit.jupiter.api.BeforeAll;
15+
import org.junit.jupiter.api.Tag;
16+
import org.junit.jupiter.api.Test;
17+
import picocli.CommandLine;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
import static org.junit.jupiter.api.Assertions.assertNull;
22+
23+
@Tag("unit")
24+
public class AddPatchEntryTest {
25+
private static LoggingFacade commandLogger = LoggingFactory.getLogger(AddPatchEntry.class);
26+
private static Level oldLevel;
27+
28+
@BeforeAll
29+
static void setUp() throws NoSuchFieldException, IllegalAccessException {
30+
// disable logging for the tested tool to prevent filling up the screen with errors (that are expected)
31+
oldLevel = commandLogger.getLevel();
32+
commandLogger.setLevel(Level.OFF);
33+
}
34+
35+
@AfterAll
36+
static void tearDown() {
37+
commandLogger.setLevel(oldLevel);
38+
}
39+
40+
private static CommandLine getCommand() {
41+
AddPatchEntry app = new AddPatchEntry();
42+
CommandLine cmd = new CommandLine(app);
43+
StringWriter sw = new StringWriter();
44+
cmd.setOut(new PrintWriter(sw));
45+
cmd.setErr(new PrintWriter(sw));
46+
return cmd;
47+
}
48+
49+
@Test
50+
void testMissingParameters() {
51+
CommandLine cmd = getCommand();
52+
// missing valid parameters should generate USAGE output
53+
int exitCode = cmd.execute("-x", "-y=123");
54+
CommandResponse result = cmd.getExecutionResult();
55+
assertNull(result, "Should not have a response, picoli should intercept usage error");
56+
assertEquals(CommandLine.ExitCode.USAGE, exitCode);
57+
}
58+
59+
@Test
60+
void testInvalidFileParameter() {
61+
CommandLine cmd = getCommand();
62+
// invalid file (file does not exist), should generate an error response
63+
int exitCode = cmd.execute("--patchId=12345678_12.2.1.3.0", "--path=/here/there");
64+
CommandResponse result = cmd.getExecutionResult();
65+
assertNotNull(result, "Response missing from call to addPatch");
66+
assertEquals(-1, result.getStatus());
67+
}
68+
69+
@Test
70+
void testInvalidPatchId() {
71+
CommandLine cmd = getCommand();
72+
// invalid patch ID should generate an error response
73+
cmd.execute("--patchId=12345678", "--path=pom.xml");
74+
CommandResponse result = cmd.getExecutionResult();
75+
assertNotNull(result, "Response missing from call to addPatch");
76+
assertEquals(-1, result.getStatus());
77+
}
78+
}

imagetool/src/test/resources/releases.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,6 @@
7575
<release id="604" name="10.4.0.3.0"><![CDATA[OSB 10.4.0.3.0]]></release>
7676
<release id="605" name="11.1.1.7.0"><![CDATA[OSB 11.1.1.7.0]]></release>
7777
<release id="606" name="12.1.0.1.0"><![CDATA[OSB 12.1.0.1.0]]></release>
78+
<release id="701" name="12.2.1.4.0"><![CDATA[Oracle Coherence 12.2.1.4.0]]></release>
79+
<release id="702" name="12.2.1.3.0"><![CDATA[Oracle Coherence 12.2.1.3.0]]></release>
7880
</results>

tests/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
<WLSIMG_BLDDIR>${project.build.directory}/blddir</WLSIMG_BLDDIR>
148148
<WLSIMG_CACHEDIR>${project.build.directory}/cachedir</WLSIMG_CACHEDIR>
149149
<STAGING_DIR>${test.staging.dir}</STAGING_DIR>
150+
<DB_IMAGE>phx.ocir.io/weblogick8s/database/enterprise:12.2.0.1-slim</DB_IMAGE>
150151
</systemPropertyVariables>
151152
</configuration>
152153
<executions>

tests/src/test/java/com/oracle/weblogic/imagetool/tests/ITImagetool.java

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ class ITImagetool {
5454
private static final String wlsImgCacheDir = getEnvironmentProperty("WLSIMG_CACHEDIR");
5555

5656
// Docker images
57-
private static final String BASE_OS_IMG = "phx.ocir.io/weblogick8s/oraclelinux";
58-
private static final String BASE_OS_IMG_TAG = "7-4imagetooltest";
59-
private static final String ORACLE_DB_IMG = "phx.ocir.io/weblogick8s/database/enterprise";
60-
private static final String ORACLE_DB_IMG_TAG = "12.2.0.1-slim";
57+
private static final String DB_IMAGE = getEnvironmentProperty("DB_IMAGE");
6158

6259
// Staging Dir files
6360
private static final String JDK_INSTALLER = "jdk-8u202-linux-x64.tar.gz";
@@ -99,6 +96,7 @@ class ITImagetool {
9996
private static String getEnvironmentProperty(String name) {
10097
String result = System.getenv(name);
10198
if (result == null || result.isEmpty()) {
99+
logger.info("Environment variable {0} not set, using Java system property", name);
102100
result = System.getProperty(name);
103101
}
104102
return result;
@@ -120,6 +118,10 @@ private static void validateEnvironmentSettings() {
120118
missingSettings.add("STAGING_DIR");
121119
}
122120

121+
if (DB_IMAGE == null || DB_IMAGE.isEmpty()) {
122+
missingSettings.add("DB_IMAGE");
123+
}
124+
123125
if (missingSettings.size() > 0) {
124126
String error = String.join(", ", missingSettings)
125127
+ " must be set as a system property or ENV variable";
@@ -137,6 +139,8 @@ private static void validateEnvironmentSettings() {
137139
logger.info("build_tag = " + build_tag);
138140
logger.info("WLSIMG_BLDDIR = " + wlsImgBldDir);
139141
logger.info("WLSIMG_CACHEDIR = " + wlsImgCacheDir);
142+
logger.info("STAGING_DIR = " + STAGING_DIR);
143+
logger.info("DB_IMAGE = " + DB_IMAGE);
140144
}
141145

142146
private static void verifyStagedFiles(String... installers) {
@@ -201,22 +205,6 @@ private static void cleanup() throws Exception {
201205
executeNoVerify(command);
202206
}
203207

204-
private static void pullDockerImage(String imagename, String imagetag) throws Exception {
205-
206-
String pullCommand = "docker pull " + imagename + ":" + imagetag;
207-
logger.info(pullCommand);
208-
Runner.run(pullCommand);
209-
210-
// verify the docker image is pulled
211-
CommandResult result = Runner.run("docker images | grep " + imagename + " | grep "
212-
+ imagetag + "| wc -l");
213-
String resultString = result.stdout();
214-
if (Integer.parseInt(resultString.trim()) != 1) {
215-
throw new Exception("docker image " + imagename + ":" + imagetag + " is not pulled as expected."
216-
+ " Expected 1 image, found " + resultString);
217-
}
218-
}
219-
220208
@BeforeAll
221209
static void staticPrepare() throws Exception {
222210
logger.info("prepare for image tool test ...");
@@ -234,12 +222,6 @@ static void staticPrepare() throws Exception {
234222
}
235223
}
236224

237-
logger.info("Pulling OS base images from OCIR ...");
238-
pullDockerImage(BASE_OS_IMG, BASE_OS_IMG_TAG);
239-
240-
logger.info("Pulling Oracle DB image from OCIR ...");
241-
pullDockerImage(ORACLE_DB_IMG, ORACLE_DB_IMG_TAG);
242-
243225
// verify that required files/installers are available
244226
verifyStagedFiles(JDK_INSTALLER, WLS_INSTALLER, WDT_INSTALLER, P27342434_INSTALLER, P28186730_INSTALLER,
245227
FMW_INSTALLER, JDK_INSTALLER_NEWER);
@@ -315,8 +297,7 @@ private void createDBContainer() throws Exception {
315297
String command = "docker rm -f " + dbContainerName;
316298
Runner.run(command);
317299
command = "docker run -d --name " + dbContainerName + " --env=\"DB_PDB=InfraPDB1\""
318-
+ " --env=\"DB_DOMAIN=us.oracle.com\" --env=\"DB_BUNDLE=basic\" " + ORACLE_DB_IMG + ":"
319-
+ ORACLE_DB_IMG_TAG;
300+
+ " --env=\"DB_DOMAIN=us.oracle.com\" --env=\"DB_BUNDLE=basic\" " + DB_IMAGE;
320301
logger.info("executing command: " + command);
321302
Runner.run(command);
322303

@@ -613,7 +594,6 @@ void createWlsImgUsingWdt(TestInfo testInfo) throws Exception {
613594
String tagName = build_tag + ":" + getMethodName(testInfo);
614595
// create a WLS image with a domain
615596
String command = new CreateCommand()
616-
.fromImage(BASE_OS_IMG, BASE_OS_IMG_TAG)
617597
.tag(tagName)
618598
.patches(P27342434_ID)
619599
.wdtVersion(WDT_VERSION)
@@ -756,7 +736,6 @@ void createJrfDomainImgUsingWdt(TestInfo testInfo) throws Exception {
756736

757737
String tagName = build_tag + ":" + getMethodName(testInfo);
758738
String command = new CreateCommand()
759-
.fromImage(BASE_OS_IMG, BASE_OS_IMG_TAG)
760739
.tag(tagName)
761740
.version(WLS_VERSION)
762741
.wdtVersion(WDT_VERSION)
@@ -796,7 +775,6 @@ void createRestrictedJrfDomainImgUsingWdt(TestInfo testInfo) throws Exception {
796775

797776
String tagName = build_tag + ":" + getMethodName(testInfo);
798777
String command = new CreateCommand()
799-
.fromImage(BASE_OS_IMG, BASE_OS_IMG_TAG)
800778
.tag(tagName)
801779
.version(WLS_VERSION)
802780
.latestPsu(true)
@@ -842,7 +820,6 @@ void createWlsImgUsingMultiModels(TestInfo testInfo) throws Exception {
842820

843821
String tagName = build_tag + ":" + getMethodName(testInfo);
844822
String command = new CreateCommand()
845-
.fromImage(BASE_OS_IMG, BASE_OS_IMG_TAG)
846823
.tag(tagName)
847824
.version(WLS_VERSION)
848825
.wdtVersion(WDT_VERSION)

0 commit comments

Comments
 (0)