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 (#4447)

+ 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 authored and manusa committed Dec 12, 2022
1 parent ad33027 commit 09edb3d
Show file tree
Hide file tree
Showing 40 changed files with 949 additions and 17 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 @@ -62,7 +62,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.isAdaptable(CamelKClient.class)).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/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 @@ -136,6 +136,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,0 +1,81 @@
/**
* 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.chaosmesh.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.mockConstruction;
import static org.mockito.Mockito.when;

class ChaosMeshClientAdaptTest {
private KubernetesClient kubernetesClient;

@BeforeEach
public void setUp() {
HttpClient mockClient = Mockito.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.isAdaptable(ChaosMeshClient.class)).isEqualTo(expectedResult);
}
}

private static Stream<Arguments> getInputData() {
return Stream.of(
Arguments.of("chaos-mesh.org", true),
Arguments.of("test.chaos-mesh.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());
}
}
15 changes: 15 additions & 0 deletions extensions/istio/client/pom.xml
Expand Up @@ -82,6 +82,21 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<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>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Expand Up @@ -60,6 +60,6 @@ public V1beta1APIGroupDSL v1beta1() {

@Override
public boolean isSupported() {
return hasApiGroup("networking.istio.io", true);
return hasApiGroup("istio.io", false);
}
}
@@ -0,0 +1,81 @@
/**
* 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.istio.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.mockConstruction;
import static org.mockito.Mockito.when;

class IstioClientAdaptTest {
private KubernetesClient kubernetesClient;

@BeforeEach
public void setUp() {
HttpClient mockClient = Mockito.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.isAdaptable(IstioClient.class)).isEqualTo(expectedResult);
}
}

private static Stream<Arguments> getInputData() {
return Stream.of(
Arguments.of("istio.io", true),
Arguments.of("security.istio.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/knative/client/pom.xml
Expand Up @@ -80,7 +80,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 @@ -241,6 +241,6 @@ public MixedOperation<KafkaBinding, KafkaBindingList, Resource<KafkaBinding>> ka

@Override
public boolean isSupported() {
return hasApiGroup("knative.dev", true);
return hasApiGroup("knative.dev", false);
}
}

0 comments on commit 09edb3d

Please sign in to comment.