Skip to content

Commit

Permalink
fix (kubernetes-server-mock) : KubernetesMockServer does not return s…
Browse files Browse the repository at this point in the history
…ame List type as Kubernetes Api Server

KubernetesResponseComposer now infers list type from list items instead
of returning a hardcoded `List` kind value

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Sep 5, 2024
1 parent eec18ee commit 54e0ca7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#### Bugs
* Fix #6038: Support for Gradle configuration cache
* Fix #6220: KubernetesMockServer does not return same List type as Kubernetes Api Server

#### Improvements
* Fix #5264: Remove deprecated `Config.errorMessages` field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package io.fabric8.kubernetes.client.server.mock;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.utils.Utils;
import io.fabric8.mockwebserver.crud.ResponseComposer;

import java.util.Collection;
Expand All @@ -28,8 +31,18 @@ public String compose(Collection<String> collection) {

public String compose(Collection<String> collection, String resourceVersion) {
return String.format(
"{\"apiVersion\":\"v1\",\"kind\":\"List\", \"items\": [%s], " +
"{\"apiVersion\":\"v1\",\"kind\":\"%s\", \"items\": [%s], " +
"\"metadata\": {\"resourceVersion\": \"%s\", \"selfLink\": \"\"}}",
String.join(",", collection), resourceVersion);
inferListKindFromJsonObject(collection), String.join(",", collection), resourceVersion);
}

private String inferListKindFromJsonObject(Collection<String> kubernetesResourceList) {
return kubernetesResourceList.stream()
.map(k -> Serialization.unmarshal(k, HasMetadata.class))
.map(HasMetadata::getKind)
.filter(Utils::isNotNullOrEmpty)
.findFirst()
.map(k -> k + "List")
.orElse("List");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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(
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,38 @@
/*
* 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.SecretBuilder;
import io.fabric8.kubernetes.api.model.SecretList;
import org.junit.jupiter.api.Test;

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

class KubernetesCrudDispatcherGetListTest extends KubernetesCrudDispatcherTestBase {
@Test
void list_shouldReturnListObjectWithCorrectKind() {
// Given
client.secrets().resource(new SecretBuilder().withNewMetadata()
.withName("test-secret").endMetadata()
.build())
.create();
// When
SecretList secretList = client.secrets().list();

// Then
assertThat(secretList.getKind()).isEqualTo("SecretList");
}
}

0 comments on commit 54e0ca7

Please sign in to comment.