Skip to content

Owls88697 stop namespaces that no longer managed by operator when change to dedicated strategy #2345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 4, 2021
Prev Previous commit
Next Next commit
unit test use recheckDomains when testing a specific strategy
  • Loading branch information
doxiao committed Apr 26, 2021
commit 781c8eb2bbd70d349aed4a99dea807273751c5ac
79 changes: 38 additions & 41 deletions operator/src/test/java/oracle/kubernetes/operator/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,14 @@ public void afterMissingCRDcorrected_subsequentFailureLogsReasonForFailure() {
}

@Test
public void withNamespaceList_onStartNamespaceStep_startsNamespaces() {
public void withNamespaceList_onRecheckDomains_startsNamespaces() {
defineSelectionStrategy(SelectionStrategy.List);
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES,
String.join(",", NS_WEBLOGIC1, NS_WEBLOGIC2, NS_WEBLOGIC3));
testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3,
NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat(getStartingNamespaces(), contains(NS_WEBLOGIC1, NS_WEBLOGIC2, NS_WEBLOGIC3));
}
Expand Down Expand Up @@ -443,25 +443,25 @@ public void describeTo(Description description) {
}

@Test
public void withRegExp_onReadExistingNamespaces_startsNamespaces() {
public void withRegExp_onRecheckDomains_startsNamespaces() {
defineSelectionStrategy(SelectionStrategy.RegExp);
testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3,
NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5);

TuningParameters.getInstance().put("domainNamespaceRegExp", REGEXP);
testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat(getStartingNamespaces(), contains(NS_WEBLOGIC2, NS_WEBLOGIC4));
}

@Test
public void withLabelSelector_onReadExistingNamespaces_startsNamespaces() {
public void withLabelSelector_onRecheckDomains_startsNamespaces() {
defineSelectionStrategy(SelectionStrategy.LabelSelector);
testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3,
NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5);

TuningParameters.getInstance().put("domainNamespaceLabelSelector", LABEL + "=" + VALUE);
testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat(getStartingNamespaces(), contains(NS_WEBLOGIC1, NS_WEBLOGIC3, NS_WEBLOGIC5));
}
Expand Down Expand Up @@ -781,50 +781,49 @@ public void withNamespaceList_onCreateStartNamespacesStep_foundExpectedLogMessag
}

@Test
public void withNamespaceList_onReadExistingNamespaces_whenConfiguredDomainNamespaceMissing_noEventCreated() {
public void withNamespaceList_onRecheckDomains_whenConfiguredDomainNamespaceMissing_noEventCreated() {
defineSelectionStrategy(SelectionStrategy.List);
String namespaceString = "NS" + LAST_NAMESPACE_NUM + ",NS" + DEFAULT_CALL_LIMIT;
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString);
createNamespaces(LAST_NAMESPACE_NUM - 1);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat("Found no event",
containsEvent(getEvents(testSupport), NAMESPACE_WATCHING_STARTED_EVENT), is(false));
}

@Test
public void withNamespaceList_onReadExistingNamespaces_whenDomainNamespaceRemoved_nsWatchStoppedEventCreated() {
public void withNamespaceList_onRecheckDomains_whenDomainNamespaceRemoved_nsWatchStoppedEventCreated() {
domainNamespaces.isStopping("NS3");
defineSelectionStrategy(SelectionStrategy.List);
String namespaceString = "NS1,NS2";
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString);
createNamespaces(4);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());

recheckDomains();
assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message",
containsEventWithMessageForNamespaces(getEvents(testSupport),
NAMESPACE_WATCHING_STOPPED, Collections.singletonList("NS3")), is(true));
}

@Test
public void withNamespaceList_onReadExistingNamespaces_whenDomainNamespaceRemoved_stopManagingNSEventCreated() {
public void withNamespaceList_onRecheckDomains_whenDomainNamespaceRemoved_stopManagingNSEventCreated() {
domainNamespaces.isStopping("NS3");
defineSelectionStrategy(SelectionStrategy.List);
String namespaceString = "NS1,NS2";
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString);
createNamespaces(4);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message",
containsEventWithMessageForNamespaces(getEvents(testSupport),
STOP_MANAGING_NAMESPACE, Collections.singletonList("NS3")), is(true));
}

@Test
public void withNamespaceList_onReadExistingNamespaces_whenDomainNamespaceRemoved_foundExpectedLogMessage() {
public void withNamespaceList_onRecheckDomains_whenDomainNamespaceRemoved_foundExpectedLogMessage() {
logRecords.clear();
loggerControl.withLogLevel(Level.INFO).collectLogMessages(logRecords, MessageKeys.END_MANAGING_NAMESPACE);
domainNamespaces.isStopping("NS3");
Expand All @@ -833,18 +832,18 @@ public void withNamespaceList_onReadExistingNamespaces_whenDomainNamespaceRemove
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString);
createNamespaces(4);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();
assertThat(logRecords, containsInfo(MessageKeys.END_MANAGING_NAMESPACE, "NS3"));
}

@Test
public void withNamespaceList_changeToDedicatedAfterReadExistingNSs_onRecheck_nsWatchStoppedEventCreated() {
public void withNamespaceList_changeToDedicatedAfterRecheckDomains_onRecheckAgain_nsWatchStoppedEventCreated() {
defineSelectionStrategy(SelectionStrategy.List);
String namespaceString = "NS1";
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString);
createNamespaces(4);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have found that organizing tests can help readability:

  1. general setup (what the world looks like before the operation)
  2. the action to test
  3. the assertion.

You can do that with judicious use of spaces. Here, lines 845-850 establish your starting point, and 852-854 are the action, so I would remove the blank lines in between them, and reserve those as separators only between the three sections.

If those are complex, you might consider extract them to their own methods, especially if you do the same setup in multiple tests.


testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

defineSelectionStrategy(SelectionStrategy.Dedicated);

Expand All @@ -856,13 +855,13 @@ public void withNamespaceList_changeToDedicatedAfterReadExistingNSs_onRecheck_ns
}

@Test
public void withNamespaceList_changeToDedicatedAfterReadExistingNSs_onRecheck_nsWatchStartedEventCreatedInOpNS() {
public void withNamespaceList_changeToDedicatedAfterRecheckDomains_onRecheckAgain_nsWatchStartedEventCreatedInOpNS() {
defineSelectionStrategy(SelectionStrategy.List);
String namespaceString = "NS1";
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString);
createNamespaces(4);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

defineSelectionStrategy(SelectionStrategy.Dedicated);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are changing to a new strategy, so line 859 begins the 'action' part of the test and should be separated by a blank line from the 'setup'


Expand All @@ -874,13 +873,13 @@ public void withNamespaceList_changeToDedicatedAfterReadExistingNSs_onRecheck_ns
}

@Test
public void withNamespaceList_changeToDedicatedAfterReadExistingNSs_onRecheck_StartManagingNSEventCreatedInOpNS() {
public void withNamespaceList_changeToDedicatedAfterRecheckDomains_onRecheckAgain_StartManagingEventCreatedInOpNS() {
defineSelectionStrategy(SelectionStrategy.List);
String namespaceString = "NS1";
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString);
createNamespaces(4);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

defineSelectionStrategy(SelectionStrategy.Dedicated);

Expand Down Expand Up @@ -927,40 +926,39 @@ public void withNamespaceLabelSelector_onCreateStartNamespacesStep_startManaging


@Test
public void withNamespaceLabelSelector_onReadExistingNamespaces_whenLabelRemoved_nsWatchStoppedEventCreated() {
public void withNamespaceLabelSelector_onRecheckDomains_whenLabelRemoved_nsWatchStoppedEventCreated() {
domainNamespaces.isStopping("NS3");
testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3,
NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5);

defineSelectionStrategy(SelectionStrategy.LabelSelector);
TuningParameters.getInstance().put("domainNamespaceLabelSelector", LABEL + "=" + VALUE);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());

recheckDomains();

assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message",
containsEventWithMessageForNamespaces(getEvents(testSupport),
NAMESPACE_WATCHING_STOPPED, Collections.singletonList("NS3")), is(true));
}

@Test
public void withNamespaceLabelSelector_onReadExistingNamespaces_whenLabelRemoved_stopManagingNSEventCreated() {
public void withNamespaceLabelSelector_onRecheckDomains_whenLabelRemoved_stopManagingNSEventCreated() {
domainNamespaces.isStopping("NS3");
testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3,
NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5);

defineSelectionStrategy(SelectionStrategy.LabelSelector);
TuningParameters.getInstance().put("domainNamespaceLabelSelector", LABEL + "=" + VALUE);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message",
containsEventWithMessageForNamespaces(getEvents(testSupport),
STOP_MANAGING_NAMESPACE, Collections.singletonList("NS3")), is(true));
}

@Test
public void withNamespaceLabelSelector_onReadExistingNamespaces_whenNamespaceLabelRemoved_foundExpectedLogMessage() {
public void withNamespaceLabelSelector_onRecheckDomains_whenNamespaceLabelRemoved_foundExpectedLogMessage() {
logRecords.clear();
loggerControl.withLogLevel(Level.INFO).collectLogMessages(logRecords, MessageKeys.END_MANAGING_NAMESPACE);
domainNamespaces.isStopping("NS3");
Expand All @@ -970,7 +968,7 @@ public void withNamespaceLabelSelector_onReadExistingNamespaces_whenNamespaceLab
defineSelectionStrategy(SelectionStrategy.LabelSelector);
TuningParameters.getInstance().put("domainNamespaceLabelSelector", LABEL + "=" + VALUE);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat(logRecords, containsInfo(MessageKeys.END_MANAGING_NAMESPACE, "NS3"));
}
Expand Down Expand Up @@ -1010,37 +1008,37 @@ public void withNamespaceRegExp_onCreateStartNamespacesStep_startManagingNSEvent
}

@Test
public void withNamespaceRegExp_onReadExistingNamespaces_whenNamespaceLabelRemoved_nsWatchStoppedEventCreated() {
public void withNamespaceRegExp_onRecheckDomains_whenNamespaceLabelRemoved_nsWatchStoppedEventCreated() {
domainNamespaces.isStopping("NS3");
testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3,
NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5);

defineSelectionStrategy(SelectionStrategy.RegExp);
TuningParameters.getInstance().put("domainNamespaceRegExp", REGEXP);
testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message",
containsEventWithMessageForNamespaces(getEvents(testSupport),
NAMESPACE_WATCHING_STOPPED, Collections.singletonList("NS3")), is(true));
}

@Test
public void withNamespaceRegExp_onReadExistingNamespaces_whenNamespaceLabelRemoved_stopManagingNSEventCreated() {
public void withNamespaceRegExp_onRecheckDomains_whenNamespaceLabelRemoved_stopManagingNSEventCreated() {
domainNamespaces.isStopping("NS3");
testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3,
NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5);

defineSelectionStrategy(SelectionStrategy.RegExp);
TuningParameters.getInstance().put("domainNamespaceRegExp", REGEXP);
testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message",
containsEventWithMessageForNamespaces(getEvents(testSupport),
STOP_MANAGING_NAMESPACE, Collections.singletonList("NS3")), is(true));
}

@Test
public void withNamespaceRegExp_onReadExistingNamespaces_whenNamespaceLabelRemoved_foundExpectedLogMessage() {
public void withNamespaceRegExp_onRecheckDomains_whenNamespaceLabelRemoved_foundExpectedLogMessage() {
logRecords.clear();
loggerControl.withLogLevel(Level.INFO).collectLogMessages(logRecords, MessageKeys.END_MANAGING_NAMESPACE);
domainNamespaces.isStopping("NS3");
Expand All @@ -1049,7 +1047,7 @@ public void withNamespaceRegExp_onReadExistingNamespaces_whenNamespaceLabelRemov

defineSelectionStrategy(SelectionStrategy.RegExp);
TuningParameters.getInstance().put("domainNamespaceRegExp", REGEXP);
testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat(logRecords, containsInfo(MessageKeys.END_MANAGING_NAMESPACE, "NS3"));
}
Expand Down Expand Up @@ -1078,7 +1076,7 @@ public void withNamespaceDedicated_onRecheckDomains_startManagingNSEventCreatedW
}

@Test
public void withNamespaceDedicated_changeToListAfterRecheck_onReadExistingNSs_nsWatchStoppedEventCreatedInOpNS() {
public void withNamespaceDedicated_changeToListAfterRecheckDomains_onRecheckAgain_nsWatchStoppedEventCreatedInOpNS() {
defineSelectionStrategy(SelectionStrategy.Dedicated);
recheckDomains();

Expand All @@ -1087,15 +1085,14 @@ public void withNamespaceDedicated_changeToListAfterRecheck_onReadExistingNSs_ns
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString);
createNamespaces(4);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());

recheckDomains();
assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message",
containsEventWithMessageForNamespaces(getEvents(testSupport),
NAMESPACE_WATCHING_STOPPED, Collections.singletonList(OP_NS)), is(true));
}

@Test
public void withNamespaceDedicated_changeToListAfterRecheck_onReadingExistingNSs_nsWatchStartedEventCreated() {
public void withNamespaceDedicated_changeToListAfterRecheckDomains_onRecheckAgain_nsWatchStartedEventCreated() {
defineSelectionStrategy(SelectionStrategy.Dedicated);
recheckDomains();

Expand All @@ -1104,15 +1101,15 @@ public void withNamespaceDedicated_changeToListAfterRecheck_onReadingExistingNSs
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString);
createNamespaces(4);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat("Found NAMESPACE_WATCHING_STARTED event with expected message",
containsEventWithMessageForNamespaces(getEvents(testSupport),
NAMESPACE_WATCHING_STARTED, Collections.singletonList("NS1")), is(true));
}

@Test
public void withNamespaceDedicated_changeToListAfterRecheck_onReadingExistingNSs_StartManagingNSEventCreated() {
public void withNamespaceDedicated_changeToListAfterRecheckDomains_onRecheckAgain_StartManagingNSEventCreated() {
defineSelectionStrategy(SelectionStrategy.Dedicated);
recheckDomains();

Expand All @@ -1121,7 +1118,7 @@ public void withNamespaceDedicated_changeToListAfterRecheck_onReadingExistingNSs
HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString);
createNamespaces(4);

testSupport.runSteps(createDomainRecheck().readExistingNamespaces());
recheckDomains();

assertThat("Found START_MANAGING_NAMESPACE event with expected message",
containsEventWithMessageForNamespaces(getEvents(testSupport),
Expand Down