|
| 1 | +/* |
| 2 | + * Copyright 2012-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.elasticsearch; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import okhttp3.mockwebserver.MockResponse; |
| 22 | +import okhttp3.mockwebserver.MockWebServer; |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import org.springframework.boot.actuate.health.Health; |
| 28 | +import org.springframework.boot.actuate.health.Status; |
| 29 | +import org.springframework.data.elasticsearch.client.ClientConfiguration; |
| 30 | +import org.springframework.data.elasticsearch.client.reactive.DefaultReactiveElasticsearchClient; |
| 31 | +import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient; |
| 32 | +import org.springframework.http.HttpHeaders; |
| 33 | +import org.springframework.http.HttpStatus; |
| 34 | +import org.springframework.http.MediaType; |
| 35 | +import org.springframework.web.reactive.function.client.WebClient; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | +import static org.assertj.core.api.Assertions.entry; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests for {@link ElasticsearchReactiveHealthIndicator} |
| 42 | + * |
| 43 | + * @author Brian Clozel |
| 44 | + */ |
| 45 | +class ElasticsearchReactiveHealthIndicatorTests { |
| 46 | + |
| 47 | + private MockWebServer server; |
| 48 | + |
| 49 | + private WebClient.Builder builder; |
| 50 | + |
| 51 | + private ElasticsearchReactiveHealthIndicator healthIndicator; |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + void setup() throws Exception { |
| 55 | + this.server = new MockWebServer(); |
| 56 | + this.server.start(); |
| 57 | + this.builder = WebClient.builder().baseUrl(this.server.url("/").toString()); |
| 58 | + ReactiveElasticsearchClient client = DefaultReactiveElasticsearchClient |
| 59 | + .create(ClientConfiguration.create(this.server.getHostName() + ":" + this.server.getPort())); |
| 60 | + this.healthIndicator = new ElasticsearchReactiveHealthIndicator(client); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterEach |
| 64 | + void shutdown() throws Exception { |
| 65 | + this.server.shutdown(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void elasticsearchIsUp() { |
| 70 | + setupMockResponse(200, "green"); |
| 71 | + Health health = this.healthIndicator.health().block(); |
| 72 | + assertThat(health.getStatus()).isEqualTo(Status.UP); |
| 73 | + assertHealthDetailsWithStatus(health.getDetails(), "green"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void elasticsearchWithYellowStatusIsUp() { |
| 78 | + setupMockResponse(200, "yellow"); |
| 79 | + Health health = this.healthIndicator.health().block(); |
| 80 | + assertThat(health.getStatus()).isEqualTo(Status.UP); |
| 81 | + assertHealthDetailsWithStatus(health.getDetails(), "yellow"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void elasticsearchIsDown() throws Exception { |
| 86 | + this.server.shutdown(); |
| 87 | + Health health = this.healthIndicator.health().block(); |
| 88 | + assertThat(health.getStatus()).isEqualTo(Status.DOWN); |
| 89 | + assertThat(health.getDetails().get("error")).asString() |
| 90 | + .contains("org.springframework.data.elasticsearch.client.NoReachableHostException"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void elasticsearchIsDownByResponseCode() { |
| 95 | + // first enqueue an OK response since the HostChecker first sends a HEAD request |
| 96 | + // to "/" |
| 97 | + this.server.enqueue(new MockResponse().setResponseCode(HttpStatus.OK.value())); |
| 98 | + this.server.enqueue(new MockResponse().setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value())); |
| 99 | + Health health = this.healthIndicator.health().block(); |
| 100 | + assertThat(health.getStatus()).isEqualTo(Status.DOWN); |
| 101 | + assertThat(health.getDetails().get("statusCode")).asString().isEqualTo("500"); |
| 102 | + assertThat(health.getDetails().get("reasonPhrase")).asString().isEqualTo("Internal Server Error"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void elasticsearchIsOutOfServiceByStatus() { |
| 107 | + setupMockResponse(200, "red"); |
| 108 | + Health health = this.healthIndicator.health().block(); |
| 109 | + assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE); |
| 110 | + assertHealthDetailsWithStatus(health.getDetails(), "red"); |
| 111 | + } |
| 112 | + |
| 113 | + private void assertHealthDetailsWithStatus(Map<String, Object> details, String status) { |
| 114 | + assertThat(details).contains(entry("cluster_name", "elasticsearch"), entry("status", status), |
| 115 | + entry("timed_out", false), entry("number_of_nodes", 1), entry("number_of_data_nodes", 1), |
| 116 | + entry("active_primary_shards", 0), entry("active_shards", 0), entry("relocating_shards", 0), |
| 117 | + entry("initializing_shards", 0), entry("unassigned_shards", 0), entry("delayed_unassigned_shards", 0), |
| 118 | + entry("number_of_pending_tasks", 0), entry("number_of_in_flight_fetch", 0), |
| 119 | + entry("task_max_waiting_in_queue_millis", 0), entry("active_shards_percent_as_number", 100.0)); |
| 120 | + } |
| 121 | + |
| 122 | + private void setupMockResponse(int responseCode, String status) { |
| 123 | + // first enqueue an OK response since the HostChecker first sends a HEAD request |
| 124 | + // to "/" |
| 125 | + this.server.enqueue(new MockResponse()); |
| 126 | + MockResponse mockResponse = new MockResponse().setResponseCode(HttpStatus.valueOf(responseCode).value()) |
| 127 | + .setBody(createJsonResult(responseCode, status)) |
| 128 | + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); |
| 129 | + this.server.enqueue(mockResponse); |
| 130 | + } |
| 131 | + |
| 132 | + private String createJsonResult(int responseCode, String status) { |
| 133 | + if (responseCode == 200) { |
| 134 | + return String.format( |
| 135 | + "{\"cluster_name\":\"elasticsearch\"," |
| 136 | + + "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1," |
| 137 | + + "\"number_of_data_nodes\":1,\"active_primary_shards\":0," |
| 138 | + + "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0," |
| 139 | + + "\"unassigned_shards\":0,\"delayed_unassigned_shards\":0," |
| 140 | + + "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0," |
| 141 | + + "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}", |
| 142 | + status); |
| 143 | + } |
| 144 | + return "{\n \"error\": \"Server Error\",\n \"status\": " + responseCode + "\n}"; |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments