Skip to content

Commit

Permalink
[ISSUE#4982] Fix naming module unit test and generate jacoco coverage…
Browse files Browse the repository at this point in the history
… report (#8282)

* 更新jacoco版本

* Fix naming module unit test and generate jacoco coverage.

* Fix log too long problem.

* Fix MetricsMonitorTest
  • Loading branch information
KomachiSion authored May 9, 2022
1 parent 234e6f2 commit a1c7715
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
- name: Build with Maven
run: mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
- name: Test With Maven
run: mvn -Prelease-nacos clean test -DforkCount=0
run: mvn -Prelease-nacos clean test
16 changes: 8 additions & 8 deletions naming/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
<argLine>-Dnacos.standalone=true</argLine>
</configuration>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-surefire-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <skipTests>true</skipTests>-->
<!-- <argLine>-Dnacos.standalone=true</argLine>-->
<!-- </configuration>-->
<!-- </plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public abstract class BaseTest {
protected UpgradeJudgement upgradeJudgement;

@Spy
private MockEnvironment environment;
protected MockEnvironment environment;

@Before
public void before() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void setUp() throws Exception {
MockEnvironment environment = new MockEnvironment();
environment.setProperty(Constants.SUPPORT_UPGRADE_FROM_1X, "true");
EnvUtil.setEnvironment(environment);
EnvUtil.setIsStandalone(true);
oldPersistentConsistencyServiceDelegate = new PersistentConsistencyServiceDelegateImpl(clusterVersionJudgement,
raftConsistencyService, protocolManager);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void testServiceDetail() throws Exception {
assertTrue(actual.contains("\"groupName\":\"test-group-name\""));
assertTrue(actual.contains("\"metadata\":{}"));
assertTrue(actual.contains("\"name\":\"test-service\""));
assertTrue(actual.contains("\"selector\":{\"type\":\"none\"}"));
assertTrue(actual.contains("\"selector\":{\"type\":\"none\",\"contextType\":\"NONE\"}"));
assertTrue(actual.contains("\"protectThreshold\":12.34"));
assertTrue(actual.contains("\"clusters\":[{"));
assertTrue(actual.contains("\"defaultCheckPort\":80"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class InstanceControllerV2Test extends BaseTest {
public void before() {
super.before();
ReflectionTestUtils.setField(instanceControllerV2, "instanceServiceV2", instanceServiceV2);
mockInjectPushServer();
mockmvc = MockMvcBuilders.standaloneSetup(instanceControllerV2).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class DistroMapperTest {
public void setUp() {
ConcurrentSkipListMap<String, Member> serverList = new ConcurrentSkipListMap<>();
EnvUtil.setEnvironment(new StandardEnvironment());
EnvUtil.setIsStandalone(true);
serverList.put(ip1, Member.builder().ip(ip1).port(port).build());
serverList.put(ip2, Member.builder().ip(ip2).port(port).build());
serverList.put(ip3, Member.builder().ip(ip3).port(port).build());
Expand All @@ -79,4 +80,4 @@ public void testMapSrv() {
String server = distroMapper.mapSrv(serviceName);
assertEquals(server, ip4);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.nacos.naming.BaseTest;
import com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.DoubleWriteEventListener;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -48,6 +49,11 @@ public void before() {
mockInjectPushServer();
}

@After
public void tearDown() throws Exception {
service.destroy();
}

@Test
public void updateDomain() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingResponseCode;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.naming.core.v2.ServiceManager;
import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient;
Expand All @@ -30,14 +31,15 @@
import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.core.v2.service.ClientOperationServiceProxy;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.naming.healthcheck.RsInfo;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.pojo.InstanceOperationInfo;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.pojo.instance.BeatInfoInstanceBuilder;
import com.alibaba.nacos.naming.push.UdpPushService;
import com.alibaba.nacos.naming.selector.SelectorManager;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -47,6 +49,8 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mock.env.MockEnvironment;

import java.util.Collections;
Expand All @@ -55,6 +59,9 @@
import java.util.Map;
import java.util.Optional;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

/**
* {@link InstanceOperatorClientImpl} unit tests.
*
Expand Down Expand Up @@ -88,11 +95,21 @@ public class InstanceOperatorClientImplTest {
@Mock
private UdpPushService pushService;

@Mock
private SelectorManager selectorManager;

@Mock
private ConfigurableApplicationContext context;

@Before
public void setUp() {
Service service = Service.newService("A", "DEFAULT_GROUP", "C");
ServiceManager.getInstance().getSingleton(service);
EnvUtil.setEnvironment(new MockEnvironment());
ApplicationUtils.injectContext(context);
when(context.getBean(SelectorManager.class)).thenReturn(selectorManager);
when(selectorManager.select(any(), any(), any()))
.then((Answer<List<Instance>>) invocationOnMock -> invocationOnMock.getArgument(2));
}

@After
Expand All @@ -104,13 +121,13 @@ public void tearDown() {
@Test
public void testRegisterInstance() {
instanceOperatorClient.registerInstance("A", "B", new Instance());

Mockito.verify(clientOperationService).registerInstance(Mockito.any(), Mockito.any(), Mockito.anyString());
}

@Test
public void testRemoveInstance() {
Mockito.when(clientManager.contains(Mockito.anyString())).thenReturn(true);
when(clientManager.contains(Mockito.anyString())).thenReturn(true);

instanceOperatorClient.removeInstance("A", "B", new Instance());

Expand All @@ -136,24 +153,25 @@ public void testPatchInstance() throws NacosException {

ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setHosts(instances);
Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);

instanceOperatorClient.patchInstance("A", "B", new InstancePatchObject("C", "1.1.1.1", 8848));

Mockito.verify(metadataOperateService).updateInstanceMetadata(Mockito.any(), Mockito.anyString(), Mockito.any());
Mockito.verify(metadataOperateService)
.updateInstanceMetadata(Mockito.any(), Mockito.anyString(), Mockito.any());
}

@Test
public void testListInstance() {
Mockito.when(pushService.canEnablePush(Mockito.anyString())).thenReturn(true);
when(pushService.canEnablePush(Mockito.anyString())).thenReturn(true);

ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setGroupName("DEFAULT_GROUP");
serviceInfo.setName("B");
Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);

when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
ServiceMetadata metadata = new ServiceMetadata();
Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata));
when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata));

Subscriber subscriber = new Subscriber("2.2.2.2", "", "app", "1.1.1.1", "A", "B", 8848);
instanceOperatorClient.listInstance("A", "B", subscriber, "C", true);
Expand All @@ -164,13 +182,15 @@ public void testListInstance() {
@Test
public void testHandleBeat() throws NacosException {
IpPortBasedClient ipPortBasedClient = Mockito.mock(IpPortBasedClient.class);
Mockito.when(clientManager.getClient(Mockito.anyString())).thenReturn(ipPortBasedClient);
when(clientManager.getClient(Mockito.anyString())).thenReturn(ipPortBasedClient);

Mockito.when(ipPortBasedClient.getAllPublishedService()).thenReturn(Collections.emptyList());
when(ipPortBasedClient.getAllPublishedService()).thenReturn(Collections.emptyList());

RsInfo rsInfo = new RsInfo();
rsInfo.setMetadata(new HashMap<>(1));
int res = instanceOperatorClient
.handleBeat("A", "C", "1.1.1.1", 8848, "D", new RsInfo(), BeatInfoInstanceBuilder.newBuilder());

.handleBeat("A", "C", "1.1.1.1", 8848, "D", rsInfo, BeatInfoInstanceBuilder.newBuilder());
Assert.assertEquals(NamingResponseCode.OK, res);
}

Expand All @@ -179,9 +199,10 @@ public void testGetHeartBeatInterval() {
InstanceMetadata instanceMetadata = new InstanceMetadata();
Map<String, Object> map = new HashMap<>(2);
instanceMetadata.setExtendData(map);
Mockito.when(metadataManager.getInstanceMetadata(Mockito.any(), Mockito.anyString())).thenReturn(Optional.of(instanceMetadata));
when(metadataManager.getInstanceMetadata(Mockito.any(), Mockito.anyString()))
.thenReturn(Optional.of(instanceMetadata));

Mockito.when(switchDomain.getClientBeatInterval()).thenReturn(100L);
when(switchDomain.getClientBeatInterval()).thenReturn(100L);

long interval = instanceOperatorClient.getHeartBeatInterval("A", "C", "1.1.1.1", 8848, "D");

Expand All @@ -193,7 +214,7 @@ public void testListAllInstances() throws NacosException {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setHosts(Collections.emptyList());

Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);

List<? extends Instance> instances = instanceOperatorClient.listAllInstances("A", "C");

Expand All @@ -208,7 +229,7 @@ public void testBatchUpdateMetadata() throws NacosException {
instance.setPort(8848);
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setHosts(Collections.singletonList(instance));
Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);

InstanceOperationInfo instanceOperationInfo = new InstanceOperationInfo();
List<String> res = instanceOperatorClient.batchUpdateMetadata("A", instanceOperationInfo, new HashMap<>());
Expand All @@ -224,9 +245,10 @@ public void testBatchDeleteMetadata() throws NacosException {
instance.setPort(8848);
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setHosts(Collections.singletonList(instance));
Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);

List<String> res = instanceOperatorClient.batchDeleteMetadata("A", new InstanceOperationInfo(), new HashMap<>());
List<String> res = instanceOperatorClient
.batchDeleteMetadata("A", new InstanceOperationInfo(), new HashMap<>());

Assert.assertEquals(1, res.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.KeyBuilder;
import com.alibaba.nacos.naming.core.ServiceManager.ServiceChecksum;
import com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.DoubleWriteEventListener;
import com.alibaba.nacos.naming.misc.Message;
import com.alibaba.nacos.naming.misc.Synchronizer;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -71,6 +73,9 @@ public class ServiceManagerTest extends BaseTest {
@Mock
private ServerMemberManager serverMemberManager;

@Mock
private DoubleWriteEventListener doubleWriteEventListener;

private Service service;

private Cluster cluster;
Expand All @@ -93,6 +98,12 @@ public void before() {
mockCluster();
mockInstance();
mockServiceName();
when(context.getBean(DoubleWriteEventListener.class)).thenReturn(doubleWriteEventListener);
}

@After
public void tearDown() throws Exception {
service.destroy();
}

private void mockService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -66,15 +68,24 @@ public class ServiceOperatorV2ImplTest {
private ServiceStorage serviceStorage;

@Before
public void setUp() {
public void setUp() throws IllegalAccessException {
cleanNamespace();
Service service = Service.newService("A", "B", "C");
ServiceManager.getInstance().getSingleton(service);
}

@After
public void tearDown() {
public void tearDown() throws IllegalAccessException {
Service service = Service.newService("A", "B", "C");
ServiceManager.getInstance().removeSingleton(service);
cleanNamespace();
}

private void cleanNamespace() throws IllegalAccessException {
Field field = ReflectionUtils.findField(ServiceManager.class, "namespaceSingletonMaps");
field.setAccessible(true);
Map map = (Map) field.get(ServiceManager.getInstance());
map.clear();
}

@Test
Expand All @@ -97,7 +108,7 @@ public void testDelete() throws NacosException {
serviceInfo.setHosts(Collections.emptyList());
Mockito.when(serviceStorage.getPushData(Mockito.any())).thenReturn(serviceInfo);

serviceOperatorV2.delete("A", "C");
serviceOperatorV2.delete("A", "B@@C");

Mockito.verify(metadataOperateService).deleteServiceMetadata(Mockito.any());
}
Expand All @@ -113,7 +124,7 @@ public void testQueryService() throws NacosException {

Mockito.when(serviceStorage.getClusters(Mockito.any())).thenReturn(Collections.singleton("D"));

ObjectNode objectNode = serviceOperatorV2.queryService("A", "C");
ObjectNode objectNode = serviceOperatorV2.queryService("A", "B@@C");

Assert.assertEquals("A", objectNode.get(FieldsConstants.NAME_SPACE_ID).asText());
Assert.assertEquals("C", objectNode.get(FieldsConstants.NAME).asText());
Expand Down
Loading

0 comments on commit a1c7715

Please sign in to comment.