|
| 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.openshift.client.server.mock; |
| 17 | + |
| 18 | +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; |
| 19 | +import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; |
| 20 | +import io.fabric8.openshift.api.model.machine.v1.ControlPlaneMachineSet; |
| 21 | +import io.fabric8.openshift.api.model.machine.v1.ControlPlaneMachineSetBuilder; |
| 22 | +import io.fabric8.openshift.api.model.machine.v1.ControlPlaneMachineSetList; |
| 23 | +import io.fabric8.openshift.api.model.machine.v1.ControlPlaneMachineSetListBuilder; |
| 24 | +import io.fabric8.openshift.client.OpenShiftClient; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import java.net.HttpURLConnection; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 32 | + |
| 33 | +@EnableKubernetesMockClient |
| 34 | +class ControlPlaneMachineSetTest { |
| 35 | + KubernetesMockServer server; |
| 36 | + OpenShiftClient client; |
| 37 | + |
| 38 | + @Test |
| 39 | + void create() { |
| 40 | + // Given |
| 41 | + ControlPlaneMachineSet controlPlaneMachineSet = getControlPlaneMachineSet(); |
| 42 | + server.expect().post() |
| 43 | + .withPath("/apis/machine.openshift.io/v1/namespaces/ns1/controlplanemachinesets") |
| 44 | + .andReturn(HttpURLConnection.HTTP_OK, controlPlaneMachineSet) |
| 45 | + .once(); |
| 46 | + |
| 47 | + // When |
| 48 | + controlPlaneMachineSet = client.machine().controlPlaneMachineSets().inNamespace("ns1").resource(controlPlaneMachineSet) |
| 49 | + .create(); |
| 50 | + |
| 51 | + // Then |
| 52 | + assertNotNull(controlPlaneMachineSet); |
| 53 | + assertEquals("cluster", controlPlaneMachineSet.getMetadata().getName()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void get() { |
| 58 | + // Given |
| 59 | + server.expect().get() |
| 60 | + .withPath("/apis/machine.openshift.io/v1/namespaces/ns1/controlplanemachinesets/cluster") |
| 61 | + .andReturn(HttpURLConnection.HTTP_OK, getControlPlaneMachineSet()) |
| 62 | + .once(); |
| 63 | + |
| 64 | + // When |
| 65 | + ControlPlaneMachineSet controlPlaneMachineSet = client.machine().controlPlaneMachineSets().inNamespace("ns1") |
| 66 | + .withName("cluster").get(); |
| 67 | + |
| 68 | + // Then |
| 69 | + assertNotNull(controlPlaneMachineSet); |
| 70 | + assertEquals("cluster", controlPlaneMachineSet.getMetadata().getName()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void list() { |
| 75 | + // Given |
| 76 | + server.expect().get() |
| 77 | + .withPath("/apis/machine.openshift.io/v1/namespaces/ns1/controlplanemachinesets") |
| 78 | + .andReturn(HttpURLConnection.HTTP_OK, |
| 79 | + new ControlPlaneMachineSetListBuilder().withItems(getControlPlaneMachineSet()).build()) |
| 80 | + .once(); |
| 81 | + |
| 82 | + // When |
| 83 | + ControlPlaneMachineSetList controlPlaneMachineSetList = client.machine().controlPlaneMachineSets().inNamespace("ns1") |
| 84 | + .list(); |
| 85 | + |
| 86 | + // Then |
| 87 | + assertNotNull(controlPlaneMachineSetList); |
| 88 | + assertNotNull(controlPlaneMachineSetList.getItems()); |
| 89 | + assertEquals(1, controlPlaneMachineSetList.getItems().size()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void delete() { |
| 94 | + // Given |
| 95 | + server.expect().delete() |
| 96 | + .withPath("/apis/machine.openshift.io/v1/namespaces/ns1/controlplanemachinesets/cluster") |
| 97 | + .andReturn(HttpURLConnection.HTTP_OK, getControlPlaneMachineSet()) |
| 98 | + .once(); |
| 99 | + |
| 100 | + // When |
| 101 | + boolean deleted = client.machine().controlPlaneMachineSets().inNamespace("ns1").withName("cluster").delete().size() == 1; |
| 102 | + |
| 103 | + // Then |
| 104 | + assertTrue(deleted); |
| 105 | + } |
| 106 | + |
| 107 | + private ControlPlaneMachineSet getControlPlaneMachineSet() { |
| 108 | + return new ControlPlaneMachineSetBuilder() |
| 109 | + .withNewMetadata() |
| 110 | + .withName("cluster") |
| 111 | + .endMetadata() |
| 112 | + .withNewSpec() |
| 113 | + .withReplicas(3) |
| 114 | + .withState("Active") |
| 115 | + .withNewStrategy().withType("RollingUpdate").endStrategy() |
| 116 | + .withNewSelector() |
| 117 | + .addToMatchLabels("machine.openshift.io/cluster-api-machine-role", "master") |
| 118 | + .addToMatchLabels("machine.openshift.io/cluster-api-machine-type", "master") |
| 119 | + .endSelector() |
| 120 | + .withNewTemplate() |
| 121 | + .withMachineType("machines_v1beta1_machine_openshift_io") |
| 122 | + .withNewMachinesV1beta1MachineOpenshiftIo() |
| 123 | + .withNewMetadata() |
| 124 | + .addToLabels("machine.openshift.io/cluster-api-machine-role", "master") |
| 125 | + .addToLabels("machine.openshift.io/cluster-api-machine-type", "master") |
| 126 | + .addToLabels("machine.openshift.io/cluster-api-cluster", "cluster") |
| 127 | + .endMetadata() |
| 128 | + .withNewSpec() |
| 129 | + .withNewProviderSpec() |
| 130 | + .endProviderSpec() |
| 131 | + .endSpec() |
| 132 | + .endMachinesV1beta1MachineOpenshiftIo() |
| 133 | + .endTemplate() |
| 134 | + .endSpec() |
| 135 | + .build(); |
| 136 | + } |
| 137 | +} |
0 commit comments