Skip to content

Commit

Permalink
fix (extensions) : isSupported doesn't check all of the applicable AP…
Browse files Browse the repository at this point in the history
…I Groups (fabric8io#4447)

+ Extension client interfaces should extend SupportTestingClient rather
  than implementations
+ Disable exact apiGroup match in isSupported method call in
  extension clients which use more than one apiGroup. This effects Istio,
  Knative and Tekton extensions that use more than one apiGroups

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Dec 2, 2022
1 parent b743b2f commit 705ba06
Show file tree
Hide file tree
Showing 56 changed files with 1,079 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
#### Bugs
* Fix #4590: only using a builder if there are visitors
* Fix #4159: ensure the token refresh obeys how the Config was created
* Fix #4447: `isSupported` doesn't check all of the applicable API Groups
* Fix #4491: added a more explicit shutdown exception for okhttp
* Fix #4510: Fix StackOverflow on cyclic references involving collections.
* Fix #4534: Java Generator CLI default handling of skipGeneratedAnnotations
Expand Down
12 changes: 11 additions & 1 deletion extensions/camel-k/client/pom.xml
Expand Up @@ -84,7 +84,17 @@
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Expand Up @@ -18,11 +18,12 @@
import io.fabric8.camelk.client.dsl.V1APIGroupDSL;
import io.fabric8.camelk.client.dsl.V1alpha1APIGroupDSL;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.extension.SupportTestingClient;

/**
* Main interface for CamelK client library.
*/
public interface CamelKClient extends Client {
public interface CamelKClient extends Client, SupportTestingClient {
/**
* API entrypoint for camel.apache.org/v1 API group resources
*
Expand Down
Expand Up @@ -23,10 +23,9 @@
import io.fabric8.kubernetes.client.WithRequestCallable;
import io.fabric8.kubernetes.client.dsl.FunctionCallable;
import io.fabric8.kubernetes.client.extension.ExtensionRootClientAdapter;
import io.fabric8.kubernetes.client.extension.SupportTestingClient;

public class DefaultCamelKClient extends ExtensionRootClientAdapter<DefaultCamelKClient>
implements NamespacedCamelKClient, SupportTestingClient {
implements NamespacedCamelKClient {

public DefaultCamelKClient() {
super();
Expand Down Expand Up @@ -62,7 +61,7 @@ public V1alpha1APIGroupDSL v1alpha1() {

@Override
public boolean isSupported() {
return hasApiGroup("camel.apache.org", true);
return hasApiGroup("camel.apache.org", false);
}

}
@@ -0,0 +1,82 @@
/**
* 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.camelk.client;

import io.fabric8.kubernetes.api.model.APIGroup;
import io.fabric8.kubernetes.api.model.APIGroupBuilder;
import io.fabric8.kubernetes.api.model.APIGroupList;
import io.fabric8.kubernetes.api.model.APIGroupListBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.internal.OperationSupport;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.impl.KubernetesClientImpl;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.when;

class CamelKClientAdaptTest {
private KubernetesClient kubernetesClient;

@BeforeEach
public void setUp() {
HttpClient mockClient = mock(HttpClient.class, Mockito.RETURNS_DEEP_STUBS);
Config config = new ConfigBuilder().withMasterUrl("https://localhost:8443/").build();
kubernetesClient = new KubernetesClientImpl(mockClient, config);
}

@AfterEach
void tearDown() {
kubernetesClient.close();
kubernetesClient = null;
}

@ParameterizedTest
@MethodSource("getInputData")
void isSupported_withGivenApiGroup_shouldValidateSupport(String apiGroupName, boolean expectedResult) {
try (MockedConstruction<OperationSupport> ignored = mockConstruction(OperationSupport.class, (mock, ctx) -> {
givenApiGroupsCallReturns(mock, new APIGroupBuilder().withName(apiGroupName).build());
})) {
assertThat(kubernetesClient.adapt(CamelKClient.class).isSupported()).isEqualTo(expectedResult);
}
}

private static Stream<Arguments> getInputData() {
return Stream.of(
Arguments.of("camel.apache.org", true),
Arguments.of("test.camel.apache.org", true),
Arguments.of("tekton.dev", false));
}

private void givenApiGroupsCallReturns(OperationSupport operationSupport, APIGroup apiGroup) {
when(operationSupport.restCall(APIGroupList.class, "/apis"))
.thenReturn(new APIGroupListBuilder()
.addToGroups(apiGroup)
.build());
}
}
@@ -0,0 +1 @@
mock-maker-inline
12 changes: 11 additions & 1 deletion extensions/certmanager/client/pom.xml
Expand Up @@ -93,7 +93,17 @@
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Expand Up @@ -20,11 +20,12 @@
import io.fabric8.certmanager.client.dsl.V1alpha3APIGroupDSL;
import io.fabric8.certmanager.client.dsl.V1beta1APIGroupDSL;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.extension.SupportTestingClient;

/**
* Main interface for CertManager Client library.
*/
public interface CertManagerClient extends Client {
public interface CertManagerClient extends Client, SupportTestingClient {
V1APIGroupDSL v1();

V1alpha2APIGroupDSL v1alpha2();
Expand Down
Expand Up @@ -71,4 +71,8 @@ public V1beta1APIGroupDSL v1beta1() {
return adapt(V1beta1APIGroupClient.class);
}

@Override
public boolean isSupported() {
return hasApiGroup("cert-manager.io", false);
}
}
@@ -0,0 +1,82 @@
/**
* 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.certmanager.client;

import io.fabric8.kubernetes.api.model.APIGroup;
import io.fabric8.kubernetes.api.model.APIGroupBuilder;
import io.fabric8.kubernetes.api.model.APIGroupList;
import io.fabric8.kubernetes.api.model.APIGroupListBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.internal.OperationSupport;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.impl.KubernetesClientImpl;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.when;

class CertManagerClientAdaptTest {
private KubernetesClient kubernetesClient;

@BeforeEach
public void setUp() {
HttpClient mockClient = mock(HttpClient.class, Mockito.RETURNS_DEEP_STUBS);
Config config = new ConfigBuilder().withMasterUrl("https://localhost:8443/").build();
kubernetesClient = new KubernetesClientImpl(mockClient, config);
}

@AfterEach
void tearDown() {
kubernetesClient.close();
kubernetesClient = null;
}

@ParameterizedTest
@MethodSource("getInputData")
void isSupported_withGivenApiGroup_shouldValidateSupport(String apiGroupName, boolean expectedResult) {
try (MockedConstruction<OperationSupport> ignored = mockConstruction(OperationSupport.class, (mock, ctx) -> {
givenApiGroupsCallReturns(mock, new APIGroupBuilder().withName(apiGroupName).build());
})) {
assertThat(kubernetesClient.adapt(CertManagerClient.class).isSupported()).isEqualTo(expectedResult);
}
}

private static Stream<Arguments> getInputData() {
return Stream.of(
Arguments.of("cert-manager.io", true),
Arguments.of("test.cert-manager.io", true),
Arguments.of("tekton.dev", false));
}

private void givenApiGroupsCallReturns(OperationSupport operationSupport, APIGroup apiGroup) {
when(operationSupport.restCall(APIGroupList.class, "/apis"))
.thenReturn(new APIGroupListBuilder()
.addToGroups(apiGroup)
.build());
}
}
@@ -0,0 +1 @@
mock-maker-inline
12 changes: 11 additions & 1 deletion extensions/chaosmesh/client/pom.xml
Expand Up @@ -95,7 +95,17 @@
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Expand Up @@ -42,11 +42,12 @@
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.extension.SupportTestingClient;

/**
* Main interface for Chaos Mesh Client.
*/
public interface ChaosMeshClient extends Client {
public interface ChaosMeshClient extends Client, SupportTestingClient {

// Serving
/**
Expand Down
Expand Up @@ -47,10 +47,9 @@
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.extension.ExtensionRootClientAdapter;
import io.fabric8.kubernetes.client.extension.SupportTestingClient;

public class DefaultChaosMeshClient extends ExtensionRootClientAdapter<DefaultChaosMeshClient>
implements NamespacedChaosMeshClient, SupportTestingClient {
implements NamespacedChaosMeshClient {

public DefaultChaosMeshClient() {
super();
Expand Down Expand Up @@ -136,6 +135,6 @@ public MixedOperation<AWSChaos, AWSChaosList, Resource<AWSChaos>> awsChaos() {

@Override
public boolean isSupported() {
return hasApiGroup("chaos-mesh.org", true);
return hasApiGroup("chaos-mesh.org", false);
}
}
@@ -0,0 +1 @@
mock-maker-inline

0 comments on commit 705ba06

Please sign in to comment.