Skip to content

Commit 8467fed

Browse files
authored
BIGTOP-4355: Add and optimize ut cases for agent module (#173)
1 parent 0ec263e commit 8467fed

File tree

8 files changed

+495
-19
lines changed

8 files changed

+495
-19
lines changed

bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/grpc/interceptor/TaskInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void onReady() {
8888
};
8989
}
9090

91-
private void truncateLogFile(Long taskId) {
91+
protected void truncateLogFile(Long taskId) {
9292
String filePath = ProjectPathUtils.getLogFilePath(taskId);
9393
File file = new File(filePath);
9494
if (file.exists()) {
@@ -100,7 +100,7 @@ private void truncateLogFile(Long taskId) {
100100
}
101101
}
102102

103-
private Boolean isTaskRequest(Object obj) {
103+
protected Boolean isTaskRequest(Object obj) {
104104
if (obj == null) {
105105
return false;
106106
}
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
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.bigtop.manager.agent;
20+
21+
import org.apache.bigtop.manager.agent.monitoring.AgentHostMonitoring;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.mockito.InjectMocks;
26+
import org.mockito.Mock;
27+
import org.mockito.MockedStatic;
28+
import org.mockito.junit.jupiter.MockitoExtension;
29+
import org.springframework.boot.SpringApplication;
30+
31+
import io.micrometer.core.instrument.MeterRegistry;
32+
import io.micrometer.core.instrument.MultiGauge;
33+
34+
import static org.junit.jupiter.api.Assertions.assertNotNull;
35+
import static org.mockito.Mockito.mockStatic;
36+
import static org.mockito.Mockito.when;
37+
38+
@ExtendWith(MockitoExtension.class)
39+
public class AgentApplicationTest {
40+
41+
@Mock
42+
private MeterRegistry meterRegistry;
43+
44+
@Mock
45+
private MultiGauge diskMultiGauge;
46+
47+
@Mock
48+
private MultiGauge cpuMultiGauge;
49+
50+
@Mock
51+
private MultiGauge memMultiGauge;
52+
53+
@Mock
54+
private MultiGauge diskIOMultiGauge;
55+
56+
@InjectMocks
57+
private AgentApplication agentApplication;
58+
59+
@Test
60+
public void testDiskMultiGaugeBean() {
61+
try (MockedStatic<AgentHostMonitoring> mockedStatic = mockStatic(AgentHostMonitoring.class)) {
62+
when(AgentHostMonitoring.newDiskMultiGauge(meterRegistry)).thenReturn(diskMultiGauge);
63+
MultiGauge result = agentApplication.diskMultiGauge(meterRegistry);
64+
assertNotNull(result);
65+
}
66+
}
67+
68+
@Test
69+
public void testCPUMultiGaugeBean() {
70+
try (MockedStatic<AgentHostMonitoring> mockedStatic = mockStatic(AgentHostMonitoring.class)) {
71+
when(AgentHostMonitoring.newCPUMultiGauge(meterRegistry)).thenReturn(cpuMultiGauge);
72+
MultiGauge result = agentApplication.cpuMultiGauge(meterRegistry);
73+
assertNotNull(result);
74+
}
75+
}
76+
77+
@Test
78+
public void testMemMultiGaugeBean() {
79+
try (MockedStatic<AgentHostMonitoring> mockedStatic = mockStatic(AgentHostMonitoring.class)) {
80+
when(AgentHostMonitoring.newMemMultiGauge(meterRegistry)).thenReturn(memMultiGauge);
81+
MultiGauge result = agentApplication.memMultiGauge(meterRegistry);
82+
assertNotNull(result);
83+
}
84+
}
85+
86+
@Test
87+
public void testDiskIOMultiGaugeBean() {
88+
try (MockedStatic<AgentHostMonitoring> mockedStatic = mockStatic(AgentHostMonitoring.class)) {
89+
when(AgentHostMonitoring.newDiskIOMultiGauge(meterRegistry)).thenReturn(diskIOMultiGauge);
90+
MultiGauge result = agentApplication.diskIOMultiGauge(meterRegistry);
91+
assertNotNull(result);
92+
}
93+
}
94+
95+
@Test
96+
public void testMainMethod() {
97+
try (MockedStatic<SpringApplication> mockedStatic = mockStatic(SpringApplication.class)) {
98+
AgentApplication.main(new String[] {});
99+
mockedStatic.verify(() -> SpringApplication.run(AgentApplication.class, new String[] {}));
100+
}
101+
}
102+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.bigtop.manager.agent.grpc.interceptor;
20+
21+
import org.apache.bigtop.manager.common.utils.ProjectPathUtils;
22+
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.extension.ExtendWith;
27+
import org.mockito.Mock;
28+
import org.mockito.MockedStatic;
29+
import org.mockito.junit.jupiter.MockitoExtension;
30+
import org.slf4j.Logger;
31+
32+
import java.io.File;
33+
import java.io.IOException;
34+
35+
import static org.junit.jupiter.api.Assertions.assertFalse;
36+
import static org.junit.jupiter.api.Assertions.assertTrue;
37+
import static org.mockito.Mockito.mockStatic;
38+
import static org.mockito.Mockito.times;
39+
import static org.mockito.Mockito.verifyNoInteractions;
40+
41+
@ExtendWith(MockitoExtension.class)
42+
public class TaskInterceptorTest {
43+
44+
@Mock
45+
private Logger log;
46+
47+
private TaskInterceptor taskInterceptor;
48+
private File tempFile;
49+
50+
@BeforeEach
51+
public void setUp() {
52+
taskInterceptor = new TaskInterceptor();
53+
}
54+
55+
@AfterEach
56+
public void tearDown() {
57+
if (tempFile != null && tempFile.exists()) {
58+
tempFile.delete();
59+
}
60+
}
61+
62+
@Test
63+
public void testTruncateLogFileFileExists() throws IOException {
64+
Long taskId = 1L;
65+
tempFile = File.createTempFile("logfile", ".log");
66+
67+
try (MockedStatic<ProjectPathUtils> projectPathUtils = mockStatic(ProjectPathUtils.class)) {
68+
projectPathUtils.when(() -> ProjectPathUtils.getLogFilePath(taskId)).thenReturn(tempFile.getAbsolutePath());
69+
70+
taskInterceptor.truncateLogFile(taskId);
71+
72+
projectPathUtils.verify(() -> ProjectPathUtils.getLogFilePath(taskId), times(1));
73+
assertTrue(tempFile.exists());
74+
assertTrue(tempFile.length() == 0);
75+
}
76+
}
77+
78+
@Test
79+
public void testTruncateLogFileFileDoesNotExist() throws IOException {
80+
Long taskId = 1L;
81+
String filePath = "path/to/nonexistentfile.log";
82+
83+
try (MockedStatic<ProjectPathUtils> projectPathUtils = mockStatic(ProjectPathUtils.class)) {
84+
projectPathUtils.when(() -> ProjectPathUtils.getLogFilePath(taskId)).thenReturn(filePath);
85+
86+
taskInterceptor.truncateLogFile(taskId);
87+
88+
projectPathUtils.verify(() -> ProjectPathUtils.getLogFilePath(taskId), times(1));
89+
verifyNoInteractions(log);
90+
}
91+
}
92+
93+
@Test
94+
public void testIsTaskRequestTaskRequest() {
95+
TaskLogRequest taskLogRequest = new TaskLogRequest();
96+
97+
Boolean result = taskInterceptor.isTaskRequest(taskLogRequest);
98+
99+
assertTrue(result);
100+
}
101+
102+
@Test
103+
public void testIsTaskRequestTaskWithGetTaskIdMethod() {
104+
Task task = new Task();
105+
106+
Boolean result = taskInterceptor.isTaskRequest(task);
107+
108+
assertTrue(result);
109+
}
110+
111+
@Test
112+
public void testIsTaskRequestTaskWithoutGetTaskIdMethod() {
113+
Object obj = new Object();
114+
115+
Boolean result = taskInterceptor.isTaskRequest(obj);
116+
117+
assertFalse(result);
118+
}
119+
120+
@Test
121+
public void testIsTaskRequestNullObject() {
122+
Boolean result = taskInterceptor.isTaskRequest(null);
123+
124+
assertFalse(result);
125+
}
126+
127+
private static class Task {
128+
public Long getTaskId() {
129+
return 1L;
130+
}
131+
}
132+
133+
private static class TaskLogRequest {
134+
public Long getTaskId() {
135+
return 1L;
136+
}
137+
}
138+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.bigtop.manager.agent.grpc.service;
20+
21+
import org.apache.bigtop.manager.common.shell.ShellResult;
22+
import org.apache.bigtop.manager.common.utils.Environments;
23+
import org.apache.bigtop.manager.common.utils.os.TimeSyncDetection;
24+
import org.apache.bigtop.manager.grpc.generated.HostCheckReply;
25+
import org.apache.bigtop.manager.grpc.generated.HostCheckRequest;
26+
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.extension.ExtendWith;
30+
import org.mockito.Mock;
31+
import org.mockito.junit.jupiter.MockitoExtension;
32+
33+
import io.grpc.stub.StreamObserver;
34+
35+
import static org.mockito.ArgumentMatchers.any;
36+
import static org.mockito.BDDMockito.then;
37+
import static org.mockito.Mockito.mock;
38+
import static org.mockito.Mockito.mockStatic;
39+
import static org.mockito.Mockito.never;
40+
import static org.mockito.Mockito.times;
41+
42+
@ExtendWith(MockitoExtension.class)
43+
public class HostCheckServiceGrpcImplTest {
44+
45+
@Mock
46+
private StreamObserver<HostCheckReply> responseObserver;
47+
48+
private HostCheckServiceGrpcImpl hostCheckServiceGrpcImpl;
49+
50+
@BeforeEach
51+
public void setUp() {
52+
hostCheckServiceGrpcImpl = new HostCheckServiceGrpcImpl();
53+
}
54+
55+
@Test
56+
public void testCheckInDevMode() {
57+
try (var environmentsMockedStatic = mockStatic(Environments.class)) {
58+
// Mock environment in development mode
59+
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(true);
60+
61+
// Call check method
62+
hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class), responseObserver);
63+
64+
// Verify responseObserver is correctly called
65+
then(responseObserver).should(times(1)).onNext(any());
66+
then(responseObserver).should(times(1)).onCompleted();
67+
then(responseObserver).should(never()).onError(any());
68+
}
69+
}
70+
71+
@Test
72+
public void testCheckInNonDevModeWithSuccess() {
73+
try (var environmentsMockedStatic = mockStatic(Environments.class);
74+
var timeSyncDetectionMockedStatic = mockStatic(TimeSyncDetection.class)) {
75+
// Mock environment not in development mode
76+
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(false);
77+
// Mock successful time synchronization check
78+
timeSyncDetectionMockedStatic
79+
.when(TimeSyncDetection::checkTimeSync)
80+
.thenReturn(ShellResult.success("test"));
81+
82+
// Call check method
83+
hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class), responseObserver);
84+
85+
// Verify responseObserver is correctly called
86+
then(responseObserver).should(times(1)).onNext(any());
87+
then(responseObserver).should(times(1)).onCompleted();
88+
then(responseObserver).should(never()).onError(any());
89+
}
90+
}
91+
92+
@Test
93+
public void testCheckInNonDevModeWithError() {
94+
try (var environmentsMockedStatic = mockStatic(Environments.class);
95+
var timeSyncDetectionMockedStatic = mockStatic(TimeSyncDetection.class)) {
96+
// Mock environment not in development mode
97+
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(false);
98+
// Mock failed time synchronization check
99+
timeSyncDetectionMockedStatic.when(TimeSyncDetection::checkTimeSync).thenReturn(ShellResult.fail("test"));
100+
101+
// Call check method
102+
hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class), responseObserver);
103+
104+
// Verify responseObserver is correctly called
105+
then(responseObserver).should(times(1)).onNext(any());
106+
then(responseObserver).should(times(1)).onCompleted();
107+
then(responseObserver).should(never()).onError(any());
108+
}
109+
}
110+
111+
@Test
112+
public void testCheckWithError() {
113+
try (var environmentsMockedStatic = mockStatic(Environments.class);
114+
var timeSyncDetectionMockedStatic = mockStatic(TimeSyncDetection.class)) {
115+
// Mock environment not in development mode
116+
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(false);
117+
// Mock a runtime exception during check execution
118+
timeSyncDetectionMockedStatic
119+
.when(TimeSyncDetection::checkTimeSync)
120+
.thenThrow(new RuntimeException("Test Simulated Error"));
121+
122+
// Call check method
123+
hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class), responseObserver);
124+
125+
// Verify responseObserver is correctly called
126+
then(responseObserver).should(never()).onNext(any());
127+
then(responseObserver).should(never()).onCompleted();
128+
then(responseObserver).should(times(1)).onError(any());
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)