|
| 1 | +package com.defold.extender.services; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +import org.apache.commons.io.FileUtils; |
| 16 | +import org.json.simple.JSONObject; |
| 17 | +import org.json.simple.parser.JSONParser; |
| 18 | +import org.json.simple.parser.ParseException; |
| 19 | +import org.junit.jupiter.api.AfterAll; |
| 20 | +import org.junit.jupiter.api.BeforeAll; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import com.defold.extender.remote.RemoteInstanceConfig; |
| 24 | +import com.defold.extender.services.HealthReporterService.OperationalStatus; |
| 25 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 26 | +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
| 27 | + |
| 28 | +import io.micrometer.tracing.propagation.Propagator; |
| 29 | +import io.micrometer.tracing.test.simple.SimpleTracer; |
| 30 | + |
| 31 | +public class HealthReporterServiceTest { |
| 32 | + private static final String OPERATIONAL_RESPONSE = JSONObject.toJSONString(Collections.singletonMap("status", OperationalStatus.Operational.toString())); |
| 33 | + private static final String UNREACHABLE_RESPONSE = JSONObject.toJSONString(Collections.singletonMap("status", OperationalStatus.Unreachable.toString())); |
| 34 | + |
| 35 | + private static Path tmpHTTPRoot = Path.of("/tmp/__health_check_http"); |
| 36 | + |
| 37 | + private static WireMockServer normalServer1; |
| 38 | + private static WireMockServer normalServer2; |
| 39 | + private static WireMockServer unreachableServer; |
| 40 | + private static WireMockServer timeoutServer; |
| 41 | + |
| 42 | + private static HealthReporterService service; |
| 43 | + |
| 44 | + @BeforeAll |
| 45 | + public static void beforeAll() throws IOException { |
| 46 | + Files.createDirectories(tmpHTTPRoot); |
| 47 | + |
| 48 | + normalServer1 = new WireMockServer(WireMockConfiguration.options() |
| 49 | + .port(9678) |
| 50 | + .withRootDirectory(tmpHTTPRoot.toString()) |
| 51 | + ); |
| 52 | + normalServer1.start(); |
| 53 | + |
| 54 | + normalServer1.stubFor(get(urlEqualTo("/health_report")) |
| 55 | + .willReturn(aResponse() |
| 56 | + .withStatus(200) |
| 57 | + .withBody(OPERATIONAL_RESPONSE) |
| 58 | + .withHeader("Content-Type", "application/json"))); |
| 59 | + |
| 60 | + normalServer2 = new WireMockServer(WireMockConfiguration.options() |
| 61 | + .port(9679) |
| 62 | + .withRootDirectory(tmpHTTPRoot.toString()) |
| 63 | + ); |
| 64 | + normalServer2.start(); |
| 65 | + |
| 66 | + normalServer2.stubFor(get(urlEqualTo("/health_report")) |
| 67 | + .willReturn(aResponse() |
| 68 | + .withStatus(200) |
| 69 | + .withBody(OPERATIONAL_RESPONSE) |
| 70 | + .withHeader("Content-Type", "application/json"))); |
| 71 | + |
| 72 | + unreachableServer = new WireMockServer(WireMockConfiguration.options() |
| 73 | + .port(9680) |
| 74 | + .withRootDirectory(tmpHTTPRoot.toString()) |
| 75 | + ); |
| 76 | + unreachableServer.start(); |
| 77 | + |
| 78 | + unreachableServer.stubFor(get(urlEqualTo("/health_report")) |
| 79 | + .willReturn(aResponse() |
| 80 | + .withStatus(200) |
| 81 | + .withBody(UNREACHABLE_RESPONSE) |
| 82 | + .withHeader("Content-Type", "application/json"))); |
| 83 | + |
| 84 | + timeoutServer = new WireMockServer(WireMockConfiguration.options() |
| 85 | + .port(9681) |
| 86 | + .withRootDirectory(tmpHTTPRoot.toString()) |
| 87 | + ); |
| 88 | + timeoutServer.start(); |
| 89 | + |
| 90 | + timeoutServer.stubFor(get(urlEqualTo("/health_report")) |
| 91 | + .willReturn(aResponse() |
| 92 | + .withFixedDelay(15000) |
| 93 | + .withStatus(200) |
| 94 | + .withBody(OPERATIONAL_RESPONSE) |
| 95 | + .withHeader("Content-Type", "application/json"))); |
| 96 | + |
| 97 | + service = new HealthReporterService(Optional.empty(), new SimpleTracer(), Propagator.NOOP); |
| 98 | + service.connectionTimeout = 5000; |
| 99 | + } |
| 100 | + |
| 101 | + @AfterAll |
| 102 | + public static void afterAll() throws IOException { |
| 103 | + if (normalServer1 != null) { |
| 104 | + normalServer1.stop(); |
| 105 | + } |
| 106 | + if (normalServer2 != null) { |
| 107 | + normalServer2.stop(); |
| 108 | + } |
| 109 | + if (unreachableServer != null) { |
| 110 | + unreachableServer.stop(); |
| 111 | + } |
| 112 | + if (timeoutServer != null) { |
| 113 | + timeoutServer.stop(); |
| 114 | + } |
| 115 | + |
| 116 | + FileUtils.deleteDirectory(tmpHTTPRoot.toFile()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testSingleNode() { |
| 121 | + String result = service.collectHealthReport(false, Map.of()); |
| 122 | + assertEquals(result, OPERATIONAL_RESPONSE); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testRemoteNodesNormal() throws ParseException { |
| 127 | + Map<String, RemoteInstanceConfig> conf = Map.of( |
| 128 | + "linux-latest", new RemoteInstanceConfig("http://localhost:9678", "linux-latest", true), |
| 129 | + "emsdk-406", new RemoteInstanceConfig("http://localhost:9679", "emsdk-406", true) |
| 130 | + ); |
| 131 | + JSONObject expected = new JSONObject(Map.of( |
| 132 | + "linux", "Operational", |
| 133 | + "emsdk", "Operational" |
| 134 | + )); |
| 135 | + String response = service.collectHealthReport(true, conf); |
| 136 | + JSONParser jsonParser = new JSONParser(); |
| 137 | + JSONObject result = (JSONObject)jsonParser.parse(response); |
| 138 | + assertEquals(expected, result); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testRemoteNodesUnreachable() { |
| 143 | + Map<String, RemoteInstanceConfig> conf = Map.of( |
| 144 | + "android-latest", new RemoteInstanceConfig("http://localhost:9680", "android-latest", true) |
| 145 | + ); |
| 146 | + Map<String, String> expected = Map.of( |
| 147 | + "android", "Unreachable" |
| 148 | + ); |
| 149 | + String result = service.collectHealthReport(true, conf); |
| 150 | + assertEquals(JSONObject.toJSONString(expected), result); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testRemoteNodesTimeout() { |
| 155 | + Map<String, RemoteInstanceConfig> conf = Map.of( |
| 156 | + "windows-latest", new RemoteInstanceConfig("http://localhost:9681", "windows-latest", true) |
| 157 | + ); |
| 158 | + Map<String, Object> expected = Map.of( |
| 159 | + "windows", "Unreachable" |
| 160 | + ); |
| 161 | + String result = service.collectHealthReport(true, conf); |
| 162 | + assertEquals(JSONObject.toJSONString(expected), result); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void testRemoteNodesNotFullyOperational() { |
| 167 | + Map<String, RemoteInstanceConfig> conf = Map.of( |
| 168 | + "android-ndk25", new RemoteInstanceConfig("http://localhost:9679", "android-ndk25", true), |
| 169 | + "android-latest", new RemoteInstanceConfig("http://localhost:9681", "android-latest", true) |
| 170 | + ); |
| 171 | + Map<String, Object> expected = Map.of( |
| 172 | + "android", "NotFullyOperational" |
| 173 | + ); |
| 174 | + String result = service.collectHealthReport(true, conf); |
| 175 | + assertEquals(JSONObject.toJSONString(expected), result); |
| 176 | + } |
| 177 | +} |
0 commit comments