|
| 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 | + |
| 5 | +package com.oracle.weblogic.imagetool.integration; |
| 6 | + |
| 7 | +import com.oracle.weblogic.imagetool.integration.utils.ExecCommand; |
| 8 | +import com.oracle.weblogic.imagetool.integration.utils.ExecResult; |
| 9 | +import java.io.File; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.Paths; |
| 13 | +import java.util.logging.Logger; |
| 14 | + |
| 15 | +public class BaseTest { |
| 16 | + |
| 17 | + protected static final Logger logger = Logger.getLogger(ITImagetool.class.getName()); |
| 18 | + protected static final String VERSION = "1.0.1"; |
| 19 | + protected static final String PS = File.pathSeparator; |
| 20 | + protected static final String FS = File.separator; |
| 21 | + private static final String OCIR_SERVER = "phx.ocir.io"; |
| 22 | + private static final String OCIR_TENENT = "weblogick8s"; |
| 23 | + private static final String OCR_SERVER = "container-registry.oracle.com"; |
| 24 | + protected static final String BASE_OS_IMG = "phx.ocir.io/weblogick8s/oraclelinux"; |
| 25 | + protected static final String BASE_OS_IMG_TAG = "7-4imagetooltest"; |
| 26 | + protected static final String ORACLE_DB_IMG = "container-registry.oracle.com/database/enterprise"; |
| 27 | + protected static final String ORACLE_DB_IMG_TAG = "12.2.0.1-slim"; |
| 28 | + private static final String DB_CONTAINER_NAME = "InfraDB"; |
| 29 | + private static String projectRoot = ""; |
| 30 | + protected static String wlsImgBldDir = ""; |
| 31 | + protected static String wlsImgCacheDir = ""; |
| 32 | + protected static String imagetool = ""; |
| 33 | + private static String imagetoolZipfile = ""; |
| 34 | + private static int maxIterations = 50; |
| 35 | + private static int waitTime = 5; |
| 36 | + |
| 37 | + protected static void initialize() throws Exception { |
| 38 | + logger.info("Initializing the tests ..."); |
| 39 | + |
| 40 | + projectRoot = System.getProperty("user.dir"); |
| 41 | + |
| 42 | + if(System.getenv("WLSIMG_BLDDIR") != null) { |
| 43 | + wlsImgBldDir = System.getenv("WLSIMG_BLDDIR"); |
| 44 | + } else { |
| 45 | + wlsImgBldDir = System.getenv("HOME"); |
| 46 | + } |
| 47 | + if(System.getenv("WLSIMG_CACHEDIR") != null) { |
| 48 | + wlsImgCacheDir = System.getenv("WLSIMG_CACHEDIR"); |
| 49 | + } else { |
| 50 | + wlsImgCacheDir = System.getenv("HOME") + FS + "cache"; |
| 51 | + } |
| 52 | + |
| 53 | + imagetoolZipfile = "imagetool-" + VERSION + "-SNAPSHOT.zip"; |
| 54 | + |
| 55 | + imagetool = "java -cp \"" + getImagetoolHome() + FS + "lib" + FS + "*\" -Djava.util.logging.config.file=" + |
| 56 | + getImagetoolHome() + FS + "bin" + FS + "logging.properties com.oracle.weblogic.imagetool.cli.CLIDriver"; |
| 57 | + |
| 58 | + logger.info("DEBUG: WLSIMG_BLDDIR=" + wlsImgBldDir); |
| 59 | + logger.info("DEBUG: WLSIMG_CACHEDIR=" + wlsImgCacheDir); |
| 60 | + logger.info("DEBUG: imagetool=" + imagetool); |
| 61 | + } |
| 62 | + |
| 63 | + protected static void setup() throws Exception { |
| 64 | + |
| 65 | + logger.info("Setting up the test environment ..."); |
| 66 | + String command = "/bin/rm -rf " + getImagetoolHome(); |
| 67 | + executeNoVerify(command); |
| 68 | + |
| 69 | + // unzip the weblogic-image-tool/imagetool/target/imagetool-${VERSION}-SNAPSHOT.zip |
| 70 | + command = "/bin/unzip " + getTargetDir() + FS + imagetoolZipfile; |
| 71 | + executeNoVerify(command); |
| 72 | + |
| 73 | + command = "source " + getImagetoolHome() + FS + "bin" + FS + "setup.sh"; |
| 74 | + executeNoVerify(command); |
| 75 | + } |
| 76 | + |
| 77 | + protected static void cleanup() throws Exception { |
| 78 | + logger.info("cleaning up the test environment ..."); |
| 79 | + String command = "/bin/rm -rf " + wlsImgCacheDir; |
| 80 | + executeNoVerify(command); |
| 81 | + |
| 82 | + command = "/bin/mkdir " + wlsImgCacheDir; |
| 83 | + executeNoVerify(command); |
| 84 | + |
| 85 | + // clean up the docker images |
| 86 | + command = "docker stop " + DB_CONTAINER_NAME; |
| 87 | + executeNoVerify(command); |
| 88 | + command = "docker rmi -f " + BASE_OS_IMG + ":" + BASE_OS_IMG_TAG + " " + ORACLE_DB_IMG + ":" + |
| 89 | + ORACLE_DB_IMG_TAG; |
| 90 | + executeNoVerify(command); |
| 91 | + |
| 92 | + // clean up the possible left over wlsimgbuilder_temp* |
| 93 | + command = "/bin/rm -rf " + wlsImgBldDir + FS + "wlsimgbuilder_temp*"; |
| 94 | + executeNoVerify(command); |
| 95 | + } |
| 96 | + |
| 97 | + protected static void pullBaseOSDockerImage() throws Exception { |
| 98 | + logger.info("Pulling OS base images from OCIR ..."); |
| 99 | + String ocir_username = System.getenv("OCIR_USERNAME"); |
| 100 | + String ocir_password = System.getenv("OCIR_PASSWORD"); |
| 101 | + |
| 102 | + if(ocir_username == null || ocir_password == null) { |
| 103 | + throw new Exception("You need to set OCIR_USERNAME and OCIR_PASSWORD environment variable to pull base" + |
| 104 | + " OS image " + BASE_OS_IMG + ":" + BASE_OS_IMG_TAG); |
| 105 | + } |
| 106 | + |
| 107 | + pullDockerImage(OCIR_SERVER, OCIR_TENENT + "/" + ocir_username , ocir_password, BASE_OS_IMG, |
| 108 | + BASE_OS_IMG_TAG); |
| 109 | + } |
| 110 | + |
| 111 | + protected static void pullOracleDBDockerImage() throws Exception { |
| 112 | + logger.info("Pulling Oracle DB image from OCR ..."); |
| 113 | + String ocr_username = System.getenv("OCR_USERNAME"); |
| 114 | + String ocr_password = System.getenv("OCR_PASSWORD"); |
| 115 | + |
| 116 | + if(ocr_username == null || ocr_password == null) { |
| 117 | + throw new Exception("You need to set OCR_USERNAME and OCR_PASSWORD environment variable to pull DB " + |
| 118 | + "image " + ORACLE_DB_IMG + ":" + ORACLE_DB_IMG_TAG); |
| 119 | + } |
| 120 | + |
| 121 | + pullDockerImage(OCR_SERVER, ocr_username, ocr_password, ORACLE_DB_IMG, ORACLE_DB_IMG_TAG); |
| 122 | + } |
| 123 | + |
| 124 | + protected static void downloadInstallers(String... installers) throws Exception { |
| 125 | + // create the cache dir for downloading installers if not exists |
| 126 | + File cacheDir = new File(getInstallerCacheDir()); |
| 127 | + if( !cacheDir.exists()) { |
| 128 | + cacheDir.mkdir(); |
| 129 | + } |
| 130 | + |
| 131 | + boolean missingInstaller = false; |
| 132 | + StringBuffer errorMsg = new StringBuffer(); |
| 133 | + errorMsg.append("The test installers are missing. Please download: \n"); |
| 134 | + // check the required installer is downloaded |
| 135 | + for(String installer : installers) { |
| 136 | + File installFile = new File(getInstallerCacheDir() + FS + installer); |
| 137 | + if(!installFile.exists()) { |
| 138 | + missingInstaller = true; |
| 139 | + errorMsg.append( " " + installer + "\n"); |
| 140 | + } |
| 141 | + } |
| 142 | + errorMsg.append("and put them in " + getInstallerCacheDir()); |
| 143 | + if(missingInstaller) { |
| 144 | + throw new Exception(errorMsg.toString()); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + protected static String getProjectRoot() { |
| 149 | + return projectRoot; |
| 150 | + } |
| 151 | + |
| 152 | + protected static String getTargetDir() { |
| 153 | + return getProjectRoot() + FS + "target"; |
| 154 | + } |
| 155 | + |
| 156 | + protected static String getImagetoolHome() { |
| 157 | + return getProjectRoot() + FS + "imagetool-" + VERSION + "-SNAPSHOT"; |
| 158 | + } |
| 159 | + |
| 160 | + protected static String getInstallerCacheDir() { |
| 161 | + return getProjectRoot() + FS + "caches"; |
| 162 | + } |
| 163 | + |
| 164 | + protected static String getWDTResourcePath() { |
| 165 | + return getProjectRoot() + FS + "src" + FS + "test" + FS + "resources" + FS + "wdt"; |
| 166 | + } |
| 167 | + |
| 168 | + protected static void executeNoVerify(String command) throws Exception { |
| 169 | + logger.info("executing command: " + command); |
| 170 | + ExecCommand.exec(command); |
| 171 | + } |
| 172 | + |
| 173 | + protected void verifyResult(ExecResult result, String matchString) throws Exception { |
| 174 | + if(result.exitValue() != 0 || !result.stdout().contains(matchString)) { |
| 175 | + throw new Exception("verifying test result failed."); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + protected void verifyExitValue(ExecResult result, String command) throws Exception { |
| 180 | + if(result.exitValue() != 0) { |
| 181 | + logger.info(result.stderr()); |
| 182 | + throw new Exception("executing the following command failed: " + command); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + protected void verifyDockerImages(String imageTag) throws Exception { |
| 187 | + // verify the docker image is created |
| 188 | + ExecResult result = ExecCommand.exec("docker images | grep imagetool | grep " + imageTag + |
| 189 | + "| wc -l"); |
| 190 | + if(Integer.parseInt(result.stdout().trim()) != 1) { |
| 191 | + throw new Exception("wls docker image is not created as expected"); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + protected void logTestBegin(String testMethodName) throws Exception { |
| 196 | + logger.info("======================================="); |
| 197 | + logger.info("BEGIN test " + testMethodName + " ..."); |
| 198 | + } |
| 199 | + |
| 200 | + protected void logTestEnd(String testMethodName) throws Exception { |
| 201 | + logger.info("SUCCESS - " + testMethodName); |
| 202 | + logger.info("======================================="); |
| 203 | + } |
| 204 | + |
| 205 | + protected ExecResult listItemsInCache() throws Exception { |
| 206 | + String command = imagetool + " cache listItems"; |
| 207 | + return executeAndVerify(command, false); |
| 208 | + } |
| 209 | + |
| 210 | + protected ExecResult addInstallerToCache(String type, String version, String path) throws Exception { |
| 211 | + String command = imagetool + " cache addInstaller --type " + type + " --version " + version + |
| 212 | + " --path " + path; |
| 213 | + return executeAndVerify(command, false); |
| 214 | + } |
| 215 | + |
| 216 | + protected ExecResult addPatchToCache(String type, String patchId, String version, String path) throws Exception { |
| 217 | + String command = imagetool + " cache addPatch --type " + type + " --patchId " + patchId + "_" + |
| 218 | + version + " --path " + path; |
| 219 | + return executeAndVerify(command, false); |
| 220 | + } |
| 221 | + |
| 222 | + protected ExecResult addEntryToCache(String entryKey, String entryValue) throws Exception { |
| 223 | + String command = imagetool + " cache addEntry --key " + entryKey + " --value " + entryValue; |
| 224 | + return executeAndVerify(command, false); |
| 225 | + } |
| 226 | + |
| 227 | + protected ExecResult deleteEntryFromCache(String entryKey) throws Exception { |
| 228 | + String command = imagetool + " cache deleteEntry --key " + entryKey; |
| 229 | + return executeAndVerify(command, false); |
| 230 | + } |
| 231 | + |
| 232 | + protected ExecResult buildWDTArchive() throws Exception { |
| 233 | + logger.info("Building WDT archive ..."); |
| 234 | + String command = "sh " + getWDTResourcePath() + FS + "build-archive.sh"; |
| 235 | + return executeAndVerify(command, true); |
| 236 | + } |
| 237 | + |
| 238 | + protected void createDBContainer() throws Exception { |
| 239 | + logger.info("Creating an Oracle db docker container ..."); |
| 240 | + String command = "docker rm -f " + DB_CONTAINER_NAME; |
| 241 | + ExecCommand.exec(command); |
| 242 | + command = "docker run -d --name " + DB_CONTAINER_NAME + " -p 1521:1521 -p 5500:5500 --env=\"DB_PDB=InfraPDB1\"" + |
| 243 | + " --env=\"DB_DOMAIN=us.oracle.com\" --env=\"DB_BUNDLE=basic\" " + ORACLE_DB_IMG + ":" + |
| 244 | + ORACLE_DB_IMG_TAG; |
| 245 | + ExecCommand.exec(command); |
| 246 | + |
| 247 | + // wait for the db is ready |
| 248 | + command = "docker ps | grep " + DB_CONTAINER_NAME; |
| 249 | + checkCmdInLoop(command, "healthy"); |
| 250 | + } |
| 251 | + |
| 252 | + protected static void replaceStringInFile(String filename, String originalString, String newString) |
| 253 | + throws Exception { |
| 254 | + Path path = Paths.get(filename); |
| 255 | + |
| 256 | + String content = new String(Files.readAllBytes(path)); |
| 257 | + content = content.replaceAll(originalString, newString); |
| 258 | + Files.write(path, content.getBytes()); |
| 259 | + } |
| 260 | + |
| 261 | + private ExecResult executeAndVerify(String command, boolean isRedirectToOut) throws Exception { |
| 262 | + logger.info("Executing command: " + command); |
| 263 | + ExecResult result = ExecCommand.exec(command, isRedirectToOut); |
| 264 | + verifyExitValue(result, command); |
| 265 | + logger.info(result.stdout()); |
| 266 | + return result; |
| 267 | + } |
| 268 | + |
| 269 | + private static void pullDockerImage(String repoServer, String username, String password, |
| 270 | + String imagename, String imagetag) throws Exception { |
| 271 | + |
| 272 | + ExecCommand.exec("docker login " + repoServer + " -u " + username + |
| 273 | + " -p " + password); |
| 274 | + ExecCommand.exec("docker pull " + imagename + ":" + imagetag); |
| 275 | + |
| 276 | + // verify the docker image is pulled |
| 277 | + ExecResult result = ExecCommand.exec("docker images | grep " + imagename + " | grep " + |
| 278 | + imagetag + "| wc -l"); |
| 279 | + String resultString = result.stdout(); |
| 280 | + if(Integer.parseInt(resultString.trim()) != 1) { |
| 281 | + throw new Exception("docker image " + imagename + ":" + imagetag + " is not pulled as expected." |
| 282 | + + " Expected 1 image, found " + resultString); |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + private static void checkCmdInLoop(String cmd, String matchStr) |
| 287 | + throws Exception { |
| 288 | + int i = 0; |
| 289 | + while (i < maxIterations) { |
| 290 | + ExecResult result = ExecCommand.exec(cmd); |
| 291 | + |
| 292 | + // pod might not have been created or if created loop till condition |
| 293 | + if (result.exitValue() != 0 |
| 294 | + || (result.exitValue() == 0 && !result.stdout().contains(matchStr))) { |
| 295 | + logger.info("Output for " + cmd + "\n" + result.stdout() + "\n " + result.stderr()); |
| 296 | + // check for last iteration |
| 297 | + if (i == (maxIterations - 1)) { |
| 298 | + throw new RuntimeException( |
| 299 | + "FAILURE: " + cmd + " does not return the expected string " + matchStr + ", exiting!"); |
| 300 | + } |
| 301 | + logger.info( |
| 302 | + "Waiting for the expected String " + matchStr |
| 303 | + + ": Ite [" |
| 304 | + + i |
| 305 | + + "/" |
| 306 | + + maxIterations |
| 307 | + + "], sleeping " |
| 308 | + + waitTime |
| 309 | + + " seconds more"); |
| 310 | + |
| 311 | + Thread.sleep(waitTime * 1000); |
| 312 | + i++; |
| 313 | + } else { |
| 314 | + logger.info("get the expected String " + matchStr); |
| 315 | + break; |
| 316 | + } |
| 317 | + } |
| 318 | + } |
| 319 | +} |
0 commit comments