Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nacos discovery tests #2718

Open
wants to merge 11 commits into
base: 2021.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,36 @@
* @author xiaojing
*/

@SpringBootTest(
classes = NacosAutoServiceRegistrationIpNetworkInterfaceTests.TestConfig.class,
properties = { "spring.application.name=myTestService1",
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848" },
webEnvironment = RANDOM_PORT)
@SpringBootTest(classes = NacosAutoServiceRegistrationIpNetworkInterfaceTests.TestConfig.class, properties = {
"spring.application.name=myTestService1",
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848" }, webEnvironment = RANDOM_PORT)
public class NacosAutoServiceRegistrationIpNetworkInterfaceTests {

private static final MockedStatic<NacosFactory> nacosFactoryMockedStatic;

static {
nacosFactoryMockedStatic = Mockito.mockStatic(NacosFactory.class);
nacosFactoryMockedStatic
.when(() -> NacosFactory.createNamingService((Properties) any()))
.thenReturn(new MockNamingService());
}

@Autowired
private NacosRegistration registration;

@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;

@Autowired
private NacosDiscoveryProperties properties;

@Autowired
private InetUtils inetUtils;
private static MockedStatic<NacosFactory> nacosFactoryMockedStatic;
static {
nacosFactoryMockedStatic = Mockito.mockStatic(NacosFactory.class);
nacosFactoryMockedStatic.when(() -> NacosFactory.createNamingService((Properties) any()))
.thenReturn(new MockNamingService());
}

@AfterAll
public static void finished() {
if (nacosFactoryMockedStatic != null) {
nacosFactoryMockedStatic.close();
}
}

@Test
public void contextLoads() throws Exception {
assertThat(registration).isNotNull();
Expand All @@ -86,7 +86,7 @@ public void contextLoads() throws Exception {
}

private void checkoutNacosDiscoveryServiceIP() {
assertThat(registration.getHost())
assertThat(inetUtils.findFirstNonLoopbackAddress().getHostAddress())
.isEqualTo(getIPFromNetworkInterface(TestConfig.netWorkInterfaceName));
}

Expand Down
10 changes: 10 additions & 0 deletions spring-cloud-alibaba-tests/nacos-tests/nacos-config-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
<artifactId>spring-cloud-alibaba-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.tests.nacos.discovery;

import java.util.Arrays;
import java.util.List;

import com.alibaba.cloud.nacos.NacosServiceInstance;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient;
import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
import com.alibaba.cloud.nacos.discovery.ServiceCache;
import com.alibaba.cloud.testsupport.SpringCloudAlibaba;
import com.alibaba.cloud.testsupport.TestExtend;
import com.alibaba.nacos.api.exception.NacosException;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.test.util.ReflectionTestUtils;

import static com.alibaba.cloud.testsupport.Constant.TIME_OUT;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;

@SpringCloudAlibaba(composeFiles = "docker/nacos-compose-test.yml", serviceName = "nacos-standalone")
@TestExtend(time = 4 * TIME_OUT)
@SpringBootTest(classes = NacosDiscoveryPropertiesServerAddressBothLevelTests.TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=app",
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848",
"spring.cloud.nacos.server-addr=127.0.0.1:8848" })
public class NacosDiscoveryClientTests {

private static final String serviceName = "test-service";
@Mock
private NacosServiceDiscovery serviceDiscovery;
@Mock
private NacosServiceInstance serviceInstance;
@InjectMocks
private NacosDiscoveryClient client;

@Test
public void testGetInstances() throws Exception {

when(serviceDiscovery.getInstances(serviceName))
.thenReturn(singletonList(serviceInstance));

List<ServiceInstance> serviceInstances = client.getInstances(serviceName);

assertThat(serviceInstances).isNotEmpty();

}

@Test
public void testGetServices() throws Exception {

when(serviceDiscovery.getServices()).thenReturn(singletonList(serviceName));

List<String> services = client.getServices();

assertThat(services).contains(serviceName).size().isEqualTo(1);

}

@Test
public void testGetInstancesFailureToleranceEnabled() throws NacosException {
ServiceCache.setInstances("a", singletonList(serviceInstance));

when(serviceDiscovery.getInstances("a")).thenThrow(new NacosException());
ReflectionTestUtils.setField(client, "failureToleranceEnabled", true);

List<ServiceInstance> instances = this.client.getInstances("a");

assertThat(instances).isEqualTo(singletonList(serviceInstance));
}

@Test
public void testGetInstancesFailureToleranceDisabled() throws NacosException {
ServiceCache.setInstances("a", singletonList(serviceInstance));

when(serviceDiscovery.getInstances("a")).thenThrow(new NacosException());
ReflectionTestUtils.setField(client, "failureToleranceEnabled", false);

assertThatThrownBy(() -> this.client.getInstances("a"));
}

@Test
public void testFailureToleranceEnabled() throws NacosException {
ServiceCache.setServiceIds(Arrays.asList("a", "b"));

when(serviceDiscovery.getServices()).thenThrow(new NacosException());
ReflectionTestUtils.setField(client, "failureToleranceEnabled", true);

List<String> services = this.client.getServices();

assertThat(services).isEqualTo(Arrays.asList("a", "b"));
}

@Test
public void testFailureToleranceDisabled() throws NacosException {
ServiceCache.setServiceIds(Arrays.asList("a", "b"));

when(serviceDiscovery.getServices()).thenThrow(new NacosException());
ReflectionTestUtils.setField(client, "failureToleranceEnabled", false);

List<String> services = this.client.getServices();

assertThat(services).isEqualTo(emptyList());
}

@Test
public void testCacheIsOK() throws NacosException {
when(serviceDiscovery.getInstances("a"))
.thenReturn(singletonList(serviceInstance));
this.client.getInstances("a");
assertThat(ServiceCache.getInstances("a"))
.isEqualTo(singletonList(serviceInstance));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.tests.nacos.discovery;

import com.alibaba.cloud.nacos.NacosConfigAutoConfiguration;
import com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import com.alibaba.cloud.testsupport.SpringCloudAlibaba;
import com.alibaba.cloud.testsupport.TestExtend;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;

import static com.alibaba.cloud.testsupport.Constant.TIME_OUT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;

@SpringCloudAlibaba(composeFiles = "docker/nacos-compose-test.yml", serviceName = "nacos-standalone")
@TestExtend(time = 4 * TIME_OUT)
@SpringBootTest(classes = NacosDiscoveryPropertiesServerAddressBothLevelTests.TestConfig.class, webEnvironment = NONE, properties = {
"spring.application.name=app",
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848",
"spring.cloud.nacos.server-addr=127.0.0.1:8848" })
public class NacosDiscoveryPropertiesServerAddressBothLevelTests {

@Autowired
private NacosDiscoveryProperties properties;

@Test
public void testGetServerAddr() {

assertThat(properties.getServerAddr()).isEqualTo("127.0.0.1:8848");
}

@Configuration
@EnableAutoConfiguration
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
public static class TestConfig {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.tests.nacos.discovery;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration;
import com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = NacosDiscoveryPropertiesServerAddressBothLevelTests.TestConfig.class, properties = {
"spring.application.name=app", "spring.cloud.nacos.server-addr=127.0.0.1:8848" })
public class NacosDiscoveryPropertiesServerAddressTopLevelTests {

@Autowired
private NacosDiscoveryProperties properties;

@Test
public void testGetServerAddr() {
assertThat(properties.getServerAddr()).isEqualTo("127.0.0.1:8848");
}

@Configuration
@EnableAutoConfiguration
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class,
NacosDiscoveryClientConfiguration.class,
NacosServiceRegistryAutoConfiguration.class })
public static class TestConfig {

}
}
Loading