This is an automated email from the ASF dual-hosted git repository.
wuzhiguo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/bigtop-manager.git
The following commit(s) were added to refs/heads/main by this push:
new 8467fed7 BIGTOP-4355: Add and optimize ut cases for agent module (#173)
8467fed7 is described below
commit 8467fed7f5cdbf99a6930168d4044a326d88b0da
Author: xianrenzw <[email protected]>
AuthorDate: Mon Feb 10 10:36:52 2025 +0800
BIGTOP-4355: Add and optimize ut cases for agent module (#173)
---
.../agent/grpc/interceptor/TaskInterceptor.java | 4 +-
.../bigtop/manager/agent/AgentApplicationTest.java | 102 +++++++++++++++
.../grpc/interceptor/TaskInterceptorTest.java | 138 +++++++++++++++++++++
.../grpc/service/HostCheckServiceGrpcImplTest.java | 131 +++++++++++++++++++
.../grpc/service/JobCacheServiceGrpcImplTest.java | 25 +++-
.../agent/holder/SpringContextHolderTest.java | 24 ++--
.../agent/metrics/MetricsCollectorTest.java | 62 +++++++++
.../agent/monitoring/AgentHostMonitoringTest.java | 28 +++++
8 files changed, 495 insertions(+), 19 deletions(-)
diff --git
a/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/grpc/interceptor/TaskInterceptor.java
b/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/grpc/interceptor/TaskInterceptor.java
index 99fdb709..c2550509 100644
---
a/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/grpc/interceptor/TaskInterceptor.java
+++
b/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/grpc/interceptor/TaskInterceptor.java
@@ -88,7 +88,7 @@ public class TaskInterceptor implements ServerInterceptor {
};
}
- private void truncateLogFile(Long taskId) {
+ protected void truncateLogFile(Long taskId) {
String filePath = ProjectPathUtils.getLogFilePath(taskId);
File file = new File(filePath);
if (file.exists()) {
@@ -100,7 +100,7 @@ public class TaskInterceptor implements ServerInterceptor {
}
}
- private Boolean isTaskRequest(Object obj) {
+ protected Boolean isTaskRequest(Object obj) {
if (obj == null) {
return false;
}
diff --git
a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/AgentApplicationTest.java
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/AgentApplicationTest.java
new file mode 100644
index 00000000..0d0eb16f
--- /dev/null
+++
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/AgentApplicationTest.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.bigtop.manager.agent;
+
+import org.apache.bigtop.manager.agent.monitoring.AgentHostMonitoring;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.boot.SpringApplication;
+
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.MultiGauge;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+public class AgentApplicationTest {
+
+ @Mock
+ private MeterRegistry meterRegistry;
+
+ @Mock
+ private MultiGauge diskMultiGauge;
+
+ @Mock
+ private MultiGauge cpuMultiGauge;
+
+ @Mock
+ private MultiGauge memMultiGauge;
+
+ @Mock
+ private MultiGauge diskIOMultiGauge;
+
+ @InjectMocks
+ private AgentApplication agentApplication;
+
+ @Test
+ public void testDiskMultiGaugeBean() {
+ try (MockedStatic<AgentHostMonitoring> mockedStatic =
mockStatic(AgentHostMonitoring.class)) {
+
when(AgentHostMonitoring.newDiskMultiGauge(meterRegistry)).thenReturn(diskMultiGauge);
+ MultiGauge result = agentApplication.diskMultiGauge(meterRegistry);
+ assertNotNull(result);
+ }
+ }
+
+ @Test
+ public void testCPUMultiGaugeBean() {
+ try (MockedStatic<AgentHostMonitoring> mockedStatic =
mockStatic(AgentHostMonitoring.class)) {
+
when(AgentHostMonitoring.newCPUMultiGauge(meterRegistry)).thenReturn(cpuMultiGauge);
+ MultiGauge result = agentApplication.cpuMultiGauge(meterRegistry);
+ assertNotNull(result);
+ }
+ }
+
+ @Test
+ public void testMemMultiGaugeBean() {
+ try (MockedStatic<AgentHostMonitoring> mockedStatic =
mockStatic(AgentHostMonitoring.class)) {
+
when(AgentHostMonitoring.newMemMultiGauge(meterRegistry)).thenReturn(memMultiGauge);
+ MultiGauge result = agentApplication.memMultiGauge(meterRegistry);
+ assertNotNull(result);
+ }
+ }
+
+ @Test
+ public void testDiskIOMultiGaugeBean() {
+ try (MockedStatic<AgentHostMonitoring> mockedStatic =
mockStatic(AgentHostMonitoring.class)) {
+
when(AgentHostMonitoring.newDiskIOMultiGauge(meterRegistry)).thenReturn(diskIOMultiGauge);
+ MultiGauge result =
agentApplication.diskIOMultiGauge(meterRegistry);
+ assertNotNull(result);
+ }
+ }
+
+ @Test
+ public void testMainMethod() {
+ try (MockedStatic<SpringApplication> mockedStatic =
mockStatic(SpringApplication.class)) {
+ AgentApplication.main(new String[] {});
+ mockedStatic.verify(() ->
SpringApplication.run(AgentApplication.class, new String[] {}));
+ }
+ }
+}
diff --git
a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/grpc/interceptor/TaskInterceptorTest.java
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/grpc/interceptor/TaskInterceptorTest.java
new file mode 100644
index 00000000..f9425578
--- /dev/null
+++
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/grpc/interceptor/TaskInterceptorTest.java
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.bigtop.manager.agent.grpc.interceptor;
+
+import org.apache.bigtop.manager.common.utils.ProjectPathUtils;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.slf4j.Logger;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verifyNoInteractions;
+
+@ExtendWith(MockitoExtension.class)
+public class TaskInterceptorTest {
+
+ @Mock
+ private Logger log;
+
+ private TaskInterceptor taskInterceptor;
+ private File tempFile;
+
+ @BeforeEach
+ public void setUp() {
+ taskInterceptor = new TaskInterceptor();
+ }
+
+ @AfterEach
+ public void tearDown() {
+ if (tempFile != null && tempFile.exists()) {
+ tempFile.delete();
+ }
+ }
+
+ @Test
+ public void testTruncateLogFileFileExists() throws IOException {
+ Long taskId = 1L;
+ tempFile = File.createTempFile("logfile", ".log");
+
+ try (MockedStatic<ProjectPathUtils> projectPathUtils =
mockStatic(ProjectPathUtils.class)) {
+ projectPathUtils.when(() ->
ProjectPathUtils.getLogFilePath(taskId)).thenReturn(tempFile.getAbsolutePath());
+
+ taskInterceptor.truncateLogFile(taskId);
+
+ projectPathUtils.verify(() ->
ProjectPathUtils.getLogFilePath(taskId), times(1));
+ assertTrue(tempFile.exists());
+ assertTrue(tempFile.length() == 0);
+ }
+ }
+
+ @Test
+ public void testTruncateLogFileFileDoesNotExist() throws IOException {
+ Long taskId = 1L;
+ String filePath = "path/to/nonexistentfile.log";
+
+ try (MockedStatic<ProjectPathUtils> projectPathUtils =
mockStatic(ProjectPathUtils.class)) {
+ projectPathUtils.when(() ->
ProjectPathUtils.getLogFilePath(taskId)).thenReturn(filePath);
+
+ taskInterceptor.truncateLogFile(taskId);
+
+ projectPathUtils.verify(() ->
ProjectPathUtils.getLogFilePath(taskId), times(1));
+ verifyNoInteractions(log);
+ }
+ }
+
+ @Test
+ public void testIsTaskRequestTaskRequest() {
+ TaskLogRequest taskLogRequest = new TaskLogRequest();
+
+ Boolean result = taskInterceptor.isTaskRequest(taskLogRequest);
+
+ assertTrue(result);
+ }
+
+ @Test
+ public void testIsTaskRequestTaskWithGetTaskIdMethod() {
+ Task task = new Task();
+
+ Boolean result = taskInterceptor.isTaskRequest(task);
+
+ assertTrue(result);
+ }
+
+ @Test
+ public void testIsTaskRequestTaskWithoutGetTaskIdMethod() {
+ Object obj = new Object();
+
+ Boolean result = taskInterceptor.isTaskRequest(obj);
+
+ assertFalse(result);
+ }
+
+ @Test
+ public void testIsTaskRequestNullObject() {
+ Boolean result = taskInterceptor.isTaskRequest(null);
+
+ assertFalse(result);
+ }
+
+ private static class Task {
+ public Long getTaskId() {
+ return 1L;
+ }
+ }
+
+ private static class TaskLogRequest {
+ public Long getTaskId() {
+ return 1L;
+ }
+ }
+}
diff --git
a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/grpc/service/HostCheckServiceGrpcImplTest.java
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/grpc/service/HostCheckServiceGrpcImplTest.java
new file mode 100644
index 00000000..b6622ff6
--- /dev/null
+++
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/grpc/service/HostCheckServiceGrpcImplTest.java
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.bigtop.manager.agent.grpc.service;
+
+import org.apache.bigtop.manager.common.shell.ShellResult;
+import org.apache.bigtop.manager.common.utils.Environments;
+import org.apache.bigtop.manager.common.utils.os.TimeSyncDetection;
+import org.apache.bigtop.manager.grpc.generated.HostCheckReply;
+import org.apache.bigtop.manager.grpc.generated.HostCheckRequest;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import io.grpc.stub.StreamObserver;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.BDDMockito.then;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+
+@ExtendWith(MockitoExtension.class)
+public class HostCheckServiceGrpcImplTest {
+
+ @Mock
+ private StreamObserver<HostCheckReply> responseObserver;
+
+ private HostCheckServiceGrpcImpl hostCheckServiceGrpcImpl;
+
+ @BeforeEach
+ public void setUp() {
+ hostCheckServiceGrpcImpl = new HostCheckServiceGrpcImpl();
+ }
+
+ @Test
+ public void testCheckInDevMode() {
+ try (var environmentsMockedStatic = mockStatic(Environments.class)) {
+ // Mock environment in development mode
+
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(true);
+
+ // Call check method
+ hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class),
responseObserver);
+
+ // Verify responseObserver is correctly called
+ then(responseObserver).should(times(1)).onNext(any());
+ then(responseObserver).should(times(1)).onCompleted();
+ then(responseObserver).should(never()).onError(any());
+ }
+ }
+
+ @Test
+ public void testCheckInNonDevModeWithSuccess() {
+ try (var environmentsMockedStatic = mockStatic(Environments.class);
+ var timeSyncDetectionMockedStatic =
mockStatic(TimeSyncDetection.class)) {
+ // Mock environment not in development mode
+
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(false);
+ // Mock successful time synchronization check
+ timeSyncDetectionMockedStatic
+ .when(TimeSyncDetection::checkTimeSync)
+ .thenReturn(ShellResult.success("test"));
+
+ // Call check method
+ hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class),
responseObserver);
+
+ // Verify responseObserver is correctly called
+ then(responseObserver).should(times(1)).onNext(any());
+ then(responseObserver).should(times(1)).onCompleted();
+ then(responseObserver).should(never()).onError(any());
+ }
+ }
+
+ @Test
+ public void testCheckInNonDevModeWithError() {
+ try (var environmentsMockedStatic = mockStatic(Environments.class);
+ var timeSyncDetectionMockedStatic =
mockStatic(TimeSyncDetection.class)) {
+ // Mock environment not in development mode
+
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(false);
+ // Mock failed time synchronization check
+
timeSyncDetectionMockedStatic.when(TimeSyncDetection::checkTimeSync).thenReturn(ShellResult.fail("test"));
+
+ // Call check method
+ hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class),
responseObserver);
+
+ // Verify responseObserver is correctly called
+ then(responseObserver).should(times(1)).onNext(any());
+ then(responseObserver).should(times(1)).onCompleted();
+ then(responseObserver).should(never()).onError(any());
+ }
+ }
+
+ @Test
+ public void testCheckWithError() {
+ try (var environmentsMockedStatic = mockStatic(Environments.class);
+ var timeSyncDetectionMockedStatic =
mockStatic(TimeSyncDetection.class)) {
+ // Mock environment not in development mode
+
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(false);
+ // Mock a runtime exception during check execution
+ timeSyncDetectionMockedStatic
+ .when(TimeSyncDetection::checkTimeSync)
+ .thenThrow(new RuntimeException("Test Simulated Error"));
+
+ // Call check method
+ hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class),
responseObserver);
+
+ // Verify responseObserver is correctly called
+ then(responseObserver).should(never()).onNext(any());
+ then(responseObserver).should(never()).onCompleted();
+ then(responseObserver).should(times(1)).onError(any());
+ }
+ }
+}
diff --git
a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/grpc/service/JobCacheServiceGrpcImplTest.java
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/grpc/service/JobCacheServiceGrpcImplTest.java
index bcf5f551..e5ff466e 100644
---
a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/grpc/service/JobCacheServiceGrpcImplTest.java
+++
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/grpc/service/JobCacheServiceGrpcImplTest.java
@@ -22,6 +22,7 @@ import
org.apache.bigtop.manager.common.utils.ProjectPathUtils;
import org.apache.bigtop.manager.grpc.generated.JobCacheReply;
import org.apache.bigtop.manager.grpc.generated.JobCacheRequest;
+import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -46,18 +47,31 @@ public class JobCacheServiceGrpcImplTest {
@Mock
private StreamObserver<JobCacheReply> responseObserver;
+ private Path cacheDirPath;
+
@BeforeEach
- public void setUp() {
- // Initialize mock objects
+ public void setUp() throws Exception {
+ // Create a real temporary directory for testing
+ cacheDirPath = Files.createTempDirectory("test-cache-dir");
jobCacheServiceGrpcImpl = new JobCacheServiceGrpcImpl();
}
+ @AfterEach
+ public void tearDown() throws Exception {
+ // Delete the test directory after each test
+ if (cacheDirPath != null) {
+ Files.walk(cacheDirPath)
+ .sorted((p1, p2) -> -p1.compareTo(p2))
+ .map(Path::toFile)
+ .forEach(java.io.File::delete);
+ }
+ }
+
@Test
public void testSaveSuccess() {
// Mock the static behavior of ProjectPathUtils.getAgentCachePath
method
try (MockedStatic<ProjectPathUtils> mockedStatic =
mockStatic(ProjectPathUtils.class)) {
- String cacheDir = "mock/cache/dir";
-
mockedStatic.when(ProjectPathUtils::getAgentCachePath).thenReturn(cacheDir);
+
mockedStatic.when(ProjectPathUtils::getAgentCachePath).thenReturn(cacheDirPath.toString());
// Construct JobCacheRequest
String payloadJson = "{\"configurations\": {\"configKey\":
{\"subKey\": \"subValue\"}}}";
@@ -79,8 +93,7 @@ public class JobCacheServiceGrpcImplTest {
public void testSaveDirectoryCreationFailure() {
// Mock the static behavior of ProjectPathUtils.getAgentCachePath
method
try (MockedStatic<ProjectPathUtils> mockedStatic =
mockStatic(ProjectPathUtils.class)) {
- String cacheDir = "mock/cache/dir";
-
mockedStatic.when(ProjectPathUtils::getAgentCachePath).thenReturn(cacheDir);
+
mockedStatic.when(ProjectPathUtils::getAgentCachePath).thenReturn(cacheDirPath.toString());
// Mock Files.createDirectories to throw an exception
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
diff --git
a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/holder/SpringContextHolderTest.java
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/holder/SpringContextHolderTest.java
index e8fdd9d1..b8dd23e1 100644
---
a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/holder/SpringContextHolderTest.java
+++
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/holder/SpringContextHolderTest.java
@@ -18,25 +18,27 @@
*/
package org.apache.bigtop.manager.agent.holder;
-import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationContext;
-import java.lang.reflect.Field;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+@ExtendWith(MockitoExtension.class)
public class SpringContextHolderTest {
@Mock
- private ApplicationContext mockApplicationContext;
+ private ApplicationContext applicationContext;
- @BeforeEach
- public void setUp() throws Exception {
- MockitoAnnotations.openMocks(this);
+ @InjectMocks
+ private SpringContextHolder springContextHolder;
- // Use reflection to set the static variable applicationContext
- Field field =
SpringContextHolder.class.getDeclaredField("applicationContext");
- field.setAccessible(true);
- field.set(null, mockApplicationContext);
+ @Test
+ public void testSetApplicationContext() {
+ springContextHolder.setApplicationContext(applicationContext);
+ assertEquals(applicationContext,
SpringContextHolder.getApplicationContext());
}
}
diff --git
a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/metrics/MetricsCollectorTest.java
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/metrics/MetricsCollectorTest.java
new file mode 100644
index 00000000..6450e6aa
--- /dev/null
+++
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/metrics/MetricsCollectorTest.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.bigtop.manager.agent.metrics;
+
+import org.apache.bigtop.manager.agent.monitoring.AgentHostMonitoring;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import io.micrometer.core.instrument.MultiGauge;
+
+import static org.mockito.Mockito.mockStatic;
+
+@ExtendWith(MockitoExtension.class)
+public class MetricsCollectorTest {
+
+ @Mock
+ private MultiGauge diskMultiGauge;
+
+ @Mock
+ private MultiGauge memMultiGauge;
+
+ @Mock
+ private MultiGauge cpuMultiGauge;
+
+ @Mock
+ private MultiGauge diskIOMultiGauge;
+
+ @InjectMocks
+ private MetricsCollector metricsCollector;
+
+ @Test
+ public void testCollect() {
+ try (MockedStatic<AgentHostMonitoring> mockedStatic =
mockStatic(AgentHostMonitoring.class)) {
+ metricsCollector.collect();
+ mockedStatic.verify(() ->
AgentHostMonitoring.diskMultiGaugeUpdateData(diskMultiGauge));
+ mockedStatic.verify(() ->
AgentHostMonitoring.memMultiGaugeUpdateData(memMultiGauge));
+ mockedStatic.verify(() ->
AgentHostMonitoring.cpuMultiGaugeUpdateData(cpuMultiGauge));
+ mockedStatic.verify(() ->
AgentHostMonitoring.diskIOMultiGaugeUpdateData(diskIOMultiGauge));
+ }
+ }
+}
diff --git
a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/monitoring/AgentHostMonitoringTest.java
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/monitoring/AgentHostMonitoringTest.java
index 8fcb8cbc..fa1afeff 100644
---
a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/monitoring/AgentHostMonitoringTest.java
+++
b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/monitoring/AgentHostMonitoringTest.java
@@ -27,6 +27,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MultiGauge;
import oshi.SystemInfo;
import oshi.hardware.GlobalMemory;
@@ -73,6 +74,9 @@ class AgentHostMonitoringTest {
@Mock
private MultiGauge diskIOMultiGauge;
+ @Mock
+ private MeterRegistry registry;
+
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
@@ -175,6 +179,30 @@ class AgentHostMonitoringTest {
assertFalse(memGauge.isEmpty());
}
+ @Test
+ public void testNewDiskMultiGauge() {
+ MultiGauge result = AgentHostMonitoring.newDiskMultiGauge(registry);
+ assertNotNull(result);
+ }
+
+ @Test
+ public void testNewMemMultiGauge() {
+ MultiGauge result = AgentHostMonitoring.newMemMultiGauge(registry);
+ assertNotNull(result);
+ }
+
+ @Test
+ public void testNewCPUMultiGauge() {
+ MultiGauge result = AgentHostMonitoring.newCPUMultiGauge(registry);
+ assertNotNull(result);
+ }
+
+ @Test
+ public void testNewDiskIOMultiGauge() {
+ MultiGauge result = AgentHostMonitoring.newDiskIOMultiGauge(registry);
+ assertNotNull(result);
+ }
+
@Test
void testMultiGaugeUpdateData() {
// Create a mock Map object