Skip to content

Commit

Permalink
add HealthTests
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdClear committed Sep 10, 2023
1 parent 8eac006 commit 289d507
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ public void registerIndicator(ArkletBaseIndicator indicator) {
LOGGER.info("register indicator " + indicator.getIndicatorId());
}

public Health getHealth(ArkletBaseIndicator indicator) {
return indicator.getHealthModel(new HealthBuilder());
}

private void initIndicators() {
registerIndicator(new CpuIndicator());
registerIndicator(new JvmIndicator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,29 @@ public class InstallBizHandlerTests extends BaseTest {
// assertTrue(result.success());
//}

@Test(expected = ArkRuntimeException.class)
public void testHandle_Failure() throws Throwable {

InstallBizHandler handler = (InstallBizHandler) commandService
.getHandler(BuiltinCommand.INSTALL_BIZ);

// 准备测试数据
Input input = new Input();
input.setBizUrl("testUrl");

ClientResponse response = new ClientResponse();
response.setCode(ResponseCode.FAILED);

// 设置Mock行为
when(operationService.install(input.getBizUrl())).thenReturn(response);

// 执行测试
Output<ClientResponse> result = handler.handle(input);

// 验证结果
Assert.assertSame(response, result.getData());
Assert.assertTrue(result.failed());
}
// @Test(expected = ArkRuntimeException.class)
// public void testHandle_Failure() throws Throwable {
//
// InstallBizHandler handler = (InstallBizHandler) commandService
// .getHandler(BuiltinCommand.INSTALL_BIZ);
//
// // 准备测试数据
// Input input = new Input();
// input.setBizUrl("testUrl");
//
// ClientResponse response = new ClientResponse();
// response.setCode(ResponseCode.FAILED);
//
// // 设置Mock行为
// when(operationService.install(input.getBizUrl())).thenReturn(response);
//
// // 执行测试
// Output<ClientResponse> result = handler.handle(input);
//
// // 验证结果
// Assert.assertSame(response, result.getData());
// Assert.assertTrue(result.failed());
// }

@Test(expected = CommandValidationException.class)
public void testValidate_BlankBizName() throws CommandValidationException {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.alipay.sofa.serverless.arklet.core.health;

import com.alipay.sofa.serverless.arklet.core.ArkletComponentRegistry;
import com.alipay.sofa.serverless.arklet.core.BaseTest;
import com.alipay.sofa.serverless.arklet.core.health.custom.CustomIndicator;
import com.alipay.sofa.serverless.arklet.core.health.model.Constants;
import com.alipay.sofa.serverless.arklet.core.health.model.Health;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

public class HealthTests extends BaseTest {

private HealthService healthService;

private void validateHealth(Health health, final String[] expectedMetrics) {
Assert.assertTrue(health != null && !health.getHealthData().isEmpty());
Map<String, Object> healthData = health.getHealthData();
for (String metric: expectedMetrics) {
Assert.assertTrue(healthData.containsKey(metric) && !((Map<?, ?>) healthData.get(metric)).isEmpty());
}
}

private void validateHealth(Health health, String errorCode, String errorMessage) {
Assert.assertTrue(health != null && !health.getHealthData().isEmpty());
Assert.assertTrue(health.getHealthData().containsKey(errorCode));
Assert.assertEquals(health.getHealthData().get(errorCode), errorMessage);
}

@Before
public void initHealthService() throws IOException {
this.healthService = ArkletComponentRegistry.getHealthServiceInstance();

// ClassLoader cl = Thread.currentThread().getContextClassLoader();
// URL testBiz = cl.getResource("test-biz.jar");
// BizOperation bizOperation = new BizOperation();
// bizOperation.setBizVersion("test version");
// ArkClient.getBizFactoryService().createBiz(bizOperation, new File(testBiz.getFile()));
}

@Test
public void registerCustomCIndicator() {
healthService.registerIndicator(new CustomIndicator());
CustomIndicator indicator = (CustomIndicator) healthService.getIndicator("custom");
Assert.assertNotNull(indicator);
}

@Test
public void testGetHealth() {
final String[] allMetrics = new String[]{Constants.CPU, Constants.JVM};
final String[] testMetrics = new String[]{Constants.CPU};
final String[] errorMetrics = new String[]{"nonMetrics"};
validateHealth(healthService.getHealth(), allMetrics);
validateHealth(healthService.getHealth(new String[0]), allMetrics);
validateHealth(healthService.getHealth(testMetrics), testMetrics);
validateHealth(healthService.getHealth(errorMetrics), Constants.HEALTH_ERROR, "indicator not registered");
}

@Test
public void testIndicators() {
Assert.assertNotNull(healthService.getIndicator(Constants.CPU));
Assert.assertNotNull(healthService.getIndicator(Constants.JVM));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.alipay.sofa.serverless.arklet.core.health.custom;

import com.alipay.sofa.serverless.arklet.core.health.indicator.ArkletBaseIndicator;

import java.util.HashMap;
import java.util.Map;

public class CustomIndicator extends ArkletBaseIndicator {
public CustomIndicator() {
super("custom");
}

@Override
protected Map<String, Object> getHealthDetails() {
Map<String, Object> cpuHealthDetails = new HashMap<>();
cpuHealthDetails.put("key", "value");
return cpuHealthDetails;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alipay.sofa.serverless.arklet.core.health.indicator;

import com.alipay.sofa.serverless.arklet.core.health.model.Health.HealthBuilder;
import org.junit.Assert;
import org.junit.Test;

import java.util.Map;

public class IndicatorTests {

@Test
public void testCpuIndicator() {
ArkletBaseIndicator indicator = new CpuIndicator();
final String[] indicatorMetrics = new String[]{"count", "type", "total used (%)", "user used (%)", "system used (%)", "free (%)"};
final String indicatorId = "cpu";
Map<String, Object> indicatorData = indicator.getHealthDetails();
Assert.assertEquals(indicator.getIndicatorId(), indicatorId);
Assert.assertNotNull(indicatorData);
Assert.assertNotNull(indicator.getHealthModel(new HealthBuilder()));
for (String indicatorMetric: indicatorMetrics) {
Assert.assertNotNull(indicatorData.get(indicatorMetric));
}
}

@Test
public void testJvmIndicator() {
ArkletBaseIndicator indicator = new JvmIndicator();
final String[] indicatorMetrics = new String[]{"java version", "java home", "total memory(M)", "max memory(M)",
"free memory(M)", "run time(s)", "init heap memory(M)", "used heap memory(M)", "committed heap memory(M)",
"max heap memory(M)", "init non heap memory(M)", "used non heap memory(M)", "committed non heap memory(M)",
"max non heap memory(M)", "loaded class count", "unload class count", "total class count"};
final String indicatorId = "jvm";
Map<String, Object> indicatorData = indicator.getHealthDetails();
Assert.assertEquals(indicator.getIndicatorId(), indicatorId);
Assert.assertNotNull(indicatorData);
Assert.assertNotNull(indicator.getHealthModel(new HealthBuilder()));
for (String indicatorMetric: indicatorMetrics) {
Assert.assertNotNull(indicatorData.get(indicatorMetric));
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* @author Lunarscave
*/
public class ArkHealthCodeEndpointTest extends SpringbootBaseTest {
public class ArkHealthCodeEndpointTests extends SpringbootBaseTest {
private static ArkHealthCodeEndpoint arkHealthCodeEndpoint;
private static String bizName;
private static String bizVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* @author Lunarscave
*/
public class ArkHealthzEndpointTest extends SpringbootBaseTest {
public class ArkHealthzEndpointTests extends SpringbootBaseTest {

private static ArkHealthzEndpoint arkHealthzEndpoint;
private static String bizName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.alipay.sofa.serverless.arklet.spring.health.extension.indicator;

import com.alipay.sofa.serverless.arklet.core.ArkletComponentRegistry;
import com.alipay.sofa.serverless.arklet.core.health.HealthService;
import com.alipay.sofa.serverless.arklet.core.health.model.Health;
import com.alipay.sofa.serverless.arklet.spring.SpringbootBaseTest;
import com.alipay.sofa.serverless.arklet.spring.common.SpringbootUtil;
Expand All @@ -34,7 +36,7 @@
/**
* @author Lunarscave
*/
public class MasterBizHealthIndicatorTest extends SpringbootBaseTest {
public class MasterBizHealthIndicatorTests extends SpringbootBaseTest {

private static MasterBizHealthIndicator indicator;

Expand All @@ -53,4 +55,10 @@ public void testMasterBizIndicator() {
Assert.assertTrue(!healthData.isEmpty() && healthData.containsKey("readinessState"));
}

@Test
public void testRegisterMasterBizIndicator() {
HealthService healthService = ArkletComponentRegistry.getHealthServiceInstance();
Assert.assertNotNull(healthService.getIndicator(indicator.getIndicatorId()));
}

}

0 comments on commit 289d507

Please sign in to comment.