|
2 | 2 |
|
3 | 3 | import java.lang.reflect.AnnotatedElement; |
4 | 4 | import java.util.Arrays; |
| 5 | +import java.util.Optional; |
5 | 6 | import java.util.stream.Collectors; |
6 | 7 |
|
7 | | -import org.junit.jupiter.api.extension.AfterAllCallback; |
8 | | -import org.junit.jupiter.api.extension.BeforeAllCallback; |
9 | | -import org.junit.jupiter.api.extension.ExtensionContext; |
| 8 | +import org.junit.jupiter.api.extension.*; |
10 | 9 | import org.slf4j.Logger; |
11 | 10 | import org.slf4j.LoggerFactory; |
12 | 11 |
|
13 | 12 | import io.javaoperatorsdk.jenvtest.JenvtestException; |
14 | 13 | import io.javaoperatorsdk.jenvtest.KubeAPIServer; |
| 14 | +import io.javaoperatorsdk.jenvtest.KubeAPIServerConfig; |
15 | 15 | import io.javaoperatorsdk.jenvtest.KubeAPIServerConfigBuilder; |
16 | 16 |
|
17 | 17 | import static io.javaoperatorsdk.jenvtest.junit.EnableKubeAPIServer.NOT_SET; |
18 | 18 |
|
19 | | -public class KubeAPIServerExtension implements BeforeAllCallback, AfterAllCallback { |
| 19 | +public class KubeAPIServerExtension |
| 20 | + implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback { |
20 | 21 |
|
21 | 22 | private static final Logger log = LoggerFactory.getLogger(KubeAPIServerExtension.class); |
22 | 23 |
|
23 | 24 | private KubeAPIServer kubeApiServer; |
24 | 25 |
|
25 | 26 | @Override |
26 | | - public void beforeAll(ExtensionContext extensionContext) throws Exception { |
27 | | - String targetVersion = extensionContext.getElement().map(this::annotatedElementToVersion) |
28 | | - .orElse(null); |
| 27 | + public void beforeAll(ExtensionContext extensionContext) { |
| 28 | + startIfAnnotationPresent(extensionContext); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void afterAll(ExtensionContext extensionContext) { |
| 33 | + stopIfAnnotationPresent(extensionContext); |
| 34 | + } |
29 | 35 |
|
| 36 | + @Override |
| 37 | + public void beforeEach(ExtensionContext extensionContext) { |
| 38 | + startIfAnnotationPresent(extensionContext); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void afterEach(ExtensionContext extensionContext) { |
| 43 | + stopIfAnnotationPresent(extensionContext); |
| 44 | + } |
| 45 | + |
| 46 | + private void startIfAnnotationPresent(ExtensionContext extensionContext) { |
| 47 | + extensionContext.getElement().ifPresent(ae -> { |
| 48 | + var annotation = getExtensionAnnotationInstance(ae); |
| 49 | + annotation.ifPresent(a -> startApiServer(extensionContext, a)); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + private void startApiServer(ExtensionContext context, EnableKubeAPIServer annotation) { |
| 54 | + kubeApiServer = new KubeAPIServer(annotationToConfig(annotation)); |
| 55 | + kubeApiServer.start(); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + private void stopIfAnnotationPresent(ExtensionContext extensionContext) { |
| 60 | + extensionContext.getElement().ifPresent(ae -> { |
| 61 | + var annotation = getExtensionAnnotationInstance(ae); |
| 62 | + annotation.ifPresent(a -> kubeApiServer.stop()); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + private KubeAPIServerConfig annotationToConfig(EnableKubeAPIServer annotation) { |
30 | 67 | var builder = KubeAPIServerConfigBuilder.anAPIServerConfig(); |
31 | | - if (targetVersion != null) { |
32 | | - log.debug("Using api version: {}", targetVersion); |
33 | | - builder.withApiServerVersion(targetVersion); |
| 68 | + var version = annotation.kubeAPIVersion(); |
| 69 | + if (!NOT_SET.equals(version)) { |
| 70 | + builder.withApiServerVersion(version); |
34 | 71 | } |
35 | | - kubeApiServer = new KubeAPIServer(builder.build()); |
36 | | - kubeApiServer.start(); |
| 72 | + return builder.build(); |
37 | 73 | } |
38 | 74 |
|
39 | | - private String annotatedElementToVersion(AnnotatedElement ae) { |
| 75 | + private Optional<EnableKubeAPIServer> getExtensionAnnotationInstance(AnnotatedElement ae) { |
40 | 76 | var annotations = Arrays.stream(ae.getAnnotations()) |
41 | 77 | .filter(a -> a.annotationType().isAssignableFrom(EnableKubeAPIServer.class)) |
42 | 78 | .collect(Collectors.toList()); |
43 | 79 | if (annotations.size() > 1) { |
44 | 80 | throw new JenvtestException( |
45 | 81 | "Only one instance of @EnableKubeAPIServer annotation is allowed"); |
| 82 | + } else if (annotations.isEmpty()) { |
| 83 | + return Optional.empty(); |
| 84 | + } else { |
| 85 | + return Optional.of((EnableKubeAPIServer) annotations.get(0)); |
46 | 86 | } |
47 | | - EnableKubeAPIServer target = (EnableKubeAPIServer) annotations.get(0); |
48 | | - var version = target.kubeAPIVersion(); |
49 | | - return NOT_SET.equals(version) ? null : version; |
50 | 87 | } |
51 | 88 |
|
52 | | - @Override |
53 | | - public void afterAll(ExtensionContext extensionContext) throws Exception { |
54 | | - kubeApiServer.stop(); |
55 | | - } |
56 | 89 | } |
0 commit comments