18
18
19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
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 ;
21
31
import com .google .common .testing .TestLogHandler ;
22
32
import com .google .gson .Gson ;
23
33
import com .google .gson .JsonObject ;
24
34
import io .cloudevents .CloudEvent ;
25
35
import io .cloudevents .core .builder .CloudEventBuilder ;
36
+ import java .io .IOException ;
26
37
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 ;
27
42
import java .util .logging .Logger ;
43
+ import org .junit .AfterClass ;
28
44
import org .junit .BeforeClass ;
29
45
import org .junit .Test ;
30
46
import org .junit .runner .RunWith ;
@@ -37,13 +53,26 @@ public class AutoLabelInstanceTest {
37
53
38
54
private static final String PROJECT_ID = System .getenv ("GOOGLE_CLOUD_PROJECT" );
39
55
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 ;
41
58
42
59
@ 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
+ }
44
68
logger .addHandler (logHandler );
45
69
}
46
70
71
+ @ AfterClass
72
+ public static void cleanup () throws Exception {
73
+ deleteInstance (PROJECT_ID , ZONE , INSTANCE );
74
+ }
75
+
47
76
@ Test
48
77
public void functionsAutoLabelInstance () throws Exception {
49
78
// Build a CloudEvent Log Entry
@@ -81,4 +110,90 @@ public void functionsAutoLabelInstance() throws Exception {
81
110
"test-gmail-com" , INSTANCE ))
82
111
.isEqualTo (logHandler .getStoredLogRecords ().get (0 ).getMessage ());
83
112
}
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
+ }
84
199
}
0 commit comments