|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shenyu.registry.kubernetes; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.springframework.http.HttpStatus; |
| 23 | +import org.springframework.http.ResponseEntity; |
| 24 | +import org.springframework.web.client.RestTemplate; |
| 25 | + |
| 26 | +import java.lang.reflect.Field; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 33 | +import static org.mockito.ArgumentMatchers.any; |
| 34 | +import static org.mockito.ArgumentMatchers.anyString; |
| 35 | +import static org.mockito.ArgumentMatchers.eq; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | +import static org.mockito.Mockito.when; |
| 38 | + |
| 39 | +/** |
| 40 | + * Test for KubernetesClient. |
| 41 | + */ |
| 42 | +public final class KubernetesClientTest { |
| 43 | + |
| 44 | + private KubernetesClient kubernetesClient; |
| 45 | + |
| 46 | + private RestTemplate restTemplate; |
| 47 | + |
| 48 | + private KubernetesConfig kubernetesConfig; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + public void setUp() throws Exception { |
| 52 | + kubernetesConfig = new KubernetesConfig(); |
| 53 | + kubernetesConfig.setDiscoveryServerUrl("http://localhost:8761"); |
| 54 | + kubernetesConfig.setEnabled(true); |
| 55 | + kubernetesConfig.setNamespaces(Arrays.asList("default", "shenyu")); |
| 56 | + |
| 57 | + kubernetesClient = new KubernetesClient(kubernetesConfig); |
| 58 | + |
| 59 | + // Mock the RestTemplate |
| 60 | + restTemplate = mock(RestTemplate.class); |
| 61 | + Class<? extends KubernetesClient> clazz = kubernetesClient.getClass(); |
| 62 | + Field restField = clazz.getDeclaredField("rest"); |
| 63 | + restField.setAccessible(true); |
| 64 | + restField.set(kubernetesClient, restTemplate); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testSelectInstances() { |
| 69 | + String serviceId = "test-service"; |
| 70 | + KubernetesInstance instance1 = createKubernetesInstance("instance-1", serviceId, "192.168.1.1", 8080, "default"); |
| 71 | + KubernetesInstance instance2 = createKubernetesInstance("instance-2", serviceId, "192.168.1.2", 8081, "shenyu"); |
| 72 | + KubernetesInstance[] instances = new KubernetesInstance[]{instance1, instance2}; |
| 73 | + |
| 74 | + ResponseEntity<KubernetesInstance[]> responseEntity = new ResponseEntity<>(instances, HttpStatus.OK); |
| 75 | + when(restTemplate.getForEntity(anyString(), eq(KubernetesInstance[].class), any(Object[].class))) |
| 76 | + .thenReturn(responseEntity); |
| 77 | + |
| 78 | + List<KubernetesInstance> result = kubernetesClient.selectInstances(serviceId); |
| 79 | + |
| 80 | + assertNotNull(result); |
| 81 | + assertEquals(2, result.size()); |
| 82 | + assertEquals("192.168.1.1", result.get(0).getHost()); |
| 83 | + assertEquals(8080, result.get(0).getPort()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testSelectInstancesWithNamespaceFilter() { |
| 88 | + String serviceId = "test-service"; |
| 89 | + KubernetesInstance instance1 = createKubernetesInstance("instance-1", serviceId, "192.168.1.1", 8080, "default"); |
| 90 | + KubernetesInstance instance2 = createKubernetesInstance("instance-2", serviceId, "192.168.1.2", 8081, "other-namespace"); |
| 91 | + KubernetesInstance instance3 = createKubernetesInstance("instance-3", serviceId, "192.168.1.3", 8082, "shenyu"); |
| 92 | + KubernetesInstance[] instances = new KubernetesInstance[]{instance1, instance2, instance3}; |
| 93 | + |
| 94 | + ResponseEntity<KubernetesInstance[]> responseEntity = new ResponseEntity<>(instances, HttpStatus.OK); |
| 95 | + when(restTemplate.getForEntity(anyString(), eq(KubernetesInstance[].class), any(Object[].class))) |
| 96 | + .thenReturn(responseEntity); |
| 97 | + |
| 98 | + List<KubernetesInstance> result = kubernetesClient.selectInstances(serviceId); |
| 99 | + |
| 100 | + // Only instances in "default" and "shenyu" namespaces should be returned |
| 101 | + assertEquals(2, result.size()); |
| 102 | + assertTrue(result.stream().anyMatch(i -> "default".equals(i.getNamespace()))); |
| 103 | + assertTrue(result.stream().anyMatch(i -> "shenyu".equals(i.getNamespace()))); |
| 104 | + assertTrue(result.stream().noneMatch(i -> "other-namespace".equals(i.getNamespace()))); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testSelectInstancesWithEmptyNamespaceFilter() { |
| 109 | + KubernetesConfig config = new KubernetesConfig(); |
| 110 | + config.setDiscoveryServerUrl("http://localhost:8761"); |
| 111 | + config.setEnabled(true); |
| 112 | + config.setNamespaces(Arrays.asList()); |
| 113 | + |
| 114 | + KubernetesClient client = new KubernetesClient(config); |
| 115 | + |
| 116 | + String serviceId = "test-service"; |
| 117 | + KubernetesInstance instance1 = createKubernetesInstance("instance-1", serviceId, "192.168.1.1", 8080, "any-namespace"); |
| 118 | + KubernetesInstance instance2 = createKubernetesInstance("instance-2", serviceId, "192.168.1.2", 8081, "other-namespace"); |
| 119 | + KubernetesInstance[] instances = new KubernetesInstance[]{instance1, instance2}; |
| 120 | + |
| 121 | + RestTemplate mockRest = mock(RestTemplate.class); |
| 122 | + ResponseEntity<KubernetesInstance[]> responseEntity = new ResponseEntity<>(instances, HttpStatus.OK); |
| 123 | + when(mockRest.getForEntity(anyString(), eq(KubernetesInstance[].class), any(Object[].class))) |
| 124 | + .thenReturn(responseEntity); |
| 125 | + |
| 126 | + try { |
| 127 | + Field restField = client.getClass().getDeclaredField("rest"); |
| 128 | + restField.setAccessible(true); |
| 129 | + restField.set(client, mockRest); |
| 130 | + } catch (Exception e) { |
| 131 | + throw new RuntimeException(e); |
| 132 | + } |
| 133 | + |
| 134 | + List<KubernetesInstance> result = client.selectInstances(serviceId); |
| 135 | + |
| 136 | + // All instances should be returned when namespace filter is empty |
| 137 | + assertEquals(2, result.size()); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testSelectInstancesWithNullResponse() { |
| 142 | + String serviceId = "test-service"; |
| 143 | + |
| 144 | + ResponseEntity<KubernetesInstance[]> responseEntity = new ResponseEntity<>(null, HttpStatus.OK); |
| 145 | + when(restTemplate.getForEntity(anyString(), eq(KubernetesInstance[].class), any(Object[].class))) |
| 146 | + .thenReturn(responseEntity); |
| 147 | + |
| 148 | + List<KubernetesInstance> result = kubernetesClient.selectInstances(serviceId); |
| 149 | + |
| 150 | + assertNotNull(result); |
| 151 | + assertTrue(result.isEmpty()); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testSelectInstancesWithEmptyResponse() { |
| 156 | + String serviceId = "test-service"; |
| 157 | + KubernetesInstance[] instances = new KubernetesInstance[]{}; |
| 158 | + |
| 159 | + ResponseEntity<KubernetesInstance[]> responseEntity = new ResponseEntity<>(instances, HttpStatus.OK); |
| 160 | + when(restTemplate.getForEntity(anyString(), eq(KubernetesInstance[].class), any(Object[].class))) |
| 161 | + .thenReturn(responseEntity); |
| 162 | + |
| 163 | + List<KubernetesInstance> result = kubernetesClient.selectInstances(serviceId); |
| 164 | + |
| 165 | + assertNotNull(result); |
| 166 | + assertTrue(result.isEmpty()); |
| 167 | + } |
| 168 | + |
| 169 | + private KubernetesInstance createKubernetesInstance(final String instanceId, final String serviceId, |
| 170 | + final String host, final int port, final String namespace) { |
| 171 | + KubernetesInstance instance = new KubernetesInstance(); |
| 172 | + instance.setInstanceId(instanceId); |
| 173 | + instance.setServiceId(serviceId); |
| 174 | + instance.setHost(host); |
| 175 | + instance.setPort(port); |
| 176 | + instance.setNamespace(namespace); |
| 177 | + return instance; |
| 178 | + } |
| 179 | +} |
0 commit comments