From 15d02b3c1e5e2a9b36f264e75884af0cdf599ea4 Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Wed, 27 Apr 2022 20:35:28 +0530 Subject: [PATCH 01/11] docs(compute-samples): added parallel testing support --- compute/cloud-client/pom.xml | 43 ++- .../java/compute/CreateEncryptedInstance.java | 9 +- .../main/java/compute/CreateFirewallRule.java | 9 +- .../src/main/java/compute/CreateInstance.java | 9 +- .../compute/CreateInstanceFromTemplate.java | 10 +- ...eateInstanceFromTemplateWithOverrides.java | 9 +- .../java/compute/CreateInstanceTemplate.java | 16 +- .../java/compute/CreateInstancesAdvanced.java | 19 +- .../compute/CreateTemplateFromInstance.java | 9 +- .../compute/CreateTemplateWithSubnet.java | 9 +- .../main/java/compute/DeleteFirewallRule.java | 9 +- .../src/main/java/compute/DeleteInstance.java | 9 +- .../java/compute/DeleteInstanceTemplate.java | 10 +- .../main/java/compute/PatchFirewallRule.java | 9 +- .../src/main/java/compute/ResetInstance.java | 9 +- .../java/compute/SetUsageExportBucket.java | 13 +- .../java/compute/StartEncryptedInstance.java | 9 +- .../src/main/java/compute/StartInstance.java | 9 +- .../src/main/java/compute/StopInstance.java | 9 +- .../CreateInstanceWithCustomHostname.java | 9 +- .../CreateInstanceDeleteProtection.java | 10 +- .../deleteprotection/SetDeleteProtection.java | 9 +- .../CreatePreemptibleInstance.java | 10 +- .../src/test/java/compute/FirewallIT.java | 163 ++++++++++ .../java/compute/InstanceTemplatesIT.java | 37 ++- .../java/compute/InstancesAdvancedIT.java | 284 ++++++++++++++++ .../src/test/java/compute/SnippetsIT.java | 304 ++---------------- .../src/test/java/compute/Util.java | 5 +- .../CustomHostnameInstanceIT.java | 29 +- .../deleteprotection/DeleteProtectionIT.java | 31 +- .../compute/preemptible/PreemptibleIT.java | 28 +- 31 files changed, 743 insertions(+), 404 deletions(-) create mode 100644 compute/cloud-client/src/test/java/compute/FirewallIT.java create mode 100644 compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java diff --git a/compute/cloud-client/pom.xml b/compute/cloud-client/pom.xml index 46f262fc7ab..60eff0d8fdd 100644 --- a/compute/cloud-client/pom.xml +++ b/compute/cloud-client/pom.xml @@ -37,7 +37,7 @@ com.google.api gax-httpjson - 0.100.0 + 0.99.0 @@ -54,6 +54,25 @@ test 4.13.2 + + + + org.junit.jupiter + junit-jupiter-api + 5.8.2 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.8.2 + test + @@ -63,7 +82,7 @@ com.google.cloud import pom - 25.1.0 + 25.0.0 @@ -88,4 +107,24 @@ 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M6 + + + all + true + 10C + true + + **/*IT.java + + + + + + diff --git a/compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java b/compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java index c1a36efa86b..3a5e1c9b631 100644 --- a/compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java +++ b/compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java @@ -30,11 +30,13 @@ import com.google.cloud.compute.v1.Operation; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateEncryptedInstance { public static void main(String[] args) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { // TODO(developer): Replace these variables before running the sample. String project = "your-project-id"; String zone = "zone-name"; @@ -48,7 +50,7 @@ public static void main(String[] args) // in the specified project and zone. public static void createEncryptedInstance(String project, String zone, String instanceName, String diskEncryptionKey) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { /* Below are sample values that can be replaced. machineType: machine type of the VM being created. (This value uses the format zones/{zone}/machineTypes/{type_name}. @@ -111,7 +113,8 @@ public static void createEncryptedInstance(String project, String zone, String i instancesClient.insertAsync(insertInstanceRequest); // Wait for the operation to complete. - Operation response = operation.get(); + Operation response = operation.get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Instance creation failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/CreateFirewallRule.java b/compute/cloud-client/src/main/java/compute/CreateFirewallRule.java index dff25241c83..cc6a4c1d1de 100644 --- a/compute/cloud-client/src/main/java/compute/CreateFirewallRule.java +++ b/compute/cloud-client/src/main/java/compute/CreateFirewallRule.java @@ -26,11 +26,13 @@ import java.io.IOException; import java.util.UUID; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateFirewallRule { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample /* project: project ID or project number of the Cloud project you want to use. firewallRuleName: name of the rule that is created. @@ -49,7 +51,7 @@ public static void main(String[] args) // Creates a simple firewall rule allowing for incoming HTTP and // HTTPS access from the entire Internet. public static void createFirewall(String project, String firewallRuleName, String network) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { /* Initialize client that will be used to send requests. This client only needs to be created once, and can be reused for multiple requests. After completing all of your requests, call the `firewallsClient.close()` method on the client to safely @@ -78,7 +80,8 @@ public static void createFirewall(String project, String firewallRuleName, Strin .setFirewallResource(firewallRule) .setProject(project).build(); - firewallsClient.insertAsync(insertFirewallRequest).get(); + firewallsClient.insertAsync(insertFirewallRequest).get(3, TimeUnit.MINUTES); + ; System.out.println("Firewall rule created successfully -> " + firewallRuleName); } diff --git a/compute/cloud-client/src/main/java/compute/CreateInstance.java b/compute/cloud-client/src/main/java/compute/CreateInstance.java index bc45d1ac6d4..6dbd83ab83b 100644 --- a/compute/cloud-client/src/main/java/compute/CreateInstance.java +++ b/compute/cloud-client/src/main/java/compute/CreateInstance.java @@ -29,11 +29,13 @@ import com.google.cloud.compute.v1.Operation; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateInstance { public static void main(String[] args) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { // TODO(developer): Replace these variables before running the sample. String project = "your-project-id"; String zone = "zone-name"; @@ -44,7 +46,7 @@ public static void main(String[] args) // Create a new instance with the provided "instanceName" value in the specified project and zone. public static void createInstance(String project, String zone, String instanceName) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { // Below are sample values that can be replaced. // machineType: machine type of the VM being created. // * This value uses the format zones/{zone}/machineTypes/{type_name}. @@ -105,7 +107,8 @@ public static void createInstance(String project, String zone, String instanceNa insertInstanceRequest); // Wait for the operation to complete. - Operation response = operation.get(); + Operation response = operation.get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Instance creation failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplate.java b/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplate.java index 6bb7e5a216d..22b65211ede 100644 --- a/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplate.java +++ b/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplate.java @@ -25,11 +25,13 @@ import com.google.cloud.compute.v1.Operation; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateInstanceFromTemplate { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { /* TODO(developer): Replace these variables before running the sample. projectId - ID or number of the project you want to use. zone - Name of the zone you want to check, for example: us-west3-b @@ -51,7 +53,7 @@ public static void main(String[] args) // Create a new instance from template in the specified project and zone. public static void createInstanceFromTemplate(String projectId, String zone, String instanceName, String instanceTemplateUrl) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { try (InstancesClient instancesClient = InstancesClient.create()) { @@ -61,7 +63,9 @@ public static void createInstanceFromTemplate(String projectId, String zone, Str .setInstanceResource(Instance.newBuilder().setName(instanceName).build()) .setSourceInstanceTemplate(instanceTemplateUrl).build(); - Operation response = instancesClient.insertAsync(insertInstanceRequest).get(); + Operation response = instancesClient.insertAsync(insertInstanceRequest) + .get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Instance creation from template failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplateWithOverrides.java b/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplateWithOverrides.java index 27bc0f92df4..3ff0e4530cb 100644 --- a/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplateWithOverrides.java +++ b/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplateWithOverrides.java @@ -28,11 +28,13 @@ import com.google.cloud.compute.v1.Operation; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateInstanceFromTemplateWithOverrides { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { /* TODO(developer): Replace these variables before running the sample. * projectId - ID or number of the project you want to use. * zone - Name of the zone you want to check, for example: us-west3-b @@ -62,7 +64,7 @@ public static void main(String[] args) // but overrides the disk and machine type options in the template. public static void createInstanceFromTemplateWithOverrides(String projectId, String zone, String instanceName, String instanceTemplateName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { try (InstancesClient instancesClient = InstancesClient.create(); InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) { @@ -100,7 +102,8 @@ public static void createInstanceFromTemplateWithOverrides(String projectId, Str .setInstanceResource(instance) .setSourceInstanceTemplate(instanceTemplate.getSelfLink()).build(); - Operation response = instancesClient.insertAsync(insertInstanceRequest).get(); + Operation response = instancesClient.insertAsync(insertInstanceRequest) + .get(3, TimeUnit.MINUTES); if (response.hasError()) { System.out.println("Instance creation from template with overrides failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/CreateInstanceTemplate.java b/compute/cloud-client/src/main/java/compute/CreateInstanceTemplate.java index 9bc0db60222..de90f9f2076 100644 --- a/compute/cloud-client/src/main/java/compute/CreateInstanceTemplate.java +++ b/compute/cloud-client/src/main/java/compute/CreateInstanceTemplate.java @@ -29,11 +29,13 @@ import com.google.cloud.compute.v1.Operation; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateInstanceTemplate { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. // projectId: project ID or project number of the Cloud project you use. // templateName: name of the new template to create. @@ -47,7 +49,7 @@ public static void main(String[] args) instance configuration. */ public static void createInstanceTemplate(String projectId, String templateName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) { String machineType = "e2-standard-4"; @@ -85,7 +87,9 @@ public static void createInstanceTemplate(String projectId, String templateName) .setProperties(instanceProperties).build()).build(); // Create the Instance Template. - Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get(); + Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest) + .get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Instance Template creation failed ! ! " + response); @@ -97,7 +101,7 @@ public static void createInstanceTemplate(String projectId, String templateName) } public static void createInstanceTemplateWithDiskType(String projectId, String templateName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create(); GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) { @@ -122,7 +126,9 @@ public static void createInstanceTemplateWithDiskType(String projectId, String t .setProject(projectId) .setInstanceTemplateResource(instanceTemplate).build(); - Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get(); + Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest) + .get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Instance Template creation failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/CreateInstancesAdvanced.java b/compute/cloud-client/src/main/java/compute/CreateInstancesAdvanced.java index 1c6c2217ded..931399b3a28 100644 --- a/compute/cloud-client/src/main/java/compute/CreateInstancesAdvanced.java +++ b/compute/cloud-client/src/main/java/compute/CreateInstancesAdvanced.java @@ -37,6 +37,8 @@ import java.io.IOException; import java.util.Vector; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateInstancesAdvanced { // [END compute_instances_create_from_image] @@ -195,7 +197,7 @@ private static AttachedDisk diskFromSnapshot(String diskType, int diskSizeGb, bo */ private static Instance createWithDisks(String project, String zone, String instanceName, Vector disks, String machineType, String network, String subnetwork) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { try (InstancesClient instancesClient = InstancesClient.create()) { // Use the network interface provided in the networkName argument. NetworkInterface networkInterface; @@ -231,7 +233,8 @@ private static Instance createWithDisks(String project, String zone, String inst insertInstanceRequest); // Wait for the operation to complete. - Operation response = operation.get(); + Operation response = operation.get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Instance creation failed ! ! " + response); @@ -260,7 +263,7 @@ private static Instance createWithDisks(String project, String zone, String inst * @return Instance object. */ public static Instance createFromPublicImage(String project, String zone, String instanceName) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { try (ImagesClient imagesClient = ImagesClient.create()) { // List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details Image image = imagesClient.getFromFamily("debian-cloud", "debian-10"); @@ -287,7 +290,7 @@ public static Instance createFromPublicImage(String project, String zone, String */ public static Instance createFromCustomImage(String project, String zone, String instanceName, String customImage) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { String diskType = String.format("zones/%s/diskTypes/pd-standard", zone); Vector disks = new Vector<>(); disks.add(diskFromImage(diskType, 10, true, customImage)); @@ -307,7 +310,7 @@ public static Instance createFromCustomImage(String project, String zone, String * @return Instance object. */ public static Instance createWithAdditionalDisk(String project, String zone, String instanceName) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { try (ImagesClient imagesClient = ImagesClient.create()) { // List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details Image image = imagesClient.getFromFamily("debian-cloud", "debian-10"); @@ -335,7 +338,7 @@ public static Instance createWithAdditionalDisk(String project, String zone, Str */ public static Instance createFromSnapshot(String project, String zone, String instanceName, String snapshotName) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { String diskType = String.format("zones/%s/diskTypes/pd-standard", zone); Vector disks = new Vector<>(); disks.add(diskFromSnapshot(diskType, 11, true, snapshotName)); @@ -358,7 +361,7 @@ public static Instance createFromSnapshot(String project, String zone, String in */ public static Instance createWithSnapshottedDataDisk(String project, String zone, String instanceName, String snapshotName) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { try (ImagesClient imagesClient = ImagesClient.create()) { // List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details Image image = imagesClient.getFromFamily("debian-cloud", "debian-10"); @@ -389,7 +392,7 @@ public static Instance createWithSnapshottedDataDisk(String project, String zone */ public static Instance createWithSubnetwork(String project, String zone, String instanceName, String networkLink, String subnetworkLink) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { try (ImagesClient imagesClient = ImagesClient.create()) { // List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details Image image = imagesClient.getFromFamily("debian-cloud", "debian-10"); diff --git a/compute/cloud-client/src/main/java/compute/CreateTemplateFromInstance.java b/compute/cloud-client/src/main/java/compute/CreateTemplateFromInstance.java index 1e766647931..81676cefacf 100644 --- a/compute/cloud-client/src/main/java/compute/CreateTemplateFromInstance.java +++ b/compute/cloud-client/src/main/java/compute/CreateTemplateFromInstance.java @@ -28,11 +28,13 @@ import com.google.cloud.compute.v1.SourceInstanceParams; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateTemplateFromInstance { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. // projectId: project ID or project number of the Cloud project you use. // instance: the instance to base the new template on. This value uses the following format: @@ -49,7 +51,7 @@ public static void main(String[] args) // This new template specifies a different boot disk. public static void createTemplateFromInstance(String projectId, String templateName, String instance) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create(); GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) { @@ -81,7 +83,8 @@ public static void createTemplateFromInstance(String projectId, String templateN .build(); Operation operation = instanceTemplatesClient.insertCallable() - .futureCall(insertInstanceTemplateRequest).get(); + .futureCall(insertInstanceTemplateRequest).get(3, TimeUnit.MINUTES); + ; Operation response = globalOperationsClient.wait(projectId, operation.getName()); diff --git a/compute/cloud-client/src/main/java/compute/CreateTemplateWithSubnet.java b/compute/cloud-client/src/main/java/compute/CreateTemplateWithSubnet.java index f92ae6f65cd..a90a48dbd3a 100644 --- a/compute/cloud-client/src/main/java/compute/CreateTemplateWithSubnet.java +++ b/compute/cloud-client/src/main/java/compute/CreateTemplateWithSubnet.java @@ -29,11 +29,13 @@ import com.google.cloud.compute.v1.Operation; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateTemplateWithSubnet { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { /* TODO(developer): Replace these variables before running the sample. projectId: project ID or project number of the Cloud project you use. @@ -54,7 +56,7 @@ public static void main(String[] args) // Create an instance template that uses a provided subnet. public static void createTemplateWithSubnet(String projectId, String network, String subnetwork, String templateName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create(); GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) { @@ -87,7 +89,8 @@ public static void createTemplateWithSubnet(String projectId, String network, St .build(); Operation operation = instanceTemplatesClient.insertCallable() - .futureCall(insertInstanceTemplateRequest).get(); + .futureCall(insertInstanceTemplateRequest).get(3, TimeUnit.MINUTES); + ; Operation response = globalOperationsClient.wait(projectId, operation.getName()); diff --git a/compute/cloud-client/src/main/java/compute/DeleteFirewallRule.java b/compute/cloud-client/src/main/java/compute/DeleteFirewallRule.java index aa755db0883..1e755c6b666 100644 --- a/compute/cloud-client/src/main/java/compute/DeleteFirewallRule.java +++ b/compute/cloud-client/src/main/java/compute/DeleteFirewallRule.java @@ -24,11 +24,13 @@ import java.io.IOException; import java.util.UUID; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class DeleteFirewallRule { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample // project: project ID or project number of the Cloud project you want to use. // firewallRuleName: name of the firewall rule you want to delete. @@ -40,7 +42,7 @@ public static void main(String[] args) // Deletes a firewall rule from the project. public static void deleteFirewallRule(String project, String firewallRuleName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { /* Initialize client that will be used to send requests. This client only needs to be created once, and can be reused for multiple requests. After completing all of your requests, call the `firewallsClient.close()` method on the client to safely @@ -49,7 +51,8 @@ public static void deleteFirewallRule(String project, String firewallRuleName) OperationFuture operation = firewallsClient.deleteAsync(project, firewallRuleName); - operation.get(); + operation.get(3, TimeUnit.MINUTES); + ; System.out.println("Deleted firewall rule -> " + firewallRuleName); } diff --git a/compute/cloud-client/src/main/java/compute/DeleteInstance.java b/compute/cloud-client/src/main/java/compute/DeleteInstance.java index f5279aa485b..d8bfc54265e 100644 --- a/compute/cloud-client/src/main/java/compute/DeleteInstance.java +++ b/compute/cloud-client/src/main/java/compute/DeleteInstance.java @@ -24,11 +24,13 @@ import com.google.cloud.compute.v1.Operation; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class DeleteInstance { public static void main(String[] args) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { // TODO(developer): Replace these variables before running the sample. String project = "your-project-id"; String zone = "zone-name"; @@ -39,7 +41,7 @@ public static void main(String[] args) // Delete the instance specified by `instanceName` // if it's present in the given project and zone. public static void deleteInstance(String project, String zone, String instanceName) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the `instancesClient.close()` method on the client to safely @@ -57,7 +59,8 @@ public static void deleteInstance(String project, String zone, String instanceNa OperationFuture operation = instancesClient.deleteAsync( deleteInstanceRequest); // Wait for the operation to complete. - Operation response = operation.get(); + Operation response = operation.get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Instance deletion failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/DeleteInstanceTemplate.java b/compute/cloud-client/src/main/java/compute/DeleteInstanceTemplate.java index 604c4f6d1f6..d8be1e04adc 100644 --- a/compute/cloud-client/src/main/java/compute/DeleteInstanceTemplate.java +++ b/compute/cloud-client/src/main/java/compute/DeleteInstanceTemplate.java @@ -21,11 +21,13 @@ import com.google.cloud.compute.v1.Operation; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class DeleteInstanceTemplate { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. // projectId: project ID or project number of the Cloud project you use. // templateName: name of the new template to create. @@ -36,7 +38,7 @@ public static void main(String[] args) // Delete an instance template. public static void deleteInstanceTemplate(String projectId, String templateName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) { DeleteInstanceTemplateRequest deleteInstanceTemplateRequest = DeleteInstanceTemplateRequest @@ -44,7 +46,9 @@ public static void deleteInstanceTemplate(String projectId, String templateName) .setProject(projectId) .setInstanceTemplate(templateName).build(); - Operation response = instanceTemplatesClient.deleteAsync(deleteInstanceTemplateRequest).get(); + Operation response = instanceTemplatesClient.deleteAsync(deleteInstanceTemplateRequest) + .get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Instance template deletion failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/PatchFirewallRule.java b/compute/cloud-client/src/main/java/compute/PatchFirewallRule.java index d7e7f91f87a..9acc29d50c4 100644 --- a/compute/cloud-client/src/main/java/compute/PatchFirewallRule.java +++ b/compute/cloud-client/src/main/java/compute/PatchFirewallRule.java @@ -26,11 +26,13 @@ import java.io.IOException; import java.util.UUID; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class PatchFirewallRule { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample // project: project ID or project number of the Cloud project you want to use. // firewallRuleName: name of the rule you want to modify. @@ -44,7 +46,7 @@ public static void main(String[] args) // Modifies the priority of a given firewall rule. public static void patchFirewallPriority(String project, String firewallRuleName, int priority) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { /* Initialize client that will be used to send requests. This client only needs to be created once, and can be reused for multiple requests. After completing all of your requests, call the `firewallsClient.close()` method on the client to safely @@ -63,7 +65,8 @@ public static void patchFirewallPriority(String project, String firewallRuleName OperationFuture operation = firewallsClient.patchAsync( patchFirewallRequest); - operation.get(); + operation.get(3, TimeUnit.MINUTES); + ; System.out.println("Firewall Patch applied successfully ! "); } } diff --git a/compute/cloud-client/src/main/java/compute/ResetInstance.java b/compute/cloud-client/src/main/java/compute/ResetInstance.java index 480f412f699..9321cfc4e1a 100644 --- a/compute/cloud-client/src/main/java/compute/ResetInstance.java +++ b/compute/cloud-client/src/main/java/compute/ResetInstance.java @@ -25,11 +25,13 @@ import com.google.cloud.compute.v1.ResetInstanceRequest; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class ResetInstance { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. /* project: project ID or project number of the Cloud project your instance belongs to. zone: name of the zone your instance belongs to. @@ -44,7 +46,7 @@ public static void main(String[] args) // Resets a running Google Compute Engine instance (with unencrypted disks). public static void resetInstance(String project, String zone, String instanceName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { /* Initialize client that will be used to send requests. This client only needs to be created once, and can be reused for multiple requests. After completing all of your requests, call the `instancesClient.close()` method on the client to safely @@ -59,7 +61,8 @@ public static void resetInstance(String project, String zone, String instanceNam OperationFuture operation = instancesClient.resetAsync( resetInstanceRequest); - Operation response = operation.get(); + Operation response = operation.get(3, TimeUnit.MINUTES); + ; if (response.getStatus() == Status.DONE) { System.out.println("Instance reset successfully ! "); diff --git a/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java b/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java index faf7a3bf9b7..a12fb8798b7 100644 --- a/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java +++ b/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; // [END compute_usage_report_disable] // [END compute_usage_report_get] @@ -40,7 +41,7 @@ public class SetUsageExportBucket { public static void main(String[] args) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { // TODO(developer): Replace these variables before running the sample. // TODO(developer): Create a Google Cloud Storage bucket. // bucketName: Cloud Storage Bucket used to store Compute Engine usage reports. @@ -59,7 +60,7 @@ public static void main(String[] args) // This sample presents how to interpret the default value for the report name prefix parameter. public static void setUsageExportBucket(String project, String bucketName, String reportNamePrefix) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { // bucketName: Cloud Storage Bucket used to store Compute Engine usage reports. // An existing Google Cloud Storage bucket is required. @@ -89,7 +90,8 @@ public static void setUsageExportBucket(String project, String bucketName, .build()); // Wait for the operation to complete. - Operation response = operation.get(); + Operation response = operation.get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Setting usage export bucket failed ! ! " + response); @@ -143,7 +145,7 @@ public static UsageExportLocation getUsageExportBucket(String project) throws IO // Disable Compute Engine usage export bucket for the Cloud project. public static boolean disableUsageExportBucket(String project) - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { try (ProjectsClient projectsClient = ProjectsClient.create()) { @@ -158,7 +160,8 @@ public static boolean disableUsageExportBucket(String project) .build()); // Wait for the operation to complete. - Operation response = operation.get(); + Operation response = operation.get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Disable usage export bucket failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/StartEncryptedInstance.java b/compute/cloud-client/src/main/java/compute/StartEncryptedInstance.java index 7dc0eb70102..8bb5883eb83 100644 --- a/compute/cloud-client/src/main/java/compute/StartEncryptedInstance.java +++ b/compute/cloud-client/src/main/java/compute/StartEncryptedInstance.java @@ -30,11 +30,13 @@ import com.google.cloud.compute.v1.StartWithEncryptionKeyInstanceRequest; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class StartEncryptedInstance { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. /* project: project ID or project number of the Cloud project your instance belongs to. zone: name of the zone your instance belongs to. @@ -54,7 +56,7 @@ public static void main(String[] args) // Starts a stopped Google Compute Engine instance (with encrypted disks). public static void startEncryptedInstance(String project, String zone, String instanceName, String key) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { /* Initialize client that will be used to send requests. This client only needs to be created once, and can be reused for multiple requests. After completing all of your requests, call the `instancesClient.close()` method on the client to safely @@ -94,7 +96,8 @@ public static void startEncryptedInstance(String project, String zone, String in OperationFuture operation = instancesClient.startWithEncryptionKeyAsync( encryptionKeyInstanceRequest); - Operation response = operation.get(); + Operation response = operation.get(3, TimeUnit.MINUTES); + ; if (response.getStatus() == Status.DONE) { System.out.println("Encrypted instance started successfully ! "); diff --git a/compute/cloud-client/src/main/java/compute/StartInstance.java b/compute/cloud-client/src/main/java/compute/StartInstance.java index 4bcf20de89e..2b8f000ec5f 100644 --- a/compute/cloud-client/src/main/java/compute/StartInstance.java +++ b/compute/cloud-client/src/main/java/compute/StartInstance.java @@ -25,11 +25,13 @@ import com.google.cloud.compute.v1.StartInstanceRequest; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class StartInstance { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. /* project: project ID or project number of the Cloud project your instance belongs to. zone: name of the zone your instance belongs to. @@ -43,7 +45,7 @@ public static void main(String[] args) // Starts a stopped Google Compute Engine instance (with unencrypted disks). public static void startInstance(String project, String zone, String instanceName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { /* Initialize client that will be used to send requests. This client only needs to be created once, and can be reused for multiple requests. After completing all of your requests, call the `instancesClient.close()` method on the client to safely @@ -61,7 +63,8 @@ public static void startInstance(String project, String zone, String instanceNam startInstanceRequest); // Wait for the operation to complete. - Operation response = operation.get(); + Operation response = operation.get(3, TimeUnit.MINUTES); + ; if (response.getStatus() == Status.DONE) { System.out.println("Instance started successfully ! "); diff --git a/compute/cloud-client/src/main/java/compute/StopInstance.java b/compute/cloud-client/src/main/java/compute/StopInstance.java index 7fd341b5ee5..a7123e7f5c2 100644 --- a/compute/cloud-client/src/main/java/compute/StopInstance.java +++ b/compute/cloud-client/src/main/java/compute/StopInstance.java @@ -25,11 +25,13 @@ import com.google.cloud.compute.v1.StopInstanceRequest; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class StopInstance { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. /* project: project ID or project number of the Cloud project your instance belongs to. zone: name of the zone your instance belongs to. @@ -44,7 +46,7 @@ public static void main(String[] args) // Stops a started Google Compute Engine instance. public static void stopInstance(String project, String zone, String instanceName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { /* Initialize client that will be used to send requests. This client only needs to be created once, and can be reused for multiple requests. After completing all of your requests, call the `instancesClient.close()` method on the client to safely @@ -59,7 +61,8 @@ public static void stopInstance(String project, String zone, String instanceName OperationFuture operation = instancesClient.stopAsync( stopInstanceRequest); - Operation response = operation.get(); + Operation response = operation.get(3, TimeUnit.MINUTES); + ; if (response.getStatus() == Status.DONE) { System.out.println("Instance stopped successfully ! "); diff --git a/compute/cloud-client/src/main/java/compute/customhostname/CreateInstanceWithCustomHostname.java b/compute/cloud-client/src/main/java/compute/customhostname/CreateInstanceWithCustomHostname.java index 03238336edc..03f07491a37 100644 --- a/compute/cloud-client/src/main/java/compute/customhostname/CreateInstanceWithCustomHostname.java +++ b/compute/cloud-client/src/main/java/compute/customhostname/CreateInstanceWithCustomHostname.java @@ -27,11 +27,13 @@ import com.google.cloud.compute.v1.Operation; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateInstanceWithCustomHostname { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. // hostName: Custom hostname of the new VM instance. // * Custom hostnames must conform to RFC 1035 requirements for valid hostnames. @@ -45,7 +47,7 @@ public static void main(String[] args) // Creates an instance with custom hostname. public static void createInstanceWithCustomHostname(String projectId, String zone, String instanceName, String hostName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // machineType - Machine type for the VM instance specified in the following format: // * "zones/{zone}/machineTypes/{type_name}". For example: // * "zones/europe-west3-c/machineTypes/f1-micro" @@ -101,7 +103,8 @@ public static void createInstanceWithCustomHostname(String projectId, String zon .setInstanceResource(instanceResource).build(); // Wait for the create operation to complete. - Operation response = instancesClient.insertAsync(request).get(); + Operation response = instancesClient.insertAsync(request).get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.printf("Instance creation failed for instance: %s ; Response: %s ! ! ", diff --git a/compute/cloud-client/src/main/java/compute/deleteprotection/CreateInstanceDeleteProtection.java b/compute/cloud-client/src/main/java/compute/deleteprotection/CreateInstanceDeleteProtection.java index c8fafb7e5f1..71b880e2301 100644 --- a/compute/cloud-client/src/main/java/compute/deleteprotection/CreateInstanceDeleteProtection.java +++ b/compute/cloud-client/src/main/java/compute/deleteprotection/CreateInstanceDeleteProtection.java @@ -27,11 +27,13 @@ import com.google.cloud.compute.v1.Operation; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreateInstanceDeleteProtection { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. // project: project ID or project number of the Cloud project you want to use. // zone: name of the zone you want to use. For example: “us-west3-b” @@ -48,7 +50,7 @@ public static void main(String[] args) // Send an instance creation request to the Compute Engine API and wait for it to complete. public static void createInstanceDeleteProtection(String projectId, String zone, String instanceName, boolean deleteProtection) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { String machineType = String.format("zones/%s/machineTypes/e2-small", zone); String sourceImage = String @@ -98,7 +100,9 @@ public static void createInstanceDeleteProtection(String projectId, String zone, .build(); // Wait for the create operation to complete. - Operation response = instancesClient.insertAsync(insertInstanceRequest).get(); + Operation response = instancesClient.insertAsync(insertInstanceRequest) + .get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Instance creation failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/deleteprotection/SetDeleteProtection.java b/compute/cloud-client/src/main/java/compute/deleteprotection/SetDeleteProtection.java index 13df47926a0..3720261d3a4 100644 --- a/compute/cloud-client/src/main/java/compute/deleteprotection/SetDeleteProtection.java +++ b/compute/cloud-client/src/main/java/compute/deleteprotection/SetDeleteProtection.java @@ -22,11 +22,13 @@ import com.google.cloud.compute.v1.SetDeletionProtectionInstanceRequest; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class SetDeleteProtection { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. // project: project ID or project number of the Cloud project you want to use. // zone: name of the zone you want to use. For example: “us-west3-b” @@ -43,7 +45,7 @@ public static void main(String[] args) // Updates the "Delete Protection" setting of given instance. public static void setDeleteProtection(String projectId, String zone, String instanceName, boolean deleteProtection) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { try (InstancesClient instancesClient = InstancesClient.create()) { @@ -55,7 +57,8 @@ public static void setDeleteProtection(String projectId, String zone, .setDeletionProtection(deleteProtection) .build(); - instancesClient.setDeletionProtectionAsync(request).get(); + instancesClient.setDeletionProtectionAsync(request).get(3, TimeUnit.MINUTES); + ; // Retrieve the updated setting from the instance. System.out.printf("Updated Delete Protection setting: %s", instancesClient.get(projectId, zone, instanceName).getDeletionProtection()); diff --git a/compute/cloud-client/src/main/java/compute/preemptible/CreatePreemptibleInstance.java b/compute/cloud-client/src/main/java/compute/preemptible/CreatePreemptibleInstance.java index 05c2ce0c6c0..2965512eb4a 100644 --- a/compute/cloud-client/src/main/java/compute/preemptible/CreatePreemptibleInstance.java +++ b/compute/cloud-client/src/main/java/compute/preemptible/CreatePreemptibleInstance.java @@ -28,11 +28,13 @@ import com.google.cloud.compute.v1.Scheduling; import java.io.IOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class CreatePreemptibleInstance { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. // projectId: project ID or project number of the Cloud project you want to use. // zone: name of the zone you want to use. For example: “us-west3-b” @@ -47,7 +49,7 @@ public static void main(String[] args) // Send an instance creation request with preemptible settings to the Compute Engine API // and wait for it to complete. public static void createPremptibleInstance(String projectId, String zone, String instanceName) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { String machineType = String.format("zones/%s/machineTypes/e2-small", zone); String sourceImage = "projects/debian-cloud/global/images/family/debian-11"; @@ -97,7 +99,9 @@ public static void createPremptibleInstance(String projectId, String zone, Strin .build(); // Wait for the create operation to complete. - Operation response = instancesClient.insertAsync(insertInstanceRequest).get(); + Operation response = instancesClient.insertAsync(insertInstanceRequest) + .get(3, TimeUnit.MINUTES); + ; if (response.hasError()) { System.out.println("Instance creation failed ! ! " + response); diff --git a/compute/cloud-client/src/test/java/compute/FirewallIT.java b/compute/cloud-client/src/test/java/compute/FirewallIT.java new file mode 100644 index 00000000000..0929a17f80a --- /dev/null +++ b/compute/cloud-client/src/test/java/compute/FirewallIT.java @@ -0,0 +1,163 @@ +/* + * 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 compute; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; + +import com.google.api.gax.rpc.InvalidArgumentException; +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.compute.v1.FirewallsClient; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.Assert; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) +public class FirewallIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static String FIREWALL_RULE_CREATE; + private static String NETWORK_NAME; + + private ByteArrayOutputStream stdOut; + + // Check if the required environment variables are set. + public static void requireEnvVar(String envVarName) { + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) + .that(System.getenv(envVarName)).isNotEmpty(); + } + + @BeforeAll + public static void setUp() + throws IOException, InterruptedException, ExecutionException, TimeoutException { + final PrintStream out = System.out; + ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + FIREWALL_RULE_CREATE = "firewall-rule-" + UUID.randomUUID(); + NETWORK_NAME = "global/networks/default"; + + compute.CreateFirewallRule.createFirewall(PROJECT_ID, FIREWALL_RULE_CREATE, NETWORK_NAME); + TimeUnit.SECONDS.sleep(10); + + stdOut.close(); + System.setOut(out); + } + + + @AfterAll + public static void cleanup() + throws IOException, InterruptedException, ExecutionException, TimeoutException { + final PrintStream out = System.out; + ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + // Delete all instances created for testing. + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + if (!isFirewallRuleDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE)) { + DeleteFirewallRule.deleteFirewallRule(PROJECT_ID, FIREWALL_RULE_CREATE); + } + + stdOut.close(); + System.setOut(out); + } + + public static boolean isFirewallRuleDeletedByGceEnforcer(String projectId, + String firewallRule) throws IOException, ExecutionException, InterruptedException { + /* (**INTERNAL method**) + This method will prevent test failure if the firewall rule was auto-deleted by GCE Enforcer. + (Feel free to remove this method if not running on a Google-owned project.) + */ + try { + GetFirewallRule.getFirewallRule(projectId, firewallRule); + } catch (NotFoundException e) { + System.out.println("Rule already deleted ! "); + return true; + } catch (InvalidArgumentException | NullPointerException e) { + System.out.println("Rule is not ready (probably being deleted)."); + return true; + } + return false; + } + + @BeforeEach + public void beforeEach() { + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + } + + @AfterEach + public void afterEach() { + stdOut = null; + System.setOut(null); + } + + @Test + public void testListFirewallRules() + throws IOException, ExecutionException, InterruptedException { + final PrintStream out = System.out; + ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + if (!isFirewallRuleDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE)) { + compute.ListFirewallRules.listFirewallRules(PROJECT_ID); + assertThat(stdOut.toString()).contains(FIREWALL_RULE_CREATE); + } + // Clear system output to not affect other tests. + // Refrain from setting out to null. + stdOut.close(); + System.setOut(out); + } + + @Test + public void testPatchFirewallRule() + throws IOException, InterruptedException, ExecutionException, TimeoutException { + final PrintStream out = System.out; + ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + // Firewall rule is auto-deleted by GCE Enforcer within a few minutes. + if (!isFirewallRuleDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE)) { + try (FirewallsClient client = FirewallsClient.create()) { + Assert.assertEquals(1000, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority()); + compute.PatchFirewallRule.patchFirewallPriority(PROJECT_ID, FIREWALL_RULE_CREATE, 500); + TimeUnit.SECONDS.sleep(5); + Assert.assertEquals(500, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority()); + } + } + // Clear system output to not affect other tests. + // Refrain from setting out to null as it will throw NullPointer in the subsequent tests. + stdOut.close(); + System.setOut(out); + } + +} diff --git a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java index 0fb8b4bef49..b65b3f0d7df 100644 --- a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java +++ b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java @@ -27,16 +27,19 @@ import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; -import org.junit.After; -import org.junit.AfterClass; +import java.util.concurrent.TimeoutException; import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class InstanceTemplatesIT { @@ -59,8 +62,10 @@ public static void requireEnvVar(String envVarName) { .that(System.getenv(envVarName)).isNotEmpty(); } - @BeforeClass - public static void setup() throws IOException, ExecutionException, InterruptedException { + @BeforeAll + public static void setup() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + final PrintStream out = System.out; ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); @@ -78,8 +83,8 @@ public static void setup() throws IOException, ExecutionException, InterruptedEx "test-csam-inst-temp-or-" + instanceUUID; // Check for resources created >24hours which haven't been deleted in the project. - Util.cleanUpExistingInstanceTemplates("test-csam-", PROJECT_ID); - Util.cleanUpExistingInstances("test-csam-", PROJECT_ID, DEFAULT_ZONE); + // Util.cleanUpExistingInstanceTemplates("test-csam-", PROJECT_ID); + // Util.cleanUpExistingInstances("test-csam-", PROJECT_ID, DEFAULT_ZONE); // Create templates. CreateInstanceTemplate.createInstanceTemplate(PROJECT_ID, TEMPLATE_NAME); @@ -113,11 +118,13 @@ public static void setup() throws IOException, ExecutionException, InterruptedEx Assert.assertEquals( getInstance(DEFAULT_ZONE, MACHINE_NAME_CR_TEMPLATE_OR).getDisksCount(), 2); stdOut.close(); - System.setOut(null); + System.setOut(out); } - @AfterClass - public static void cleanup() throws IOException, ExecutionException, InterruptedException { + @AfterAll + public static void cleanup() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + final PrintStream out = System.out; ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); // Delete instances. @@ -135,7 +142,7 @@ public static void cleanup() throws IOException, ExecutionException, Interrupted assertThat(stdOut.toString()) .contains("Instance template deletion operation status for " + TEMPLATE_NAME_WITH_SUBNET); stdOut.close(); - System.setOut(null); + System.setOut(out); } public static Instance getInstance(String zone, String instanceName) throws IOException { @@ -144,13 +151,13 @@ public static Instance getInstance(String zone, String instanceName) throws IOEx } } - @Before + @BeforeEach public void beforeEach() { stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); } - @After + @AfterEach public void afterEach() { stdOut = null; System.setOut(null); diff --git a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java new file mode 100644 index 00000000000..3e0b621735b --- /dev/null +++ b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java @@ -0,0 +1,284 @@ +/* + * 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 compute; + +import static com.google.common.truth.Truth.assertWithMessage; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.compute.v1.Disk; +import com.google.cloud.compute.v1.DisksClient; +import com.google.cloud.compute.v1.Image; +import com.google.cloud.compute.v1.ImagesClient; +import com.google.cloud.compute.v1.Instance; +import com.google.cloud.compute.v1.Instance.Status; +import com.google.cloud.compute.v1.InstancesClient; +import com.google.cloud.compute.v1.Operation; +import com.google.cloud.compute.v1.Snapshot; +import com.google.cloud.compute.v1.SnapshotsClient; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.Assert; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) +public class InstancesAdvancedIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static String ZONE; + private static String MACHINE_NAME_PUBLIC_IMAGE; + private static String MACHINE_NAME_CUSTOM_IMAGE; + private static String MACHINE_NAME_ADDITIONAL_DISK; + private static String MACHINE_NAME_SNAPSHOT; + private static String MACHINE_NAME_SNAPSHOT_ADDITIONAL; + private static String MACHINE_NAME_SUBNETWORK; + private static Disk TEST_DISK; + private static Image TEST_IMAGE; + private static Snapshot TEST_SNAPSHOT; + private static String NETWORK_NAME; + private static String SUBNETWORK_NAME; + + private ByteArrayOutputStream stdOut; + + // Check if the required environment variables are set. + public static void requireEnvVar(String envVarName) { + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) + .that(System.getenv(envVarName)).isNotEmpty(); + } + + @BeforeAll + public static void setup() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + final PrintStream out = System.out; + ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + ZONE = "us-central1-a"; + MACHINE_NAME_PUBLIC_IMAGE = "test-instance-pub-" + UUID.randomUUID(); + MACHINE_NAME_CUSTOM_IMAGE = "test-instance-cust-" + UUID.randomUUID(); + MACHINE_NAME_ADDITIONAL_DISK = "test-instance-add-" + UUID.randomUUID(); + MACHINE_NAME_SNAPSHOT = "test-instance-snap-" + UUID.randomUUID(); + MACHINE_NAME_SNAPSHOT_ADDITIONAL = "test-instance-snapa-" + UUID.randomUUID(); + MACHINE_NAME_SUBNETWORK = "test-instance-subnet-" + UUID.randomUUID(); + NETWORK_NAME = "global/networks/default"; + SUBNETWORK_NAME = "regions/us-central1/subnetworks/default"; + + TEST_DISK = createSourceDisk(); + TEST_SNAPSHOT = createSnapshot(TEST_DISK); + TEST_IMAGE = createImage(TEST_DISK); + + // Util.cleanUpExistingInstances("test-instance", PROJECT_ID, ZONE); + + compute.CreateInstancesAdvanced.createFromPublicImage(PROJECT_ID, ZONE, + MACHINE_NAME_PUBLIC_IMAGE); + compute.CreateInstancesAdvanced.createFromCustomImage(PROJECT_ID, ZONE, + MACHINE_NAME_CUSTOM_IMAGE, TEST_IMAGE.getSelfLink()); + compute.CreateInstancesAdvanced.createWithAdditionalDisk(PROJECT_ID, ZONE, + MACHINE_NAME_ADDITIONAL_DISK); + compute.CreateInstancesAdvanced.createFromSnapshot(PROJECT_ID, ZONE, MACHINE_NAME_SNAPSHOT, + TEST_SNAPSHOT.getSelfLink()); + compute.CreateInstancesAdvanced.createWithSnapshottedDataDisk(PROJECT_ID, ZONE, + MACHINE_NAME_SNAPSHOT_ADDITIONAL, TEST_SNAPSHOT.getSelfLink()); + compute.CreateInstancesAdvanced.createWithSubnetwork(PROJECT_ID, ZONE, MACHINE_NAME_SUBNETWORK, + NETWORK_NAME, SUBNETWORK_NAME); + + TimeUnit.SECONDS.sleep(10); + stdOut.close(); + System.setOut(out); + } + + @AfterAll + public static void cleanup() + throws IOException, InterruptedException, ExecutionException, TimeoutException { + final PrintStream out = System.out; + ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + // Delete all instances created for testing. + compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_PUBLIC_IMAGE); + compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_CUSTOM_IMAGE); + compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_ADDITIONAL_DISK); + compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_SNAPSHOT); + compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_SNAPSHOT_ADDITIONAL); + compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_SUBNETWORK); + + deleteImage(TEST_IMAGE); + deleteSnapshot(TEST_SNAPSHOT); + deleteDisk(TEST_DISK); + + stdOut.close(); + System.setOut(out); + } + + private static Image getActiveDebian() + throws IOException { + try (ImagesClient imagesClient = ImagesClient.create()) { + return imagesClient.getFromFamily("debian-cloud", "debian-11"); + } + } + + private static Disk createSourceDisk() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + try (DisksClient disksClient = DisksClient.create()) { + + Disk disk = Disk.newBuilder() + .setSourceImage(getActiveDebian().getSelfLink()) + .setName("test-disk-" + UUID.randomUUID()) + .build(); + + OperationFuture operation = disksClient.insertAsync(PROJECT_ID, ZONE, + disk); + // Wait for the operation to complete. + operation.get(3, TimeUnit.MINUTES); + return disksClient.get(PROJECT_ID, ZONE, disk.getName()); + } + } + + private static void deleteDisk(Disk disk) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + try (DisksClient disksClient = DisksClient.create()) { + OperationFuture operation = disksClient.deleteAsync(PROJECT_ID, ZONE, + disk.getName()); + operation.get(3, TimeUnit.MINUTES); + } + } + + private static Snapshot createSnapshot(Disk srcDisk) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + try (SnapshotsClient snapshotsClient = SnapshotsClient.create(); + DisksClient disksClient = DisksClient.create()) { + + Snapshot snapshot = Snapshot.newBuilder() + .setName("test-snap-" + UUID.randomUUID()) + .build(); + + OperationFuture operation = disksClient.createSnapshotAsync(PROJECT_ID, + ZONE, srcDisk.getName(), + snapshot); + operation.get(3, TimeUnit.MINUTES); + return snapshotsClient.get(PROJECT_ID, snapshot.getName()); + } + } + + private static void deleteSnapshot(Snapshot snapshot) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + try (SnapshotsClient snapshotsClient = SnapshotsClient.create()) { + OperationFuture operation = snapshotsClient.deleteAsync(PROJECT_ID, + snapshot.getName()); + operation.get(3, TimeUnit.MINUTES); + } + } + + private static Image createImage(Disk srcDisk) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + try (ImagesClient imagesClient = ImagesClient.create()) { + + Image image = Image.newBuilder() + .setName("test-img-" + UUID.randomUUID()) + .setSourceDisk(srcDisk.getSelfLink()) + .build(); + + OperationFuture operation = imagesClient.insertAsync(PROJECT_ID, image); + operation.get(3, TimeUnit.MINUTES); + return imagesClient.get(PROJECT_ID, image.getName()); + } + } + + private static void deleteImage(Image image) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + try (ImagesClient imagesClient = ImagesClient.create()) { + OperationFuture operation = imagesClient.deleteAsync(PROJECT_ID, + image.getName()); + operation.get(3, TimeUnit.MINUTES); + } + } + + public static String getInstanceStatus(String instanceName) throws IOException { + try (InstancesClient instancesClient = InstancesClient.create()) { + Instance response = instancesClient.get(PROJECT_ID, ZONE, instanceName); + return response.getStatus(); + } + } + + @BeforeEach + public void beforeEach() { + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + } + + @AfterEach + public void afterEach() { + stdOut = null; + System.setOut(null); + } + + @Test + public void testCreatePublicImage() throws IOException { + // Check if the instance was successfully created during the setup. + String response = getInstanceStatus(MACHINE_NAME_PUBLIC_IMAGE); + Assert.assertEquals(response, Status.RUNNING.toString()); + } + + @Test + public void testCreateCustomImage() throws IOException { + // Check if the instance was successfully created during the setup. + String response = getInstanceStatus(MACHINE_NAME_CUSTOM_IMAGE); + Assert.assertEquals(response, Status.RUNNING.toString()); + } + + @Test + public void testCreateAdditionalDisk() throws IOException { + // Check if the instance was successfully created during the setup. + String response = getInstanceStatus(MACHINE_NAME_ADDITIONAL_DISK); + Assert.assertEquals(response, Status.RUNNING.toString()); + } + + @Test + public void testCreateFromSnapshot() throws IOException { + // Check if the instance was successfully created during the setup. + String response = getInstanceStatus(MACHINE_NAME_SNAPSHOT); + Assert.assertEquals(response, Status.RUNNING.toString()); + } + + @Test + public void testCreateFromSnapshotAdditional() throws IOException { + // Check if the instance was successfully created during the setup. + String response = getInstanceStatus(MACHINE_NAME_SNAPSHOT_ADDITIONAL); + Assert.assertEquals(response, Status.RUNNING.toString()); + } + + @Test + public void testCreateInSubnetwork() throws IOException { + // Check if the instance was successfully created during the setup. + String response = getInstanceStatus(MACHINE_NAME_SUBNETWORK); + Assert.assertEquals(response, Status.RUNNING.toString()); + } + +} diff --git a/compute/cloud-client/src/test/java/compute/SnippetsIT.java b/compute/cloud-client/src/test/java/compute/SnippetsIT.java index 673f9fc446d..6ed440b1f6a 100644 --- a/compute/cloud-client/src/test/java/compute/SnippetsIT.java +++ b/compute/cloud-client/src/test/java/compute/SnippetsIT.java @@ -20,19 +20,10 @@ import static com.google.common.truth.Truth.assertWithMessage; import com.google.api.gax.longrunning.OperationFuture; -import com.google.api.gax.rpc.InvalidArgumentException; -import com.google.api.gax.rpc.NotFoundException; -import com.google.cloud.compute.v1.Disk; -import com.google.cloud.compute.v1.DisksClient; -import com.google.cloud.compute.v1.FirewallsClient; -import com.google.cloud.compute.v1.Image; -import com.google.cloud.compute.v1.ImagesClient; import com.google.cloud.compute.v1.Instance; import com.google.cloud.compute.v1.Instance.Status; import com.google.cloud.compute.v1.InstancesClient; import com.google.cloud.compute.v1.Operation; -import com.google.cloud.compute.v1.Snapshot; -import com.google.cloud.compute.v1.SnapshotsClient; import com.google.cloud.compute.v1.UsageExportLocation; import com.google.cloud.storage.Bucket; import com.google.cloud.storage.BucketInfo; @@ -50,16 +41,18 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.IntStream; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class SnippetsIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); @@ -69,21 +62,9 @@ public class SnippetsIT { private static String MACHINE_NAME_LIST_INSTANCE; private static String MACHINE_NAME_WAIT_FOR_OP; private static String MACHINE_NAME_ENCRYPTED; - private static String MACHINE_NAME_PUBLIC_IMAGE; - private static String MACHINE_NAME_CUSTOM_IMAGE; - private static String MACHINE_NAME_ADDITIONAL_DISK; - private static String MACHINE_NAME_SNAPSHOT; - private static String MACHINE_NAME_SNAPSHOT_ADDITIONAL; - private static String MACHINE_NAME_SUBNETWORK; private static String BUCKET_NAME; private static String IMAGE_PROJECT_NAME; - private static String FIREWALL_RULE_CREATE; - private static String NETWORK_NAME; - private static String SUBNETWORK_NAME; private static String RAW_KEY; - private static Disk TEST_DISK; - private static Image TEST_IMAGE; - private static Snapshot TEST_SNAPSHOT; private ByteArrayOutputStream stdOut; @@ -93,8 +74,10 @@ public static void requireEnvVar(String envVarName) { .that(System.getenv(envVarName)).isNotEmpty(); } - @BeforeClass - public static void setUp() throws IOException, InterruptedException, ExecutionException { + @BeforeAll + public static void setUp() + throws IOException, InterruptedException, ExecutionException, TimeoutException { + final PrintStream out = System.out; ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); @@ -106,23 +89,14 @@ public static void setUp() throws IOException, InterruptedException, ExecutionEx MACHINE_NAME_LIST_INSTANCE = "my-new-test-instance" + UUID.randomUUID(); MACHINE_NAME_WAIT_FOR_OP = "my-new-test-instance" + UUID.randomUUID(); MACHINE_NAME_ENCRYPTED = "encrypted-test-instance" + UUID.randomUUID(); - MACHINE_NAME_PUBLIC_IMAGE = "test-instance-pub-" + UUID.randomUUID(); - MACHINE_NAME_CUSTOM_IMAGE = "test-instance-cust-" + UUID.randomUUID(); - MACHINE_NAME_ADDITIONAL_DISK = "test-instance-add-" + UUID.randomUUID(); - MACHINE_NAME_SNAPSHOT = "test-instance-snap-" + UUID.randomUUID(); - MACHINE_NAME_SNAPSHOT_ADDITIONAL = "test-instance-snapa-" + UUID.randomUUID(); - MACHINE_NAME_SUBNETWORK = "test-instance-subnet-" + UUID.randomUUID(); BUCKET_NAME = "my-new-test-bucket" + UUID.randomUUID(); IMAGE_PROJECT_NAME = "windows-sql-cloud"; - FIREWALL_RULE_CREATE = "firewall-rule-" + UUID.randomUUID(); - NETWORK_NAME = "global/networks/default"; - SUBNETWORK_NAME = "regions/us-central1/subnetworks/default"; RAW_KEY = getBase64EncodedKey(); // Cleanup existing stale resources. - Util.cleanUpExistingInstances("my-new-test-instance", PROJECT_ID, ZONE); - Util.cleanUpExistingInstances("encrypted-test-instance", PROJECT_ID, ZONE); - Util.cleanUpExistingInstances("test-instance-", PROJECT_ID, ZONE); + // Util.cleanUpExistingInstances("my-new-test-instance", PROJECT_ID, ZONE); + // Util.cleanUpExistingInstances("encrypted-test-instance", PROJECT_ID, ZONE); + // Util.cleanUpExistingInstances("test-instance-", PROJECT_ID, ZONE); compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME); compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME_DELETE); @@ -131,157 +105,40 @@ public static void setUp() throws IOException, InterruptedException, ExecutionEx compute.CreateEncryptedInstance .createEncryptedInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED, RAW_KEY); - TEST_DISK = createSourceDisk(); - TEST_SNAPSHOT = createSnapshot(TEST_DISK); - TEST_IMAGE = createImage(TEST_DISK); - - compute.CreateInstancesAdvanced.createFromPublicImage(PROJECT_ID, ZONE, - MACHINE_NAME_PUBLIC_IMAGE); - compute.CreateInstancesAdvanced.createFromCustomImage(PROJECT_ID, ZONE, - MACHINE_NAME_CUSTOM_IMAGE, TEST_IMAGE.getSelfLink()); - compute.CreateInstancesAdvanced.createWithAdditionalDisk(PROJECT_ID, ZONE, - MACHINE_NAME_ADDITIONAL_DISK); - compute.CreateInstancesAdvanced.createFromSnapshot(PROJECT_ID, ZONE, MACHINE_NAME_SNAPSHOT, - TEST_SNAPSHOT.getSelfLink()); - compute.CreateInstancesAdvanced.createWithSnapshottedDataDisk(PROJECT_ID, ZONE, - MACHINE_NAME_SNAPSHOT_ADDITIONAL, TEST_SNAPSHOT.getSelfLink()); - compute.CreateInstancesAdvanced.createWithSubnetwork(PROJECT_ID, ZONE, MACHINE_NAME_SUBNETWORK, - NETWORK_NAME, SUBNETWORK_NAME); - - TimeUnit.SECONDS.sleep(10); - compute.CreateFirewallRule.createFirewall(PROJECT_ID, FIREWALL_RULE_CREATE, NETWORK_NAME); TimeUnit.SECONDS.sleep(10); - // Moving the following tests to setup section as the created firewall rule is auto-deleted - // by GCE Enforcer within a few minutes. - testListFirewallRules(); - testPatchFirewallRule(); // Create a Google Cloud Storage bucket for UsageReports Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService(); storage.create(BucketInfo.of(BUCKET_NAME)); stdOut.close(); - System.setOut(null); + System.setOut(out); } - @AfterClass - public static void cleanup() throws IOException, InterruptedException, ExecutionException { + @AfterAll + public static void cleanup() + throws IOException, InterruptedException, ExecutionException, TimeoutException { + final PrintStream out = System.out; ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); // Delete all instances created for testing. requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("GOOGLE_CLOUD_PROJECT"); - if (!isFirewallRuleDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE)) { - DeleteFirewallRule.deleteFirewallRule(PROJECT_ID, FIREWALL_RULE_CREATE); - } compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED); compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME); compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_LIST_INSTANCE); - compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_PUBLIC_IMAGE); - compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_CUSTOM_IMAGE); - compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_ADDITIONAL_DISK); - compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_SNAPSHOT); - compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_SNAPSHOT_ADDITIONAL); - compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_SUBNETWORK); - - deleteImage(TEST_IMAGE); - deleteSnapshot(TEST_SNAPSHOT); - deleteDisk(TEST_DISK); - // Delete the Google Cloud Storage bucket created for usage reports. Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService(); Bucket bucket = storage.get(BUCKET_NAME); bucket.delete(); stdOut.close(); - System.setOut(null); - } - - private static Image getActiveDebian() - throws IOException { - try (ImagesClient imagesClient = ImagesClient.create()) { - return imagesClient.getFromFamily("debian-cloud", "debian-11"); - } - } - - private static Disk createSourceDisk() - throws IOException, ExecutionException, InterruptedException { - try (DisksClient disksClient = DisksClient.create()) { - - Disk disk = Disk.newBuilder() - .setSourceImage(getActiveDebian().getSelfLink()) - .setName("test-disk-" + UUID.randomUUID()) - .build(); - - OperationFuture operation = disksClient.insertAsync(PROJECT_ID, ZONE, - disk); - // Wait for the operation to complete. - operation.get(); - return disksClient.get(PROJECT_ID, ZONE, disk.getName()); - } - } - - private static void deleteDisk(Disk disk) - throws IOException, InterruptedException, ExecutionException { - try (DisksClient disksClient = DisksClient.create()) { - OperationFuture operation = disksClient.deleteAsync(PROJECT_ID, ZONE, - disk.getName()); - operation.get(); - } - } - - private static Snapshot createSnapshot(Disk srcDisk) - throws IOException, InterruptedException, ExecutionException { - try (SnapshotsClient snapshotsClient = SnapshotsClient.create(); - DisksClient disksClient = DisksClient.create()) { - - Snapshot snapshot = Snapshot.newBuilder() - .setName("test-snap-" + UUID.randomUUID()) - .build(); - - OperationFuture operation = disksClient.createSnapshotAsync(PROJECT_ID, - ZONE, srcDisk.getName(), - snapshot); - operation.get(); - return snapshotsClient.get(PROJECT_ID, snapshot.getName()); - } - } - - private static void deleteSnapshot(Snapshot snapshot) - throws IOException, InterruptedException, ExecutionException { - try (SnapshotsClient snapshotsClient = SnapshotsClient.create()) { - OperationFuture operation = snapshotsClient.deleteAsync(PROJECT_ID, - snapshot.getName()); - operation.get(); - } - } - - private static Image createImage(Disk srcDisk) - throws IOException, InterruptedException, ExecutionException { - try (ImagesClient imagesClient = ImagesClient.create()) { - - Image image = Image.newBuilder() - .setName("test-img-" + UUID.randomUUID()) - .setSourceDisk(srcDisk.getSelfLink()) - .build(); - - OperationFuture operation = imagesClient.insertAsync(PROJECT_ID, image); - operation.get(); - return imagesClient.get(PROJECT_ID, image.getName()); - } + System.setOut(out); } - private static void deleteImage(Image image) - throws IOException, InterruptedException, ExecutionException { - try (ImagesClient imagesClient = ImagesClient.create()) { - OperationFuture operation = imagesClient.deleteAsync(PROJECT_ID, - image.getName()); - operation.get(); - } - } public static String getBase64EncodedKey() { String sampleSpace = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -295,58 +152,6 @@ public static String getBase64EncodedKey() { .encodeToString(stringBuilder.toString().getBytes(StandardCharsets.US_ASCII)); } - public static void testListFirewallRules() - throws IOException, ExecutionException, InterruptedException { - final PrintStream out = System.out; - ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); - System.setOut(new PrintStream(stdOut)); - if (!isFirewallRuleDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE)) { - compute.ListFirewallRules.listFirewallRules(PROJECT_ID); - assertThat(stdOut.toString()).contains(FIREWALL_RULE_CREATE); - } - // Clear system output to not affect other tests. - // Refrain from setting out to null. - stdOut.close(); - System.setOut(out); - } - - public static void testPatchFirewallRule() - throws IOException, InterruptedException, ExecutionException { - final PrintStream out = System.out; - ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); - System.setOut(new PrintStream(stdOut)); - if (!isFirewallRuleDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE)) { - try (FirewallsClient client = FirewallsClient.create()) { - Assert.assertEquals(1000, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority()); - compute.PatchFirewallRule.patchFirewallPriority(PROJECT_ID, FIREWALL_RULE_CREATE, 500); - TimeUnit.SECONDS.sleep(5); - Assert.assertEquals(500, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority()); - } - } - // Clear system output to not affect other tests. - // Refrain from setting out to null as it will throw NullPointer in the subsequent tests. - stdOut.close(); - System.setOut(out); - } - - public static boolean isFirewallRuleDeletedByGceEnforcer(String projectId, - String firewallRule) throws IOException, ExecutionException, InterruptedException { - /* (**INTERNAL method**) - This method will prevent test failure if the firewall rule was auto-deleted by GCE Enforcer. - (Feel free to remove this method if not running on a Google-owned project.) - */ - try { - GetFirewallRule.getFirewallRule(projectId, firewallRule); - } catch (NotFoundException e) { - System.out.println("Rule already deleted ! "); - return true; - } catch (InvalidArgumentException | NullPointerException e) { - System.out.println("Rule is not ready (probably being deleted)."); - return true; - } - return false; - } - public static String getInstanceStatus(String instanceName) throws IOException { try (InstancesClient instancesClient = InstancesClient.create()) { Instance response = instancesClient.get(PROJECT_ID, ZONE, instanceName); @@ -354,13 +159,13 @@ public static String getInstanceStatus(String instanceName) throws IOException { } } - @Before + @BeforeEach public void beforeEach() { stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); } - @After + @AfterEach public void afterEach() { stdOut = null; System.setOut(null); @@ -380,47 +185,6 @@ public void testCreateEncryptedInstance() throws IOException { Assert.assertEquals(response, Status.RUNNING.toString()); } - @Test - public void testCreatePublicImage() throws IOException { - // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_PUBLIC_IMAGE); - Assert.assertEquals(response, Status.RUNNING.toString()); - } - - @Test - public void testCreateCustomImage() throws IOException { - // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_CUSTOM_IMAGE); - Assert.assertEquals(response, Status.RUNNING.toString()); - } - - @Test - public void testCreateAdditionalDisk() throws IOException { - // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_ADDITIONAL_DISK); - Assert.assertEquals(response, Status.RUNNING.toString()); - } - - @Test - public void testCreateFromSnapshot() throws IOException { - // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_SNAPSHOT); - Assert.assertEquals(response, Status.RUNNING.toString()); - } - - @Test - public void testCreateFromSnapshotAdditional() throws IOException { - // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_SNAPSHOT_ADDITIONAL); - Assert.assertEquals(response, Status.RUNNING.toString()); - } - - @Test - public void testCreateInSubnetwork() throws IOException { - // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_SUBNETWORK); - Assert.assertEquals(response, Status.RUNNING.toString()); - } @Test public void testListInstance() throws IOException { @@ -435,25 +199,27 @@ public void testListAllInstances() throws IOException { } @Test - public void testDeleteInstance() throws IOException, InterruptedException, ExecutionException { + public void testDeleteInstance() + throws IOException, InterruptedException, ExecutionException, TimeoutException { compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_DELETE); assertThat(stdOut.toString()).contains("Operation Status: DONE"); } @Test - public void testWaitForOperation() throws IOException, InterruptedException, ExecutionException { + public void testWaitForOperation() + throws IOException, InterruptedException, ExecutionException, TimeoutException { // Construct a delete request and get the operation instance. InstancesClient instancesClient = InstancesClient.create(); OperationFuture operation = instancesClient.deleteAsync(PROJECT_ID, ZONE, MACHINE_NAME_WAIT_FOR_OP); // Wait for the operation to complete. - operation.get(); + operation.get(3, TimeUnit.MINUTES); assertThat(stdOut.toString().contains("Operation Status: DONE")); } @Test public void testSetUsageBucketExportCustomPrefix() - throws IOException, InterruptedException, ExecutionException { + throws IOException, InterruptedException, ExecutionException, TimeoutException { // Set custom Report Name Prefix. String customPrefix = "my-custom-prefix"; compute.SetUsageExportBucket.setUsageExportBucket(PROJECT_ID, BUCKET_NAME, customPrefix); @@ -494,18 +260,6 @@ public void testInstanceOperations() throws IOException, ExecutionException, InterruptedException, TimeoutException { Assert.assertEquals(getInstanceStatus(MACHINE_NAME), Status.RUNNING.toString()); - // Suspending the instance. - // Once the machine is running, give it some time to fully start all processes - // before trying to suspend it. - TimeUnit.SECONDS.sleep(45); - SuspendInstance.suspendInstance(PROJECT_ID, ZONE, MACHINE_NAME); - TimeUnit.SECONDS.sleep(10); - Assert.assertEquals(getInstanceStatus(MACHINE_NAME), Status.SUSPENDED.toString()); - - // Resuming the instance. - ResumeInstance.resumeInstance(PROJECT_ID, ZONE, MACHINE_NAME); - Assert.assertEquals(getInstanceStatus(MACHINE_NAME), Status.RUNNING.toString()); - // Stopping the instance. StopInstance.stopInstance(PROJECT_ID, ZONE, MACHINE_NAME); // Wait for the operation to complete. Setting timeout to 3 mins. @@ -529,7 +283,7 @@ public void testInstanceOperations() @Test public void testEncryptedInstanceOperations() - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { Assert.assertEquals(getInstanceStatus(MACHINE_NAME_ENCRYPTED), Status.RUNNING.toString()); // Stopping the encrypted instance. diff --git a/compute/cloud-client/src/test/java/compute/Util.java b/compute/cloud-client/src/test/java/compute/Util.java index f68cca0f0ce..63c6802f025 100644 --- a/compute/cloud-client/src/test/java/compute/Util.java +++ b/compute/cloud-client/src/test/java/compute/Util.java @@ -25,6 +25,7 @@ import java.time.temporal.ChronoUnit; import java.util.Map.Entry; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; public class Util { // Cleans existing test resources if any. @@ -36,7 +37,7 @@ public class Util { // Delete templates which starts with the given prefixToDelete and // has creation timestamp >24 hours. public static void cleanUpExistingInstanceTemplates(String prefixToDelete, String projectId) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { for (InstanceTemplate template : ListInstanceTemplates.listInstanceTemplates(projectId) .iterateAll()) { if (!template.hasCreationTimestamp()) { @@ -54,7 +55,7 @@ && isCreatedBeforeThresholdTime(template.getCreationTimestamp())) { // has creation timestamp >24 hours. public static void cleanUpExistingInstances(String prefixToDelete, String projectId, String instanceZone) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { for (Entry instanceGroup : ListAllInstances.listAllInstances( projectId).iterateAll()) { for (Instance instance : instanceGroup.getValue().getInstancesList()) { diff --git a/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java b/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java index 770b4e2aca6..4766568c834 100644 --- a/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java +++ b/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java @@ -25,17 +25,20 @@ import java.io.PrintStream; import java.util.UUID; import java.util.concurrent.ExecutionException; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class CustomHostnameInstanceIT { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String INSTANCE_NAME; private static String ZONE; @@ -49,8 +52,9 @@ public static void requireEnvVar(String envVarName) { .that(System.getenv(envVarName)).isNotEmpty(); } - @BeforeClass - public static void setup() throws IOException, ExecutionException, InterruptedException { + @BeforeAll + public static void setup() + throws IOException, ExecutionException, InterruptedException, TimeoutException { final PrintStream out = System.out; ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); @@ -70,8 +74,9 @@ public static void setup() throws IOException, ExecutionException, InterruptedEx System.setOut(out); } - @AfterClass - public static void cleanUp() throws IOException, ExecutionException, InterruptedException { + @AfterAll + public static void cleanUp() + throws IOException, ExecutionException, InterruptedException, TimeoutException { final PrintStream out = System.out; ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); @@ -80,13 +85,13 @@ public static void cleanUp() throws IOException, ExecutionException, Interrupted System.setOut(out); } - @Before + @BeforeEach public void beforeEach() { stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); } - @After + @AfterEach public void afterEach() { stdOut = null; System.setOut(null); diff --git a/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java b/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java index 7ccda634cb1..9409b1128a7 100644 --- a/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java +++ b/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java @@ -26,16 +26,20 @@ import java.io.PrintStream; import java.util.UUID; import java.util.concurrent.ExecutionException; -import org.junit.After; -import org.junit.AfterClass; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class DeleteProtectionIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); @@ -50,8 +54,9 @@ public static void requireEnvVar(String envVarName) { .that(System.getenv(envVarName)).isNotEmpty(); } - @BeforeClass - public static void setup() throws IOException, ExecutionException, InterruptedException { + @BeforeAll + public static void setup() + throws IOException, ExecutionException, InterruptedException, TimeoutException { final PrintStream out = System.out; ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); @@ -72,8 +77,9 @@ public static void setup() throws IOException, ExecutionException, InterruptedEx System.setOut(out); } - @AfterClass - public static void cleanUp() throws IOException, ExecutionException, InterruptedException { + @AfterAll + public static void cleanUp() + throws IOException, ExecutionException, InterruptedException, TimeoutException { final PrintStream out = System.out; ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); @@ -88,20 +94,21 @@ public static void cleanUp() throws IOException, ExecutionException, Interrupted System.setOut(out); } - @Before + @BeforeEach public void beforeEach() { stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); } - @After + @AfterEach public void afterEach() { stdOut = null; System.setOut(null); } @Test - public void testDeleteProtection() throws IOException, ExecutionException, InterruptedException { + public void testDeleteProtection() + throws IOException, ExecutionException, InterruptedException, TimeoutException { Assert.assertTrue(GetDeleteProtection.getDeleteProtection(PROJECT_ID, ZONE, INSTANCE_NAME)); SetDeleteProtection.setDeleteProtection(PROJECT_ID, ZONE, INSTANCE_NAME, false); Assert.assertFalse(GetDeleteProtection.getDeleteProtection(PROJECT_ID, ZONE, INSTANCE_NAME)); diff --git a/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java b/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java index 5e33c314b99..98673bc14a9 100644 --- a/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java +++ b/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java @@ -28,15 +28,19 @@ import java.io.PrintStream; import java.util.UUID; import java.util.concurrent.ExecutionException; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class PreemptibleIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); @@ -51,8 +55,9 @@ public static void requireEnvVar(String envVarName) { .that(System.getenv(envVarName)).isNotEmpty(); } - @BeforeClass - public static void setup() throws IOException, ExecutionException, InterruptedException { + @BeforeAll + public static void setup() + throws IOException, ExecutionException, InterruptedException, TimeoutException { final PrintStream out = System.out; ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); @@ -73,8 +78,9 @@ public static void setup() throws IOException, ExecutionException, InterruptedEx System.setOut(out); } - @AfterClass - public static void cleanUp() throws IOException, ExecutionException, InterruptedException { + @AfterAll + public static void cleanUp() + throws IOException, ExecutionException, InterruptedException, TimeoutException { final PrintStream out = System.out; ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); @@ -85,13 +91,13 @@ public static void cleanUp() throws IOException, ExecutionException, Interrupted System.setOut(out); } - @Before + @BeforeEach public void beforeEach() { stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); } - @After + @AfterEach public void afterEach() { stdOut = null; System.setOut(null); From afde76e28bf8fc1c8c32ae55bc9e0869090ad1d5 Mon Sep 17 00:00:00 2001 From: kurtisvg <31518063+kurtisvg@users.noreply.github.com> Date: Fri, 29 Apr 2022 16:54:49 +0000 Subject: [PATCH 02/11] chore: try Timeout Rule --- .../cloud-client/src/test/java/compute/FirewallIT.java | 7 +++++-- .../src/test/java/compute/InstanceTemplatesIT.java | 7 +++++-- .../src/test/java/compute/InstancesAdvancedIT.java | 7 +++++-- .../cloud-client/src/test/java/compute/SnippetsIT.java | 7 +++++-- .../compute/customhostname/CustomHostnameInstanceIT.java | 9 +++++++-- .../compute/deleteprotection/DeleteProtectionIT.java | 7 +++++-- .../src/test/java/compute/preemptible/PreemptibleIT.java | 8 ++++++-- 7 files changed, 38 insertions(+), 14 deletions(-) diff --git a/compute/cloud-client/src/test/java/compute/FirewallIT.java b/compute/cloud-client/src/test/java/compute/FirewallIT.java index 0929a17f80a..c97047d8f76 100644 --- a/compute/cloud-client/src/test/java/compute/FirewallIT.java +++ b/compute/cloud-client/src/test/java/compute/FirewallIT.java @@ -30,19 +30,22 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; +import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; +import org.junit.rules.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -@Timeout(value = 10, unit = TimeUnit.MINUTES) public class FirewallIT { + @Rule + public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String FIREWALL_RULE_CREATE; private static String NETWORK_NAME; diff --git a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java index b65b3f0d7df..a333601d625 100644 --- a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java +++ b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java @@ -29,19 +29,22 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; +import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; +import org.junit.rules.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -@Timeout(value = 10, unit = TimeUnit.MINUTES) public class InstanceTemplatesIT { + @Rule + public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String DEFAULT_REGION = "us-central1"; diff --git a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java index 3e0b621735b..6a8089b0124 100644 --- a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java +++ b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java @@ -37,19 +37,22 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; +import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; +import org.junit.rules.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -@Timeout(value = 10, unit = TimeUnit.MINUTES) public class InstancesAdvancedIT { + @Rule + public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String ZONE; private static String MACHINE_NAME_PUBLIC_IMAGE; diff --git a/compute/cloud-client/src/test/java/compute/SnippetsIT.java b/compute/cloud-client/src/test/java/compute/SnippetsIT.java index 6ed440b1f6a..e160434f76d 100644 --- a/compute/cloud-client/src/test/java/compute/SnippetsIT.java +++ b/compute/cloud-client/src/test/java/compute/SnippetsIT.java @@ -42,19 +42,22 @@ import java.util.concurrent.TimeoutException; import java.util.stream.IntStream; import org.junit.Assert; +import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; +import org.junit.rules.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -@Timeout(value = 10, unit = TimeUnit.MINUTES) public class SnippetsIT { + @Rule + public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String ZONE; private static String MACHINE_NAME; diff --git a/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java b/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java index 4766568c834..efcc3a39146 100644 --- a/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java +++ b/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java @@ -27,18 +27,23 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; + +import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; +import org.junit.rules.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -@Timeout(value = 10, unit = TimeUnit.MINUTES) public class CustomHostnameInstanceIT { + + @Rule + public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String INSTANCE_NAME; private static String ZONE; diff --git a/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java b/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java index 9409b1128a7..95108c7405b 100644 --- a/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java +++ b/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java @@ -29,19 +29,22 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; +import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; +import org.junit.rules.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -@Timeout(value = 10, unit = TimeUnit.MINUTES) public class DeleteProtectionIT { + @Rule + public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String ZONE = "us-central1-a"; private static String INSTANCE_NAME; diff --git a/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java b/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java index 98673bc14a9..4f88eff9a0a 100644 --- a/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java +++ b/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java @@ -30,19 +30,23 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; + +import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; +import org.junit.rules.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -@Timeout(value = 10, unit = TimeUnit.MINUTES) public class PreemptibleIT { + @Rule + public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String ZONE = "us-central1-a"; private static String INSTANCE_NAME; From 57078a476e5b93fc790e80a5eaf98bfcfbb32158 Mon Sep 17 00:00:00 2001 From: kurtisvg <31518063+kurtisvg@users.noreply.github.com> Date: Fri, 29 Apr 2022 18:58:46 +0000 Subject: [PATCH 03/11] chore: Make it a ClassRule --- compute/cloud-client/src/test/java/compute/FirewallIT.java | 3 ++- .../src/test/java/compute/InstanceTemplatesIT.java | 3 ++- .../src/test/java/compute/InstancesAdvancedIT.java | 3 ++- .../java/compute/customhostname/CustomHostnameInstanceIT.java | 3 ++- .../test/java/compute/deleteprotection/DeleteProtectionIT.java | 3 ++- .../src/test/java/compute/preemptible/PreemptibleIT.java | 3 ++- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/compute/cloud-client/src/test/java/compute/FirewallIT.java b/compute/cloud-client/src/test/java/compute/FirewallIT.java index c97047d8f76..d982774686c 100644 --- a/compute/cloud-client/src/test/java/compute/FirewallIT.java +++ b/compute/cloud-client/src/test/java/compute/FirewallIT.java @@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -43,7 +44,7 @@ @RunWith(JUnit4.class) public class FirewallIT { - @Rule + @ClassRule public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); diff --git a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java index a333601d625..3991f872b52 100644 --- a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java +++ b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java @@ -29,6 +29,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -42,7 +43,7 @@ @RunWith(JUnit4.class) public class InstanceTemplatesIT { - @Rule + @ClassRule public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); diff --git a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java index 6a8089b0124..a815a1ad899 100644 --- a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java +++ b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java @@ -37,6 +37,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -50,7 +51,7 @@ @RunWith(JUnit4.class) public class InstancesAdvancedIT { - @Rule + @ClassRule public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); diff --git a/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java b/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java index efcc3a39146..4ad1b6f4ef7 100644 --- a/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java +++ b/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java @@ -28,6 +28,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -41,7 +42,7 @@ @RunWith(JUnit4.class) public class CustomHostnameInstanceIT { - @Rule + @ClassRule public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); diff --git a/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java b/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java index 95108c7405b..31f7beb79de 100644 --- a/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java +++ b/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java @@ -29,6 +29,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -42,7 +43,7 @@ @RunWith(JUnit4.class) public class DeleteProtectionIT { - @Rule + @ClassRule public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); diff --git a/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java b/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java index 4f88eff9a0a..17cf260171e 100644 --- a/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java +++ b/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java @@ -31,6 +31,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -44,7 +45,7 @@ @RunWith(JUnit4.class) public class PreemptibleIT { - @Rule + @ClassRule public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); From bdb19f995b9ff1313e848e6c4c66e7bdc1a61a24 Mon Sep 17 00:00:00 2001 From: kurtisvg <31518063+kurtisvg@users.noreply.github.com> Date: Fri, 29 Apr 2022 21:07:40 +0000 Subject: [PATCH 04/11] chore(debug): change to 1 s timeout --- compute/cloud-client/src/test/java/compute/FirewallIT.java | 2 +- .../src/test/java/compute/InstanceTemplatesIT.java | 2 +- .../src/test/java/compute/InstancesAdvancedIT.java | 2 +- compute/cloud-client/src/test/java/compute/SnippetsIT.java | 5 +++-- .../compute/customhostname/CustomHostnameInstanceIT.java | 2 +- .../java/compute/deleteprotection/DeleteProtectionIT.java | 2 +- .../src/test/java/compute/preemptible/PreemptibleIT.java | 2 +- 7 files changed, 9 insertions(+), 8 deletions(-) diff --git a/compute/cloud-client/src/test/java/compute/FirewallIT.java b/compute/cloud-client/src/test/java/compute/FirewallIT.java index d982774686c..b58f206115b 100644 --- a/compute/cloud-client/src/test/java/compute/FirewallIT.java +++ b/compute/cloud-client/src/test/java/compute/FirewallIT.java @@ -45,7 +45,7 @@ public class FirewallIT { @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String FIREWALL_RULE_CREATE; diff --git a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java index 3991f872b52..4cf279ca03c 100644 --- a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java +++ b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java @@ -44,7 +44,7 @@ public class InstanceTemplatesIT { @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); diff --git a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java index a815a1ad899..ed3b144fa05 100644 --- a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java +++ b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java @@ -52,7 +52,7 @@ public class InstancesAdvancedIT { @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String ZONE; diff --git a/compute/cloud-client/src/test/java/compute/SnippetsIT.java b/compute/cloud-client/src/test/java/compute/SnippetsIT.java index e160434f76d..f5542e2dda2 100644 --- a/compute/cloud-client/src/test/java/compute/SnippetsIT.java +++ b/compute/cloud-client/src/test/java/compute/SnippetsIT.java @@ -42,6 +42,7 @@ import java.util.concurrent.TimeoutException; import java.util.stream.IntStream; import org.junit.Assert; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -55,8 +56,8 @@ @RunWith(JUnit4.class) public class SnippetsIT { - @Rule - public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + @ClassRule + public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String ZONE; diff --git a/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java b/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java index 4ad1b6f4ef7..6d643e2bf7d 100644 --- a/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java +++ b/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java @@ -43,7 +43,7 @@ public class CustomHostnameInstanceIT { @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String INSTANCE_NAME; diff --git a/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java b/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java index 31f7beb79de..f066afea13a 100644 --- a/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java +++ b/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java @@ -44,7 +44,7 @@ public class DeleteProtectionIT { @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String ZONE = "us-central1-a"; diff --git a/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java b/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java index 17cf260171e..dcc3fd6e420 100644 --- a/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java +++ b/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java @@ -46,7 +46,7 @@ public class PreemptibleIT { @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.MINUTES); + public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String ZONE = "us-central1-a"; From a1e219ea50e63df16d1203a0d6c5aae87217629f Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Tue, 10 May 2022 12:44:05 +0530 Subject: [PATCH 05/11] modified cleanup function to include filter --- .../java/compute/InstanceTemplatesIT.java | 4 +- .../java/compute/InstancesAdvancedIT.java | 2 +- .../src/test/java/compute/SnippetsIT.java | 6 +-- .../src/test/java/compute/Util.java | 47 +++++++++++++++++-- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java index b65b3f0d7df..695c16c0725 100644 --- a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java +++ b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java @@ -83,8 +83,8 @@ public static void setup() "test-csam-inst-temp-or-" + instanceUUID; // Check for resources created >24hours which haven't been deleted in the project. - // Util.cleanUpExistingInstanceTemplates("test-csam-", PROJECT_ID); - // Util.cleanUpExistingInstances("test-csam-", PROJECT_ID, DEFAULT_ZONE); + Util.cleanUpExistingInstanceTemplates("test-csam-", PROJECT_ID); + Util.cleanUpExistingInstances("test-csam-", PROJECT_ID, DEFAULT_ZONE); // Create templates. CreateInstanceTemplate.createInstanceTemplate(PROJECT_ID, TEMPLATE_NAME); diff --git a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java index 3e0b621735b..87763ed7a4e 100644 --- a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java +++ b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java @@ -95,7 +95,7 @@ public static void setup() TEST_SNAPSHOT = createSnapshot(TEST_DISK); TEST_IMAGE = createImage(TEST_DISK); - // Util.cleanUpExistingInstances("test-instance", PROJECT_ID, ZONE); + Util.cleanUpExistingInstances("test-instance", PROJECT_ID, ZONE); compute.CreateInstancesAdvanced.createFromPublicImage(PROJECT_ID, ZONE, MACHINE_NAME_PUBLIC_IMAGE); diff --git a/compute/cloud-client/src/test/java/compute/SnippetsIT.java b/compute/cloud-client/src/test/java/compute/SnippetsIT.java index 6ed440b1f6a..dce8cfc15dd 100644 --- a/compute/cloud-client/src/test/java/compute/SnippetsIT.java +++ b/compute/cloud-client/src/test/java/compute/SnippetsIT.java @@ -94,9 +94,9 @@ public static void setUp() RAW_KEY = getBase64EncodedKey(); // Cleanup existing stale resources. - // Util.cleanUpExistingInstances("my-new-test-instance", PROJECT_ID, ZONE); - // Util.cleanUpExistingInstances("encrypted-test-instance", PROJECT_ID, ZONE); - // Util.cleanUpExistingInstances("test-instance-", PROJECT_ID, ZONE); + Util.cleanUpExistingInstances("my-new-test-instance", PROJECT_ID, ZONE); + Util.cleanUpExistingInstances("encrypted-test-instance", PROJECT_ID, ZONE); + Util.cleanUpExistingInstances("test-instance-", PROJECT_ID, ZONE); compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME); compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME_DELETE); diff --git a/compute/cloud-client/src/test/java/compute/Util.java b/compute/cloud-client/src/test/java/compute/Util.java index 63c6802f025..e46d44a2fbd 100644 --- a/compute/cloud-client/src/test/java/compute/Util.java +++ b/compute/cloud-client/src/test/java/compute/Util.java @@ -16,9 +16,16 @@ package compute; +import com.google.cloud.compute.v1.AggregatedListInstancesRequest; import com.google.cloud.compute.v1.Instance; +import com.google.cloud.compute.v1.Instance.Status; import com.google.cloud.compute.v1.InstanceTemplate; +import com.google.cloud.compute.v1.InstanceTemplatesClient; +import com.google.cloud.compute.v1.InstanceTemplatesClient.ListPagedResponse; +import com.google.cloud.compute.v1.InstancesClient; +import com.google.cloud.compute.v1.InstancesClient.AggregatedListPagedResponse; import com.google.cloud.compute.v1.InstancesScopedList; +import com.google.cloud.compute.v1.ListInstanceTemplatesRequest; import java.io.IOException; import java.time.Instant; import java.time.OffsetDateTime; @@ -38,13 +45,14 @@ public class Util { // has creation timestamp >24 hours. public static void cleanUpExistingInstanceTemplates(String prefixToDelete, String projectId) throws IOException, ExecutionException, InterruptedException, TimeoutException { - for (InstanceTemplate template : ListInstanceTemplates.listInstanceTemplates(projectId) + for (InstanceTemplate template : listFilteredInstanceTemplates(projectId, prefixToDelete) .iterateAll()) { if (!template.hasCreationTimestamp()) { continue; } if (template.getName().contains(prefixToDelete) - && isCreatedBeforeThresholdTime(template.getCreationTimestamp())) { + && isCreatedBeforeThresholdTime(template.getCreationTimestamp()) + && template.isInitialized()) { DeleteInstanceTemplate.deleteInstanceTemplate(projectId, template.getName()); } } @@ -56,14 +64,15 @@ && isCreatedBeforeThresholdTime(template.getCreationTimestamp())) { public static void cleanUpExistingInstances(String prefixToDelete, String projectId, String instanceZone) throws IOException, ExecutionException, InterruptedException, TimeoutException { - for (Entry instanceGroup : ListAllInstances.listAllInstances( - projectId).iterateAll()) { + for (Entry instanceGroup : listFilteredInstances( + projectId, prefixToDelete).iterateAll()) { for (Instance instance : instanceGroup.getValue().getInstancesList()) { if (!instance.hasCreationTimestamp()) { continue; } if (instance.getName().contains(prefixToDelete) - && isCreatedBeforeThresholdTime(instance.getCreationTimestamp())) { + && isCreatedBeforeThresholdTime(instance.getCreationTimestamp()) + && instance.getStatus().equalsIgnoreCase(Status.RUNNING.toString())) { DeleteInstance.deleteInstance(projectId, instanceZone, instance.getName()); } } @@ -75,4 +84,32 @@ public static boolean isCreatedBeforeThresholdTime(String timestamp) { .isBefore(Instant.now().minus(DELETION_THRESHOLD_TIME_HOURS, ChronoUnit.HOURS)); } + public static AggregatedListPagedResponse listFilteredInstances(String project, + String instanceNamePrefix) throws IOException { + try (InstancesClient instancesClient = InstancesClient.create()) { + + AggregatedListInstancesRequest aggregatedListInstancesRequest = AggregatedListInstancesRequest + .newBuilder() + .setProject(project) + .setFilter(String.format("name:%s", instanceNamePrefix)) + .build(); + + return instancesClient + .aggregatedList(aggregatedListInstancesRequest); + } + } + + public static ListPagedResponse listFilteredInstanceTemplates(String projectId, + String instanceTemplatePrefix) throws IOException { + try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) { + ListInstanceTemplatesRequest listInstanceTemplatesRequest = + ListInstanceTemplatesRequest.newBuilder() + .setProject(projectId) + .setFilter(String.format("name:%s", instanceTemplatePrefix)) + .build(); + + return instanceTemplatesClient.list(listInstanceTemplatesRequest); + } + } + } \ No newline at end of file From d39a3afb8d15deafc7540e9f3c5e04cfabb3a62f Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Tue, 10 May 2022 12:56:19 +0530 Subject: [PATCH 06/11] reverted back to jupiter class timeout rule --- .../cloud-client/src/test/java/compute/FirewallIT.java | 8 ++------ .../src/test/java/compute/InstanceTemplatesIT.java | 9 ++------- .../src/test/java/compute/InstancesAdvancedIT.java | 8 ++------ .../cloud-client/src/test/java/compute/SnippetsIT.java | 8 ++------ .../customhostname/CustomHostnameInstanceIT.java | 10 ++-------- .../compute/deleteprotection/DeleteProtectionIT.java | 8 ++------ .../test/java/compute/preemptible/PreemptibleIT.java | 9 ++------- 7 files changed, 14 insertions(+), 46 deletions(-) diff --git a/compute/cloud-client/src/test/java/compute/FirewallIT.java b/compute/cloud-client/src/test/java/compute/FirewallIT.java index b58f206115b..0929a17f80a 100644 --- a/compute/cloud-client/src/test/java/compute/FirewallIT.java +++ b/compute/cloud-client/src/test/java/compute/FirewallIT.java @@ -30,23 +30,19 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; -import org.junit.ClassRule; -import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.rules.Timeout; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class FirewallIT { - @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String FIREWALL_RULE_CREATE; private static String NETWORK_NAME; diff --git a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java index a5ea942c670..6bef26c0c06 100644 --- a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java +++ b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java @@ -29,24 +29,19 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; -import org.junit.ClassRule; -import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.rules.Timeout; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class InstanceTemplatesIT { - @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); - - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String DEFAULT_REGION = "us-central1"; private static final String DEFAULT_ZONE = DEFAULT_REGION + "-a"; diff --git a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java index 5ab3c49e2a4..87763ed7a4e 100644 --- a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java +++ b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java @@ -37,23 +37,19 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; -import org.junit.ClassRule; -import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.rules.Timeout; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class InstancesAdvancedIT { - @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String ZONE; private static String MACHINE_NAME_PUBLIC_IMAGE; diff --git a/compute/cloud-client/src/test/java/compute/SnippetsIT.java b/compute/cloud-client/src/test/java/compute/SnippetsIT.java index f6e28bf2d71..dce8cfc15dd 100644 --- a/compute/cloud-client/src/test/java/compute/SnippetsIT.java +++ b/compute/cloud-client/src/test/java/compute/SnippetsIT.java @@ -42,23 +42,19 @@ import java.util.concurrent.TimeoutException; import java.util.stream.IntStream; import org.junit.Assert; -import org.junit.ClassRule; -import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.rules.Timeout; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class SnippetsIT { - @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String ZONE; private static String MACHINE_NAME; diff --git a/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java b/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java index 6d643e2bf7d..4766568c834 100644 --- a/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java +++ b/compute/cloud-client/src/test/java/compute/customhostname/CustomHostnameInstanceIT.java @@ -27,24 +27,18 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; - -import org.junit.ClassRule; -import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.rules.Timeout; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class CustomHostnameInstanceIT { - - @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String INSTANCE_NAME; private static String ZONE; diff --git a/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java b/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java index f066afea13a..9409b1128a7 100644 --- a/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java +++ b/compute/cloud-client/src/test/java/compute/deleteprotection/DeleteProtectionIT.java @@ -29,23 +29,19 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.Assert; -import org.junit.ClassRule; -import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.rules.Timeout; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class DeleteProtectionIT { - @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String ZONE = "us-central1-a"; private static String INSTANCE_NAME; diff --git a/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java b/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java index dcc3fd6e420..98673bc14a9 100644 --- a/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java +++ b/compute/cloud-client/src/test/java/compute/preemptible/PreemptibleIT.java @@ -30,24 +30,19 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; - -import org.junit.ClassRule; -import org.junit.Rule; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.rules.Timeout; +import org.junit.jupiter.api.Timeout; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) public class PreemptibleIT { - @ClassRule - public Timeout timeout = new Timeout(1, TimeUnit.SECONDS); - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String ZONE = "us-central1-a"; private static String INSTANCE_NAME; From e04ce8c81a034c17898303cf8888a1d1ae6310d7 Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Tue, 10 May 2022 22:44:50 +0530 Subject: [PATCH 07/11] split huge test file to smaller ones --- .../java/compute/InstanceOperationsIT.java | 160 ++++++++++++++++++ .../java/compute/InstancesAdvancedIT.java | 20 +-- .../src/test/java/compute/SnippetsIT.java | 94 +--------- .../src/test/java/compute/Util.java | 24 +++ 4 files changed, 193 insertions(+), 105 deletions(-) create mode 100644 compute/cloud-client/src/test/java/compute/InstanceOperationsIT.java diff --git a/compute/cloud-client/src/test/java/compute/InstanceOperationsIT.java b/compute/cloud-client/src/test/java/compute/InstanceOperationsIT.java new file mode 100644 index 00000000000..bbabd022845 --- /dev/null +++ b/compute/cloud-client/src/test/java/compute/InstanceOperationsIT.java @@ -0,0 +1,160 @@ +package compute; + +import static com.google.common.truth.Truth.assertWithMessage; + +import com.google.cloud.compute.v1.Instance.Status; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.time.LocalDateTime; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.Assert; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@Timeout(value = 10, unit = TimeUnit.MINUTES) +public class InstanceOperationsIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static String ZONE; + private static String MACHINE_NAME; + private static String MACHINE_NAME_ENCRYPTED; + private static String RAW_KEY; + + private ByteArrayOutputStream stdOut; + + // Check if the required environment variables are set. + public static void requireEnvVar(String envVarName) { + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) + .that(System.getenv(envVarName)).isNotEmpty(); + } + + @BeforeAll + public static void setUp() + throws IOException, InterruptedException, ExecutionException, TimeoutException { + final PrintStream out = System.out; + ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + ZONE = "us-central1-a"; + MACHINE_NAME = "my-new-test-instance" + UUID.randomUUID(); + MACHINE_NAME_ENCRYPTED = "encrypted-test-instance" + UUID.randomUUID(); + RAW_KEY = Util.getBase64EncodedKey(); + + // Cleanup existing stale resources. + Util.cleanUpExistingInstances("my-new-test-instance", PROJECT_ID, ZONE); + Util.cleanUpExistingInstances("encrypted-test-instance", PROJECT_ID, ZONE); + + compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME); + compute.CreateEncryptedInstance + .createEncryptedInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED, RAW_KEY); + + TimeUnit.SECONDS.sleep(10); + + stdOut.close(); + System.setOut(out); + } + + + @AfterAll + public static void cleanup() + throws IOException, InterruptedException, ExecutionException, TimeoutException { + final PrintStream out = System.out; + ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + + // Delete all instances created for testing. + compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED); + compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME); + + stdOut.close(); + System.setOut(out); + } + + @BeforeEach + public void beforeEach() { + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + } + + @AfterEach + public void afterEach() { + stdOut = null; + System.setOut(null); + } + + @Test + public void testInstanceOperations() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + Assert.assertEquals(Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME), + Status.RUNNING.toString()); + + // Stopping the instance. + StopInstance.stopInstance(PROJECT_ID, ZONE, MACHINE_NAME); + // Wait for the operation to complete. Setting timeout to 3 mins. + LocalDateTime endTime = LocalDateTime.now().plusMinutes(3); + while (!Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME) + .equalsIgnoreCase(Status.STOPPED.toString()) + && LocalDateTime.now().isBefore(endTime)) { + TimeUnit.SECONDS.sleep(5); + } + Assert.assertEquals(Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME), + Status.TERMINATED.toString()); + + // Starting the instance. + StartInstance.startInstance(PROJECT_ID, ZONE, MACHINE_NAME); + // Wait for the operation to complete. Setting timeout to 3 mins. + endTime = LocalDateTime.now().plusMinutes(3); + while (!Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME) + .equalsIgnoreCase(Status.RUNNING.toString()) + && LocalDateTime.now().isBefore(endTime)) { + TimeUnit.SECONDS.sleep(5); + } + Assert.assertEquals(Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME), + Status.RUNNING.toString()); + } + + @Test + public void testEncryptedInstanceOperations() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + Assert.assertEquals(Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED), + Status.RUNNING.toString()); + + // Stopping the encrypted instance. + StopInstance.stopInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED); + // Wait for the operation to complete. Setting timeout to 3 mins. + LocalDateTime endTime = LocalDateTime.now().plusMinutes(3); + while (!Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED) + .equalsIgnoreCase(Status.STOPPED.toString()) + && LocalDateTime.now().isBefore(endTime)) { + TimeUnit.SECONDS.sleep(5); + } + Assert.assertEquals(Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED), + Status.TERMINATED.toString()); + + // Starting the encrypted instance. + StartEncryptedInstance + .startEncryptedInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED, RAW_KEY); + // Wait for the operation to complete. Setting timeout to 3 mins. + endTime = LocalDateTime.now().plusMinutes(3); + while (!Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED) + .equalsIgnoreCase(Status.RUNNING.toString()) + && LocalDateTime.now().isBefore(endTime)) { + TimeUnit.SECONDS.sleep(5); + } + Assert.assertEquals(Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED), + Status.RUNNING.toString()); + } +} diff --git a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java index 87763ed7a4e..cd0552603f7 100644 --- a/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java +++ b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java @@ -23,9 +23,7 @@ import com.google.cloud.compute.v1.DisksClient; import com.google.cloud.compute.v1.Image; import com.google.cloud.compute.v1.ImagesClient; -import com.google.cloud.compute.v1.Instance; import com.google.cloud.compute.v1.Instance.Status; -import com.google.cloud.compute.v1.InstancesClient; import com.google.cloud.compute.v1.Operation; import com.google.cloud.compute.v1.Snapshot; import com.google.cloud.compute.v1.SnapshotsClient; @@ -220,12 +218,6 @@ private static void deleteImage(Image image) } } - public static String getInstanceStatus(String instanceName) throws IOException { - try (InstancesClient instancesClient = InstancesClient.create()) { - Instance response = instancesClient.get(PROJECT_ID, ZONE, instanceName); - return response.getStatus(); - } - } @BeforeEach public void beforeEach() { @@ -242,42 +234,42 @@ public void afterEach() { @Test public void testCreatePublicImage() throws IOException { // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_PUBLIC_IMAGE); + String response = Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_PUBLIC_IMAGE); Assert.assertEquals(response, Status.RUNNING.toString()); } @Test public void testCreateCustomImage() throws IOException { // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_CUSTOM_IMAGE); + String response = Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_CUSTOM_IMAGE); Assert.assertEquals(response, Status.RUNNING.toString()); } @Test public void testCreateAdditionalDisk() throws IOException { // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_ADDITIONAL_DISK); + String response = Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_ADDITIONAL_DISK); Assert.assertEquals(response, Status.RUNNING.toString()); } @Test public void testCreateFromSnapshot() throws IOException { // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_SNAPSHOT); + String response = Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_SNAPSHOT); Assert.assertEquals(response, Status.RUNNING.toString()); } @Test public void testCreateFromSnapshotAdditional() throws IOException { // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_SNAPSHOT_ADDITIONAL); + String response = Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_SNAPSHOT_ADDITIONAL); Assert.assertEquals(response, Status.RUNNING.toString()); } @Test public void testCreateInSubnetwork() throws IOException { // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_SUBNETWORK); + String response = Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_SUBNETWORK); Assert.assertEquals(response, Status.RUNNING.toString()); } diff --git a/compute/cloud-client/src/test/java/compute/SnippetsIT.java b/compute/cloud-client/src/test/java/compute/SnippetsIT.java index dce8cfc15dd..9c0a0f1dd2b 100644 --- a/compute/cloud-client/src/test/java/compute/SnippetsIT.java +++ b/compute/cloud-client/src/test/java/compute/SnippetsIT.java @@ -20,7 +20,6 @@ import static com.google.common.truth.Truth.assertWithMessage; import com.google.api.gax.longrunning.OperationFuture; -import com.google.cloud.compute.v1.Instance; import com.google.cloud.compute.v1.Instance.Status; import com.google.cloud.compute.v1.InstancesClient; import com.google.cloud.compute.v1.Operation; @@ -32,15 +31,10 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; -import java.nio.charset.StandardCharsets; -import java.security.SecureRandom; -import java.time.LocalDateTime; -import java.util.Base64; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import java.util.stream.IntStream; import org.junit.Assert; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -58,7 +52,6 @@ public class SnippetsIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static String ZONE; private static String MACHINE_NAME; - private static String MACHINE_NAME_DELETE; private static String MACHINE_NAME_LIST_INSTANCE; private static String MACHINE_NAME_WAIT_FOR_OP; private static String MACHINE_NAME_ENCRYPTED; @@ -85,13 +78,12 @@ public static void setUp() ZONE = "us-central1-a"; MACHINE_NAME = "my-new-test-instance" + UUID.randomUUID(); - MACHINE_NAME_DELETE = "my-new-test-instance" + UUID.randomUUID(); MACHINE_NAME_LIST_INSTANCE = "my-new-test-instance" + UUID.randomUUID(); MACHINE_NAME_WAIT_FOR_OP = "my-new-test-instance" + UUID.randomUUID(); MACHINE_NAME_ENCRYPTED = "encrypted-test-instance" + UUID.randomUUID(); BUCKET_NAME = "my-new-test-bucket" + UUID.randomUUID(); IMAGE_PROJECT_NAME = "windows-sql-cloud"; - RAW_KEY = getBase64EncodedKey(); + RAW_KEY = Util.getBase64EncodedKey(); // Cleanup existing stale resources. Util.cleanUpExistingInstances("my-new-test-instance", PROJECT_ID, ZONE); @@ -99,7 +91,6 @@ public static void setUp() Util.cleanUpExistingInstances("test-instance-", PROJECT_ID, ZONE); compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME); - compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME_DELETE); compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME_LIST_INSTANCE); compute.CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME_WAIT_FOR_OP); compute.CreateEncryptedInstance @@ -140,25 +131,6 @@ public static void cleanup() } - public static String getBase64EncodedKey() { - String sampleSpace = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - StringBuilder stringBuilder = new StringBuilder(); - SecureRandom random = new SecureRandom(); - IntStream.range(0, 32) - .forEach( - x -> stringBuilder.append(sampleSpace.charAt(random.nextInt(sampleSpace.length())))); - - return Base64.getEncoder() - .encodeToString(stringBuilder.toString().getBytes(StandardCharsets.US_ASCII)); - } - - public static String getInstanceStatus(String instanceName) throws IOException { - try (InstancesClient instancesClient = InstancesClient.create()) { - Instance response = instancesClient.get(PROJECT_ID, ZONE, instanceName); - return response.getStatus(); - } - } - @BeforeEach public void beforeEach() { stdOut = new ByteArrayOutputStream(); @@ -174,14 +146,14 @@ public void afterEach() { @Test public void testCreateInstance() throws IOException { // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME); + String response = Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME); Assert.assertEquals(response, Status.RUNNING.toString()); } @Test public void testCreateEncryptedInstance() throws IOException { // Check if the instance was successfully created during the setup. - String response = getInstanceStatus(MACHINE_NAME_ENCRYPTED); + String response = Util.getInstanceStatus(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED); Assert.assertEquals(response, Status.RUNNING.toString()); } @@ -198,13 +170,6 @@ public void testListAllInstances() throws IOException { assertThat(stdOut.toString()).contains(MACHINE_NAME_LIST_INSTANCE); } - @Test - public void testDeleteInstance() - throws IOException, InterruptedException, ExecutionException, TimeoutException { - compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_DELETE); - assertThat(stdOut.toString()).contains("Operation Status: DONE"); - } - @Test public void testWaitForOperation() throws IOException, InterruptedException, ExecutionException, TimeoutException { @@ -255,57 +220,4 @@ public void testListImagesByPage() throws IOException { Assert.assertTrue(stdOut.toString().contains("Page Number: 1")); } - @Test - public void testInstanceOperations() - throws IOException, ExecutionException, InterruptedException, TimeoutException { - Assert.assertEquals(getInstanceStatus(MACHINE_NAME), Status.RUNNING.toString()); - - // Stopping the instance. - StopInstance.stopInstance(PROJECT_ID, ZONE, MACHINE_NAME); - // Wait for the operation to complete. Setting timeout to 3 mins. - LocalDateTime endTime = LocalDateTime.now().plusMinutes(3); - while (getInstanceStatus(MACHINE_NAME).equalsIgnoreCase(Status.STOPPING.toString()) - && LocalDateTime.now().isBefore(endTime)) { - TimeUnit.SECONDS.sleep(5); - } - Assert.assertEquals(getInstanceStatus(MACHINE_NAME), Status.TERMINATED.toString()); - - // Starting the instance. - StartInstance.startInstance(PROJECT_ID, ZONE, MACHINE_NAME); - // Wait for the operation to complete. Setting timeout to 3 mins. - endTime = LocalDateTime.now().plusMinutes(3); - while (getInstanceStatus(MACHINE_NAME).equalsIgnoreCase(Status.RUNNING.toString()) - && LocalDateTime.now().isBefore(endTime)) { - TimeUnit.SECONDS.sleep(5); - } - Assert.assertEquals(getInstanceStatus(MACHINE_NAME), Status.RUNNING.toString()); - } - - @Test - public void testEncryptedInstanceOperations() - throws IOException, ExecutionException, InterruptedException, TimeoutException { - Assert.assertEquals(getInstanceStatus(MACHINE_NAME_ENCRYPTED), Status.RUNNING.toString()); - - // Stopping the encrypted instance. - StopInstance.stopInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED); - // Wait for the operation to complete. Setting timeout to 3 mins. - LocalDateTime endTime = LocalDateTime.now().plusMinutes(3); - while (getInstanceStatus(MACHINE_NAME_ENCRYPTED).equalsIgnoreCase(Status.STOPPING.toString()) - && LocalDateTime.now().isBefore(endTime)) { - TimeUnit.SECONDS.sleep(5); - } - Assert.assertEquals(getInstanceStatus(MACHINE_NAME_ENCRYPTED), Status.TERMINATED.toString()); - - // Starting the encrypted instance. - StartEncryptedInstance - .startEncryptedInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED, RAW_KEY); - // Wait for the operation to complete. Setting timeout to 3 mins. - endTime = LocalDateTime.now().plusMinutes(3); - while (getInstanceStatus(MACHINE_NAME_ENCRYPTED).equalsIgnoreCase(Status.RUNNING.toString()) - && LocalDateTime.now().isBefore(endTime)) { - TimeUnit.SECONDS.sleep(5); - } - Assert.assertEquals(getInstanceStatus(MACHINE_NAME_ENCRYPTED), Status.RUNNING.toString()); - } - } diff --git a/compute/cloud-client/src/test/java/compute/Util.java b/compute/cloud-client/src/test/java/compute/Util.java index e46d44a2fbd..cf0adb0de7e 100644 --- a/compute/cloud-client/src/test/java/compute/Util.java +++ b/compute/cloud-client/src/test/java/compute/Util.java @@ -27,12 +27,16 @@ import com.google.cloud.compute.v1.InstancesScopedList; import com.google.cloud.compute.v1.ListInstanceTemplatesRequest; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.security.SecureRandom; import java.time.Instant; import java.time.OffsetDateTime; import java.time.temporal.ChronoUnit; +import java.util.Base64; import java.util.Map.Entry; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; +import java.util.stream.IntStream; public class Util { // Cleans existing test resources if any. @@ -112,4 +116,24 @@ public static ListPagedResponse listFilteredInstanceTemplates(String projectId, } } + public static String getBase64EncodedKey() { + String sampleSpace = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + StringBuilder stringBuilder = new StringBuilder(); + SecureRandom random = new SecureRandom(); + IntStream.range(0, 32) + .forEach( + x -> stringBuilder.append(sampleSpace.charAt(random.nextInt(sampleSpace.length())))); + + return Base64.getEncoder() + .encodeToString(stringBuilder.toString().getBytes(StandardCharsets.US_ASCII)); + } + + public static String getInstanceStatus(String project, String zone, String instanceName) + throws IOException { + try (InstancesClient instancesClient = InstancesClient.create()) { + Instance response = instancesClient.get(project, zone, instanceName); + return response.getStatus(); + } + } + } \ No newline at end of file From a0f1ad63d5cc5d6a2cdf27eb2256eda227d154f1 Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Tue, 10 May 2022 23:23:00 +0530 Subject: [PATCH 08/11] lint fix and refactored acc to review comments --- .../java/compute/CreateEncryptedInstance.java | 1 - .../main/java/compute/CreateFirewallRule.java | 1 - .../src/main/java/compute/CreateInstance.java | 1 - .../java/compute/CreateInstanceFromTemplate.java | 1 - .../java/compute/CreateInstanceTemplate.java | 2 -- .../java/compute/CreateInstancesAdvanced.java | 1 - .../java/compute/CreateTemplateFromInstance.java | 1 - .../java/compute/CreateTemplateWithSubnet.java | 1 - .../main/java/compute/DeleteFirewallRule.java | 1 - .../src/main/java/compute/DeleteInstance.java | 1 - .../java/compute/DeleteInstanceTemplate.java | 1 - .../src/main/java/compute/PatchFirewallRule.java | 1 - .../src/main/java/compute/ResetInstance.java | 1 - .../main/java/compute/SetUsageExportBucket.java | 3 +-- .../java/compute/StartEncryptedInstance.java | 1 - .../src/main/java/compute/StartInstance.java | 1 - .../src/main/java/compute/StopInstance.java | 1 - .../src/test/java/compute/FirewallIT.java | 7 ++++--- .../test/java/compute/InstanceOperationsIT.java | 16 ++++++++++++++++ 19 files changed, 21 insertions(+), 22 deletions(-) diff --git a/compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java b/compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java index 3a5e1c9b631..8829834adbc 100644 --- a/compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java +++ b/compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java @@ -114,7 +114,6 @@ public static void createEncryptedInstance(String project, String zone, String i // Wait for the operation to complete. Operation response = operation.get(3, TimeUnit.MINUTES); - ; if (response.hasError()) { System.out.println("Instance creation failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/CreateFirewallRule.java b/compute/cloud-client/src/main/java/compute/CreateFirewallRule.java index cc6a4c1d1de..61a72e8ad10 100644 --- a/compute/cloud-client/src/main/java/compute/CreateFirewallRule.java +++ b/compute/cloud-client/src/main/java/compute/CreateFirewallRule.java @@ -81,7 +81,6 @@ public static void createFirewall(String project, String firewallRuleName, Strin .setProject(project).build(); firewallsClient.insertAsync(insertFirewallRequest).get(3, TimeUnit.MINUTES); - ; System.out.println("Firewall rule created successfully -> " + firewallRuleName); } diff --git a/compute/cloud-client/src/main/java/compute/CreateInstance.java b/compute/cloud-client/src/main/java/compute/CreateInstance.java index 6dbd83ab83b..6c012330f1f 100644 --- a/compute/cloud-client/src/main/java/compute/CreateInstance.java +++ b/compute/cloud-client/src/main/java/compute/CreateInstance.java @@ -108,7 +108,6 @@ public static void createInstance(String project, String zone, String instanceNa // Wait for the operation to complete. Operation response = operation.get(3, TimeUnit.MINUTES); - ; if (response.hasError()) { System.out.println("Instance creation failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplate.java b/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplate.java index 22b65211ede..3d8e2bea792 100644 --- a/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplate.java +++ b/compute/cloud-client/src/main/java/compute/CreateInstanceFromTemplate.java @@ -65,7 +65,6 @@ public static void createInstanceFromTemplate(String projectId, String zone, Str Operation response = instancesClient.insertAsync(insertInstanceRequest) .get(3, TimeUnit.MINUTES); - ; if (response.hasError()) { System.out.println("Instance creation from template failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/CreateInstanceTemplate.java b/compute/cloud-client/src/main/java/compute/CreateInstanceTemplate.java index de90f9f2076..085162f1824 100644 --- a/compute/cloud-client/src/main/java/compute/CreateInstanceTemplate.java +++ b/compute/cloud-client/src/main/java/compute/CreateInstanceTemplate.java @@ -89,7 +89,6 @@ public static void createInstanceTemplate(String projectId, String templateName) // Create the Instance Template. Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest) .get(3, TimeUnit.MINUTES); - ; if (response.hasError()) { System.out.println("Instance Template creation failed ! ! " + response); @@ -128,7 +127,6 @@ public static void createInstanceTemplateWithDiskType(String projectId, String t Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest) .get(3, TimeUnit.MINUTES); - ; if (response.hasError()) { System.out.println("Instance Template creation failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/CreateInstancesAdvanced.java b/compute/cloud-client/src/main/java/compute/CreateInstancesAdvanced.java index 931399b3a28..dad7c6f6372 100644 --- a/compute/cloud-client/src/main/java/compute/CreateInstancesAdvanced.java +++ b/compute/cloud-client/src/main/java/compute/CreateInstancesAdvanced.java @@ -234,7 +234,6 @@ private static Instance createWithDisks(String project, String zone, String inst // Wait for the operation to complete. Operation response = operation.get(3, TimeUnit.MINUTES); - ; if (response.hasError()) { System.out.println("Instance creation failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/CreateTemplateFromInstance.java b/compute/cloud-client/src/main/java/compute/CreateTemplateFromInstance.java index 81676cefacf..1d9595972d4 100644 --- a/compute/cloud-client/src/main/java/compute/CreateTemplateFromInstance.java +++ b/compute/cloud-client/src/main/java/compute/CreateTemplateFromInstance.java @@ -84,7 +84,6 @@ public static void createTemplateFromInstance(String projectId, String templateN Operation operation = instanceTemplatesClient.insertCallable() .futureCall(insertInstanceTemplateRequest).get(3, TimeUnit.MINUTES); - ; Operation response = globalOperationsClient.wait(projectId, operation.getName()); diff --git a/compute/cloud-client/src/main/java/compute/CreateTemplateWithSubnet.java b/compute/cloud-client/src/main/java/compute/CreateTemplateWithSubnet.java index a90a48dbd3a..602db9d2d57 100644 --- a/compute/cloud-client/src/main/java/compute/CreateTemplateWithSubnet.java +++ b/compute/cloud-client/src/main/java/compute/CreateTemplateWithSubnet.java @@ -90,7 +90,6 @@ public static void createTemplateWithSubnet(String projectId, String network, St Operation operation = instanceTemplatesClient.insertCallable() .futureCall(insertInstanceTemplateRequest).get(3, TimeUnit.MINUTES); - ; Operation response = globalOperationsClient.wait(projectId, operation.getName()); diff --git a/compute/cloud-client/src/main/java/compute/DeleteFirewallRule.java b/compute/cloud-client/src/main/java/compute/DeleteFirewallRule.java index 1e755c6b666..0985adc6c3a 100644 --- a/compute/cloud-client/src/main/java/compute/DeleteFirewallRule.java +++ b/compute/cloud-client/src/main/java/compute/DeleteFirewallRule.java @@ -52,7 +52,6 @@ public static void deleteFirewallRule(String project, String firewallRuleName) OperationFuture operation = firewallsClient.deleteAsync(project, firewallRuleName); operation.get(3, TimeUnit.MINUTES); - ; System.out.println("Deleted firewall rule -> " + firewallRuleName); } diff --git a/compute/cloud-client/src/main/java/compute/DeleteInstance.java b/compute/cloud-client/src/main/java/compute/DeleteInstance.java index d8bfc54265e..f3533778ec7 100644 --- a/compute/cloud-client/src/main/java/compute/DeleteInstance.java +++ b/compute/cloud-client/src/main/java/compute/DeleteInstance.java @@ -60,7 +60,6 @@ public static void deleteInstance(String project, String zone, String instanceNa deleteInstanceRequest); // Wait for the operation to complete. Operation response = operation.get(3, TimeUnit.MINUTES); - ; if (response.hasError()) { System.out.println("Instance deletion failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/DeleteInstanceTemplate.java b/compute/cloud-client/src/main/java/compute/DeleteInstanceTemplate.java index d8be1e04adc..5b05620e39e 100644 --- a/compute/cloud-client/src/main/java/compute/DeleteInstanceTemplate.java +++ b/compute/cloud-client/src/main/java/compute/DeleteInstanceTemplate.java @@ -48,7 +48,6 @@ public static void deleteInstanceTemplate(String projectId, String templateName) Operation response = instanceTemplatesClient.deleteAsync(deleteInstanceTemplateRequest) .get(3, TimeUnit.MINUTES); - ; if (response.hasError()) { System.out.println("Instance template deletion failed ! ! " + response); diff --git a/compute/cloud-client/src/main/java/compute/PatchFirewallRule.java b/compute/cloud-client/src/main/java/compute/PatchFirewallRule.java index 9acc29d50c4..5cbda7d58e4 100644 --- a/compute/cloud-client/src/main/java/compute/PatchFirewallRule.java +++ b/compute/cloud-client/src/main/java/compute/PatchFirewallRule.java @@ -66,7 +66,6 @@ public static void patchFirewallPriority(String project, String firewallRuleName OperationFuture operation = firewallsClient.patchAsync( patchFirewallRequest); operation.get(3, TimeUnit.MINUTES); - ; System.out.println("Firewall Patch applied successfully ! "); } } diff --git a/compute/cloud-client/src/main/java/compute/ResetInstance.java b/compute/cloud-client/src/main/java/compute/ResetInstance.java index 9321cfc4e1a..ed6381e5759 100644 --- a/compute/cloud-client/src/main/java/compute/ResetInstance.java +++ b/compute/cloud-client/src/main/java/compute/ResetInstance.java @@ -62,7 +62,6 @@ public static void resetInstance(String project, String zone, String instanceNam OperationFuture operation = instancesClient.resetAsync( resetInstanceRequest); Operation response = operation.get(3, TimeUnit.MINUTES); - ; if (response.getStatus() == Status.DONE) { System.out.println("Instance reset successfully ! "); diff --git a/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java b/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java index a12fb8798b7..2645b042241 100644 --- a/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java +++ b/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java @@ -91,7 +91,6 @@ public static void setUsageExportBucket(String project, String bucketName, // Wait for the operation to complete. Operation response = operation.get(3, TimeUnit.MINUTES); - ; if (response.hasError()) { System.out.println("Setting usage export bucket failed ! ! " + response); @@ -169,7 +168,7 @@ public static boolean disableUsageExportBucket(String project) } // Wait for the settings to be effected. - TimeUnit.SECONDS.sleep(5); + TimeUnit.SECONDS.sleep(10); // Return false if the usage reports is disabled. return projectsClient.get(project).getUsageExportLocation().hasBucketName(); } diff --git a/compute/cloud-client/src/main/java/compute/StartEncryptedInstance.java b/compute/cloud-client/src/main/java/compute/StartEncryptedInstance.java index 8bb5883eb83..9e5b2b62704 100644 --- a/compute/cloud-client/src/main/java/compute/StartEncryptedInstance.java +++ b/compute/cloud-client/src/main/java/compute/StartEncryptedInstance.java @@ -97,7 +97,6 @@ public static void startEncryptedInstance(String project, String zone, String in OperationFuture operation = instancesClient.startWithEncryptionKeyAsync( encryptionKeyInstanceRequest); Operation response = operation.get(3, TimeUnit.MINUTES); - ; if (response.getStatus() == Status.DONE) { System.out.println("Encrypted instance started successfully ! "); diff --git a/compute/cloud-client/src/main/java/compute/StartInstance.java b/compute/cloud-client/src/main/java/compute/StartInstance.java index 2b8f000ec5f..3c8ab90bb7e 100644 --- a/compute/cloud-client/src/main/java/compute/StartInstance.java +++ b/compute/cloud-client/src/main/java/compute/StartInstance.java @@ -64,7 +64,6 @@ public static void startInstance(String project, String zone, String instanceNam // Wait for the operation to complete. Operation response = operation.get(3, TimeUnit.MINUTES); - ; if (response.getStatus() == Status.DONE) { System.out.println("Instance started successfully ! "); diff --git a/compute/cloud-client/src/main/java/compute/StopInstance.java b/compute/cloud-client/src/main/java/compute/StopInstance.java index a7123e7f5c2..ce22aa11458 100644 --- a/compute/cloud-client/src/main/java/compute/StopInstance.java +++ b/compute/cloud-client/src/main/java/compute/StopInstance.java @@ -62,7 +62,6 @@ public static void stopInstance(String project, String zone, String instanceName OperationFuture operation = instancesClient.stopAsync( stopInstanceRequest); Operation response = operation.get(3, TimeUnit.MINUTES); - ; if (response.getStatus() == Status.DONE) { System.out.println("Instance stopped successfully ! "); diff --git a/compute/cloud-client/src/test/java/compute/FirewallIT.java b/compute/cloud-client/src/test/java/compute/FirewallIT.java index 0929a17f80a..6a64961274d 100644 --- a/compute/cloud-client/src/test/java/compute/FirewallIT.java +++ b/compute/cloud-client/src/test/java/compute/FirewallIT.java @@ -93,8 +93,9 @@ public static void cleanup() System.setOut(out); } - public static boolean isFirewallRuleDeletedByGceEnforcer(String projectId, - String firewallRule) throws IOException, ExecutionException, InterruptedException { + public static boolean isFirewallRuleDeletedByGceEnforcer( + String projectId, String firewallRule) + throws IOException { /* (**INTERNAL method**) This method will prevent test failure if the firewall rule was auto-deleted by GCE Enforcer. (Feel free to remove this method if not running on a Google-owned project.) @@ -102,7 +103,7 @@ public static boolean isFirewallRuleDeletedByGceEnforcer(String projectId, try { GetFirewallRule.getFirewallRule(projectId, firewallRule); } catch (NotFoundException e) { - System.out.println("Rule already deleted ! "); + System.out.println("Rule already deleted! "); return true; } catch (InvalidArgumentException | NullPointerException e) { System.out.println("Rule is not ready (probably being deleted)."); diff --git a/compute/cloud-client/src/test/java/compute/InstanceOperationsIT.java b/compute/cloud-client/src/test/java/compute/InstanceOperationsIT.java index bbabd022845..eae0ac8c2ec 100644 --- a/compute/cloud-client/src/test/java/compute/InstanceOperationsIT.java +++ b/compute/cloud-client/src/test/java/compute/InstanceOperationsIT.java @@ -1,3 +1,19 @@ +/* + * 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 compute; import static com.google.common.truth.Truth.assertWithMessage; From 8adf81977dbda0e6c514fcc0852c8890bc930d4b Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Wed, 11 May 2022 13:20:02 +0530 Subject: [PATCH 09/11] force adding junit4 to be used in surefire plugin --- compute/cloud-client/pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compute/cloud-client/pom.xml b/compute/cloud-client/pom.xml index 60eff0d8fdd..cc4d85a5b5c 100644 --- a/compute/cloud-client/pom.xml +++ b/compute/cloud-client/pom.xml @@ -113,6 +113,13 @@ org.apache.maven.plugins maven-surefire-plugin 3.0.0-M6 + + + org.apache.maven.surefire + surefire-junit47 + 3.0.0-M6 + + all From 44ec3be074c86320af68e2d4491c2256b55f91a3 Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Wed, 11 May 2022 14:15:34 +0530 Subject: [PATCH 10/11] revert junit dependency and adding skiptest --- compute/cloud-client/pom.xml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/compute/cloud-client/pom.xml b/compute/cloud-client/pom.xml index cc4d85a5b5c..2760d69030f 100644 --- a/compute/cloud-client/pom.xml +++ b/compute/cloud-client/pom.xml @@ -113,13 +113,6 @@ org.apache.maven.plugins maven-surefire-plugin 3.0.0-M6 - - - org.apache.maven.surefire - surefire-junit47 - 3.0.0-M6 - - all @@ -129,6 +122,7 @@ **/*IT.java + ${skipTests} From 001a326c55eb2652c82a8d65f2551362e3b8ff52 Mon Sep 17 00:00:00 2001 From: SitaLakshmi Date: Wed, 11 May 2022 15:42:36 +0530 Subject: [PATCH 11/11] added skiptest for failsafe plugin --- compute/cloud-client/pom.xml | 10 +++++++++- .../src/main/java/compute/SetUsageExportBucket.java | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/compute/cloud-client/pom.xml b/compute/cloud-client/pom.xml index 2760d69030f..e07ac0200df 100644 --- a/compute/cloud-client/pom.xml +++ b/compute/cloud-client/pom.xml @@ -122,7 +122,15 @@ **/*IT.java - ${skipTests} + false + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.0.0-M6 + + true diff --git a/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java b/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java index 2645b042241..4ffc676aac2 100644 --- a/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java +++ b/compute/cloud-client/src/main/java/compute/SetUsageExportBucket.java @@ -168,7 +168,7 @@ public static boolean disableUsageExportBucket(String project) } // Wait for the settings to be effected. - TimeUnit.SECONDS.sleep(10); + TimeUnit.SECONDS.sleep(15); // Return false if the usage reports is disabled. return projectsClient.get(project).getUsageExportLocation().hasBucketName(); }