Skip to content

Commit 9203e00

Browse files
authored
fix: Update GCF compute tests (GoogleCloudPlatform#7698)
* Update GCF compute tests * lint * add exception * Use UUID * Update AutoLabelInstanceTest.java
1 parent 04ec857 commit 9203e00

File tree

1 file changed

+117
-2
lines changed

1 file changed

+117
-2
lines changed

functions/v2/label-compute-instance/src/test/java/functions/AutoLabelInstanceTest.java

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,29 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.compute.v1.AttachedDisk;
23+
import com.google.cloud.compute.v1.AttachedDisk.Type;
24+
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
25+
import com.google.cloud.compute.v1.DeleteInstanceRequest;
26+
import com.google.cloud.compute.v1.InsertInstanceRequest;
27+
import com.google.cloud.compute.v1.Instance;
28+
import com.google.cloud.compute.v1.InstancesClient;
29+
import com.google.cloud.compute.v1.NetworkInterface;
30+
import com.google.cloud.compute.v1.Operation;
2131
import com.google.common.testing.TestLogHandler;
2232
import com.google.gson.Gson;
2333
import com.google.gson.JsonObject;
2434
import io.cloudevents.CloudEvent;
2535
import io.cloudevents.core.builder.CloudEventBuilder;
36+
import java.io.IOException;
2637
import java.net.URI;
38+
import java.util.UUID;
39+
import java.util.concurrent.ExecutionException;
40+
import java.util.concurrent.TimeUnit;
41+
import java.util.concurrent.TimeoutException;
2742
import java.util.logging.Logger;
43+
import org.junit.AfterClass;
2844
import org.junit.BeforeClass;
2945
import org.junit.Test;
3046
import org.junit.runner.RunWith;
@@ -37,13 +53,26 @@ public class AutoLabelInstanceTest {
3753

3854
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
3955
private static final String ZONE = "us-central1-a";
40-
private static final String INSTANCE = "lite1";
56+
private static final String INSTANCE = "gcf-test" + UUID.randomUUID().toString();
57+
private static final int TIMEOUT = 3;
4158

4259
@BeforeClass
43-
public static void beforeClass() {
60+
public static void beforeClass()
61+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
62+
assertThat(PROJECT_ID).isNotNull();
63+
try {
64+
createInstance(PROJECT_ID, ZONE, INSTANCE);
65+
} catch (Exception e) {
66+
System.out.println("VM already exists: " + e);
67+
}
4468
logger.addHandler(logHandler);
4569
}
4670

71+
@AfterClass
72+
public static void cleanup() throws Exception {
73+
deleteInstance(PROJECT_ID, ZONE, INSTANCE);
74+
}
75+
4776
@Test
4877
public void functionsAutoLabelInstance() throws Exception {
4978
// Build a CloudEvent Log Entry
@@ -81,4 +110,90 @@ public void functionsAutoLabelInstance() throws Exception {
81110
"test-gmail-com", INSTANCE))
82111
.isEqualTo(logHandler.getStoredLogRecords().get(0).getMessage());
83112
}
113+
114+
public static void createInstance(String project, String zone, String instanceName)
115+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
116+
String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
117+
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
118+
long diskSizeGb = 10L;
119+
String networkName = "default";
120+
121+
try (InstancesClient instancesClient = InstancesClient.create()) {
122+
// Instance creation requires at least one persistent disk and one network interface.
123+
AttachedDisk disk =
124+
AttachedDisk.newBuilder()
125+
.setBoot(true)
126+
.setAutoDelete(true)
127+
.setType(Type.PERSISTENT.toString())
128+
.setDeviceName("disk-1")
129+
.setInitializeParams(
130+
AttachedDiskInitializeParams.newBuilder()
131+
.setSourceImage(sourceImage)
132+
.setDiskSizeGb(diskSizeGb)
133+
.build())
134+
.build();
135+
136+
// Use the network interface provided in the networkName argument.
137+
NetworkInterface networkInterface =
138+
NetworkInterface.newBuilder().setName(networkName).build();
139+
140+
// Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
141+
Instance instanceResource =
142+
Instance.newBuilder()
143+
.setName(instanceName)
144+
.setMachineType(machineType)
145+
.addDisks(disk)
146+
.addNetworkInterfaces(networkInterface)
147+
.build();
148+
149+
System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
150+
151+
// Insert the instance in the specified project and zone.
152+
InsertInstanceRequest insertInstanceRequest =
153+
InsertInstanceRequest.newBuilder()
154+
.setProject(project)
155+
.setZone(zone)
156+
.setInstanceResource(instanceResource)
157+
.build();
158+
159+
OperationFuture<Operation, Operation> operation =
160+
instancesClient.insertAsync(insertInstanceRequest);
161+
162+
// Wait for the operation to complete.
163+
Operation response = operation.get(TIMEOUT, TimeUnit.MINUTES);
164+
165+
if (response.hasError()) {
166+
System.out.println("Instance creation failed ! ! " + response);
167+
return;
168+
}
169+
System.out.println("Operation Status: " + response.getStatus());
170+
}
171+
}
172+
173+
public static void deleteInstance(String project, String zone, String instanceName)
174+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
175+
try (InstancesClient instancesClient = InstancesClient.create()) {
176+
177+
System.out.printf("Deleting instance: %s ", instanceName);
178+
179+
// Describe which instance is to be deleted.
180+
DeleteInstanceRequest deleteInstanceRequest =
181+
DeleteInstanceRequest.newBuilder()
182+
.setProject(project)
183+
.setZone(zone)
184+
.setInstance(instanceName)
185+
.build();
186+
187+
OperationFuture<Operation, Operation> operation =
188+
instancesClient.deleteAsync(deleteInstanceRequest);
189+
// Wait for the operation to complete.
190+
Operation response = operation.get(TIMEOUT, TimeUnit.MINUTES);
191+
192+
if (response.hasError()) {
193+
System.out.println("Instance deletion failed ! ! " + response);
194+
return;
195+
}
196+
System.out.println("Operation Status: " + response.getStatus());
197+
}
198+
}
84199
}

0 commit comments

Comments
 (0)