Skip to content

Commit b426a88

Browse files
committed
Added tests, which cover replacing existing service, for ExternalService. WIP (due to broken Cluster#fillInFrom) for adding more the aforementioned tests for ClusterService,
1 parent eff9fb8 commit b426a88

File tree

2 files changed

+144
-59
lines changed

2 files changed

+144
-59
lines changed

model/src/main/java/oracle/kubernetes/weblogic/domain/v2/DomainV2Configurator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,13 @@ public ClusterConfigurator withPodAnnotation(String name, String value) {
451451

452452
@Override
453453
public ClusterConfigurator withServiceLabel(String name, String value) {
454-
cluster.addServiceLabel(name, value);
454+
cluster.addClusterLabel(name, value);
455455
return this;
456456
}
457457

458458
@Override
459459
public ClusterConfigurator withServiceAnnotation(String name, String value) {
460-
cluster.addServiceAnnotation(name, value);
460+
cluster.addClusterAnnotation(name, value);
461461
return this;
462462
}
463463

operator/src/test/java/oracle/kubernetes/operator/helpers/ServiceHelperTest.java

Lines changed: 142 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@
5656
import oracle.kubernetes.operator.wlsconfig.WlsDomainConfig;
5757
import oracle.kubernetes.operator.work.Step;
5858
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;
5962
import oracle.kubernetes.weblogic.domain.v2.Domain;
6063
import oracle.kubernetes.weblogic.domain.v2.DomainSpec;
61-
import oracle.kubernetes.weblogic.domain.v2.ManagedServer;
6264
import org.junit.After;
6365
import org.junit.Before;
6466
import org.junit.Test;
@@ -69,11 +71,11 @@ public class ServiceHelperTest {
6971
private static final String NS = "namespace";
7072
private static final String TEST_CLUSTER = "cluster-1";
7173
private static final int TEST_NODE_PORT = 7002;
74+
private static final int BAD_NODE_PORT = 9900;
7275
private static final int TEST_PORT = 7000;
7376
private static final int BAD_PORT = 9999;
7477
private static final String DOMAIN_NAME = "domain1";
7578
private static final String TEST_SERVER_NAME = "server1";
76-
private static final String TEST_ADMIN_NAME = "admin";
7779
private static final String SERVICE_NAME = "service1";
7880
private static final String UID = "uid1";
7981
private static final String BAD_VERSION = "bad-version";
@@ -98,24 +100,26 @@ public class ServiceHelperTest {
98100
private final TerminalStep terminalStep = new TerminalStep();
99101
private RetryStrategyStub retryStrategy = createStrictStub(RetryStrategyStub.class);
100102
private List<LogRecord> logRecords = new ArrayList<>();
103+
private WlsDomainConfig domainConfig;
101104

102105
public ServiceHelperTest() {}
103106

104107
@Before
105108
public void setUp() throws Exception {
109+
configureAdminServer().configureAdminService().withChannel("default", TEST_NODE_PORT);
106110
mementos.add(
107111
TestUtils.silenceOperatorLogger()
108112
.collectLogMessages(logRecords, MESSAGE_KEYS)
109113
.withLogLevel(Level.FINE));
110114
mementos.add(testSupport.installRequestStepFactory());
111115

112116
WlsDomainConfigSupport configSupport = new WlsDomainConfigSupport(DOMAIN_NAME);
113-
configSupport.addWlsServer(ADMIN_SERVER);
117+
configSupport.addWlsServer(ADMIN_SERVER, TEST_PORT);
114118
configSupport.addWlsServer(TEST_SERVER_NAME, TEST_PORT);
115119
configSupport.addWlsCluster(TEST_CLUSTER, TEST_SERVER_NAME);
116120
configSupport.setAdminServerName(ADMIN_SERVER);
117121

118-
WlsDomainConfig domainConfig = configSupport.createDomainConfig();
122+
domainConfig = configSupport.createDomainConfig();
119123
testSupport
120124
.addToPacket(CLUSTER_NAME, TEST_CLUSTER)
121125
.addToPacket(SERVER_NAME, TEST_SERVER_NAME)
@@ -141,6 +145,14 @@ private DomainSpec createDomainSpec() {
141145
return new DomainSpec().withDomainUID(UID);
142146
}
143147

148+
private AdminServerConfigurator configureAdminServer() {
149+
return configureDomain().configureAdminServer();
150+
}
151+
152+
private DomainConfigurator configureDomain() {
153+
return DomainConfiguratorFactory.forDomain(domainPresenceInfo.getDomain());
154+
}
155+
144156
// ------ service deletion --------
145157

146158
@Test
@@ -266,38 +278,25 @@ public void onClusterStepRunWithMatchingServiceWithoutSpecType_addToDomainPresen
266278

267279
@Test
268280
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);
277282
}
278283

279284
@Test
280285
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);
289287
}
290288

291289
@Test
292290
public void onClusterStepRunWithServiceWithBadPort_replaceIt() {
293-
initializeClusterServiceFromRecord(createClusterServiceWithBadPort());
294-
expectDeleteServiceSuccessful(getClusterServiceName());
295-
expectSuccessfulCreateClusterService();
296-
297-
testSupport.runSteps(ServiceHelper.createForClusterStep(terminalStep));
291+
verifyClusterServiceReplaced(this::withBadPort);
292+
}
298293

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()));
301300
}
302301

303302
@Test
@@ -394,12 +393,6 @@ private V1Service createClusterServiceWithBadVersion() {
394393
.metadata(new V1ObjectMeta().putLabelsItem(RESOURCE_VERSION_LABEL, BAD_VERSION));
395394
}
396395

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-
403396
private V1Service createClusterServiceWithBadPort() {
404397
return new V1Service()
405398
.spec(createSpecWithBadPort())
@@ -479,14 +472,14 @@ public void onServerStepRunWithServiceWithBadVersion_replaceIt() {
479472

480473
@Test
481474
public void onServerStepRunWithServiceWithLabelAdded_replaceIt() {
482-
createManagedServerWithLabel("anyLabel", "anyValue");
475+
configureManagedServerWithLabel("anyLabel", "anyValue");
483476
verifyServerServiceReplaced(createServerService(), withLabel(createServerService()));
484477
}
485478

486479
@Test
487480
public void onServerStepRunWithServiceWithLabelValueChanged_replaceIt() {
488481
final String newLabelValue = "newValue";
489-
createManagedServerWithLabel("anyLabel", newLabelValue);
482+
configureManagedServerWithLabel("anyLabel", newLabelValue);
490483
verifyServerServiceReplaced(
491484
withLabel(createServerService()), withLabel(createServerService(), newLabelValue));
492485
}
@@ -498,14 +491,14 @@ public void onServerStepRunWithServiceWithLabelRemoved_replaceIt() {
498491

499492
@Test
500493
public void onServerStepRunWithServiceWithAnnotationAdded_replaceIt() {
501-
createManagedServerWithAnnotation("anyAnnotation", "anyValue");
494+
configureManagedServerWithAnnotation("anyAnnotation", "anyValue");
502495
verifyServerServiceReplaced(createServerService(), withAnnotation(createServerService()));
503496
}
504497

505498
@Test
506499
public void onServerStepRunWithServiceWithAnnotationValueChanged_replaceIt() {
507500
final String newAnnotationValue = "newValue";
508-
createManagedServerWithAnnotation("anyAnnotation", newAnnotationValue);
501+
configureManagedServerWithAnnotation("anyAnnotation", newAnnotationValue);
509502
verifyServerServiceReplaced(
510503
withAnnotation(createServerService()),
511504
withAnnotation(createServerService(), newAnnotationValue));
@@ -517,8 +510,57 @@ public void onServerStepRunWithServiceWithAnnotationRemoved_replaceIt() {
517510
}
518511

519512
@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);
522564
}
523565

524566
private void verifyServerServiceReplaced(V1Service oldService, V1Service newService) {
@@ -531,15 +573,17 @@ private void verifyServerServiceReplaced(V1Service oldService, V1Service newServ
531573
assertThat(logRecords, containsInfo(MANAGED_SERVICE_REPLACED));
532574
}
533575

534-
private void verifyAdminServiceReplaced(ServiceMutator mutator) {
535-
verifyAdminServiceReplaced(mutator.change(createAdminService()), createAdminService());
576+
private void verifyExternalServiceReplaced(ServiceMutator mutator) {
577+
verifyExternalServiceReplaced(mutator.change(createAdminService()), createAdminService());
536578
}
537579

538-
private void verifyAdminServiceReplaced(V1Service oldService, V1Service newService) {
580+
private void verifyExternalServiceReplaced(V1Service oldService, V1Service newService) {
539581
initializeAdminServiceFromRecord(oldService);
540582
expectDeleteServiceSuccessful(getAdminServiceName());
541583
expectSuccessfulCreateService(newService);
542584

585+
testSupport.addToPacket(SERVER_SCAN, domainConfig.getServerConfig(ADMIN_SERVER));
586+
testSupport.addToPacket(SERVER_NAME, ADMIN_SERVER);
543587
testSupport.runSteps(ServiceHelper.createForExternalServiceStep(terminalStep));
544588

545589
assertThat(logRecords, containsInfo(ADMIN_SERVICE_REPLACED));
@@ -549,6 +593,21 @@ private void verifyServerServiceReplaced(ServiceMutator mutator) {
549593
verifyServerServiceReplaced(mutator.change(createServerService()), createServerService());
550594
}
551595

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+
552611
private V1ServiceSpec createServerServiceSpec() {
553612
return createUntypedServerServiceSpec(TEST_SERVER_NAME).type("ClusterIP").clusterIP("None");
554613
}
@@ -568,15 +627,15 @@ private void initializeServiceFromRecord(V1Service service) {
568627
}
569628

570629
private void initializeAdminServiceFromRecord(V1Service service) {
571-
domainPresenceInfo.getServers().put(TEST_ADMIN_NAME, createSko(service));
630+
domainPresenceInfo.getServers().put(ADMIN_SERVER, createSko(service));
572631
}
573632

574633
private String getServerServiceName() {
575634
return LegalNames.toServerServiceName(UID, TEST_SERVER_NAME);
576635
}
577636

578637
private String getAdminServiceName() {
579-
return LegalNames.toExternalServiceName(UID, TEST_ADMIN_NAME);
638+
return LegalNames.toExternalServiceName(UID, ADMIN_SERVER);
580639
}
581640

582641
private void expectSuccessfulCreateService(V1Service service) {
@@ -624,13 +683,12 @@ private V1Service createAdminService(V1ServiceSpec serviceSpec) {
624683
.putLabelsItem(RESOURCE_VERSION_LABEL, VersionConstants.DOMAIN_V2)
625684
.putLabelsItem(DOMAINUID_LABEL, UID)
626685
.putLabelsItem(DOMAINNAME_LABEL, DOMAIN_NAME)
627-
.putLabelsItem(SERVERNAME_LABEL, TEST_ADMIN_NAME)
686+
.putLabelsItem(SERVERNAME_LABEL, ADMIN_SERVER)
628687
.putLabelsItem(CREATEDBYOPERATOR_LABEL, "true"));
629688
}
630689

631690
private V1ServiceSpec createAdminServiceSpec() {
632-
final V1ServiceSpec serviceSpec =
633-
createUntypedServerServiceSpec(TEST_ADMIN_NAME).type("NodePort");
691+
final V1ServiceSpec serviceSpec = createUntypedServerServiceSpec(ADMIN_SERVER).type("NodePort");
634692

635693
serviceSpec.getPorts().stream().findAny().ifPresent(port -> port.setNodePort(TEST_NODE_PORT));
636694

@@ -646,6 +704,27 @@ private V1Service withBadVersion(V1Service service) {
646704
return service;
647705
}
648706

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+
649728
private V1Service withLabel(V1Service service) {
650729
return withLabel(service, "anyValue");
651730
}
@@ -674,18 +753,24 @@ private V1Service withAnnotation(V1Service service, String value) {
674753
return service;
675754
}
676755

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);
682770
}
683771

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);
689774
}
690775

691776
private CallTestSupport.CannedResponse expectReadService(String serviceName) {

0 commit comments

Comments
 (0)