From 513c89307bd28bc37b6233243b2204098b95817a Mon Sep 17 00:00:00 2001 From: Sarthak Aggarwal Date: Thu, 25 Jan 2024 15:06:58 +0530 Subject: [PATCH] fix test Signed-off-by: Sarthak Aggarwal --- .github/actions/run-bwc-suite/action.yaml | 2 +- bwc-test/build.gradle | 4 +- .../bwc/CustomCodecsCompatibilityIT.java | 152 +++++++----------- 3 files changed, 60 insertions(+), 98 deletions(-) diff --git a/.github/actions/run-bwc-suite/action.yaml b/.github/actions/run-bwc-suite/action.yaml index 2186df7..4c5e7ea 100644 --- a/.github/actions/run-bwc-suite/action.yaml +++ b/.github/actions/run-bwc-suite/action.yaml @@ -47,7 +47,7 @@ runs: -Dtests.opensearch.username=${{ inputs.username }} -Dtests.opensearch.password=${{ inputs.password }} -Dbwc.version.previous=${{ steps.build-previous.outputs.built-version }} - -Dbwc.version.next=${{ steps.build-next.outputs.built-version }} -i --info --stacktrace + -Dbwc.version.next=${{ steps.build-next.outputs.built-version }} --debug - uses: alehechka/upload-tartifact@v2 if: always() diff --git a/bwc-test/build.gradle b/bwc-test/build.gradle index 2fa8f40..a98edca 100644 --- a/bwc-test/build.gradle +++ b/bwc-test/build.gradle @@ -257,7 +257,7 @@ tasks.register("${baseName}#fullRestartClusterTask", StandaloneRestIntegTestTask // A bwc test suite which runs all the bwc tasks combined. task bwcTestSuite(type: StandaloneRestIntegTestTask) { exclude '**/**' // Do not run any tests as part of this aggregate task - dependsOn tasks.named("${baseName}#mixedClusterTask") +// dependsOn tasks.named("${baseName}#mixedClusterTask") // dependsOn tasks.named("${baseName}#rollingUpgradeClusterTask") -// dependsOn tasks.named("${baseName}#fullRestartClusterTask") + dependsOn tasks.named("${baseName}#fullRestartClusterTask") } diff --git a/bwc-test/src/test/java/org/opensearch/customcodecs/bwc/CustomCodecsCompatibilityIT.java b/bwc-test/src/test/java/org/opensearch/customcodecs/bwc/CustomCodecsCompatibilityIT.java index ddc33c7..56607f2 100644 --- a/bwc-test/src/test/java/org/opensearch/customcodecs/bwc/CustomCodecsCompatibilityIT.java +++ b/bwc-test/src/test/java/org/opensearch/customcodecs/bwc/CustomCodecsCompatibilityIT.java @@ -15,9 +15,9 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.UUID; import javax.net.ssl.SSLEngine; +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; @@ -31,12 +31,10 @@ import org.apache.hc.core5.http.nio.ssl.TlsStrategy; import org.apache.hc.core5.reactor.ssl.TlsDetails; import org.apache.hc.core5.ssl.SSLContextBuilder; + import org.junit.AfterClass; -import org.junit.Assume; import org.junit.Before; - import org.opensearch.client.Response; -import org.opensearch.client.ResponseException; import org.opensearch.client.RestClient; import org.opensearch.client.RestClientBuilder; import org.opensearch.cluster.metadata.IndexMetadata; @@ -47,49 +45,27 @@ import org.opensearch.customcodecs.bwc.helper.RestHelper; import org.opensearch.test.rest.OpenSearchRestTestCase; -import static org.hamcrest.Matchers.anyOf; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasKey; import static org.opensearch.client.RestClientBuilder.DEFAULT_MAX_CONN_PER_ROUTE; import static org.opensearch.client.RestClientBuilder.DEFAULT_MAX_CONN_TOTAL; +@ThreadLeakScope(ThreadLeakScope.Scope.NONE) public class CustomCodecsCompatibilityIT extends OpenSearchRestTestCase { - private ClusterType CLUSTER_TYPE; - private String CLUSTER_NAME; - private static RestClient testUserRestClient = null; - public void testCreateIndexWithZstdCodec() throws IOException { - System.out.println("RUNNING TEST"); -// final String index = "custom-codecs-test-index" + UUID.randomUUID(); -// -// // creating index -// createIndex( -// index, -// Settings.builder() -// .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) -// .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) -// .put("index.codec", randomFrom("zstd", "zstd_no_dict")) -// .put("index.codec.compression_level", randomIntBetween(1, 6)) -// .build() -// ); -// -// try { -// ensureGreen(index); -// } finally { -// deleteIndex(index); -// } + @Before + public void testSetup() throws IOException { + testUserRestClient = buildClient(super.restClientSettings(), super.getClusterHosts().toArray(new HttpHost[0])); } -// @Override -// protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOException { -// RestClientBuilder builder = RestClient.builder(hosts); -// configureHttpOrHttpsClient(builder, settings); -// builder.setStrictDeprecationMode(true); -// return builder.build(); -// } + + @Override + protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOException { + RestClientBuilder builder = RestClient.builder(hosts); + configureHttpOrHttpsClient(builder, settings); + builder.setStrictDeprecationMode(true); + return builder.build(); + } protected void configureHttpOrHttpsClient(RestClientBuilder builder, Settings settings) throws IOException { configureClient(builder, settings); @@ -166,35 +142,33 @@ protected final boolean preserveReposUponCompletion() { @Override protected final Settings restClientSettings() { return Settings.builder() - .put(super.restClientSettings()) - // increase the timeout here to 90 seconds to handle long waits for a green - // cluster health. the waits for green need to be longer than a minute to - // account for delayed shards - .put(OpenSearchRestTestCase.CLIENT_SOCKET_TIMEOUT, "90s") - .build(); + .put(super.restClientSettings()) + // increase the timeout here to 90 seconds to handle long waits for a green + // cluster health. the waits for green need to be longer than a minute to + // account for delayed shards + .put(OpenSearchRestTestCase.CLIENT_SOCKET_TIMEOUT, "90s") + .build(); } - /** * Tests backward compatibility by created a test user and role with DLS, FLS and masked field settings. Ingests * data into a test index and runs a matchAll query against the same. */ -// public void testDataIngestionAndSearchBackwardsCompatibility() throws Exception { -// String round = System.getProperty("tests.rest.bwcsuite_round"); -// String index = "test_index"; -// if (round.equals("old")) { -//// createTestRoleIfNotExists(TEST_ROLE); -//// createUserIfNotExists(TEST_USER, TEST_PASSWORD, TEST_ROLE); -// createIndexIfNotExists(index); -// } -// ingestData(index); -// searchMatchAll(index); -// } + public void testDataIngestionAndSearchBackwardsCompatibility() throws Exception { + String round = System.getProperty("tests.rest.bwcsuite_round"); + String index = "test-custom-codec-index"; + if (round.equals("old")) { + createIndexIfNotExists(index); + } + ingestData(index); + searchMatchAll(index); + } /** * Ingests data into the test index + * * @param index index to ingest data into */ @@ -214,10 +188,10 @@ private void ingestData(String index) throws IOException { bulkRequestBody.append(objectMapper.writeValueAsString(Song.randomSong().asJson()) + "\n"); } List responses = RestHelper.requestAgainstAllNodes( - testUserRestClient, - "POST", - "_bulk?refresh=wait_for", - RestHelper.toHttpEntity(bulkRequestBody.toString()) + testUserRestClient, + "POST", + "_bulk?refresh=wait_for", + RestHelper.toHttpEntity(bulkRequestBody.toString()) ); responses.forEach(r -> assertEquals(200, r.getStatusLine().getStatusCode())); } @@ -225,6 +199,7 @@ private void ingestData(String index) throws IOException { /** * Runs a matchAll query against the test index + * * @param index index to search */ private void searchMatchAll(String index) throws IOException { @@ -232,57 +207,44 @@ private void searchMatchAll(String index) throws IOException { int numberOfRequests = Randomness.get().nextInt(10); while (numberOfRequests-- > 0) { List responses = RestHelper.requestAgainstAllNodes( - testUserRestClient, - "POST", - index + "/_search", - RestHelper.toHttpEntity(matchAllQuery) + testUserRestClient , + "POST", + index + "/_search", + RestHelper.toHttpEntity(matchAllQuery) ); responses.forEach(r -> assertEquals(200, r.getStatusLine().getStatusCode())); } } - /** - * Checks if a resource at the specified URL exists - * @param url of the resource to be checked for existence - * @return true if the resource exists, false otherwise - */ - - private boolean resourceExists(String url) throws IOException { - try { - RestHelper.get(adminClient(), url); - return true; - } catch (ResponseException e) { - if (e.getResponse().getStatusLine().getStatusCode() == 404) { - return false; - } else { - throw e; - } - } - } - /** * Creates a test index if it does not exist already + * * @param index index to create */ private void createIndexIfNotExists(String index) throws IOException { - String settings = "{\n" - + " \"settings\": {\n" - + " \"index\": {\n" - + " \"number_of_shards\": 3,\n" - + " \"number_of_replicas\": 1\n" - + " }\n" - + " }\n" - + "}"; - if (!resourceExists(index)) { - Response response = RestHelper.makeRequest(client(), "PUT", index, RestHelper.toHttpEntity(settings)); - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + + if (indexExists(index)) { + return; } + + // creating index + createIndex( + index, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put("index.codec", randomFrom("zstd", "zstd_no_dict")) + .put("index.codec.compression_level", randomIntBetween(1, 6)) + .build() + ); + ensureGreen(index); } @AfterClass public static void cleanUp() throws IOException { OpenSearchRestTestCase.closeClients(); - IOUtils.close(testUserRestClient); + IOUtils.close(); } + }