7
7
import com .oracle .weblogic .imagetool .integration .utils .ExecCommand ;
8
8
import com .oracle .weblogic .imagetool .integration .utils .ExecResult ;
9
9
import java .io .File ;
10
+ import java .nio .file .Files ;
11
+ import java .nio .file .Path ;
12
+ import java .nio .file .Paths ;
10
13
import java .util .logging .Logger ;
11
14
12
15
public class BaseTest {
@@ -17,13 +20,18 @@ public class BaseTest {
17
20
protected static final String FS = File .separator ;
18
21
private static final String OCIR_SERVER = "phx.ocir.io" ;
19
22
private static final String OCIR_TENENT = "weblogick8s" ;
23
+ private static final String OCR_SERVER = "container-registry.oracle.com" ;
20
24
protected static final String BASE_OS_IMG = "phx.ocir.io/weblogick8s/oraclelinux" ;
21
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" ;
22
28
private static String projectRoot = "" ;
23
29
protected static String wlsImgBldDir = "" ;
24
30
protected static String wlsImgCacheDir = "" ;
25
31
protected static String imagetool = "" ;
26
32
private static String imagetoolZipfile = "" ;
33
+ private static int maxIterations = 50 ;
34
+ private static int waitTime = 5 ;
27
35
28
36
protected static void initialize () throws Exception {
29
37
logger .info ("Initializing the tests ..." );
@@ -82,7 +90,7 @@ protected static void cleanup() throws Exception {
82
90
executeNoVerify (command );
83
91
}
84
92
85
- protected static void pullDockerImage () throws Exception {
93
+ protected static void pullBaseOSDockerImage () throws Exception {
86
94
logger .info ("Pulling OS base images from OCIR ..." );
87
95
String ocir_username = System .getenv ("OCIR_USERNAME" );
88
96
String ocir_password = System .getenv ("OCIR_PASSWORD" );
@@ -91,16 +99,20 @@ protected static void pullDockerImage() throws Exception {
91
99
throw new Exception ("You need to set OCIR_USERNAME and OCIR_PASSWORD environment variable to pull images" );
92
100
}
93
101
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
+ }
97
105
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" );
103
113
}
114
+
115
+ pullDockerImage (OCR_SERVER , ocr_username , ocr_password , ORACLE_DB_IMG , ORACLE_DB_IMG_TAG );
104
116
}
105
117
106
118
protected static void downloadInstallers (String ... installers ) throws Exception {
@@ -210,11 +222,83 @@ protected ExecResult buildWDTArchive() throws Exception {
210
222
return executeAndVerify (command , true );
211
223
}
212
224
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
+
213
248
private ExecResult executeAndVerify (String command , boolean isRedirectToOut ) throws Exception {
214
249
logger .info ("Executing command: " + command );
215
250
ExecResult result = ExecCommand .exec (command , isRedirectToOut );
216
251
verifyExitValue (result , command );
217
252
logger .info (result .stdout ());
218
253
return result ;
219
254
}
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
+ }
220
304
}
0 commit comments