77import com .oracle .weblogic .imagetool .integration .utils .ExecCommand ;
88import com .oracle .weblogic .imagetool .integration .utils .ExecResult ;
99import java .io .File ;
10+ import java .nio .file .Files ;
11+ import java .nio .file .Path ;
12+ import java .nio .file .Paths ;
1013import java .util .logging .Logger ;
1114
1215public class BaseTest {
@@ -17,13 +20,18 @@ public class BaseTest {
1720 protected static final String FS = File .separator ;
1821 private static final String OCIR_SERVER = "phx.ocir.io" ;
1922 private static final String OCIR_TENENT = "weblogick8s" ;
23+ private static final String OCR_SERVER = "container-registry.oracle.com" ;
2024 protected static final String BASE_OS_IMG = "phx.ocir.io/weblogick8s/oraclelinux" ;
2125 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" ;
2228 private static String projectRoot = "" ;
2329 protected static String wlsImgBldDir = "" ;
2430 protected static String wlsImgCacheDir = "" ;
2531 protected static String imagetool = "" ;
2632 private static String imagetoolZipfile = "" ;
33+ private static int maxIterations = 50 ;
34+ private static int waitTime = 5 ;
2735
2836 protected static void initialize () throws Exception {
2937 logger .info ("Initializing the tests ..." );
@@ -82,7 +90,7 @@ protected static void cleanup() throws Exception {
8290 executeNoVerify (command );
8391 }
8492
85- protected static void pullDockerImage () throws Exception {
93+ protected static void pullBaseOSDockerImage () throws Exception {
8694 logger .info ("Pulling OS base images from OCIR ..." );
8795 String ocir_username = System .getenv ("OCIR_USERNAME" );
8896 String ocir_password = System .getenv ("OCIR_PASSWORD" );
@@ -91,16 +99,20 @@ protected static void pullDockerImage() throws Exception {
9199 throw new Exception ("You need to set OCIR_USERNAME and OCIR_PASSWORD environment variable to pull images" );
92100 }
93101
94- ExecCommand . exec ( "docker login " + OCIR_SERVER + " -u " + OCIR_TENENT + "/" + ocir_username +
95- " -p " + ocir_password );
96- ExecCommand . exec ( "docker pull " + BASE_OS_IMG + ":" + BASE_OS_IMG_TAG );
102+ pullDockerImage ( OCIR_SERVER , OCIR_TENENT + "/" + ocir_username , ocir_password , BASE_OS_IMG ,
103+ BASE_OS_IMG_TAG );
104+ }
97105
98- // verify the docker image is pulled
99- ExecResult result = ExecCommand .exec ("docker images | grep " + BASE_OS_IMG + " | grep " +
100- BASE_OS_IMG_TAG + "| wc -l" );
101- if (Integer .parseInt (result .stdout ()) != 1 ) {
102- throw new Exception ("Base OS docker image is not pulled as expected" );
106+ protected static void pullOracleDBDockerImage () throws Exception {
107+ logger .info ("Pulling Oracle DB image from OCR ..." );
108+ String ocr_username = System .getenv ("OCR_USERNAME" );
109+ String ocr_password = System .getenv ("OCR_PASSWORD" );
110+
111+ if (ocr_username == null || ocr_password == null ) {
112+ throw new Exception ("You need to set OCR_USERNAME and OCR_PASSWORD environment variable to pull images" );
103113 }
114+
115+ pullDockerImage (OCR_SERVER , ocr_username , ocr_password , ORACLE_DB_IMG , ORACLE_DB_IMG_TAG );
104116 }
105117
106118 protected static void downloadInstallers (String ... installers ) throws Exception {
@@ -210,11 +222,83 @@ protected ExecResult buildWDTArchive() throws Exception {
210222 return executeAndVerify (command , true );
211223 }
212224
225+ protected void createDBContainer () throws Exception {
226+ logger .info ("Creating an Oracle db docker container ..." );
227+ String command = "docker rm -f InfraDB" ;
228+ ExecCommand .exec (command );
229+ command = "docker run -d --name InfraDB -p 1521:1521 -p 5500:5500 --env=\" DB_PDB=InfraPDB1\" " +
230+ " --env=\" DB_DOMAIN=us.oracle.com\" --env=\" DB_BUNDLE=basic\" " + ORACLE_DB_IMG + ":" +
231+ ORACLE_DB_IMG_TAG ;
232+ ExecCommand .exec (command );
233+
234+ // wait for the db is ready
235+ command = "docker ps | grep InfraDB" ;
236+ checkCmdInLoop (command , "healthy" );
237+ }
238+
239+ protected static void replaceStringInFile (String filename , String originalString , String newString )
240+ throws Exception {
241+ Path path = Paths .get (filename );
242+
243+ String content = new String (Files .readAllBytes (path ));
244+ content = content .replaceAll (originalString , newString );
245+ Files .write (path , content .getBytes ());
246+ }
247+
213248 private ExecResult executeAndVerify (String command , boolean isRedirectToOut ) throws Exception {
214249 logger .info ("Executing command: " + command );
215250 ExecResult result = ExecCommand .exec (command , isRedirectToOut );
216251 verifyExitValue (result , command );
217252 logger .info (result .stdout ());
218253 return result ;
219254 }
255+
256+ private static void pullDockerImage (String repoServer , String username , String password ,
257+ String imagename , String imagetag ) throws Exception {
258+
259+ ExecCommand .exec ("docker login " + repoServer + " -u " + username +
260+ " -p " + password );
261+ ExecCommand .exec ("docker pull " + imagename + ":" + imagetag );
262+
263+ // verify the docker image is pulled
264+ ExecResult result = ExecCommand .exec ("docker images | grep " + imagename + " | grep " +
265+ imagetag + "| wc -l" );
266+ if (Integer .parseInt (result .stdout ()) != 1 ) {
267+ throw new Exception ("docker image " + imagename + ":" + imagetag + " is not pulled as expected" );
268+ }
269+ }
270+
271+ private static void checkCmdInLoop (String cmd , String matchStr )
272+ throws Exception {
273+ int i = 0 ;
274+ while (i < maxIterations ) {
275+ ExecResult result = ExecCommand .exec (cmd );
276+
277+ // pod might not have been created or if created loop till condition
278+ if (result .exitValue () != 0
279+ || (result .exitValue () == 0 && !result .stdout ().contains (matchStr ))) {
280+ logger .info ("Output for " + cmd + "\n " + result .stdout () + "\n " + result .stderr ());
281+ // check for last iteration
282+ if (i == (maxIterations - 1 )) {
283+ throw new RuntimeException (
284+ "FAILURE: " + cmd + " does not return the expected string " + matchStr + ", exiting!" );
285+ }
286+ logger .info (
287+ "Waiting for the expected String " + matchStr
288+ + ": Ite ["
289+ + i
290+ + "/"
291+ + maxIterations
292+ + "], sleeping "
293+ + waitTime
294+ + " seconds more" );
295+
296+ Thread .sleep (waitTime * 1000 );
297+ i ++;
298+ } else {
299+ logger .info ("get the expected String " + matchStr );
300+ break ;
301+ }
302+ }
303+ }
220304}
0 commit comments