diff --git a/compute/cloud-client/pom.xml b/compute/cloud-client/pom.xml
index 46f262fc7ab..e07ac0200df 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,33 @@
1.0-SNAPSHOT
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 3.0.0-M6
+
+
+ all
+ true
+ 10C
+ true
+
+ **/*IT.java
+
+ false
+
+
+
+ org.apache.maven.plugins
+ maven-failsafe-plugin
+ 3.0.0-M6
+
+ true
+
+
+
+
+
diff --git a/compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java b/compute/cloud-client/src/main/java/compute/CreateEncryptedInstance.java
index c1a36efa86b..8829834adbc 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,7 @@ 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..61a72e8ad10 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,7 @@ 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..6c012330f1f 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,7 @@ 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..3d8e2bea792 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,8 @@ 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..085162f1824 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,8 @@ 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 +100,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 +125,8 @@ 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..dad7c6f6372 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,7 @@ 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 +262,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 +289,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 +309,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 +337,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 +360,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 +391,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..1d9595972d4 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,7 @@ 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..602db9d2d57 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,7 @@ 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..0985adc6c3a 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,7 @@ 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..f3533778ec7 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,7 @@ 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..5b05620e39e 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,8 @@ 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..5cbda7d58e4 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,7 @@ 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..ed6381e5759 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,7 @@ 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..4ffc676aac2 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,7 @@ 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 +144,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 +159,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);
@@ -166,7 +168,7 @@ public static boolean disableUsageExportBucket(String project)
}
// Wait for the settings to be effected.
- TimeUnit.SECONDS.sleep(5);
+ TimeUnit.SECONDS.sleep(15);
// 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 7dc0eb70102..9e5b2b62704 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,7 @@ 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..3c8ab90bb7e 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,7 @@ 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..ce22aa11458 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,7 @@ 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..6a64961274d
--- /dev/null
+++ b/compute/cloud-client/src/test/java/compute/FirewallIT.java
@@ -0,0 +1,164 @@
+/*
+ * 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 {
+ /* (**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/InstanceOperationsIT.java b/compute/cloud-client/src/test/java/compute/InstanceOperationsIT.java
new file mode 100644
index 00000000000..eae0ac8c2ec
--- /dev/null
+++ b/compute/cloud-client/src/test/java/compute/InstanceOperationsIT.java
@@ -0,0 +1,176 @@
+/*
+ * 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.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/InstanceTemplatesIT.java b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java
index 0fb8b4bef49..6bef26c0c06 100644
--- a/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java
+++ b/compute/cloud-client/src/test/java/compute/InstanceTemplatesIT.java
@@ -27,19 +27,21 @@
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 {
-
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";
@@ -59,8 +61,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");
@@ -113,11 +117,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 +141,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 +150,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..cd0552603f7
--- /dev/null
+++ b/compute/cloud-client/src/test/java/compute/InstancesAdvancedIT.java
@@ -0,0 +1,276 @@
+/*
+ * 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.Status;
+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);
+ }
+ }
+
+
+ @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 = 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 = 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 = 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 = 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 = 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 = 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 673f9fc446d..9c0a0f1dd2b 100644
--- a/compute/cloud-client/src/test/java/compute/SnippetsIT.java
+++ b/compute/cloud-client/src/test/java/compute/SnippetsIT.java
@@ -20,19 +20,9 @@
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;
@@ -41,49 +31,33 @@
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.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");
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;
- 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 +67,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");
@@ -102,22 +78,12 @@ public static void setUp() throws IOException, InterruptedException, ExecutionEx
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();
- 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();
+ RAW_KEY = Util.getBase64EncodedKey();
// Cleanup existing stale resources.
Util.cleanUpExistingInstances("my-new-test-instance", PROJECT_ID, ZONE);
@@ -125,242 +91,53 @@ public static void setUp() throws IOException, InterruptedException, ExecutionEx
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
.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());
- }
- }
-
- 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";
- 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 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);
- return response.getStatus();
- }
- }
-
- @Before
+ @BeforeEach
public void beforeEach() {
stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
}
- @After
+ @AfterEach
public void afterEach() {
stdOut = null;
System.setOut(null);
@@ -369,58 +146,17 @@ 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());
}
- @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 +171,20 @@ public void testListAllInstances() throws IOException {
}
@Test
- public void testDeleteInstance() throws IOException, InterruptedException, ExecutionException {
- 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);
@@ -489,69 +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());
-
- // 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.
- 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 {
- 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 f68cca0f0ce..cf0adb0de7e 100644
--- a/compute/cloud-client/src/test/java/compute/Util.java
+++ b/compute/cloud-client/src/test/java/compute/Util.java
@@ -16,15 +16,27 @@
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.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.
@@ -36,14 +48,15 @@ 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 {
- for (InstanceTemplate template : ListInstanceTemplates.listInstanceTemplates(projectId)
+ throws IOException, ExecutionException, InterruptedException, TimeoutException {
+ 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());
}
}
@@ -54,15 +67,16 @@ && isCreatedBeforeThresholdTime(template.getCreationTimestamp())) {
// has creation timestamp >24 hours.
public static void cleanUpExistingInstances(String prefixToDelete, String projectId,
String instanceZone)
- throws IOException, ExecutionException, InterruptedException {
- for (Entry instanceGroup : ListAllInstances.listAllInstances(
- projectId).iterateAll()) {
+ throws IOException, ExecutionException, InterruptedException, TimeoutException {
+ 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());
}
}
@@ -74,4 +88,52 @@ 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);
+ }
+ }
+
+ 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
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);