|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client.e2e.basic; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +import io.kubernetes.client.ProtoClient; |
| 18 | +import io.kubernetes.client.ProtoClient.ObjectOrStatus; |
| 19 | +import io.kubernetes.client.openapi.ApiClient; |
| 20 | +import io.kubernetes.client.proto.Meta.ObjectMeta; |
| 21 | +import io.kubernetes.client.proto.V1.Namespace; |
| 22 | +import io.kubernetes.client.proto.V1.NamespaceList; |
| 23 | +import io.kubernetes.client.util.ClientBuilder; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +class ProtoClientTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + void createListGetAndDeleteNamespace() throws Exception { |
| 30 | + ApiClient client = ClientBuilder.defaultClient(); |
| 31 | + ProtoClient protoClient = new ProtoClient(client); |
| 32 | + |
| 33 | + // Create a namespace using protocol buffers |
| 34 | + Namespace namespace = Namespace.newBuilder() |
| 35 | + .setMetadata(ObjectMeta.newBuilder().setName("e2e-proto-test").build()) |
| 36 | + .build(); |
| 37 | + |
| 38 | + ObjectOrStatus<Namespace> createResult = protoClient.create( |
| 39 | + namespace, |
| 40 | + "/api/v1/namespaces", |
| 41 | + "v1", |
| 42 | + "Namespace"); |
| 43 | + |
| 44 | + assertThat(createResult.object).isNotNull(); |
| 45 | + assertThat(createResult.object.getMetadata().getName()).isEqualTo("e2e-proto-test"); |
| 46 | + assertThat(createResult.status).isNull(); |
| 47 | + |
| 48 | + // List namespaces using protocol buffers |
| 49 | + ObjectOrStatus<NamespaceList> listResult = protoClient.list( |
| 50 | + NamespaceList.newBuilder(), |
| 51 | + "/api/v1/namespaces"); |
| 52 | + |
| 53 | + assertThat(listResult.object).isNotNull(); |
| 54 | + assertThat(listResult.object.getItemsCount()).isGreaterThan(0); |
| 55 | + assertThat(listResult.status).isNull(); |
| 56 | + |
| 57 | + // Verify our namespace is in the list |
| 58 | + boolean found = listResult.object.getItemsList().stream() |
| 59 | + .anyMatch(ns -> ns.getMetadata().getName().equals("e2e-proto-test")); |
| 60 | + assertThat(found).isTrue(); |
| 61 | + |
| 62 | + // Get the specific namespace using protocol buffers |
| 63 | + ObjectOrStatus<Namespace> getResult = protoClient.get( |
| 64 | + Namespace.newBuilder(), |
| 65 | + "/api/v1/namespaces/e2e-proto-test"); |
| 66 | + |
| 67 | + assertThat(getResult.object).isNotNull(); |
| 68 | + assertThat(getResult.object.getMetadata().getName()).isEqualTo("e2e-proto-test"); |
| 69 | + assertThat(getResult.status).isNull(); |
| 70 | + |
| 71 | + // Delete the namespace using protocol buffers |
| 72 | + ObjectOrStatus<Namespace> deleteResult = protoClient.delete( |
| 73 | + Namespace.newBuilder(), |
| 74 | + "/api/v1/namespaces/e2e-proto-test"); |
| 75 | + |
| 76 | + assertThat(deleteResult).isNotNull(); |
| 77 | + // Either we get the namespace object back or a status |
| 78 | + assertThat(deleteResult.object != null || deleteResult.status != null).isTrue(); |
| 79 | + } |
| 80 | +} |
0 commit comments