Skip to content

Commit 3a3cc04

Browse files
yuluo-yx847850277Aias00
authored
feat: add shenyu-registry-k8s module unit test (#6206)
Signed-off-by: yuluo-yx <yuluo08290126@gmail.com> Co-authored-by: zhengpeng <847850277@qq.com> Co-authored-by: aias00 <liuhongyu@apache.org>
1 parent e0c50c3 commit 3a3cc04

File tree

5 files changed

+740
-0
lines changed

5 files changed

+740
-0
lines changed

shenyu-registry/shenyu-registry-kubernetes/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,23 @@
3838
<artifactId>spring-web</artifactId>
3939
<scope>provided</scope>
4040
</dependency>
41+
42+
<dependency>
43+
<groupId>org.junit.jupiter</groupId>
44+
<artifactId>junit-jupiter</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.mockito</groupId>
50+
<artifactId>mockito-core</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.mockito</groupId>
56+
<artifactId>mockito-junit-jupiter</artifactId>
57+
<scope>test</scope>
58+
</dependency>
4159
</dependencies>
4260
</project>
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.Test;
21+
22+
import java.util.Arrays;
23+
import java.util.List;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertFalse;
27+
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
30+
/**
31+
* Test for KubernetesConfig.
32+
*/
33+
public final class KubernetesConfigTest {
34+
35+
@Test
36+
public void testDefaultConstructor() {
37+
KubernetesConfig config = new KubernetesConfig();
38+
39+
assertNotNull(config);
40+
assertTrue(config.isEnabled());
41+
assertNotNull(config.getNamespaces());
42+
assertTrue(config.getNamespaces().isEmpty());
43+
}
44+
45+
@Test
46+
public void testSetAndGetDiscoveryServerUrl() {
47+
KubernetesConfig config = new KubernetesConfig();
48+
String url = "http://localhost:8761";
49+
50+
config.setDiscoveryServerUrl(url);
51+
52+
assertEquals(url, config.getDiscoveryServerUrl());
53+
}
54+
55+
@Test
56+
public void testSetAndGetEnabled() {
57+
KubernetesConfig config = new KubernetesConfig();
58+
59+
config.setEnabled(false);
60+
assertFalse(config.isEnabled());
61+
62+
config.setEnabled(true);
63+
assertTrue(config.isEnabled());
64+
}
65+
66+
@Test
67+
public void testSetAndGetNamespaces() {
68+
KubernetesConfig config = new KubernetesConfig();
69+
List<String> namespaces = Arrays.asList("default", "shenyu", "test");
70+
71+
config.setNamespaces(namespaces);
72+
73+
assertEquals(3, config.getNamespaces().size());
74+
assertTrue(config.getNamespaces().contains("default"));
75+
assertTrue(config.getNamespaces().contains("shenyu"));
76+
assertTrue(config.getNamespaces().contains("test"));
77+
}
78+
79+
@Test
80+
public void testSetEmptyNamespaces() {
81+
KubernetesConfig config = new KubernetesConfig();
82+
List<String> namespaces = Arrays.asList();
83+
84+
config.setNamespaces(namespaces);
85+
86+
assertNotNull(config.getNamespaces());
87+
assertTrue(config.getNamespaces().isEmpty());
88+
}
89+
90+
@Test
91+
public void testMultipleSetAndGet() {
92+
KubernetesConfig config = new KubernetesConfig();
93+
94+
config.setDiscoveryServerUrl("http://test:8080");
95+
config.setEnabled(false);
96+
config.setNamespaces(Arrays.asList("namespace1", "namespace2"));
97+
98+
assertEquals("http://test:8080", config.getDiscoveryServerUrl());
99+
assertFalse(config.isEnabled());
100+
assertEquals(2, config.getNamespaces().size());
101+
}
102+
}

0 commit comments

Comments
 (0)