29
29
import static oracle .kubernetes .operator .logging .MessageKeys .MANAGED_SERVICE_EXISTS ;
30
30
import static oracle .kubernetes .operator .logging .MessageKeys .MANAGED_SERVICE_REPLACED ;
31
31
import static org .hamcrest .Matchers .hasEntry ;
32
+ import static org .hamcrest .Matchers .hasKey ;
32
33
import static org .hamcrest .Matchers .is ;
34
+ import static org .hamcrest .Matchers .not ;
33
35
import static org .hamcrest .Matchers .nullValue ;
34
36
import static org .hamcrest .junit .MatcherAssert .assertThat ;
35
37
54
56
import oracle .kubernetes .operator .wlsconfig .WlsDomainConfig ;
55
57
import oracle .kubernetes .operator .work .Step ;
56
58
import oracle .kubernetes .operator .work .TerminalStep ;
57
- import oracle .kubernetes .weblogic .domain .AdminServerConfigurator ;
58
- import oracle .kubernetes .weblogic .domain .DomainConfigurator ;
59
- import oracle .kubernetes .weblogic .domain .DomainConfiguratorFactory ;
60
59
import oracle .kubernetes .weblogic .domain .v2 .Domain ;
61
60
import oracle .kubernetes .weblogic .domain .v2 .DomainSpec ;
61
+ import oracle .kubernetes .weblogic .domain .v2 .ManagedServer ;
62
62
import org .junit .After ;
63
63
import org .junit .Before ;
64
64
import org .junit .Test ;
@@ -68,10 +68,12 @@ public class ServiceHelperTest {
68
68
69
69
private static final String NS = "namespace" ;
70
70
private static final String TEST_CLUSTER = "cluster-1" ;
71
+ private static final int TEST_NODE_PORT = 7002 ;
71
72
private static final int TEST_PORT = 7000 ;
72
73
private static final int BAD_PORT = 9999 ;
73
74
private static final String DOMAIN_NAME = "domain1" ;
74
75
private static final String TEST_SERVER_NAME = "server1" ;
76
+ private static final String TEST_ADMIN_NAME = "admin" ;
75
77
private static final String SERVICE_NAME = "service1" ;
76
78
private static final String UID = "uid1" ;
77
79
private static final String BAD_VERSION = "bad-version" ;
@@ -463,12 +465,7 @@ public void onServerStepRunWithNoService_retryOnFailure() {
463
465
464
466
@ Test
465
467
public void onServerStepRunWithMatchingService_addToSko () {
466
- V1Service service =
467
- new V1Service ()
468
- .spec (createServerServiceSpec ())
469
- .metadata (
470
- new V1ObjectMeta ().putLabelsItem (RESOURCE_VERSION_LABEL , DEFAULT_DOMAIN_VERSION ));
471
- initializeServiceFromRecord (service );
468
+ initializeServiceFromRecord (createServerService ());
472
469
473
470
testSupport .runSteps (ServiceHelper .createForServerStep (terminalStep ));
474
471
@@ -480,6 +477,50 @@ public void onServerStepRunWithServiceWithBadVersion_replaceIt() {
480
477
verifyServerServiceReplaced (this ::withBadVersion );
481
478
}
482
479
480
+ @ Test
481
+ public void onServerStepRunWithServiceWithLabelAdded_replaceIt () {
482
+ createManagedServerWithLabel ("anyLabel" , "anyValue" );
483
+ verifyServerServiceReplaced (createServerService (), withLabel (createServerService ()));
484
+ }
485
+
486
+ @ Test
487
+ public void onServerStepRunWithServiceWithLabelValueChanged_replaceIt () {
488
+ final String newLabelValue = "newValue" ;
489
+ createManagedServerWithLabel ("anyLabel" , newLabelValue );
490
+ verifyServerServiceReplaced (
491
+ withLabel (createServerService ()), withLabel (createServerService (), newLabelValue ));
492
+ }
493
+
494
+ @ Test
495
+ public void onServerStepRunWithServiceWithLabelRemoved_replaceIt () {
496
+ verifyServerServiceReplaced (this ::withLabel );
497
+ }
498
+
499
+ @ Test
500
+ public void onServerStepRunWithServiceWithAnnotationAdded_replaceIt () {
501
+ createManagedServerWithAnnotation ("anyAnnotation" , "anyValue" );
502
+ verifyServerServiceReplaced (createServerService (), withAnnotation (createServerService ()));
503
+ }
504
+
505
+ @ Test
506
+ public void onServerStepRunWithServiceWithAnnotationValueChanged_replaceIt () {
507
+ final String newAnnotationValue = "newValue" ;
508
+ createManagedServerWithAnnotation ("anyAnnotation" , newAnnotationValue );
509
+ verifyServerServiceReplaced (
510
+ withAnnotation (createServerService ()),
511
+ withAnnotation (createServerService (), newAnnotationValue ));
512
+ }
513
+
514
+ @ Test
515
+ public void onServerStepRunWithServiceWithAnnotationRemoved_replaceIt () {
516
+ verifyServerServiceReplaced (this ::withAnnotation );
517
+ }
518
+
519
+ @ Test
520
+ public void onExternalStepRunWithServiceWithBadVersion_replaceIt () {
521
+ verifyAdminServiceReplaced (this ::withBadVersion );
522
+ }
523
+
483
524
private void verifyServerServiceReplaced (V1Service oldService , V1Service newService ) {
484
525
initializeServiceFromRecord (oldService );
485
526
expectDeleteServiceSuccessful (getServerServiceName ());
@@ -490,18 +531,32 @@ private void verifyServerServiceReplaced(V1Service oldService, V1Service newServ
490
531
assertThat (logRecords , containsInfo (MANAGED_SERVICE_REPLACED ));
491
532
}
492
533
534
+ private void verifyAdminServiceReplaced (ServiceMutator mutator ) {
535
+ verifyAdminServiceReplaced (mutator .change (createAdminService ()), createAdminService ());
536
+ }
537
+
538
+ private void verifyAdminServiceReplaced (V1Service oldService , V1Service newService ) {
539
+ initializeAdminServiceFromRecord (oldService );
540
+ expectDeleteServiceSuccessful (getAdminServiceName ());
541
+ expectSuccessfulCreateService (newService );
542
+
543
+ testSupport .runSteps (ServiceHelper .createForExternalServiceStep (terminalStep ));
544
+
545
+ assertThat (logRecords , containsInfo (ADMIN_SERVICE_REPLACED ));
546
+ }
547
+
493
548
private void verifyServerServiceReplaced (ServiceMutator mutator ) {
494
549
verifyServerServiceReplaced (mutator .change (createServerService ()), createServerService ());
495
550
}
496
551
497
552
private V1ServiceSpec createServerServiceSpec () {
498
- return createUntypedServerServiceSpec ().type ("ClusterIP" ).clusterIP ("None" );
553
+ return createUntypedServerServiceSpec (TEST_SERVER_NAME ).type ("ClusterIP" ).clusterIP ("None" );
499
554
}
500
555
501
- private V1ServiceSpec createUntypedServerServiceSpec () {
556
+ private V1ServiceSpec createUntypedServerServiceSpec (String serverName ) {
502
557
return new V1ServiceSpec ()
503
558
.putSelectorItem (DOMAINUID_LABEL , UID )
504
- .putSelectorItem (SERVERNAME_LABEL , TEST_SERVER_NAME )
559
+ .putSelectorItem (SERVERNAME_LABEL , serverName )
505
560
.putSelectorItem (CREATEDBYOPERATOR_LABEL , "true" )
506
561
.ports (
507
562
Collections .singletonList (
@@ -512,10 +567,18 @@ private void initializeServiceFromRecord(V1Service service) {
512
567
domainPresenceInfo .getServers ().put (TEST_SERVER_NAME , createSko (service ));
513
568
}
514
569
570
+ private void initializeAdminServiceFromRecord (V1Service service ) {
571
+ domainPresenceInfo .getServers ().put (TEST_ADMIN_NAME , createSko (service ));
572
+ }
573
+
515
574
private String getServerServiceName () {
516
575
return LegalNames .toServerServiceName (UID , TEST_SERVER_NAME );
517
576
}
518
577
578
+ private String getAdminServiceName () {
579
+ return LegalNames .toExternalServiceName (UID , TEST_ADMIN_NAME );
580
+ }
581
+
519
582
private void expectSuccessfulCreateService (V1Service service ) {
520
583
expectCreateService (service ).returning (service );
521
584
}
@@ -547,12 +610,31 @@ private V1Service createServerService(V1ServiceSpec serviceSpec) {
547
610
.putLabelsItem (CREATEDBYOPERATOR_LABEL , "true" ));
548
611
}
549
612
550
- private AdminServerConfigurator configureAdminServer () {
551
- return configureDomain (). configureAdminServer ( );
613
+ private V1Service createAdminService () {
614
+ return createAdminService ( createAdminServiceSpec () );
552
615
}
553
616
554
- private DomainConfigurator configureDomain () {
555
- return DomainConfiguratorFactory .forDomain (domainPresenceInfo .getDomain ());
617
+ private V1Service createAdminService (V1ServiceSpec serviceSpec ) {
618
+ return new V1Service ()
619
+ .spec (serviceSpec )
620
+ .metadata (
621
+ new V1ObjectMeta ()
622
+ .name (getAdminServiceName ())
623
+ .namespace (NS )
624
+ .putLabelsItem (RESOURCE_VERSION_LABEL , VersionConstants .DOMAIN_V2 )
625
+ .putLabelsItem (DOMAINUID_LABEL , UID )
626
+ .putLabelsItem (DOMAINNAME_LABEL , DOMAIN_NAME )
627
+ .putLabelsItem (SERVERNAME_LABEL , TEST_ADMIN_NAME )
628
+ .putLabelsItem (CREATEDBYOPERATOR_LABEL , "true" ));
629
+ }
630
+
631
+ private V1ServiceSpec createAdminServiceSpec () {
632
+ final V1ServiceSpec serviceSpec =
633
+ createUntypedServerServiceSpec (TEST_ADMIN_NAME ).type ("NodePort" );
634
+
635
+ serviceSpec .getPorts ().stream ().findAny ().ifPresent (port -> port .setNodePort (TEST_NODE_PORT ));
636
+
637
+ return serviceSpec ;
556
638
}
557
639
558
640
interface ServiceMutator {
@@ -564,6 +646,48 @@ private V1Service withBadVersion(V1Service service) {
564
646
return service ;
565
647
}
566
648
649
+ private V1Service withLabel (V1Service service ) {
650
+ return withLabel (service , "anyValue" );
651
+ }
652
+
653
+ private V1Service withLabel (V1Service service , String labelValue ) {
654
+ final String labelName = "anyLabel" ;
655
+
656
+ assertThat (service .getMetadata ().getLabels (), not (hasKey (labelName )));
657
+ service .getMetadata ().putLabelsItem (labelName , labelValue );
658
+ assertThat (service .getMetadata ().getLabels (), hasKey (labelName ));
659
+
660
+ return service ;
661
+ }
662
+
663
+ private V1Service withAnnotation (V1Service service ) {
664
+ return withAnnotation (service , "anyValue" );
665
+ }
666
+
667
+ private V1Service withAnnotation (V1Service service , String value ) {
668
+ final String annotationName = "anyAnnotation" ;
669
+
670
+ assertThat (service .getMetadata ().getAnnotations (), not (hasKey (annotationName )));
671
+ service .getMetadata ().putAnnotationsItem (annotationName , value );
672
+ assertThat (service .getMetadata ().getAnnotations (), hasKey (annotationName ));
673
+
674
+ return service ;
675
+ }
676
+
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 );
682
+ }
683
+
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 );
689
+ }
690
+
567
691
private CallTestSupport .CannedResponse expectReadService (String serviceName ) {
568
692
return testSupport .createCannedResponse ("readService" ).withNamespace (NS ).withName (serviceName );
569
693
}
0 commit comments