20
20
import static com .google .common .truth .Truth .assertWithMessage ;
21
21
import static compute .Util .getZone ;
22
22
23
+ import com .google .cloud .compute .v1 .CreateSnapshotRegionDiskRequest ;
23
24
import com .google .cloud .compute .v1 .Disk ;
24
25
import com .google .cloud .compute .v1 .Instance ;
25
26
import com .google .cloud .compute .v1 .Instance .Status ;
26
27
import com .google .cloud .compute .v1 .InstancesClient ;
28
+ import com .google .cloud .compute .v1 .Operation ;
29
+ import com .google .cloud .compute .v1 .RegionDisksClient ;
30
+ import com .google .cloud .compute .v1 .Snapshot ;
27
31
import compute .disks .CloneEncryptedDisk ;
28
32
import compute .disks .CreateEncryptedDisk ;
29
33
import compute .disks .DeleteDisk ;
34
+ import compute .disks .DeleteSnapshot ;
35
+ import compute .disks .RegionalCreateFromSource ;
30
36
import java .io .ByteArrayOutputStream ;
31
37
import java .io .IOException ;
32
38
import java .io .PrintStream ;
33
39
import java .nio .charset .StandardCharsets ;
34
40
import java .time .LocalDateTime ;
41
+ import java .util .Arrays ;
42
+ import java .util .List ;
43
+ import java .util .Optional ;
35
44
import java .util .UUID ;
36
45
import java .util .concurrent .ExecutionException ;
37
46
import java .util .concurrent .TimeUnit ;
38
47
import java .util .concurrent .TimeoutException ;
39
48
import org .junit .Assert ;
40
49
import org .junit .jupiter .api .AfterAll ;
41
- import org .junit .jupiter .api .AfterEach ;
42
50
import org .junit .jupiter .api .BeforeAll ;
43
- import org .junit .jupiter .api .BeforeEach ;
44
51
import org .junit .jupiter .api .Test ;
45
52
import org .junit .jupiter .api .Timeout ;
46
53
import org .junit .runner .RunWith ;
@@ -52,13 +59,21 @@ public class InstanceOperationsIT {
52
59
53
60
private static final String PROJECT_ID = System .getenv ("GOOGLE_CLOUD_PROJECT" );
54
61
private static final String ZONE = getZone ();
62
+ private static final String REGION = ZONE .substring (0 , ZONE .length () - 2 );
55
63
private static String MACHINE_NAME ;
56
64
private static String MACHINE_NAME_ENCRYPTED ;
57
65
private static String DISK_NAME ;
58
66
private static String ENCRYPTED_DISK_NAME ;
59
67
private static String RAW_KEY ;
60
-
61
- private ByteArrayOutputStream stdOut ;
68
+ private static String INSTANCE_NAME ;
69
+ private static final String DISK_TYPE = String .format ("regions/%s/diskTypes/pd-standard" , REGION );
70
+ private static String REPLICATED_DISK_NAME ;
71
+ private static String SNAPSHOT_NAME ;
72
+ private static final String DISK_SNAPSHOT_LINK =
73
+ String .format ("projects/%s/global/snapshots/%s" , PROJECT_ID , SNAPSHOT_NAME );
74
+ private static final List <String > REPLICA_ZONES = Arrays .asList (
75
+ String .format ("projects/%s/zones/%s-a" , PROJECT_ID , REGION ),
76
+ String .format ("projects/%s/zones/%s-b" , PROJECT_ID , REGION ));
62
77
63
78
// Check if the required environment variables are set.
64
79
public static void requireEnvVar (String envVarName ) {
@@ -72,47 +87,42 @@ public static void setUp()
72
87
requireEnvVar ("GOOGLE_APPLICATION_CREDENTIALS" );
73
88
requireEnvVar ("GOOGLE_CLOUD_PROJECT" );
74
89
75
- final PrintStream out = System .out ;
76
- ByteArrayOutputStream stdOut = new ByteArrayOutputStream ();
77
- System .setOut (new PrintStream (stdOut ));
78
-
79
90
MACHINE_NAME = "test-instance-operation-" + UUID .randomUUID ();
80
91
MACHINE_NAME_ENCRYPTED = "test-instance-encrypted-" + UUID .randomUUID ();
81
92
DISK_NAME = "test-clone-disk-enc-" + UUID .randomUUID ();
82
93
ENCRYPTED_DISK_NAME = "test-disk-enc-" + UUID .randomUUID ();
83
94
RAW_KEY = Util .getBase64EncodedKey ();
84
-
85
- // Cleanup existing stale resources.
86
- Util .cleanUpExistingInstances ("test-instance-" , PROJECT_ID , ZONE );
87
- Util .cleanUpExistingDisks ("test-clone-disk-enc-" , PROJECT_ID , ZONE );
88
- Util .cleanUpExistingDisks ("test-disk-enc-" , PROJECT_ID , ZONE );
95
+ INSTANCE_NAME = "test-instance-" + UUID .randomUUID ();
96
+ REPLICATED_DISK_NAME = "test-disk-replicated-" + UUID .randomUUID ();
97
+ SNAPSHOT_NAME = "test-snapshot-" + UUID .randomUUID ().toString ().split ("-" )[0 ];
89
98
90
99
compute .CreateInstance .createInstance (PROJECT_ID , ZONE , MACHINE_NAME );
91
100
compute .CreateEncryptedInstance
92
101
.createEncryptedInstance (PROJECT_ID , ZONE , MACHINE_NAME_ENCRYPTED , RAW_KEY );
102
+ RegionalCreateFromSource .createRegionalDisk (PROJECT_ID , REGION , REPLICA_ZONES ,
103
+ REPLICATED_DISK_NAME , DISK_TYPE , 200 , Optional .empty (), Optional .empty ());
104
+ createDiskSnapshot (PROJECT_ID , REGION , REPLICATED_DISK_NAME , SNAPSHOT_NAME );
93
105
94
106
TimeUnit .SECONDS .sleep (30 );
95
-
96
- stdOut .close ();
97
- System .setOut (out );
98
107
}
99
108
100
-
101
109
@ AfterAll
102
110
public static void cleanup ()
103
111
throws IOException , InterruptedException , ExecutionException , TimeoutException {
104
- final PrintStream out = System .out ;
105
- ByteArrayOutputStream stdOut = new ByteArrayOutputStream ();
106
- System .setOut (new PrintStream (stdOut ));
112
+ // Cleanup existing stale resources.
113
+ Util .cleanUpExistingInstances ("test-instance-" , PROJECT_ID , ZONE );
114
+ Util .cleanUpExistingDisks ("test-clone-disk-enc-" , PROJECT_ID , ZONE );
115
+ Util .cleanUpExistingDisks ("test-disk-enc-" , PROJECT_ID , ZONE );
116
+ Util .cleanUpExistingRegionalDisks ("test-disk-replicated-" , PROJECT_ID , REGION );
117
+ Util .cleanUpExistingSnapshots ("test-snapshot-" , PROJECT_ID );
107
118
108
119
// Delete all instances created for testing.
109
120
compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , MACHINE_NAME_ENCRYPTED );
110
121
compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , MACHINE_NAME );
122
+ compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , INSTANCE_NAME );
111
123
DeleteDisk .deleteDisk (PROJECT_ID , ZONE , DISK_NAME );
112
124
DeleteDisk .deleteDisk (PROJECT_ID , ZONE , ENCRYPTED_DISK_NAME );
113
-
114
- stdOut .close ();
115
- System .setOut (out );
125
+ DeleteSnapshot .deleteSnapshot (PROJECT_ID , SNAPSHOT_NAME );
116
126
}
117
127
118
128
private static Instance getInstance (String machineName ) throws IOException {
@@ -121,16 +131,28 @@ private static Instance getInstance(String machineName) throws IOException {
121
131
}
122
132
}
123
133
124
- @ BeforeEach
125
- public void beforeEach () {
126
- stdOut = new ByteArrayOutputStream ();
127
- System .setOut (new PrintStream (stdOut ));
128
- }
129
-
130
- @ AfterEach
131
- public void afterEach () {
132
- stdOut = null ;
133
- System .setOut (null );
134
+ public static void createDiskSnapshot (String project , String region , String diskName ,
135
+ String snapshotName )
136
+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
137
+ try (RegionDisksClient disksClient = RegionDisksClient .create ()) {
138
+
139
+ CreateSnapshotRegionDiskRequest createSnapshotDiskRequest =
140
+ CreateSnapshotRegionDiskRequest .newBuilder ()
141
+ .setProject (project )
142
+ .setRegion (region )
143
+ .setDisk (diskName )
144
+ .setSnapshotResource (Snapshot .newBuilder ()
145
+ .setName (snapshotName )
146
+ .build ())
147
+ .build ();
148
+
149
+ Operation operation = disksClient .createSnapshotAsync (createSnapshotDiskRequest )
150
+ .get (3 , TimeUnit .MINUTES );
151
+
152
+ if (operation .hasError ()) {
153
+ throw new Error ("Failed to create the snapshot" );
154
+ }
155
+ }
134
156
}
135
157
136
158
@ Test
@@ -204,14 +226,17 @@ public void testEncryptedInstanceOperations()
204
226
@ Test
205
227
public void testCloneEncryptedDisk ()
206
228
throws IOException , ExecutionException , InterruptedException , TimeoutException {
207
- Assert .assertEquals (Util .getInstanceStatus (PROJECT_ID , ZONE , MACHINE_NAME_ENCRYPTED ),
208
- "RUNNING" );
229
+ ByteArrayOutputStream stdOut = new ByteArrayOutputStream ();
230
+ System .setOut (new PrintStream (stdOut ));
231
+
209
232
Instance instance = getInstance (MACHINE_NAME_ENCRYPTED );
210
233
String diskType = String .format ("zones/%s/diskTypes/pd-standard" , ZONE );
211
234
CloneEncryptedDisk .createDiskFromCustomerEncryptedKey (PROJECT_ID , ZONE , DISK_NAME , diskType , 10 ,
212
235
instance .getDisks (0 ).getSource (), RAW_KEY .getBytes (
213
236
StandardCharsets .UTF_8 ));
214
237
assertThat (stdOut .toString ()).contains ("Disk cloned with customer encryption key." );
238
+
239
+ stdOut .close ();
215
240
}
216
241
217
242
@ Test
@@ -228,4 +253,15 @@ public void testCreateEncryptedDisk()
228
253
Assert .assertNotNull (encryptedDisk .getDiskEncryptionKey ());
229
254
Assert .assertNotNull (encryptedDisk .getDiskEncryptionKey ().getSha256 ());
230
255
}
256
+
257
+ @ Test
258
+ public void testCreateInstanceWithRegionalDiskFromSnapshot ()
259
+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
260
+ Operation .Status status = CreateInstanceWithRegionalDiskFromSnapshot
261
+ .createInstanceWithRegionalDiskFromSnapshot (
262
+ PROJECT_ID , ZONE , INSTANCE_NAME , REPLICATED_DISK_NAME ,
263
+ DISK_TYPE , DISK_SNAPSHOT_LINK , REPLICA_ZONES );
264
+
265
+ assertThat (status ).isEqualTo (Operation .Status .DONE );
266
+ }
231
267
}
0 commit comments