|
| 1 | +/** |
| 2 | + * Copyright (C) 2015 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.fabric8.kubernetes.client.mock; |
| 17 | + |
| 18 | +import io.fabric8.kubernetes.api.model.Pod; |
| 19 | +import io.fabric8.kubernetes.api.model.PodBuilder; |
| 20 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 21 | +import io.fabric8.kubernetes.client.KubernetesClientException; |
| 22 | +import io.fabric8.kubernetes.client.VersionInfo; |
| 23 | +import io.fabric8.kubernetes.client.server.mock.KubernetesMixedDispatcher; |
| 24 | +import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; |
| 25 | +import io.fabric8.kubernetes.client.utils.Serialization; |
| 26 | +import io.fabric8.mockwebserver.Context; |
| 27 | +import io.fabric8.mockwebserver.ServerRequest; |
| 28 | +import io.fabric8.mockwebserver.ServerResponse; |
| 29 | +import okhttp3.mockwebserver.MockWebServer; |
| 30 | +import org.junit.jupiter.api.AfterEach; |
| 31 | +import org.junit.jupiter.api.BeforeAll; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.DisplayName; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Queue; |
| 39 | + |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 42 | + |
| 43 | +class MixedCrudTest { |
| 44 | + |
| 45 | + private KubernetesMockServer server; |
| 46 | + private KubernetesClient client; |
| 47 | + |
| 48 | + @BeforeAll |
| 49 | + static void beforeAll() { |
| 50 | + } |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + void setUp() { |
| 54 | + final Map<ServerRequest, Queue<ServerResponse>> responses = new HashMap<>(); |
| 55 | + server = new KubernetesMockServer(new Context(Serialization.jsonMapper()), |
| 56 | + new MockWebServer(), responses, new KubernetesMixedDispatcher(responses), false); |
| 57 | + client = server.createClient(); |
| 58 | + } |
| 59 | + |
| 60 | + @AfterEach |
| 61 | + void tearDown() { |
| 62 | + client.close(); |
| 63 | + server.destroy(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + @DisplayName("client.getKubernetesVersion, with no expectations, should throw Exception") |
| 68 | + void versionWithNoExpectationsShouldFail() { |
| 69 | + // When |
| 70 | + final RuntimeException exception = assertThrows(RuntimeException.class, client::getKubernetesVersion); |
| 71 | + // Then |
| 72 | + assertThat(exception).isNotNull().isInstanceOf(KubernetesClientException.class); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @DisplayName("client.getKubernetesVersion, with expectation for version, should return version") |
| 77 | + void versionWithExpectationsShouldReturnVersion() { |
| 78 | + // Given |
| 79 | + server.expect().get().withPath("/version") |
| 80 | + .andReturn(200, "{\"major\": \"13\", \"minor\": \"37\"}").always(); |
| 81 | + // When |
| 82 | + final VersionInfo result = client.getKubernetesVersion(); |
| 83 | + // Then |
| 84 | + assertThat(result) |
| 85 | + .hasFieldOrPropertyWithValue("major", "13") |
| 86 | + .hasFieldOrPropertyWithValue("minor", "37"); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + @DisplayName("CRUD get, with no expectations, should return managed resource") |
| 91 | + void crudGetWithNoExpectations() { |
| 92 | + // Given |
| 93 | + client.pods().inNamespace("ns") |
| 94 | + .create(new PodBuilder().editOrNewMetadata().withName("my-pod").addToAnnotations("my", "pod").endMetadata().build()); |
| 95 | + // When |
| 96 | + final Pod result = client.pods().inNamespace("ns").withName("my-pod").get(); |
| 97 | + // Then |
| 98 | + assertThat(result) |
| 99 | + .hasFieldOrPropertyWithValue("metadata.annotations.my", "pod") |
| 100 | + .hasFieldOrPropertyWithValue("metadata.generation", 1L); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("CRUD get, with expectations, should return expectation") |
| 105 | + void crudGetWithExpectations() { |
| 106 | + // Given |
| 107 | + client.pods().inNamespace("ns") |
| 108 | + .create(new PodBuilder().editOrNewMetadata().withName("my-pod").addToAnnotations("my", "pod").endMetadata().build()); |
| 109 | + server.expect().get().withPath("/api/v1/namespaces/ns/pods/my-pod") |
| 110 | + .andReturn(200, "{\"metadata\": {\"name\": \"override\"}}").always(); |
| 111 | + // When |
| 112 | + final Pod result = client.pods().inNamespace("ns").withName("my-pod").get(); |
| 113 | + // Then |
| 114 | + assertThat(result) |
| 115 | + .hasFieldOrPropertyWithValue("metadata.name", "override") |
| 116 | + .hasFieldOrPropertyWithValue("metadata.generation", null); |
| 117 | + } |
| 118 | +} |
0 commit comments