From b51d29f775d155587811e96e5551683187d0ec29 Mon Sep 17 00:00:00 2001 From: Miles-Garnsey Date: Thu, 21 Sep 2023 13:07:29 +1000 Subject: [PATCH] Rewrite job status test to use direct HTTP calls and avoid using the client for build reasons. --- .../datastax/mgmtapi/NonDestructiveOpsIT.java | 68 ++++++++++++------- .../mgmtapi/helpers/NettyHttpClient.java | 30 ++++++++ 2 files changed, 75 insertions(+), 23 deletions(-) diff --git a/management-api-server/src/test/java/com/datastax/mgmtapi/NonDestructiveOpsIT.java b/management-api-server/src/test/java/com/datastax/mgmtapi/NonDestructiveOpsIT.java index 0a51475f..097106d3 100644 --- a/management-api-server/src/test/java/com/datastax/mgmtapi/NonDestructiveOpsIT.java +++ b/management-api-server/src/test/java/com/datastax/mgmtapi/NonDestructiveOpsIT.java @@ -18,8 +18,6 @@ import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; -import com.datastax.mgmtapi.client.api.DefaultApi; -import com.datastax.mgmtapi.client.invoker.ApiClient; import com.datastax.mgmtapi.helpers.IntegrationTestUtils; import com.datastax.mgmtapi.helpers.NettyHttpClient; import com.datastax.mgmtapi.resources.models.CompactRequest; @@ -34,6 +32,8 @@ import com.datastax.mgmtapi.resources.models.ScrubRequest; import com.datastax.mgmtapi.resources.models.Table; import com.datastax.mgmtapi.resources.models.TakeSnapshotRequest; +import com.datastax.mgmtapi.resources.v2.models.RepairParallelism; +import com.datastax.mgmtapi.resources.v2.models.RepairRequestResponse; import com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; @@ -1040,34 +1040,56 @@ public void ensureStatusChanges() throws Exception { assumeTrue(IntegrationTestUtils.shouldRun()); ensureStarted(); NettyHttpClient client = new NettyHttpClient(BASE_URL); - DefaultApi apiClient = new DefaultApi(new ApiClient().setBasePath(BASE_HOST)); - com.datastax.mgmtapi.client.model.RepairRequest req = - new com.datastax.mgmtapi.client.model.RepairRequest() - .keyspace("system_distributed") - .fullRepair(true) - .notifications(true) - .repairParallelism( - com.datastax.mgmtapi.client.model.RepairRequest.RepairParallelismEnum.SEQUENTIAL) - .associatedTokens( - Collections.singletonList( - new com.datastax.mgmtapi.client.model.RingRange() - .start(Long.valueOf(-1)) - .end(Long.valueOf(100)))); + + com.datastax.mgmtapi.resources.v2.models.RepairRequest req = + new com.datastax.mgmtapi.resources.v2.models.RepairRequest( + "system_distributed", + null, + true, + true, + Collections.singletonList( + new com.datastax.mgmtapi.resources.v2.models.RingRange(-1L, 100L)), + RepairParallelism.SEQUENTIAL, + null, + null); + logger.info("Sending repair request: {}", req); - String jobID = apiClient.putRepairV2(req).getRepairId(); - Integer repairID = Integer.parseInt(jobID.substring(7)); // Trimming off "repair-" prefix. + URI repairUri = new URIBuilder(BASE_PATH_V2 + "repairs").build(); + Pair repairResp = + client + .put(repairUri.toURL(), new ObjectMapper().writeValueAsString(req)) + .thenApply(this::responseAsCodeAndBody) + .join(); + String jobID = + new ObjectMapper().readValue(repairResp.getRight(), RepairRequestResponse.class).repairID; + Integer repairID = + Integer.parseInt( + jobID.substring(7) // Trimming off "repair-" prefix. + ); logger.info("Repair ID: {}", repairID); assertThat(repairID).isNotNull(); assertThat(repairID).isGreaterThan(0); - com.datastax.mgmtapi.client.model.Job status = apiClient.getJobStatus(jobID); - logger.info("Repair job status: {}", status); - assertThat(status.getStatus()).isNotNull(); - assertThat(status.getStatusChanges()).isNotNull(); - await().atMost(5, SECONDS).until(() -> status.getStatusChanges().size() > 0); + URI statusUri = + new URIBuilder(BASE_PATH_V2 + "/ops/executor/job").addParameter("job_id", jobID).build(); + Pair statusResp = + client.get(statusUri.toURL()).thenApply(this::responseAsCodeAndBody).join(); + logger.info("Repair job status: {}", statusResp); + Job jobStatus = new ObjectMapper().readValue(statusResp.getRight(), Job.class); + + assertThat(jobStatus.getStatus()).isNotNull(); + assertThat(jobStatus.getStatusChanges()).isNotNull(); await() .atMost(5, SECONDS) .until( - () -> status.getStatus() == com.datastax.mgmtapi.client.model.Job.StatusEnum.COMPLETED); + () -> { + Pair statusResp2 = + client.get(statusUri.toURL()).thenApply(this::responseAsCodeAndBody).join(); + logger.info("Repair job status: {}", statusResp); + Job jobStatus2 = new ObjectMapper().readValue(statusResp.getRight(), Job.class); + return jobStatus2.getStatusChanges().size() > 0 + && jobStatus2.getStatus() + == com.datastax.mgmtapi.resources.models.Job.JobStatus.COMPLETED; + }); } } diff --git a/management-api-server/src/test/java/com/datastax/mgmtapi/helpers/NettyHttpClient.java b/management-api-server/src/test/java/com/datastax/mgmtapi/helpers/NettyHttpClient.java index c56d4f70..027f638e 100644 --- a/management-api-server/src/test/java/com/datastax/mgmtapi/helpers/NettyHttpClient.java +++ b/management-api-server/src/test/java/com/datastax/mgmtapi/helpers/NettyHttpClient.java @@ -157,6 +157,36 @@ public CompletableFuture post( return result; } + public CompletableFuture put( + URL url, final CharSequence body, String contentType) throws UnsupportedEncodingException { + CompletableFuture result = new CompletableFuture<>(); + + if (!activeRequestFuture.compareAndSet(null, result)) + throw new RuntimeException("outstanding request"); + + DefaultFullHttpRequest request = + new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, url.getFile()); + request.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType); + request.headers().set(HttpHeaderNames.HOST, url.getHost()); + + if (body != null) { + request.content().writeBytes(body.toString().getBytes(CharsetUtil.UTF_8.name())); + request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes()); + } else { + request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0); + } + + // Send the HTTP request. + client.writeAndFlush(request); + + return result; + } + + public CompletableFuture put(URL url, final CharSequence body) + throws UnsupportedEncodingException { + return post(url, body, "application/json"); + } + public CompletableFuture delete(URL url) { return buildAndSendRequest(HttpMethod.DELETE, url); }