56
56
import oracle .kubernetes .operator .wlsconfig .WlsDomainConfig ;
57
57
import oracle .kubernetes .operator .work .Step ;
58
58
import oracle .kubernetes .operator .work .TerminalStep ;
59
+ import oracle .kubernetes .weblogic .domain .AdminServerConfigurator ;
60
+ import oracle .kubernetes .weblogic .domain .DomainConfigurator ;
61
+ import oracle .kubernetes .weblogic .domain .DomainConfiguratorFactory ;
59
62
import oracle .kubernetes .weblogic .domain .v2 .Domain ;
60
63
import oracle .kubernetes .weblogic .domain .v2 .DomainSpec ;
61
- import oracle .kubernetes .weblogic .domain .v2 .ManagedServer ;
62
64
import org .junit .After ;
63
65
import org .junit .Before ;
64
66
import org .junit .Test ;
@@ -69,11 +71,11 @@ public class ServiceHelperTest {
69
71
private static final String NS = "namespace" ;
70
72
private static final String TEST_CLUSTER = "cluster-1" ;
71
73
private static final int TEST_NODE_PORT = 7002 ;
74
+ private static final int BAD_NODE_PORT = 9900 ;
72
75
private static final int TEST_PORT = 7000 ;
73
76
private static final int BAD_PORT = 9999 ;
74
77
private static final String DOMAIN_NAME = "domain1" ;
75
78
private static final String TEST_SERVER_NAME = "server1" ;
76
- private static final String TEST_ADMIN_NAME = "admin" ;
77
79
private static final String SERVICE_NAME = "service1" ;
78
80
private static final String UID = "uid1" ;
79
81
private static final String BAD_VERSION = "bad-version" ;
@@ -98,24 +100,26 @@ public class ServiceHelperTest {
98
100
private final TerminalStep terminalStep = new TerminalStep ();
99
101
private RetryStrategyStub retryStrategy = createStrictStub (RetryStrategyStub .class );
100
102
private List <LogRecord > logRecords = new ArrayList <>();
103
+ private WlsDomainConfig domainConfig ;
101
104
102
105
public ServiceHelperTest () {}
103
106
104
107
@ Before
105
108
public void setUp () throws Exception {
109
+ configureAdminServer ().configureAdminService ().withChannel ("default" , TEST_NODE_PORT );
106
110
mementos .add (
107
111
TestUtils .silenceOperatorLogger ()
108
112
.collectLogMessages (logRecords , MESSAGE_KEYS )
109
113
.withLogLevel (Level .FINE ));
110
114
mementos .add (testSupport .installRequestStepFactory ());
111
115
112
116
WlsDomainConfigSupport configSupport = new WlsDomainConfigSupport (DOMAIN_NAME );
113
- configSupport .addWlsServer (ADMIN_SERVER );
117
+ configSupport .addWlsServer (ADMIN_SERVER , TEST_PORT );
114
118
configSupport .addWlsServer (TEST_SERVER_NAME , TEST_PORT );
115
119
configSupport .addWlsCluster (TEST_CLUSTER , TEST_SERVER_NAME );
116
120
configSupport .setAdminServerName (ADMIN_SERVER );
117
121
118
- WlsDomainConfig domainConfig = configSupport .createDomainConfig ();
122
+ domainConfig = configSupport .createDomainConfig ();
119
123
testSupport
120
124
.addToPacket (CLUSTER_NAME , TEST_CLUSTER )
121
125
.addToPacket (SERVER_NAME , TEST_SERVER_NAME )
@@ -141,6 +145,14 @@ private DomainSpec createDomainSpec() {
141
145
return new DomainSpec ().withDomainUID (UID );
142
146
}
143
147
148
+ private AdminServerConfigurator configureAdminServer () {
149
+ return configureDomain ().configureAdminServer ();
150
+ }
151
+
152
+ private DomainConfigurator configureDomain () {
153
+ return DomainConfiguratorFactory .forDomain (domainPresenceInfo .getDomain ());
154
+ }
155
+
144
156
// ------ service deletion --------
145
157
146
158
@ Test
@@ -266,38 +278,25 @@ public void onClusterStepRunWithMatchingServiceWithoutSpecType_addToDomainPresen
266
278
267
279
@ Test
268
280
public void onClusterStepRunWithServiceWithBadVersion_replaceIt () {
269
- initializeClusterServiceFromRecord (createClusterServiceWithBadVersion ());
270
- expectDeleteServiceSuccessful (getClusterServiceName ());
271
- expectSuccessfulCreateClusterService ();
272
-
273
- testSupport .runSteps (ServiceHelper .createForClusterStep (terminalStep ));
274
-
275
- assertThat (domainPresenceInfo .getClusters (), hasEntry (TEST_CLUSTER , createClusterService ()));
276
- assertThat (logRecords , containsInfo (CLUSTER_SERVICE_REPLACED ));
281
+ verifyClusterServiceReplaced (this ::withBadVersion );
277
282
}
278
283
279
284
@ Test
280
285
public void onClusterStepRunWithServiceWithBadSpecType_replaceIt () {
281
- initializeClusterServiceFromRecord (createClusterServiceWithBadSpecType ());
282
- expectDeleteServiceSuccessful (getClusterServiceName ());
283
- expectSuccessfulCreateClusterService ();
284
-
285
- testSupport .runSteps (ServiceHelper .createForClusterStep (terminalStep ));
286
-
287
- assertThat (domainPresenceInfo .getClusters (), hasEntry (TEST_CLUSTER , createClusterService ()));
288
- assertThat (logRecords , containsInfo (CLUSTER_SERVICE_REPLACED ));
286
+ verifyClusterServiceReplaced (this ::withBadSpecType );
289
287
}
290
288
291
289
@ Test
292
290
public void onClusterStepRunWithServiceWithBadPort_replaceIt () {
293
- initializeClusterServiceFromRecord (createClusterServiceWithBadPort ());
294
- expectDeleteServiceSuccessful (getClusterServiceName ());
295
- expectSuccessfulCreateClusterService ();
296
-
297
- testSupport .runSteps (ServiceHelper .createForClusterStep (terminalStep ));
291
+ verifyClusterServiceReplaced (this ::withBadPort );
292
+ }
298
293
299
- assertThat (domainPresenceInfo .getClusters (), hasEntry (TEST_CLUSTER , createClusterService ()));
300
- assertThat (logRecords , containsInfo (CLUSTER_SERVICE_REPLACED ));
294
+ // TODO: oracle.kubernetes.weblogic.domain.v2.Cluster.getConfiguration is broken. Doesn't fill in
295
+ // Cluster#clusterService.
296
+ // @Test
297
+ public void onClusterStepRunWithServiceWithLabelAdded_replaceIt () {
298
+ configureClusterWithLabel ("anyLabel" , "anyValue" );
299
+ verifyClusterServiceReplaced (createClusterService (), withLabel (createClusterService ()));
301
300
}
302
301
303
302
@ Test
@@ -394,12 +393,6 @@ private V1Service createClusterServiceWithBadVersion() {
394
393
.metadata (new V1ObjectMeta ().putLabelsItem (RESOURCE_VERSION_LABEL , BAD_VERSION ));
395
394
}
396
395
397
- private V1Service createClusterServiceWithBadSpecType () {
398
- return new V1Service ()
399
- .spec (new V1ServiceSpec ().type ("BadType" ))
400
- .metadata (new V1ObjectMeta ().putLabelsItem (RESOURCE_VERSION_LABEL , DEFAULT_DOMAIN_VERSION ));
401
- }
402
-
403
396
private V1Service createClusterServiceWithBadPort () {
404
397
return new V1Service ()
405
398
.spec (createSpecWithBadPort ())
@@ -479,14 +472,14 @@ public void onServerStepRunWithServiceWithBadVersion_replaceIt() {
479
472
480
473
@ Test
481
474
public void onServerStepRunWithServiceWithLabelAdded_replaceIt () {
482
- createManagedServerWithLabel ("anyLabel" , "anyValue" );
475
+ configureManagedServerWithLabel ("anyLabel" , "anyValue" );
483
476
verifyServerServiceReplaced (createServerService (), withLabel (createServerService ()));
484
477
}
485
478
486
479
@ Test
487
480
public void onServerStepRunWithServiceWithLabelValueChanged_replaceIt () {
488
481
final String newLabelValue = "newValue" ;
489
- createManagedServerWithLabel ("anyLabel" , newLabelValue );
482
+ configureManagedServerWithLabel ("anyLabel" , newLabelValue );
490
483
verifyServerServiceReplaced (
491
484
withLabel (createServerService ()), withLabel (createServerService (), newLabelValue ));
492
485
}
@@ -498,14 +491,14 @@ public void onServerStepRunWithServiceWithLabelRemoved_replaceIt() {
498
491
499
492
@ Test
500
493
public void onServerStepRunWithServiceWithAnnotationAdded_replaceIt () {
501
- createManagedServerWithAnnotation ("anyAnnotation" , "anyValue" );
494
+ configureManagedServerWithAnnotation ("anyAnnotation" , "anyValue" );
502
495
verifyServerServiceReplaced (createServerService (), withAnnotation (createServerService ()));
503
496
}
504
497
505
498
@ Test
506
499
public void onServerStepRunWithServiceWithAnnotationValueChanged_replaceIt () {
507
500
final String newAnnotationValue = "newValue" ;
508
- createManagedServerWithAnnotation ("anyAnnotation" , newAnnotationValue );
501
+ configureManagedServerWithAnnotation ("anyAnnotation" , newAnnotationValue );
509
502
verifyServerServiceReplaced (
510
503
withAnnotation (createServerService ()),
511
504
withAnnotation (createServerService (), newAnnotationValue ));
@@ -517,8 +510,57 @@ public void onServerStepRunWithServiceWithAnnotationRemoved_replaceIt() {
517
510
}
518
511
519
512
@ Test
520
- public void onExternalStepRunWithServiceWithBadVersion_replaceIt () {
521
- verifyAdminServiceReplaced (this ::withBadVersion );
513
+ public void onExternalServiceStepRunWithServiceWithBadVersion_replaceIt () {
514
+ verifyExternalServiceReplaced (this ::withBadVersion );
515
+ }
516
+
517
+ @ Test
518
+ public void onExternalServiceStepRunWithServiceWithBadPort_replaceIt () {
519
+ verifyExternalServiceReplaced (this ::withBadPort );
520
+ }
521
+
522
+ @ Test
523
+ public void onExternalServiceStepRunWithServiceWithBadNodePort_replaceIt () {
524
+ verifyExternalServiceReplaced (this ::withBadNodePort );
525
+ }
526
+
527
+ @ Test
528
+ public void onExternalServiceStepRunWithServiceWithLabelAdded_replaceIt () {
529
+ configureAdminServerWithLabel ("anyLabel" , "anyValue" );
530
+ verifyExternalServiceReplaced (createAdminService (), withLabel (createAdminService ()));
531
+ }
532
+
533
+ @ Test
534
+ public void onExternalServiceStepRunWithServiceWithLabelValueChanged_replaceIt () {
535
+ final String newLabelValue = "newValue" ;
536
+ configureAdminServerWithLabel ("anyLabel" , newLabelValue );
537
+ verifyExternalServiceReplaced (
538
+ withLabel (createAdminService ()), withLabel (createAdminService (), newLabelValue ));
539
+ }
540
+
541
+ @ Test
542
+ public void onExternalServiceStepRunWithServiceWithLabelRemoved_replaceIt () {
543
+ verifyExternalServiceReplaced (this ::withLabel );
544
+ }
545
+
546
+ @ Test
547
+ public void onExternalServiceStepRunWithServiceWithAnnotationAdded_replaceIt () {
548
+ configureAdminServerWithAnnotation ("anyAnnotation" , "anyValue" );
549
+ verifyExternalServiceReplaced (createAdminService (), withAnnotation (createAdminService ()));
550
+ }
551
+
552
+ @ Test
553
+ public void onExternalServiceStepRunWithServiceWithAnnotationValueChanged_replaceIt () {
554
+ final String newAnnotationValue = "newValue" ;
555
+ configureAdminServerWithAnnotation ("anyAnnotation" , newAnnotationValue );
556
+ verifyExternalServiceReplaced (
557
+ withAnnotation (createAdminService ()),
558
+ withAnnotation (createAdminService (), newAnnotationValue ));
559
+ }
560
+
561
+ @ Test
562
+ public void onExternalServiceStepRunWithServiceWithAnnotationRemoved_replaceIt () {
563
+ verifyExternalServiceReplaced (this ::withAnnotation );
522
564
}
523
565
524
566
private void verifyServerServiceReplaced (V1Service oldService , V1Service newService ) {
@@ -531,15 +573,17 @@ private void verifyServerServiceReplaced(V1Service oldService, V1Service newServ
531
573
assertThat (logRecords , containsInfo (MANAGED_SERVICE_REPLACED ));
532
574
}
533
575
534
- private void verifyAdminServiceReplaced (ServiceMutator mutator ) {
535
- verifyAdminServiceReplaced (mutator .change (createAdminService ()), createAdminService ());
576
+ private void verifyExternalServiceReplaced (ServiceMutator mutator ) {
577
+ verifyExternalServiceReplaced (mutator .change (createAdminService ()), createAdminService ());
536
578
}
537
579
538
- private void verifyAdminServiceReplaced (V1Service oldService , V1Service newService ) {
580
+ private void verifyExternalServiceReplaced (V1Service oldService , V1Service newService ) {
539
581
initializeAdminServiceFromRecord (oldService );
540
582
expectDeleteServiceSuccessful (getAdminServiceName ());
541
583
expectSuccessfulCreateService (newService );
542
584
585
+ testSupport .addToPacket (SERVER_SCAN , domainConfig .getServerConfig (ADMIN_SERVER ));
586
+ testSupport .addToPacket (SERVER_NAME , ADMIN_SERVER );
543
587
testSupport .runSteps (ServiceHelper .createForExternalServiceStep (terminalStep ));
544
588
545
589
assertThat (logRecords , containsInfo (ADMIN_SERVICE_REPLACED ));
@@ -549,6 +593,21 @@ private void verifyServerServiceReplaced(ServiceMutator mutator) {
549
593
verifyServerServiceReplaced (mutator .change (createServerService ()), createServerService ());
550
594
}
551
595
596
+ private void verifyClusterServiceReplaced (ServiceMutator mutator ) {
597
+ verifyClusterServiceReplaced (mutator .change (createClusterService ()), createClusterService ());
598
+ }
599
+
600
+ private void verifyClusterServiceReplaced (V1Service oldService , V1Service newService ) {
601
+ initializeClusterServiceFromRecord (oldService );
602
+ expectDeleteServiceSuccessful (getClusterServiceName ());
603
+ expectCreateService (newService ).returning (newService );
604
+
605
+ testSupport .runSteps (ServiceHelper .createForClusterStep (terminalStep ));
606
+
607
+ assertThat (domainPresenceInfo .getClusters (), hasEntry (TEST_CLUSTER , newService ));
608
+ assertThat (logRecords , containsInfo (CLUSTER_SERVICE_REPLACED ));
609
+ }
610
+
552
611
private V1ServiceSpec createServerServiceSpec () {
553
612
return createUntypedServerServiceSpec (TEST_SERVER_NAME ).type ("ClusterIP" ).clusterIP ("None" );
554
613
}
@@ -568,15 +627,15 @@ private void initializeServiceFromRecord(V1Service service) {
568
627
}
569
628
570
629
private void initializeAdminServiceFromRecord (V1Service service ) {
571
- domainPresenceInfo .getServers ().put (TEST_ADMIN_NAME , createSko (service ));
630
+ domainPresenceInfo .getServers ().put (ADMIN_SERVER , createSko (service ));
572
631
}
573
632
574
633
private String getServerServiceName () {
575
634
return LegalNames .toServerServiceName (UID , TEST_SERVER_NAME );
576
635
}
577
636
578
637
private String getAdminServiceName () {
579
- return LegalNames .toExternalServiceName (UID , TEST_ADMIN_NAME );
638
+ return LegalNames .toExternalServiceName (UID , ADMIN_SERVER );
580
639
}
581
640
582
641
private void expectSuccessfulCreateService (V1Service service ) {
@@ -624,13 +683,12 @@ private V1Service createAdminService(V1ServiceSpec serviceSpec) {
624
683
.putLabelsItem (RESOURCE_VERSION_LABEL , VersionConstants .DOMAIN_V2 )
625
684
.putLabelsItem (DOMAINUID_LABEL , UID )
626
685
.putLabelsItem (DOMAINNAME_LABEL , DOMAIN_NAME )
627
- .putLabelsItem (SERVERNAME_LABEL , TEST_ADMIN_NAME )
686
+ .putLabelsItem (SERVERNAME_LABEL , ADMIN_SERVER )
628
687
.putLabelsItem (CREATEDBYOPERATOR_LABEL , "true" ));
629
688
}
630
689
631
690
private V1ServiceSpec createAdminServiceSpec () {
632
- final V1ServiceSpec serviceSpec =
633
- createUntypedServerServiceSpec (TEST_ADMIN_NAME ).type ("NodePort" );
691
+ final V1ServiceSpec serviceSpec = createUntypedServerServiceSpec (ADMIN_SERVER ).type ("NodePort" );
634
692
635
693
serviceSpec .getPorts ().stream ().findAny ().ifPresent (port -> port .setNodePort (TEST_NODE_PORT ));
636
694
@@ -646,6 +704,27 @@ private V1Service withBadVersion(V1Service service) {
646
704
return service ;
647
705
}
648
706
707
+ private V1Service withBadSpecType (V1Service service ) {
708
+ service .getSpec ().type ("BadType" );
709
+ return service ;
710
+ }
711
+
712
+ private V1Service withBadPort (V1Service service ) {
713
+ List <V1ServicePort > ports = service .getSpec ().getPorts ();
714
+ assertThat (ports .size (), is (1 ));
715
+
716
+ ports .stream ().findAny ().get ().setPort (BAD_PORT );
717
+ return service ;
718
+ }
719
+
720
+ private V1Service withBadNodePort (V1Service service ) {
721
+ List <V1ServicePort > ports = service .getSpec ().getPorts ();
722
+ assertThat (ports .size (), is (1 ));
723
+
724
+ ports .stream ().findAny ().get ().setNodePort (BAD_NODE_PORT );
725
+ return service ;
726
+ }
727
+
649
728
private V1Service withLabel (V1Service service ) {
650
729
return withLabel (service , "anyValue" );
651
730
}
@@ -674,18 +753,24 @@ private V1Service withAnnotation(V1Service service, String value) {
674
753
return service ;
675
754
}
676
755
677
- private void createManagedServerWithLabel (String label , String value ) {
678
- final ManagedServer ms = new ManagedServer ();
679
- ms .setServerName (TEST_SERVER_NAME );
680
- ms .getServiceLabels ().put (label , value );
681
- domainPresenceInfo .getDomain ().getSpec ().getManagedServers ().add (ms );
756
+ private void configureClusterWithLabel (String label , String value ) {
757
+ configureDomain ().configureCluster (TEST_CLUSTER ).withServiceLabel (label , value );
758
+ }
759
+
760
+ private void configureManagedServerWithLabel (String label , String value ) {
761
+ configureDomain ().configureServer (TEST_SERVER_NAME ).withServiceLabel (label , value );
762
+ }
763
+
764
+ private void configureManagedServerWithAnnotation (String annotation , String value ) {
765
+ configureDomain ().configureServer (TEST_SERVER_NAME ).withServiceAnnotation (annotation , value );
766
+ }
767
+
768
+ private void configureAdminServerWithLabel (String label , String value ) {
769
+ configureDomain ().configureAdminServer ().withServiceLabel (label , value );
682
770
}
683
771
684
- private void createManagedServerWithAnnotation (String annotation , String value ) {
685
- final ManagedServer ms = new ManagedServer ();
686
- ms .setServerName (TEST_SERVER_NAME );
687
- ms .getServiceAnnotations ().put (annotation , value );
688
- domainPresenceInfo .getDomain ().getSpec ().getManagedServers ().add (ms );
772
+ private void configureAdminServerWithAnnotation (String annotation , String value ) {
773
+ configureDomain ().configureAdminServer ().withServiceAnnotation (annotation , value );
689
774
}
690
775
691
776
private CallTestSupport .CannedResponse expectReadService (String serviceName ) {
0 commit comments