Skip to content

Commit

Permalink
test (kubernetes-server-mock) : Add disabled tests for handing Kubern…
Browse files Browse the repository at this point in the history
…etes list types

KubernetesResponseComposer should infer list type from list items instead
of returning a hardcoded `List` kind value. Added tests to provide
desired expectations.

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Sep 10, 2024
1 parent eec18ee commit cffde45
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.kubernetes.client.server.mock;

import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.client.utils.Serialization;
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 java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Stream;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

class KubernetesResponseComposerTest {
private KubernetesResponseComposer kubernetesResponseComposer;

@BeforeEach
void setUp() {
kubernetesResponseComposer = new KubernetesResponseComposer();
}

static Stream<Arguments> composeInput() {
return Stream.of(
// https://github.com/fabric8io/kubernetes-client/issues/6220
// arguments("Secret", "SecretList"),
arguments("", "List"));
}

@ParameterizedTest
@MethodSource("composeInput")
void compose_whenKindProvidedInListItem_shouldInferListKindCorrectly(String providedKind, String parsedListKind) {
// Given
Collection<String> collection = new ArrayList<>();
collection.add(String.format("{\"kind\": \"%s\", \"apiVersion\": \"v1\", \"metadata\": {}}", providedKind));

// When
String listString = kubernetesResponseComposer.compose(collection, "1");

// Then
assertThat(Serialization.unmarshal(listString, KubernetesResourceList.class))
.hasFieldOrPropertyWithValue("kind", parsedListKind);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* 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.kubernetes.client.server.mock.crud;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ConfigMapList;
import io.fabric8.kubernetes.client.dsl.NonDeletingOperation;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class KubernetesCrudDispatcherGetTest extends KubernetesCrudDispatcherTestBase {

@Nested
class WithNoItems {

@BeforeEach
void clearItems() {
client.resources(ConfigMap.class).delete();
}

@Test
void singleItem() {
assertThat(client.configMaps().withName("config-map-1").get()).isNull();
}

@Test
@Disabled("#6220: Need to figure out a way to extract the singular from the plural form in URL")
void multipleItems_shouldReturnListObjectWithCorrectKind() {
// When
final ConfigMapList result = client.configMaps().list();
// Then
assertThat(result)
.hasFieldOrPropertyWithValue("kind", "ConfigMapList")
.extracting(ConfigMapList::getItems)
.asInstanceOf(InstanceOfAssertFactories.list(ConfigMap.class))
.isEmpty();
}

}

@Nested
class WithValidItems {

@BeforeEach
void addConsistentItems() {
for (int it = 0; it < 5; it++) {
client.resource(new ConfigMapBuilder().withNewMetadata().withName("config-map-" + it).endMetadata().build())
.createOr(NonDeletingOperation::update);
}
}

@Test
void singleItem() {
assertThat(client.configMaps().withName("config-map-1").get())
.isInstanceOf(ConfigMap.class);
}

@Test
@Disabled("#6220: Need to figure out a way to extract the singular from the plural form in URL")
void multipleItems_shouldReturnListObjectWithCorrectKind() {
// When
final ConfigMapList result = client.configMaps().list();
// Then
assertThat(result)
.hasFieldOrPropertyWithValue("kind", "ConfigMapList")
.extracting(ConfigMapList::getItems)
.asInstanceOf(InstanceOfAssertFactories.list(ConfigMap.class))
.extracting("kind")
.containsOnly("ConfigMap");
}
}

@Nested
class WithInvalidItems {
@BeforeEach
void addInvalidItems() throws Exception {
client.configMaps().resource(new ConfigMapBuilder()
.withKind("NotConfigMap")
.withNewMetadata().withName("NotConfigMap").endMetadata()
.build())
.create();
for (int it = 0; it < 5; it++) {
client.resource(new ConfigMapBuilder().withNewMetadata().withName("config-map-" + it).endMetadata().build())
.createOr(NonDeletingOperation::update);
}
client.getHttpClient().sendAsync(client.getHttpClient().newHttpRequestBuilder()
.uri(server.url("/api/v1/namespaces/test/configmaps"))
.post("application/json", "{\"kind\":\"NotConfigMap\"}")
.build(), String.class)
.get(10, TimeUnit.SECONDS);
}

@BeforeEach
void addConsistentItems() {
for (int it = 0; it < 10; it++) {
client.resource(new ConfigMapBuilder().withNewMetadata().withName("config-map-" + it).endMetadata().build())
.createOr(NonDeletingOperation::update);
}
}

@Test
void singleItem() {
assertThat(client.configMaps().withName("config-map-1").get())
.isInstanceOf(ConfigMap.class);
}

@Test
@Disabled("#6220: Need to figure out a way to extract the singular from the plural form in URL")
void multipleItems_shouldReturnListObjectWithCorrectKind() {
// When
final ConfigMapList result = client.configMaps().list();
// Then
assertThat(result)
.hasFieldOrPropertyWithValue("kind", "ConfigMapList")
.extracting(ConfigMapList::getItems)
.asInstanceOf(InstanceOfAssertFactories.list(ConfigMap.class))
.extracting("kind")
.containsOnly("ConfigMap");
}
}
}

0 comments on commit cffde45

Please sign in to comment.