Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Fix #7116: (java-generator) Use timezone format compatible with Kubernetes
* Fix #7163: Ensure that streams are notified of errors
* 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
* Fix #7224: Fix KubeApiTest inheritance from base test class

#### Improvements
* Fix #7217: refinements and expanded documentation for preserve unknown handling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import org.junit.jupiter.api.extension.ExtensionContext;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

public class Fabric8ClientInjectionHandler implements ClientInjectionHandler {

Expand Down Expand Up @@ -67,9 +65,9 @@ public void cleanup(ExtensionContext extensionContext) {
public static Optional<Field> getFieldForKubeClientInjection(ExtensionContext extensionContext,
boolean staticField) {
Class<?> clazz = extensionContext.getTestClass().orElseThrow();
var kubeConfigFields = Arrays.stream(clazz.getDeclaredFields())
.filter(f -> KubernetesClient.class.isAssignableFrom(f.getType()))
.collect(Collectors.toList());
var kubeConfigFields = KubeConfigInjectionHandler.getAllFieldsIncludingAllParents(clazz,
f -> KubernetesClient.class.isAssignableFrom(f.getType()));

if (kubeConfigFields.isEmpty()) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubeapitest.junit.sample;

import io.fabric8.kubeapitest.junit.EnableKubeAPIServer;
import io.fabric8.kubeapitest.junit.KubeConfig;
import io.fabric8.kubernetes.client.KubernetesClient;

@EnableKubeAPIServer
public class BaseTestClass {

static KubernetesClient client;

// config yaml injection also works
@KubeConfig
static String configYaml;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubeapitest.junit.sample;

import io.fabric8.kubeapitest.junit.TestCaseUtils;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import org.junit.jupiter.api.Test;

public class JUnitFabric8ClientExtendingBaseClassTest extends BaseTestClass {

@Test
void testClientInjection() {
TestCaseUtils.simpleTest(client, "test1");
}

@Test
void testKubeConfigInjectionAlsoWorks() {
TestCaseUtils.simpleTest(new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(configYaml)).build(),
"test2");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.fabric8.kubeapitest.KubeAPIServerConfig;
import org.junit.jupiter.api.extension.ExtendWith;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

Expand All @@ -30,6 +31,7 @@
@ExtendWith(KubeAPIServerExtension.class)
@Target({ TYPE, METHOD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Inherited
public @interface EnableKubeAPIServer {

String NOT_SET = "NOT_SET";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import org.junit.jupiter.api.extension.ExtensionContext;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.function.Predicate;

public class KubeConfigInjectionHandler implements ClientInjectionHandler {

Expand Down Expand Up @@ -53,9 +55,9 @@ private void setKubeConfigYamlToField(ExtensionContext extensionContext,
public Optional<Field> getFieldForKubeConfigInjection(ExtensionContext extensionContext,
boolean staticField) {
Class<?> clazz = extensionContext.getTestClass().orElseThrow(IllegalStateException::new);
java.util.List<Field> kubeConfigFields = Arrays.stream(clazz.getDeclaredFields())
.filter(f -> f.getAnnotationsByType(KubeConfig.class).length > 0)
.collect(Collectors.toList());
java.util.List<Field> kubeConfigFields = getAllFieldsIncludingAllParents(clazz,
f -> f.getAnnotationsByType(KubeConfig.class).length > 0);

if (kubeConfigFields.isEmpty()) {
return Optional.empty();
}
Expand All @@ -76,4 +78,17 @@ public Optional<Field> getFieldForKubeConfigInjection(ExtensionContext extension
}
}

public static java.util.List<Field> getAllFieldsIncludingAllParents(Class<?> clazz, Predicate<Field> predicate) {
List<Field> staticFields = new ArrayList<>();
Class<?> currentClass = clazz;

while (currentClass != null) {
Field[] declaredFields = currentClass.getDeclaredFields();
Arrays.stream(declaredFields).filter(predicate).forEach(staticFields::add);
currentClass = currentClass.getSuperclass();
}

return staticFields;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubeapitest.sample;

import io.fabric8.kubeapitest.junit.EnableKubeAPIServer;
import io.fabric8.kubeapitest.junit.KubeConfig;

@EnableKubeAPIServer
public class BaseTestClass {

@KubeConfig
static String kubeConfigYaml;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubeapitest.sample;

import org.junit.jupiter.api.Test;

public class JUnitExtensionExtendingBaseTestClassTest extends BaseTestClass {

@Test
void simpleTest1() {
TestCaseUtils.simpleTest(kubeConfigYaml);
}

}
Loading