diff --git a/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/ITSystemTest.java b/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/ITSystemTest.java index 84e1a8ab..ec3a71cb 100644 --- a/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/ITSystemTest.java +++ b/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/ITSystemTest.java @@ -61,6 +61,7 @@ public class ITSystemTest { @BeforeClass public static void beforeClass() throws Exception { client = ClusterManagerClient.create(); + Util.cleanUpExistingInstanceCluster(PROJECT_ID, ZONE, client); /** create node pool* */ NodePool nodePool = diff --git a/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/Util.java b/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/Util.java new file mode 100644 index 00000000..426bd42f --- /dev/null +++ b/google-cloud-container/src/test/java/com/google/cloud/container/v1/it/Util.java @@ -0,0 +1,53 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.container.v1.it; + +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 { + + ListClustersResponse clustersResponse = client.listClusters(projectId, zone); + List clusters = clustersResponse.getClustersList(); + + for (Cluster cluster : clusters) { + if (isCreatedBeforeThresholdTime(cluster.getCreateTime())) { + client.deleteCluster(projectId, zone, cluster.getName()); + } + } + } + + private static boolean isCreatedBeforeThresholdTime(String timestamp) { + return OffsetDateTime.parse(timestamp) + .toInstant() + .isBefore(Instant.now().minus(DELETION_THRESHOLD_TIME_HOURS, ChronoUnit.HOURS)); + } +}