6
6
import java .io .BufferedReader ;
7
7
import java .io .File ;
8
8
import java .io .FileReader ;
9
+ import java .io .IOException ;
9
10
import java .nio .file .Files ;
10
11
import java .nio .file .Path ;
11
12
import java .nio .file .Paths ;
12
13
import java .util .ArrayList ;
13
14
import java .util .List ;
14
15
15
- import oracle .weblogic .deploy .integration .annotations .Logger ;
16
+ import oracle .weblogic .deploy .integration .annotations .TestingLogger ;
16
17
import oracle .weblogic .deploy .integration .utils .CommandResult ;
17
18
import oracle .weblogic .deploy .integration .utils .Runner ;
18
19
import oracle .weblogic .deploy .logging .PlatformLogger ;
19
20
import oracle .weblogic .deploy .logging .WLSDeployLogFactory ;
20
- import oracle .weblogic .deploy .util .FileUtils ;
21
+
22
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
23
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
21
24
22
25
public class BaseTest {
23
- @ Logger
26
+ @ TestingLogger
24
27
private static final PlatformLogger logger = WLSDeployLogFactory .getLogger ("integration.tests" );
25
28
protected static final String FS = File .separator ;
26
29
private static final String SAMPLE_ARCHIVE_FILE = "archive.zip" ;
27
30
private static final String WDT_ZIPFILE = "weblogic-deploy.zip" ;
28
31
private static final String WDT_HOME_DIR = "weblogic-deploy" ;
29
32
protected static final String SAMPLE_MODEL_FILE_PREFIX = "simple-topology" ;
30
33
protected static final String SAMPLE_VARIABLE_FILE = "domain.properties" ;
31
- private static int maxIterations = 50 ;
32
- private static int waitTime = 5 ;
33
- private static String projectRoot = "" ;
34
+ private static final int maxIterations = 50 ;
35
+ private static final int waitTime = 5 ;
34
36
protected static String mwhome_12213 = "" ;
35
37
protected static String createDomainScript = "" ;
36
38
protected static String compareModelScript = "" ;
@@ -39,17 +41,14 @@ public class BaseTest {
39
41
protected static String deployAppScript = "" ;
40
42
protected static String encryptModelScript = "" ;
41
43
protected static String validateModelScript = "" ;
42
- protected static String domainParent12213 = "" ;
44
+ protected static String domainParentDir = "" ;
43
45
protected static final String ORACLE_DB_IMG = "phx.ocir.io/weblogick8s/database/enterprise" ;
44
46
protected static final String ORACLE_DB_IMG_TAG = "12.2.0.1-slim" ;
45
47
private static final String DB_CONTAINER_NAME = "InfraDB" ;
46
48
47
49
protected static void initialize () {
48
50
49
51
logger .info ("Initializing the tests ..." );
50
- projectRoot = System .getProperty ("user.dir" );
51
- logger .info ("DEBUG: projectRoot=" + projectRoot );
52
-
53
52
mwhome_12213 = System .getProperty ("MW_HOME" );
54
53
55
54
createDomainScript = getWDTScriptsHome () + FS + "createDomain.sh" ;
@@ -60,7 +59,7 @@ protected static void initialize() {
60
59
validateModelScript = getWDTScriptsHome () + FS + "validateModel.sh" ;
61
60
compareModelScript = getWDTScriptsHome () + FS + "compareModel.sh" ;
62
61
63
- domainParent12213 = "." + FS + "domains" ;
62
+ domainParentDir = "." + FS + "target " + FS + "domains" ;
64
63
}
65
64
66
65
protected static void setup () throws Exception {
@@ -74,40 +73,21 @@ protected static void setup() throws Exception {
74
73
executeAndVerify (cmd );
75
74
76
75
// create domain_parent directory if not existing
77
- File domainParentDir = new File (domainParent12213 );
76
+ File domainParentDir = new File (BaseTest . domainParentDir );
78
77
if (!domainParentDir .exists ()) {
79
- domainParentDir .mkdir ();
78
+ assertTrue ( domainParentDir .mkdir (), "Setup failed to create Domain parent directory" );
80
79
}
81
-
82
- chmodScriptFiles (createDomainScript , discoverDomainScript , updateDomainScript , deployAppScript ,
83
- encryptModelScript , validateModelScript );
84
-
85
80
}
86
81
87
82
protected static void cleanup () throws Exception {
88
83
logger .info ("cleaning up the test environment ..." );
89
84
90
- // remove WDT script home directory
91
- String cmd = "rm -rf " + getTargetDir () + FS + WDT_HOME_DIR ;
92
- Runner .run (cmd );
93
-
94
85
String command = "docker rm -f " + DB_CONTAINER_NAME ;
95
86
Runner .run (command );
96
-
97
- // delete the domain directory created by the tests
98
- File domainParentDir = new File (domainParent12213 );
99
-
100
- if (domainParentDir .exists ()) {
101
- FileUtils .deleteDirectory (domainParentDir );
102
- }
103
- }
104
-
105
- protected static String getProjectRoot () {
106
- return projectRoot ;
107
87
}
108
88
109
- protected static String getTargetDir () {
110
- return getProjectRoot () + FS + "target" ;
89
+ protected static Path getTargetDir () {
90
+ return Paths . get ( "target" ) ;
111
91
}
112
92
113
93
protected static void chmodScriptFiles (String ... filenames ) throws Exception {
@@ -127,9 +107,8 @@ protected static void pullOracleDBDockerImage() throws Exception {
127
107
private static void pullDockerImage (String imagename , String imagetag ) throws Exception {
128
108
129
109
String cmd = "docker pull " + imagename + ":" + imagetag ;
130
- logger .info ("executing command: " + cmd );
131
110
CommandResult result = Runner .run (cmd );
132
- logger . info ( "DEBUG: result.stdout=" + result . stdout () );
111
+ assertEquals ( 0 , result .exitValue (), "Docker pull failed for " + imagename );
133
112
134
113
// verify the docker image is pulled
135
114
result = Runner .run ("docker images | grep " + imagename + " | grep " +
@@ -169,7 +148,6 @@ protected void verifyErrorMsg(CommandResult result, String errorMsg) throws Exce
169
148
170
149
protected void verifyModelFile (String modelFile ) throws Exception {
171
150
String cmd = "ls " + modelFile + " | wc -l" ;
172
- logger .info ("executing command: " + cmd );
173
151
CommandResult result = Runner .run (cmd );
174
152
if (Integer .parseInt (result .stdout ().trim ()) != 1 ) {
175
153
throw new Exception ("no model file is created as expected" );
@@ -188,8 +166,8 @@ protected void verifyFileDoesNotExists(String filePath) throws Exception {
188
166
}
189
167
}
190
168
191
- protected static String getResourcePath () {
192
- return getProjectRoot () + FS + "src" + FS + "test" + FS + "resources" ;
169
+ protected static Path getResourcePath () {
170
+ return Paths . get ( "src" , "test" , "resources" ) ;
193
171
}
194
172
195
173
protected static String getGeneratedResourcePath () {
@@ -210,8 +188,8 @@ protected static String getSampleModelFile(String suffix) {
210
188
return getResourcePath () + FS + SAMPLE_MODEL_FILE_PREFIX + suffix + ".yaml" ;
211
189
}
212
190
213
- protected static String getInstallerTargetDir () {
214
- return getProjectRoot () + FS + ".." + FS + "installer" + FS + "target" ;
191
+ protected static Path getInstallerTargetDir () {
192
+ return Paths . get ( ".." , "installer" , "target" ) ;
215
193
}
216
194
217
195
protected static String getSampleVariableFile () {
@@ -224,30 +202,28 @@ protected static void createDBContainer() throws Exception {
224
202
Runner .run (command );
225
203
226
204
String exposePort = "" ;
227
- System .out .println ("***********\n \n \n **********\n \n \n ********\n "
228
- + System .getProperty ("db.use.container.network" )
229
- + "\n **************\n \n ********" );
230
205
if (System .getProperty ("db.use.container.network" ).equals ("false" )) {
231
- exposePort = " -p1521:1521 " ;
206
+ exposePort = " -p1521:1521 -p5500:5500 " ;
232
207
}
233
208
234
209
command = "docker run -d --name " + DB_CONTAINER_NAME + " --env=\" DB_PDB=InfraPDB1\" " +
235
210
" --env=\" DB_DOMAIN=us.oracle.com\" --env=\" DB_BUNDLE=basic\" " + exposePort
236
211
+ ORACLE_DB_IMG + ":" + ORACLE_DB_IMG_TAG ;
237
212
Runner .run (command );
213
+ }
238
214
239
- // wait for the db is ready
240
- command = "docker ps | grep " + DB_CONTAINER_NAME ;
241
- checkCmdInLoop (command , "healthy" );
215
+ static void waitForDatabase () throws IOException , InterruptedException {
216
+ // Wait for the database container to be healthy before continuing
217
+ String command = "docker inspect --format='{{json .State.Health}}' " + DB_CONTAINER_NAME ;
218
+ checkCmdInLoop (command , "\" Status\" :\" healthy" );
242
219
}
243
220
244
- protected static void replaceStringInFile (String filename , String originalString , String newString )
221
+ protected static void replaceStringInFile (Path original , Path output , String originalString , String newString )
245
222
throws Exception {
246
- Path path = Paths .get (filename );
247
223
248
- String content = new String (Files .readAllBytes (path ));
224
+ String content = new String (Files .readAllBytes (original ));
249
225
content = content .replaceAll (originalString , newString );
250
- Files .write (path , content .getBytes ());
226
+ Files .write (output , content .getBytes ());
251
227
}
252
228
253
229
protected String getDBContainerIP () throws Exception {
@@ -289,22 +265,21 @@ protected BufferedReader inputYaml(String yamlFileName) throws Exception {
289
265
}
290
266
291
267
private static CommandResult executeAndVerify (String command ) throws Exception {
292
- logger .info ("Executing command: " + command );
293
268
CommandResult result = Runner .run (command );
294
269
verifyExitValue (result , command );
295
270
return result ;
296
271
}
297
272
298
- private static void checkCmdInLoop (String cmd , String matchStr )
299
- throws Exception {
273
+ private static void checkCmdInLoop (String cmd , String matchStr ) throws IOException , InterruptedException {
300
274
int i = 0 ;
301
275
while (i < maxIterations ) {
302
276
CommandResult result = Runner .run (cmd );
303
277
304
278
// pod might not have been created or if created loop till condition
305
279
if (result .exitValue () != 0
306
- || (result .exitValue () == 0 && !result .stdout ().contains (matchStr ))) {
307
- logger .info ("Output for " + cmd + "\n " + result .stdout () + "\n " + result .stdout ());
280
+ || (result .exitValue () == 0 && !result .stdout ().contains (matchStr ))) {
281
+
282
+ logger .info ("Output for '" + cmd + "'\n " + result .stdout () + "\n " + result .stdout ());
308
283
// check for last iteration
309
284
if (i == (maxIterations - 1 )) {
310
285
throw new RuntimeException (
@@ -323,7 +298,7 @@ private static void checkCmdInLoop(String cmd, String matchStr)
323
298
Thread .sleep (waitTime * 1000 );
324
299
i ++;
325
300
} else {
326
- logger .info ("get the expected String " + matchStr );
301
+ logger .info ("Found expected result: " + matchStr );
327
302
break ;
328
303
}
329
304
}
0 commit comments