|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.http; |
| 10 | + |
| 11 | +import org.apache.hc.core5.http.ParseException; |
| 12 | +import org.apache.hc.core5.http.io.entity.EntityUtils; |
| 13 | +import org.opensearch.client.Request; |
| 14 | +import org.opensearch.client.Response; |
| 15 | +import org.opensearch.client.RestClient; |
| 16 | +import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope; |
| 17 | +import org.opensearch.test.OpenSearchIntegTestCase.Scope; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import static org.apache.hc.core5.http.HttpStatus.SC_OK; |
| 22 | +import static org.hamcrest.Matchers.containsString; |
| 23 | + |
| 24 | +@ClusterScope(scope = Scope.SUITE, supportsDedicatedMasters = false, numDataNodes = 5, numClientNodes = 0) |
| 25 | +public class HttpCatIT extends HttpSmokeTestCase { |
| 26 | + |
| 27 | + public void testdoCatRequest() throws IOException { |
| 28 | + try (RestClient restClient = getRestClient()) { |
| 29 | + int nodesCount = restClient.getNodes().size(); |
| 30 | + assertEquals(5, nodesCount); |
| 31 | + |
| 32 | + // to make sure the timeout is working |
| 33 | + for (int i = 0; i < 5; i++) { |
| 34 | + sendRequest(restClient, 30, nodesCount); |
| 35 | + } |
| 36 | + |
| 37 | + // no timeout |
| 38 | + for (int i = 0; i < 5; i++) { |
| 39 | + sendRequest(restClient, -1, nodesCount); |
| 40 | + } |
| 41 | + |
| 42 | + for (int i = 1; i < 5; i++) { |
| 43 | + long timeout = randomInt(300); |
| 44 | + sendRequest(restClient, timeout, nodesCount); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private void sendRequest(RestClient restClient, long timeout, int nodesCount) { |
| 50 | + Request nodesRequest; |
| 51 | + if (timeout < 0) { |
| 52 | + nodesRequest = new Request("GET", "/_cat/nodes"); |
| 53 | + } else { |
| 54 | + nodesRequest = new Request("GET", "/_cat/nodes?timeout=" + timeout + "ms"); |
| 55 | + } |
| 56 | + try { |
| 57 | + Response response = restClient.performRequest(nodesRequest); |
| 58 | + assertEquals(SC_OK, response.getStatusLine().getStatusCode()); |
| 59 | + String result = EntityUtils.toString(response.getEntity()); |
| 60 | + String[] NodeInfos = result.split("\n"); |
| 61 | + assertEquals(nodesCount, NodeInfos.length); |
| 62 | + } catch (IOException | ParseException e) { |
| 63 | + // it means that it costs too long to get ClusterState from the master. |
| 64 | + assertThat(e.getMessage(), containsString("There is not enough time to obtain nodesInfo metric from the cluster manager")); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments