77import java .nio .file .Path ;
88import java .nio .file .Paths ;
99import java .nio .file .StandardCopyOption ;
10+ import java .time .OffsetDateTime ;
1011import java .util .Collections ;
1112import java .util .List ;
13+ import java .util .Map ;
1214
15+ import io .kubernetes .client .custom .V1Patch ;
16+ import oracle .weblogic .domain .CommonMount ;
1317import oracle .weblogic .domain .Domain ;
1418import oracle .weblogic .kubernetes .actions .impl .primitive .Command ;
1519import oracle .weblogic .kubernetes .actions .impl .primitive .CommandParams ;
4347import static oracle .weblogic .kubernetes .actions .TestActions .defaultAppParams ;
4448import static oracle .weblogic .kubernetes .actions .TestActions .deleteImage ;
4549import static oracle .weblogic .kubernetes .actions .TestActions .dockerPush ;
50+ import static oracle .weblogic .kubernetes .actions .TestActions .getDomainCustomResource ;
4651import static oracle .weblogic .kubernetes .actions .TestActions .getServiceNodePort ;
52+ import static oracle .weblogic .kubernetes .actions .TestActions .patchDomainCustomResource ;
53+ import static oracle .weblogic .kubernetes .assertions .TestAssertions .verifyRollingRestartOccurred ;
4754import static oracle .weblogic .kubernetes .utils .CommonMiiTestUtils .createDomainResource ;
55+ import static oracle .weblogic .kubernetes .utils .CommonTestUtils .checkSystemResourceConfig ;
4856import static oracle .weblogic .kubernetes .utils .CommonTestUtils .checkSystemResourceConfiguration ;
4957import static oracle .weblogic .kubernetes .utils .CommonTestUtils .createDomainAndVerify ;
5058import static oracle .weblogic .kubernetes .utils .CommonTestUtils .createOcirRepoSecret ;
5159import static oracle .weblogic .kubernetes .utils .CommonTestUtils .createSecretWithUsernamePassword ;
5260import static oracle .weblogic .kubernetes .utils .CommonTestUtils .getExternalServicePodName ;
61+ import static oracle .weblogic .kubernetes .utils .CommonTestUtils .getPodsWithTimeStamps ;
5362import static oracle .weblogic .kubernetes .utils .CommonTestUtils .installAndVerifyOperator ;
63+ import static oracle .weblogic .kubernetes .utils .FileUtils .replaceStringInFile ;
5464import static oracle .weblogic .kubernetes .utils .FileUtils .unzipWDTInstallationFile ;
5565import static oracle .weblogic .kubernetes .utils .ThreadSafeLogger .getLogger ;
5666import static org .awaitility .Awaitility .with ;
5767import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
68+ import static org .junit .jupiter .api .Assertions .assertFalse ;
5869import static org .junit .jupiter .api .Assertions .assertNotEquals ;
5970import static org .junit .jupiter .api .Assertions .assertNotNull ;
6071import static org .junit .jupiter .api .Assertions .assertTrue ;
@@ -69,6 +80,12 @@ public class ItMiiCommonMount {
6980 private String domainUid = "domain1" ;
7081 private static String miiCMImage1 = MII_COMMONMOUNT_IMAGE_NAME + ":" + MII_BASIC_IMAGE_TAG + "1" ;
7182 private static String miiCMImage2 = MII_COMMONMOUNT_IMAGE_NAME + ":" + MII_BASIC_IMAGE_TAG + "2" ;
83+ private static String miiCMImage3 = MII_COMMONMOUNT_IMAGE_NAME + ":" + MII_BASIC_IMAGE_TAG + "3" ;
84+ private static Map <String , OffsetDateTime > podsWithTimeStamps = null ;
85+ private final String adminServerPodName = domainUid + "-admin-server" ;
86+ private final String managedServerPrefix = domainUid + "-managed-server" ;
87+ private final int replicaCount = 2 ;
88+
7289 ConditionFactory withStandardRetryPolicy
7390 = with ().pollDelay (0 , SECONDS )
7491 .and ().with ().pollInterval (10 , SECONDS )
@@ -87,7 +104,7 @@ public static void initAll(@Namespaces(2) List<String> namespaces) {
87104 assertNotNull (namespaces .get (0 ), "Namespace list is null" );
88105 opNamespace = namespaces .get (0 );
89106
90- logger .info ("Creating unique namespace for Domain " );
107+ logger .info ("Creating unique namespace for Domain1 " );
91108 assertNotNull (namespaces .get (1 ), "Namespace list is null" );
92109 domainNamespace = namespaces .get (1 );
93110
@@ -106,9 +123,6 @@ public static void initAll(@Namespaces(2) List<String> namespaces) {
106123 public void testCreateDomainUsingMultipleCommonMounts () {
107124
108125 // admin/managed server name here should match with model yaml
109- final String adminServerPodName = domainUid + "-admin-server" ;
110- final String managedServerPrefix = domainUid + "-managed-server" ;
111- final int replicaCount = 2 ;
112126 final String commonMountVolumeName = "commonMountsVolume1" ;
113127 final String commonMountPath = "/common" ;
114128
@@ -140,6 +154,10 @@ public void testCreateDomainUsingMultipleCommonMounts() {
140154 Paths .get (MODEL_DIR , MII_BASIC_WDT_MODEL_FILE ),
141155 Paths .get (modelsPath1 .toString (), MII_BASIC_WDT_MODEL_FILE ),
142156 StandardCopyOption .REPLACE_EXISTING ));
157+ assertDoesNotThrow (() -> Files .copy (
158+ Paths .get (MODEL_DIR , "multi-model-one-ds.20.yaml" ),
159+ Paths .get (modelsPath1 .toString (), "multi-model-one-ds.20.yaml" ),
160+ StandardCopyOption .REPLACE_EXISTING ));
143161
144162 // build app
145163 assertTrue (buildAppArchive (defaultAppParams ()
@@ -200,7 +218,8 @@ public void testCreateDomainUsingMultipleCommonMounts() {
200218 // create domain and verify its running
201219 logger .info ("Creating domain {0} with common mount images {1} {2} in namespace {3}" ,
202220 domainUid , miiCMImage1 , miiCMImage2 , domainNamespace );
203- createDomainAndVerify (domainUid , domainCR , domainNamespace , adminServerPodName , managedServerPrefix , replicaCount );
221+ createDomainAndVerify (domainUid , domainCR , domainNamespace ,
222+ adminServerPodName , managedServerPrefix , replicaCount );
204223
205224 // check configuration for JMS
206225 int adminServiceNodePort
@@ -211,6 +230,122 @@ public void testCreateDomainUsingMultipleCommonMounts() {
211230 logger .info ("Found the JMSSystemResource configuration" );
212231 }
213232
233+ /**
234+ * Reuse created a domain with datasource using common mount containing the DataSource,
235+ * verify the domain is running and JDBC DataSource resource is added.
236+ * Patch domain with updated JDBC URL info and verify the update.
237+ */
238+ @ Test
239+ @ Order (2 )
240+ @ DisplayName ("Test to update data source url in the domain using common mount" )
241+ public void testUpdateDataSourceInDomainUsingCommonMount1 () {
242+
243+ Path multipleCMPath1 = Paths .get (RESULTS_ROOT , "multiplecmimage1" );
244+ Path modelsPath1 = Paths .get (multipleCMPath1 .toString (), "models" );
245+
246+
247+ // create stage dir for common mount with image3
248+ // replace DataSource URL info in the model file
249+ assertDoesNotThrow (() -> replaceStringInFile (Paths .get (modelsPath1 .toString (),
250+ "/multi-model-one-ds.20.yaml" ).toString (), "xxx.xxx.x.xxx:1521" ,
251+ "localhost:7001" ),"Can't replace datasource url in the model file" );
252+ assertDoesNotThrow (() -> replaceStringInFile (Paths .get (modelsPath1 .toString (),
253+ "/multi-model-one-ds.20.yaml" ).toString (), "ORCLCDB" ,
254+ "dbsvc" ),"Can't replace datasource url in the model file" );
255+
256+ // create image3 with model and wdt installation files
257+ createCommonMountImage (multipleCMPath1 .toString (),
258+ Paths .get (RESOURCE_DIR , "commonmount" , "Dockerfile" ).toString (), miiCMImage3 );
259+
260+ // push image3 to repo for multi node cluster
261+ if (!DOMAIN_IMAGES_REPO .isEmpty ()) {
262+ logger .info ("docker push image {0} to registry {1}" , miiCMImage3 , DOMAIN_IMAGES_REPO );
263+ assertTrue (dockerPush (miiCMImage3 ), String .format ("docker push failed for image %s" , miiCMImage3 ));
264+ }
265+
266+ // check configuration for DataSource in the running domain
267+ int adminServiceNodePort
268+ = getServiceNodePort (domainNamespace , getExternalServicePodName (adminServerPodName ), "default" );
269+ assertNotEquals (-1 , adminServiceNodePort , "admin server default node port is not valid" );
270+ assertTrue (checkSystemResourceConfig (adminServiceNodePort ,
271+ "JDBCSystemResources/TestDataSource/JDBCResource/JDBCDriverParams" ,
272+ "jdbc:oracle:thin:@\\ /\\ /xxx.xxx.x.xxx:1521\\ /ORCLCDB" ),"Can't find expected URL configuration for DataSource" );
273+
274+ logger .info ("Found the DataResource configuration" );
275+
276+ patchDomainWithCMImageAndVerify (miiCMImage1 , miiCMImage3 , domainUid , domainNamespace );
277+
278+ assertTrue (checkSystemResourceConfig (adminServiceNodePort ,
279+ "JDBCSystemResources/TestDataSource/JDBCResource/JDBCDriverParams" ,
280+ "jdbc:oracle:thin:@\\ /\\ /localhost:7001\\ /dbsvc" ), "Can't find expected URL configuration for DataSource" );
281+
282+ logger .info ("Found the DataResource configuration" );
283+ }
284+
285+ private static void patchDomainWithCMImageAndVerify (String oldImageName , String newImageName ,
286+ String domainUid , String domainNamespace ) {
287+ String adminServerPodName = domainUid + "-admin-server" ;
288+ String managedServerPrefix = domainUid + "-managed-server" ;
289+ Domain domain1 = assertDoesNotThrow (() -> getDomainCustomResource (domainUid , domainNamespace ),
290+ String .format ("getDomainCustomResource failed with ApiException when tried to get domain %s in namespace %s" ,
291+ domainUid , domainNamespace ));
292+ assertNotNull (domain1 , "Got null domain resource " );
293+ assertNotNull (domain1 .getSpec ().getServerPod ().getCommonMounts (), domain1 + "/spec/serverPod/commonMounts is null" );
294+ List <CommonMount > commonMountList = domain1 .getSpec ().getServerPod ().getCommonMounts ();
295+ assertFalse (commonMountList .isEmpty (), "CommonMount list is empty" );
296+ String searchString ;
297+ int index = 0 ;
298+
299+ CommonMount cmMount = commonMountList .stream ()
300+ .filter (commonMount -> oldImageName .equals (commonMount .getImage ()))
301+ .findAny ()
302+ .orElse (null );
303+ assertNotNull (cmMount , "Can't find common Mount with Image name " + oldImageName
304+ + "can't patch domain " + domainUid );
305+
306+ index = commonMountList .indexOf (cmMount );
307+ searchString = "\" /spec/serverPod/commonMounts/" + index + "/image\" " ;
308+ StringBuffer patchStr = new StringBuffer ("[{" );
309+ patchStr .append ("\" op\" : \" replace\" ," )
310+ .append (" \" path\" : " + searchString + "," )
311+ .append (" \" value\" : \" " + newImageName + "\" " )
312+ .append (" }]" );
313+ logger .info ("Common Mount patch string: " + patchStr );
314+
315+ //get current timestamp before domain rolling restart to verify domain roll events
316+ podsWithTimeStamps = getPodsWithTimeStamps (domainNamespace , adminServerPodName ,
317+ managedServerPrefix , 2 );
318+ V1Patch patch = new V1Patch ((patchStr ).toString ());
319+
320+ boolean cmPatched = assertDoesNotThrow (() ->
321+ patchDomainCustomResource (domainUid , domainNamespace , patch , "application/json-patch+json" ),
322+ "patchDomainCustomResource(Common Mount) failed " );
323+ assertTrue (cmPatched , "patchDomainCustomResource(common Mount) failed" );
324+
325+ domain1 = assertDoesNotThrow (() -> getDomainCustomResource (domainUid , domainNamespace ),
326+ String .format ("getDomainCustomResource failed with ApiException when tried to get domain %s in namespace %s" ,
327+ domainUid , domainNamespace ));
328+ assertNotNull (domain1 , "Got null domain resource after patching" );
329+ assertNotNull (domain1 .getSpec (), domain1 + " /spec/serverPod is null" );
330+ assertNotNull (domain1 .getSpec ().getServerPod (), domain1 + " /spec/serverPod is null" );
331+ assertNotNull (domain1 .getSpec ().getServerPod ().getCommonMounts (), domain1 + "/spec/serverPod/commonMounts is null" );
332+
333+ //verify the new CommonMount image in the new patched domain
334+ commonMountList = domain1 .getSpec ().getServerPod ().getCommonMounts ();
335+
336+ String cmImage = commonMountList .get (index ).getImage ();
337+ logger .info ("In the new patched domain imageValue is: {0}" , cmImage );
338+ assertTrue (cmImage .equalsIgnoreCase (newImageName ), "common mount image was not updated"
339+ + " in the new patched domain" );
340+
341+ // verify the server pods are rolling restarted and back to ready state
342+ logger .info ("Verifying rolling restart occurred for domain {0} in namespace {1}" ,
343+ domainUid , domainNamespace );
344+
345+ assertTrue (verifyRollingRestartOccurred (podsWithTimeStamps , 1 , domainNamespace ),
346+ String .format ("Rolling restart failed for domain %s in namespace %s" , domainUid , domainNamespace ));
347+ }
348+
214349 /**
215350 * Cleanup images.
216351 */
@@ -223,6 +358,10 @@ public void tearDownAll() {
223358 if (miiCMImage2 != null ) {
224359 deleteImage (miiCMImage2 );
225360 }
361+
362+ if (miiCMImage3 != null ) {
363+ deleteImage (miiCMImage3 );
364+ }
226365 }
227366
228367 private void createCommonMountImage (String stageDirPath , String dockerFileLocation , String cmImage ) {
@@ -234,5 +373,4 @@ private void createCommonMountImage(String stageDirPath, String dockerFileLocati
234373 .command (cmdToExecute ))
235374 .execute (), String .format ("Failed to execute" , cmdToExecute ));
236375 }
237-
238376}
0 commit comments