From 8e4378ddb38c2ff4d50b65962ce729c2b237c095 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Mon, 12 Sep 2022 11:42:45 -0400 Subject: [PATCH] chore: Fix flaky IT test cases (#8260) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: Busy wait java-container CREATE operation to finish * chore: Delete any existing java-notebook instances * chore: Use ListInstanceRequest with parent * chore: Delete gapic compute instances * chore: Use ZonedDateTime for RFC3339 with Zone * chore: Setup the correct Request object field * chore: Create new IT profile * chore: Move shared deps profile to root pom.xml * chore: Use test lifecycle phase for graalvm tests * chore: Use old pom configs * chore: Use correct path to vision resources * chore: Remove unused pom.xml * chore: Move busy wait after CREATE operation * chore: Add specific names for IT compute instances * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: Clean up compute instances names * chore: Clean up instances * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: Add checks for deletion * chore: Add missing import * chore: Add missing import for notebooks Co-authored-by: Owl Bot --- .kokoro/build.sh | 4 +- .../compute/v1/integration/BaseTest.java | 3 +- .../v1/integration/ITSmokeInstancesTest.java | 2 + .../cloud/compute/v1/integration/Util.java | 37 ++++++++++ java-container/README.md | 4 +- .../cloud/container/v1/it/ITSystemTest.java | 22 ++++-- .../google/cloud/container/v1/it/Util.java | 9 +-- .../it/ITNotebookServiceClientTest.java | 8 +- .../cloud/notebooks/v1beta1/it/Util.java | 34 +++++++++ .../google/cloud/vision/it/ITSystemTest.java | 4 +- pom.xml | 74 +++++++++---------- 11 files changed, 144 insertions(+), 57 deletions(-) create mode 100644 java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/Util.java create mode 100644 java-notebooks/google-cloud-notebooks/src/test/java/com/google/cloud/notebooks/v1beta1/it/Util.java diff --git a/.kokoro/build.sh b/.kokoro/build.sh index 869cd99014a0..342dcda9164b 100755 --- a/.kokoro/build.sh +++ b/.kokoro/build.sh @@ -156,7 +156,7 @@ case ${JOB_TYPE} in -Penable-integration-tests \ -Pnative \ -fae \ - verify + test RETURN_CODE=$? printf "Finished Unit and Integration Tests for GraalVM Native:\n%s\n" "${module_list}" else @@ -182,7 +182,7 @@ case ${JOB_TYPE} in -Penable-integration-tests \ -Pnative \ -fae \ - verify + test RETURN_CODE=$? printf "Finished Unit and Integration Tests for GraalVM Native 17:\n%s\n" "${module_list}" else diff --git a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/BaseTest.java b/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/BaseTest.java index cbe07e4ee3bd..1dae34ce376f 100644 --- a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/BaseTest.java +++ b/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/BaseTest.java @@ -22,8 +22,9 @@ public class BaseTest { protected static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId(); protected static final String DEFAULT_ZONE = "us-central1-a"; protected static final String DEFAULT_REGION = "us-west1"; + protected static final String COMPUTE_PREFIX = "it-test-compute"; public static String generateRandomName(String placeholder) { - return "gapic-" + placeholder + UUID.randomUUID().toString().substring(0, 8); + return COMPUTE_PREFIX + "-" + placeholder + "-" + UUID.randomUUID().toString().substring(0, 8); } } diff --git a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITSmokeInstancesTest.java b/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITSmokeInstancesTest.java index 33bec397c973..ac5695e692d2 100644 --- a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITSmokeInstancesTest.java +++ b/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITSmokeInstancesTest.java @@ -72,6 +72,8 @@ public static void setUp() throws IOException { instances = new ArrayList<>(); InstancesSettings instanceSettings = InstancesSettings.newBuilder().build(); instancesClient = InstancesClient.create(instanceSettings); + + Util.cleanUpComputeInstances(instancesClient, DEFAULT_PROJECT, DEFAULT_ZONE); } @Before diff --git a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/Util.java b/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/Util.java new file mode 100644 index 000000000000..6a496660dffb --- /dev/null +++ b/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/Util.java @@ -0,0 +1,37 @@ +package com.google.cloud.compute.v1.integration; + +import com.google.cloud.compute.v1.DeleteInstanceRequest; +import com.google.cloud.compute.v1.Instance; +import com.google.cloud.compute.v1.InstancesClient; +import com.google.cloud.compute.v1.InstancesClient.ListPagedResponse; +import java.time.Instant; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit; + +public class Util { + + // Cleans existing test resources if any. + private static final int DELETION_THRESHOLD_TIME_HOURS = 24; + + /** Bring down any instances that are older than 24 hours */ + public static void cleanUpComputeInstances( + InstancesClient instancesClient, String project, String zone) { + ListPagedResponse listPagedResponse = instancesClient.list(project, zone); + for (Instance instance : listPagedResponse.iterateAll()) { + if (isCreatedBeforeThresholdTime( + ZonedDateTime.parse(instance.getCreationTimestamp()).toInstant()) + && instance.getName().startsWith(BaseTest.COMPUTE_PREFIX)) { + instancesClient.deleteAsync( + DeleteInstanceRequest.newBuilder() + .setInstance(instance.getName()) + .setProject(project) + .setZone(zone) + .build()); + } + } + } + + private static boolean isCreatedBeforeThresholdTime(Instant instant) { + return instant.isBefore(Instant.now().minus(DELETION_THRESHOLD_TIME_HOURS, ChronoUnit.HOURS)); + } +} diff --git a/java-container/README.md b/java-container/README.md index 801150864ef3..1ae2a2359617 100644 --- a/java-container/README.md +++ b/java-container/README.md @@ -19,7 +19,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file: com.google.cloud libraries-bom - 26.1.0 + 26.1.1 pom import @@ -49,7 +49,7 @@ If you are using Maven without BOM, add this to your dependencies: If you are using Gradle 5.x or later, add this to your dependencies: ```Groovy -implementation platform('com.google.cloud:libraries-bom:26.1.0') +implementation platform('com.google.cloud:libraries-bom:26.1.1') implementation 'com.google.cloud:google-cloud-container' ``` diff --git a/java-container/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/ITSystemTest.java b/java-container/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/ITSystemTest.java index 085673430b3a..15204497712a 100644 --- a/java-container/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/ITSystemTest.java +++ b/java-container/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/ITSystemTest.java @@ -26,6 +26,7 @@ import com.google.container.v1.ListOperationsResponse; import com.google.container.v1.NodePool; import com.google.container.v1.Operation; +import com.google.container.v1.Operation.Status; import com.google.container.v1.ServerConfig; import java.util.List; import java.util.UUID; @@ -37,6 +38,8 @@ public class ITSystemTest { + protected static final String CONTAINER_PREFIX = "it-test-container"; + private static ClusterManagerClient client; private static Operation operation; @@ -44,9 +47,9 @@ public class ITSystemTest { private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); private static final String ZONE = "us-central1-a"; private static final String CLUSTER_NAME = - "test-cluster-" + UUID.randomUUID().toString().substring(0, 8); + CONTAINER_PREFIX + "-cluster-" + UUID.randomUUID().toString().substring(0, 8); private static final String NODE_POOL_NAME = - "test-node-pool-" + UUID.randomUUID().toString().substring(0, 8); + CONTAINER_PREFIX + "-node-pool-" + UUID.randomUUID().toString().substring(0, 8); private static final String DETAIL = "test-detail"; private static final String STATUS_MESSAGE = "test-status-message"; private static final String SELF_LINK = @@ -63,7 +66,7 @@ public static void beforeClass() throws Exception { client = ClusterManagerClient.create(); Util.cleanUpExistingInstanceCluster(PROJECT_ID, ZONE, client); - /** create node pool* */ + /* create node pool* */ NodePool nodePool = NodePool.newBuilder() .setInitialNodeCount(INITIAL_NODE_COUNT) @@ -72,7 +75,7 @@ public static void beforeClass() throws Exception { .setStatusMessage(STATUS_MESSAGE) .build(); - /** create cluster */ + /* create cluster */ Cluster cluster = Cluster.newBuilder() .setName(CLUSTER_NAME) @@ -84,13 +87,20 @@ public static void beforeClass() throws Exception { .setNetwork(NETWORK) .build(); operation = client.createCluster(PROJECT_ID, ZONE, cluster); + + Operation response = client.getOperation(PROJECT_ID, ZONE, operation.getName()); + // Busy Wait for one minute at a time until Cluster CREATE operation is complete + while (response.getStatus() != Status.DONE) { + LOG.info(String.format("Cluster CREATE Operation Status: %s", response.getStatus())); + Thread.sleep(TimeUnit.MINUTES.toMillis(1)); + response = client.getOperation(PROJECT_ID, ZONE, operation.getName()); + } LOG.info(String.format("%s cluster created successfully.", CLUSTER_NAME)); LOG.info(String.format("%s node pool created successfully.", NODE_POOL_NAME)); } @AfterClass - public static void afterClass() throws Exception { - Thread.sleep(TimeUnit.MINUTES.toMillis(5)); + public static void afterClass() { client.deleteCluster(PROJECT_ID, ZONE, CLUSTER_NAME); LOG.info(String.format("%s cluster deleted successfully.", CLUSTER_NAME)); client.close(); diff --git a/java-container/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/Util.java b/java-container/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/Util.java index 426bd42fabc4..7a09cf4cc168 100644 --- a/java-container/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/Util.java +++ b/java-container/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/Util.java @@ -19,27 +19,26 @@ import com.google.cloud.container.v1.ClusterManagerClient; import com.google.container.v1.Cluster; import com.google.container.v1.ListClustersResponse; -import java.io.IOException; import java.time.Instant; import java.time.OffsetDateTime; import java.time.temporal.ChronoUnit; import java.util.List; -import java.util.concurrent.ExecutionException; public class Util { + // Cleans existing test resources if any. private static final int DELETION_THRESHOLD_TIME_HOURS = 24; /** tear down any clusters that are older than 24 hours * */ public static void cleanUpExistingInstanceCluster( - String projectId, String zone, ClusterManagerClient client) - throws IOException, ExecutionException, InterruptedException { + String projectId, String zone, ClusterManagerClient client) { ListClustersResponse clustersResponse = client.listClusters(projectId, zone); List clusters = clustersResponse.getClustersList(); for (Cluster cluster : clusters) { - if (isCreatedBeforeThresholdTime(cluster.getCreateTime())) { + if (isCreatedBeforeThresholdTime(cluster.getCreateTime()) + && cluster.getName().startsWith(ITSystemTest.CONTAINER_PREFIX)) { client.deleteCluster(projectId, zone, cluster.getName()); } } diff --git a/java-notebooks/google-cloud-notebooks/src/test/java/com/google/cloud/notebooks/v1beta1/it/ITNotebookServiceClientTest.java b/java-notebooks/google-cloud-notebooks/src/test/java/com/google/cloud/notebooks/v1beta1/it/ITNotebookServiceClientTest.java index 5f56091c9e93..0797be1eba2f 100644 --- a/java-notebooks/google-cloud-notebooks/src/test/java/com/google/cloud/notebooks/v1beta1/it/ITNotebookServiceClientTest.java +++ b/java-notebooks/google-cloud-notebooks/src/test/java/com/google/cloud/notebooks/v1beta1/it/ITNotebookServiceClientTest.java @@ -48,13 +48,15 @@ public class ITNotebookServiceClientTest { + protected static final String NOTEBOOK_PREFIX = "it-test-notebook"; + private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); private static final String LOCATION = "us-central1-a"; private static final String PARENT = "projects/" + PROJECT_ID + "/locations/" + LOCATION; private static NotebookServiceClient client; private static final String ID = UUID.randomUUID().toString().substring(0, 8); - private static final String NOTEBOOK_INSTANCE_ID = "test-notebook-instance-id-" + ID; - private static final String ENVIRONMENT_ID = "test-environment-id-" + ID; + private static final String NOTEBOOK_INSTANCE_ID = NOTEBOOK_PREFIX + "-instance-id-" + ID; + private static final String ENVIRONMENT_ID = NOTEBOOK_PREFIX + "-environment-id-" + ID; private static final String INSTANCE_NAME = PARENT + "/instances/" + NOTEBOOK_INSTANCE_ID; private static final String ENVIRONMENT_NAME = PARENT + "/environments/" + ENVIRONMENT_ID; private static Instance expectedNotebookInstance; @@ -66,6 +68,8 @@ public class ITNotebookServiceClientTest { public static void setUp() throws IOException, ExecutionException, InterruptedException { // Create Test Notebook Instance client = NotebookServiceClient.create(); + Util.cleanUpNotebookInstances(client, PARENT); + ContainerImage containerImage = ContainerImage.newBuilder().setRepository(FieldBehavior.OPTIONAL.name()).build(); diff --git a/java-notebooks/google-cloud-notebooks/src/test/java/com/google/cloud/notebooks/v1beta1/it/Util.java b/java-notebooks/google-cloud-notebooks/src/test/java/com/google/cloud/notebooks/v1beta1/it/Util.java new file mode 100644 index 000000000000..4db68179e7dc --- /dev/null +++ b/java-notebooks/google-cloud-notebooks/src/test/java/com/google/cloud/notebooks/v1beta1/it/Util.java @@ -0,0 +1,34 @@ +package com.google.cloud.notebooks.v1beta1.it; + +import com.google.cloud.notebooks.v1beta1.DeleteInstanceRequest; +import com.google.cloud.notebooks.v1beta1.Instance; +import com.google.cloud.notebooks.v1beta1.ListInstancesRequest; +import com.google.cloud.notebooks.v1beta1.NotebookServiceClient; +import com.google.cloud.notebooks.v1beta1.NotebookServiceClient.ListInstancesPagedResponse; +import com.google.protobuf.util.Timestamps; +import java.time.Instant; +import java.time.temporal.ChronoUnit; + +public class Util { + + // Cleans existing test resources if any. + private static final int DELETION_THRESHOLD_TIME_HOURS = 24; + + /** Bring down any instances that are older than 24 hours */ + public static void cleanUpNotebookInstances(NotebookServiceClient client, String parent) { + ListInstancesPagedResponse listInstancesPagedResponse = + client.listInstances(ListInstancesRequest.newBuilder().setParent(parent).build()); + for (Instance instance : listInstancesPagedResponse.iterateAll()) { + if (isCreatedBeforeThresholdTime( + Instant.ofEpochMilli(Timestamps.toMillis(instance.getCreateTime()))) + && instance.getName().startsWith(ITNotebookServiceClientTest.NOTEBOOK_PREFIX)) { + client.deleteInstanceAsync( + DeleteInstanceRequest.newBuilder().setName(instance.getName()).build()); + } + } + } + + private static boolean isCreatedBeforeThresholdTime(Instant instant) { + return instant.isBefore(Instant.now().minus(DELETION_THRESHOLD_TIME_HOURS, ChronoUnit.HOURS)); + } +} diff --git a/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java b/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java index 517fc799a122..0cc086d8fa8e 100644 --- a/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java +++ b/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java @@ -99,8 +99,8 @@ public class ITSystemTest { private static final String ID = UUID.randomUUID().toString().substring(0, 8); // GraalVM native-image test uses the project root as working directory, not google-cloud-vision private static final String RESOURCES = - Files.exists(Paths.get("google-cloud-vision", "src", "test", "resources")) - ? "google-cloud-vision/src/test/resources/" + Files.exists(Paths.get("java-vision","google-cloud-vision", "src", "test", "resources")) + ? "java-vision/google-cloud-vision/src/test/resources/" : "src/test/resources/"; private static final String GCS_BUCKET_ENV_VAR = "GOOGLE_CLOUD_TESTS_VISION_BUCKET"; diff --git a/pom.xml b/pom.xml index 2d8f70261a4a..c8dee4f3211e 100644 --- a/pom.xml +++ b/pom.xml @@ -155,42 +155,42 @@ - - - - org.jacoco - jacoco-maven-plugin - 0.8.7 - - - - prepare-agent - - - - report - test - - report - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - ${skipUnitTests} - - - - org.apache.maven.plugins - maven-failsafe-plugin - - 1C - true - - - + + + + org.jacoco + jacoco-maven-plugin + 0.8.7 + + + + prepare-agent + + + + report + test + + report + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + ${skipUnitTests} + + + + org.apache.maven.plugins + maven-failsafe-plugin + + 1C + true + + +