Skip to content

Commit 875687e

Browse files
authored
Remove duplicate calls to TestUtils.silenceOperatorLogger (#2760)
* Remove duplicate calls to TestUtils.silenceOperatorLogger * Update copyrights
1 parent d8e1cdf commit 875687e

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-22
lines changed

operator/src/test/java/oracle/kubernetes/operator/DomainProcessorTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ public void setUp() throws Exception {
200200
consoleHandlerMemento = TestUtils.silenceOperatorLogger()
201201
.collectLogMessages(logRecords, NOT_STARTING_DOMAINUID_THREAD).withLogLevel(Level.FINE);
202202
mementos.add(consoleHandlerMemento);
203-
mementos.add(TestUtils.silenceOperatorLogger()
204-
.collectLogMessages(logRecords, NOT_STARTING_DOMAINUID_THREAD).withLogLevel(Level.FINE));
205203
mementos.add(testSupport.install());
206204
mementos.add(StaticStubSupport.install(DomainProcessorImpl.class, "DOMAINS", presenceInfoMap));
207205
mementos.add(StaticStubSupport.install(DomainProcessorImpl.class, "domainEventK8SObjects", domainEventObjects));

operator/src/test/java/oracle/kubernetes/operator/NamespaceTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2019, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package oracle.kubernetes.operator;
@@ -64,13 +64,13 @@ class NamespaceTest {
6464
private final DomainNamespaces domainNamespaces = new DomainNamespaces(null);
6565
private final DomainProcessorStub dp = Stub.createNiceStub(DomainProcessorStub.class);
6666
private final MainDelegateStub delegate = createStrictStub(MainDelegateStub.class, dp, domainNamespaces);
67-
private final TestUtils.ConsoleHandlerMemento loggerControl = TestUtils.silenceOperatorLogger();
67+
private TestUtils.ConsoleHandlerMemento loggerControl;
6868
private final Collection<LogRecord> logRecords = new ArrayList<>();
6969
private final EventRetryStrategyStub retryStrategy = createStrictStub(EventRetryStrategyStub.class);
7070

7171
@BeforeEach
7272
public void setUp() throws Exception {
73-
mementos.add(TestUtils.silenceOperatorLogger());
73+
mementos.add(loggerControl = TestUtils.silenceOperatorLogger());
7474
mementos.add(StubWatchFactory.install());
7575
mementos.add(NoopWatcherStarter.install());
7676
mementos.add(HelmAccessStub.install());

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package oracle.kubernetes.operator.helpers;
@@ -24,7 +24,6 @@
2424
import oracle.kubernetes.operator.work.FiberTestSupport;
2525
import oracle.kubernetes.operator.work.Packet;
2626
import oracle.kubernetes.operator.work.Step;
27-
import oracle.kubernetes.utils.TestUtils;
2827
import oracle.kubernetes.weblogic.domain.ServerConfigurator;
2928
import org.junit.jupiter.api.Test;
3029

@@ -74,8 +73,6 @@ class AdminPodHelperTest extends PodHelperTestBase {
7473
public static final String CUSTOM_MOUNT_PATH2 = "/common1";
7574
public static final String TEST_MEDIUM = "TestMedium";
7675

77-
private TestUtils.ConsoleHandlerMemento consoleHandlerMemento = TestUtils.silenceOperatorLogger();
78-
7976
public AdminPodHelperTest() {
8077
super(ADMIN_SERVER, ADMIN_PORT);
8178
}
@@ -831,7 +828,7 @@ void whenDomainHomeChanged_podCycleEventCreatedWithCorrectNS()
831828
@Test
832829
void whenDomainHomeChanged_generateExpectedLogMessage()
833830
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
834-
consoleHandlerMemento.collectLogMessages(logRecords, getCyclePodKey());
831+
getConsoleHandlerMemento().collectLogMessages(logRecords, getCyclePodKey(), getReplacedMessageKey());
835832
initializeExistingPod();
836833
getConfiguredDomainSpec().setDomainHome("adfgg");
837834

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2020, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package oracle.kubernetes.operator.helpers;
@@ -114,13 +114,13 @@ class EventHelperTest {
114114
private final MakeRightDomainOperation makeRightOperation
115115
= processor.createMakeRightOperation(info);
116116
private final String jobPodName = LegalNames.toJobIntrospectorName(UID);
117-
private final TestUtils.ConsoleHandlerMemento loggerControl = TestUtils.silenceOperatorLogger();
117+
private TestUtils.ConsoleHandlerMemento loggerControl;
118118
private final Collection<LogRecord> logRecords = new ArrayList<>();
119119
private final EventRetryStrategyStub retryStrategy = createStrictStub(EventRetryStrategyStub.class);
120120

121121
@BeforeEach
122122
public void setUp() throws Exception {
123-
mementos.add(TestUtils.silenceOperatorLogger());
123+
mementos.add(loggerControl = TestUtils.silenceOperatorLogger());
124124
mementos.add(testSupport.install());
125125
mementos.add(StaticStubSupport.install(DomainProcessorImpl.class, "DOMAINS", presenceInfoMap));
126126
mementos.add(StaticStubSupport.install(DomainProcessorImpl.class, "domainEventK8SObjects", domainEventObjects));

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package oracle.kubernetes.operator.helpers;
@@ -224,7 +224,7 @@ public abstract class PodHelperTestBase extends DomainValidationBaseTest {
224224
private Memento hashMemento;
225225
private final Map<String, Map<String, KubernetesEventObjects>> domainEventObjects = new ConcurrentHashMap<>();
226226

227-
private TestUtils.ConsoleHandlerMemento consoleHandlerMemento = TestUtils.silenceOperatorLogger();
227+
private TestUtils.ConsoleHandlerMemento consoleHandlerMemento;
228228

229229
PodHelperTestBase(String serverName, int listenPort) {
230230
this.serverName = serverName;
@@ -302,8 +302,8 @@ public void setUp() throws Exception {
302302
mementos.add(hashMemento = UnitTestHash.install());
303303
mementos.add(InMemoryCertificates.install());
304304
mementos.add(setProductVersion(TEST_PRODUCT_VERSION));
305-
mementos.add(
306-
TestUtils.silenceOperatorLogger()
305+
mementos.add(consoleHandlerMemento
306+
= TestUtils.silenceOperatorLogger()
307307
.collectLogMessages(logRecords, getMessageKeys())
308308
.withLogLevel(Level.FINE)
309309
.ignoringLoggedExceptions(ApiException.class));
@@ -328,6 +328,10 @@ public void setUp() throws Exception {
328328
new PassthroughPodAwaiterStepFactory());
329329
}
330330

331+
TestUtils.ConsoleHandlerMemento getConsoleHandlerMemento() {
332+
return consoleHandlerMemento;
333+
}
334+
331335
private Memento setProductVersion(String productVersion) throws NoSuchFieldException {
332336
return StaticStubSupport.install(PodStepContext.class, "productVersion", productVersion);
333337
}
@@ -2153,7 +2157,7 @@ void whenImageChanged_domainRollStartEventCreatedWithCorrectMessage()
21532157
@Test
21542158
void whenImageChanged_expectedLogMessageFound()
21552159
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
2156-
consoleHandlerMemento.collectLogMessages(logRecords, getDomainRollStartingKey());
2160+
getConsoleHandlerMemento().collectLogMessages(logRecords, getDomainRollStartingKey());
21572161
initializeExistingPod();
21582162
getConfiguredDomainSpec().setImage("adfgg");
21592163

@@ -2245,7 +2249,7 @@ void whenDomainZipHashChanged_butIsMIIDynamicUpdate_dontCreateDomainRollStartEve
22452249
@Test
22462250
void whenImageDomainHomeAndRestartVersionChanged_expectedLogMessageFound()
22472251
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
2248-
consoleHandlerMemento.collectLogMessages(logRecords, getDomainRollStartingKey());
2252+
getConsoleHandlerMemento().collectLogMessages(logRecords, getDomainRollStartingKey());
22492253
initializeExistingPod();
22502254
getConfiguredDomainSpec().setImage("adfgg");
22512255
getConfiguredDomainSpec().setDomainHome("12345");
@@ -2260,7 +2264,7 @@ void whenImageDomainHomeAndRestartVersionChanged_expectedLogMessageFound()
22602264
@Test
22612265
void whenImageDomainHomeAndRestartVersionChanged_domainRollStartEventCreatedWithCorrectMessage()
22622266
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
2263-
consoleHandlerMemento.collectLogMessages(logRecords, getDomainRollStartingKey());
2267+
getConsoleHandlerMemento().collectLogMessages(logRecords, getDomainRollStartingKey());
22642268
initializeExistingPod();
22652269
getConfiguredDomainSpec().setImage("adfgg");
22662270
getConfiguredDomainSpec().setDomainHome("12345");
@@ -2287,7 +2291,7 @@ void whenImageDomainHomeAndRestartVersionChanged_domainRollStartEventCreatedWith
22872291
@Test
22882292
void whenImageDomainHomeAndWebLogicZipHashChanged_domainRollStartEventCreatedWithCorrectMessage()
22892293
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
2290-
consoleHandlerMemento.collectLogMessages(logRecords, getDomainRollStartingKey());
2294+
getConsoleHandlerMemento().collectLogMessages(logRecords, getDomainRollStartingKey());
22912295
initializeExistingPod();
22922296
getConfiguredDomainSpec().setImage("adfgg");
22932297
getConfiguredDomainSpec().setDomainHome("12345");

operator/src/test/java/oracle/kubernetes/utils/TestUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2018, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package oracle.kubernetes.utils;
@@ -29,6 +29,7 @@ public class TestUtils {
2929
*/
3030
public static ConsoleHandlerMemento silenceOperatorLogger() {
3131
Logger logger = LoggingFactory.getLogger("Operator", "Operator").getUnderlyingLogger();
32+
Arrays.stream(logger.getHandlers()).filter(TestUtils::isTestLogHandler).forEach(TestUtils::rejectCall);
3233
List<Handler> savedHandlers = new ArrayList<>();
3334
for (Handler handler : logger.getHandlers()) {
3435
if (handler instanceof ConsoleHandler) {
@@ -46,6 +47,14 @@ public static ConsoleHandlerMemento silenceOperatorLogger() {
4647
return new ConsoleHandlerMemento(logger, testHandler, savedHandlers);
4748
}
4849

50+
private static boolean isTestLogHandler(Handler handler) {
51+
return handler instanceof TestLogHandler;
52+
}
53+
54+
private static void rejectCall(Handler handler) {
55+
throw new IllegalStateException("silenceOperatorLogger may only called once");
56+
}
57+
4958
/**
5059
* Restores the silenced logger handlers.
5160
*

0 commit comments

Comments
 (0)