-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BIGTOP-4355: Add and optimize ut cases for agent module (#173)
- Loading branch information
Showing
8 changed files
with
495 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/AgentApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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[] {})); | ||
} | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
...t/src/test/java/org/apache/bigtop/manager/agent/grpc/interceptor/TaskInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
.../test/java/org/apache/bigtop/manager/agent/grpc/service/HostCheckServiceGrpcImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.