|
1 | 1 | package io.javaoperatorsdk.operator.config.runtime;
|
2 | 2 |
|
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
3 | 4 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
5 | 6 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
6 |
| -import static org.junit.jupiter.api.Assertions.assertNull; |
7 |
| -import static org.junit.jupiter.api.Assertions.assertTrue; |
8 | 7 |
|
9 |
| -import ch.qos.logback.classic.Logger; |
10 |
| -import ch.qos.logback.classic.spi.ILoggingEvent; |
11 |
| -import ch.qos.logback.core.read.ListAppender; |
12 | 8 | import io.fabric8.kubernetes.client.CustomResource;
|
13 | 9 | import io.fabric8.kubernetes.model.annotation.Group;
|
14 | 10 | import io.fabric8.kubernetes.model.annotation.Version;
|
|
18 | 14 | import io.javaoperatorsdk.operator.api.ResourceController;
|
19 | 15 | import io.javaoperatorsdk.operator.api.UpdateControl;
|
20 | 16 | import io.javaoperatorsdk.operator.api.config.AbstractConfigurationService;
|
| 17 | +import org.apache.logging.log4j.Level; |
| 18 | +import org.apache.logging.log4j.core.LoggerContext; |
| 19 | +import org.apache.logging.log4j.core.config.AppenderRef; |
| 20 | +import org.apache.logging.log4j.core.config.LoggerConfig; |
| 21 | +import org.apache.logging.log4j.core.layout.PatternLayout; |
| 22 | +import org.apache.logging.log4j.test.appender.ListAppender; |
21 | 23 | import org.junit.jupiter.api.Test;
|
22 |
| -import org.slf4j.LoggerFactory; |
23 | 24 |
|
24 | 25 | public class DefaultConfigurationServiceTest {
|
25 | 26 |
|
26 | 27 | public static final String CUSTOM_FINALIZER_NAME = "a.custom/finalizer";
|
27 | 28 |
|
28 | 29 | @Test
|
29 | 30 | void attemptingToRetrieveAnUnknownControllerShouldLogWarning() {
|
30 |
| - final var logger = (Logger) LoggerFactory.getLogger(AbstractConfigurationService.LOGGER_NAME); |
31 |
| - final var appender = new ListAppender<ILoggingEvent>(); |
32 |
| - logger.addAppender(appender); |
| 31 | + final LoggerContext context = LoggerContext.getContext(false); |
| 32 | + final PatternLayout layout = PatternLayout.createDefaultLayout(context.getConfiguration()); |
| 33 | + final ListAppender appender = new ListAppender("list", null, layout, false, false); |
| 34 | + |
33 | 35 | appender.start();
|
| 36 | + |
| 37 | + context.getConfiguration().addAppender(appender); |
| 38 | + |
| 39 | + AppenderRef ref = AppenderRef.createAppenderRef("list", null, null); |
| 40 | + LoggerConfig loggerConfig = |
| 41 | + LoggerConfig.createLogger( |
| 42 | + false, |
| 43 | + Level.valueOf("info"), |
| 44 | + AbstractConfigurationService.LOGGER_NAME, |
| 45 | + "false", |
| 46 | + new AppenderRef[] {ref}, |
| 47 | + null, |
| 48 | + context.getConfiguration(), |
| 49 | + null); |
| 50 | + loggerConfig.addAppender(appender, null, null); |
| 51 | + |
| 52 | + context.getConfiguration().addLogger(AbstractConfigurationService.LOGGER_NAME, loggerConfig); |
| 53 | + context.updateLoggers(); |
| 54 | + |
34 | 55 | try {
|
35 | 56 | final var config =
|
36 | 57 | DefaultConfigurationService.instance()
|
37 | 58 | .getConfigurationFor(new NotAutomaticallyCreated(), false);
|
38 |
| - assertNull(config); |
39 |
| - assertEquals(1, appender.list.size()); |
40 |
| - assertTrue( |
41 |
| - appender.list.stream() |
42 |
| - .allMatch( |
43 |
| - e -> { |
44 |
| - final var message = e.getFormattedMessage(); |
45 |
| - return message.contains(NotAutomaticallyCreated.NAME) |
46 |
| - && message.contains("not found"); |
47 |
| - })); |
| 59 | + |
| 60 | + assertThat(config).isNull(); |
| 61 | + assertThat(appender.getMessages()) |
| 62 | + .hasSize(1) |
| 63 | + .allMatch(m -> m.contains(NotAutomaticallyCreated.NAME) && m.contains("not found")); |
48 | 64 | } finally {
|
49 |
| - logger.detachAndStopAllAppenders(); |
| 65 | + appender.stop(); |
| 66 | + |
| 67 | + context.getConfiguration().removeLogger(AbstractConfigurationService.LOGGER_NAME); |
| 68 | + context.updateLoggers(); |
50 | 69 | }
|
51 | 70 | }
|
52 | 71 |
|
@@ -86,16 +105,16 @@ public void supportsInnerClassCustomResources() {
|
86 | 105 | static class TestCustomFinalizerController
|
87 | 106 | implements ResourceController<TestCustomFinalizerController.InnerCustomResource> {
|
88 | 107 |
|
89 |
| - @Group("test.crd") |
90 |
| - @Version("v1") |
91 |
| - public class InnerCustomResource extends CustomResource { |
92 |
| - } |
93 |
| - |
94 | 108 | @Override
|
95 | 109 | public UpdateControl<TestCustomFinalizerController.InnerCustomResource> createOrUpdateResource(
|
96 | 110 | InnerCustomResource resource, Context<InnerCustomResource> context) {
|
97 | 111 | return null;
|
98 | 112 | }
|
| 113 | + |
| 114 | + @Group("test.crd") |
| 115 | + @Version("v1") |
| 116 | + public class InnerCustomResource extends CustomResource { |
| 117 | + } |
99 | 118 | }
|
100 | 119 |
|
101 | 120 | @Controller(name = NotAutomaticallyCreated.NAME)
|
|
0 commit comments