Skip to content

Commit 5027a5c

Browse files
authored
fix(kube-api-test): allow injected field inheritance from base test class (7224)
Fix KubeApiTest inheritance from base test class Signed-off-by: Attila Mészáros <[email protected]> --- changelog Signed-off-by: Attila Mészáros <[email protected]> --- license --- revert mvnw del Signed-off-by: Attila Mészáros <[email protected]>
1 parent 3ee30af commit 5027a5c

File tree

8 files changed

+146
-9
lines changed

8 files changed

+146
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Fix #7116: (java-generator) Use timezone format compatible with Kubernetes
1111
* Fix #7163: Ensure that streams are notified of errors
1212
* Fix #7092: (crd-generator) Add support for @Annotations and @Labels in CRD generation - CRD generator now includes annotations and labels specified via these annotations in the generated CRD metadata
13+
* Fix #7224: Fix KubeApiTest inheritance from base test class
1314

1415
#### Improvements
1516
* Fix #7217: refinements and expanded documentation for preserve unknown handling

junit/kube-api-test/client-inject/src/main/java/io/fabric8/kubeapitest/junit/Fabric8ClientInjectionHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import org.junit.jupiter.api.extension.ExtensionContext;
2424

2525
import java.lang.reflect.Field;
26-
import java.util.Arrays;
2726
import java.util.Optional;
28-
import java.util.stream.Collectors;
2927

3028
public class Fabric8ClientInjectionHandler implements ClientInjectionHandler {
3129

@@ -67,9 +65,9 @@ public void cleanup(ExtensionContext extensionContext) {
6765
public static Optional<Field> getFieldForKubeClientInjection(ExtensionContext extensionContext,
6866
boolean staticField) {
6967
Class<?> clazz = extensionContext.getTestClass().orElseThrow();
70-
var kubeConfigFields = Arrays.stream(clazz.getDeclaredFields())
71-
.filter(f -> KubernetesClient.class.isAssignableFrom(f.getType()))
72-
.collect(Collectors.toList());
68+
var kubeConfigFields = KubeConfigInjectionHandler.getAllFieldsIncludingAllParents(clazz,
69+
f -> KubernetesClient.class.isAssignableFrom(f.getType()));
70+
7371
if (kubeConfigFields.isEmpty()) {
7472
return Optional.empty();
7573
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.kubeapitest.junit.sample;
17+
18+
import io.fabric8.kubeapitest.junit.EnableKubeAPIServer;
19+
import io.fabric8.kubeapitest.junit.KubeConfig;
20+
import io.fabric8.kubernetes.client.KubernetesClient;
21+
22+
@EnableKubeAPIServer
23+
public class BaseTestClass {
24+
25+
static KubernetesClient client;
26+
27+
// config yaml injection also works
28+
@KubeConfig
29+
static String configYaml;
30+
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.kubeapitest.junit.sample;
17+
18+
import io.fabric8.kubeapitest.junit.TestCaseUtils;
19+
import io.fabric8.kubernetes.client.Config;
20+
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
21+
import org.junit.jupiter.api.Test;
22+
23+
public class JUnitFabric8ClientExtendingBaseClassTest extends BaseTestClass {
24+
25+
@Test
26+
void testClientInjection() {
27+
TestCaseUtils.simpleTest(client, "test1");
28+
}
29+
30+
@Test
31+
void testKubeConfigInjectionAlsoWorks() {
32+
TestCaseUtils.simpleTest(new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(configYaml)).build(),
33+
"test2");
34+
}
35+
36+
}

junit/kube-api-test/core/src/main/java/io/fabric8/kubeapitest/junit/EnableKubeAPIServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.fabric8.kubeapitest.KubeAPIServerConfig;
1919
import org.junit.jupiter.api.extension.ExtendWith;
2020

21+
import java.lang.annotation.Inherited;
2122
import java.lang.annotation.Retention;
2223
import java.lang.annotation.Target;
2324

@@ -30,6 +31,7 @@
3031
@ExtendWith(KubeAPIServerExtension.class)
3132
@Target({ TYPE, METHOD, ANNOTATION_TYPE })
3233
@Retention(RUNTIME)
34+
@Inherited
3335
public @interface EnableKubeAPIServer {
3436

3537
String NOT_SET = "NOT_SET";

junit/kube-api-test/core/src/main/java/io/fabric8/kubeapitest/junit/KubeConfigInjectionHandler.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import org.junit.jupiter.api.extension.ExtensionContext;
2121

2222
import java.lang.reflect.Field;
23+
import java.util.ArrayList;
2324
import java.util.Arrays;
25+
import java.util.List;
2426
import java.util.Optional;
25-
import java.util.stream.Collectors;
27+
import java.util.function.Predicate;
2628

2729
public class KubeConfigInjectionHandler implements ClientInjectionHandler {
2830

@@ -53,9 +55,9 @@ private void setKubeConfigYamlToField(ExtensionContext extensionContext,
5355
public Optional<Field> getFieldForKubeConfigInjection(ExtensionContext extensionContext,
5456
boolean staticField) {
5557
Class<?> clazz = extensionContext.getTestClass().orElseThrow(IllegalStateException::new);
56-
java.util.List<Field> kubeConfigFields = Arrays.stream(clazz.getDeclaredFields())
57-
.filter(f -> f.getAnnotationsByType(KubeConfig.class).length > 0)
58-
.collect(Collectors.toList());
58+
java.util.List<Field> kubeConfigFields = getAllFieldsIncludingAllParents(clazz,
59+
f -> f.getAnnotationsByType(KubeConfig.class).length > 0);
60+
5961
if (kubeConfigFields.isEmpty()) {
6062
return Optional.empty();
6163
}
@@ -76,4 +78,17 @@ public Optional<Field> getFieldForKubeConfigInjection(ExtensionContext extension
7678
}
7779
}
7880

81+
public static java.util.List<Field> getAllFieldsIncludingAllParents(Class<?> clazz, Predicate<Field> predicate) {
82+
List<Field> staticFields = new ArrayList<>();
83+
Class<?> currentClass = clazz;
84+
85+
while (currentClass != null) {
86+
Field[] declaredFields = currentClass.getDeclaredFields();
87+
Arrays.stream(declaredFields).filter(predicate).forEach(staticFields::add);
88+
currentClass = currentClass.getSuperclass();
89+
}
90+
91+
return staticFields;
92+
}
93+
7994
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.kubeapitest.sample;
17+
18+
import io.fabric8.kubeapitest.junit.EnableKubeAPIServer;
19+
import io.fabric8.kubeapitest.junit.KubeConfig;
20+
21+
@EnableKubeAPIServer
22+
public class BaseTestClass {
23+
24+
@KubeConfig
25+
static String kubeConfigYaml;
26+
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.kubeapitest.sample;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
public class JUnitExtensionExtendingBaseTestClassTest extends BaseTestClass {
21+
22+
@Test
23+
void simpleTest1() {
24+
TestCaseUtils.simpleTest(kubeConfigYaml);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)