2020import static com .google .common .truth .Truth .assertWithMessage ;
2121import static compute .Util .getZone ;
2222
23+ import com .google .cloud .compute .v1 .CreateSnapshotRegionDiskRequest ;
2324import com .google .cloud .compute .v1 .Disk ;
2425import com .google .cloud .compute .v1 .Instance ;
2526import com .google .cloud .compute .v1 .Instance .Status ;
2627import 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 ;
2731import compute .disks .CloneEncryptedDisk ;
2832import compute .disks .CreateEncryptedDisk ;
2933import compute .disks .DeleteDisk ;
34+ import compute .disks .DeleteSnapshot ;
35+ import compute .disks .RegionalCreateFromSource ;
3036import java .io .ByteArrayOutputStream ;
3137import java .io .IOException ;
3238import java .io .PrintStream ;
3339import java .nio .charset .StandardCharsets ;
3440import java .time .LocalDateTime ;
41+ import java .util .Arrays ;
42+ import java .util .List ;
43+ import java .util .Optional ;
3544import java .util .UUID ;
3645import java .util .concurrent .ExecutionException ;
3746import java .util .concurrent .TimeUnit ;
3847import java .util .concurrent .TimeoutException ;
3948import org .junit .Assert ;
4049import org .junit .jupiter .api .AfterAll ;
41- import org .junit .jupiter .api .AfterEach ;
4250import org .junit .jupiter .api .BeforeAll ;
43- import org .junit .jupiter .api .BeforeEach ;
4451import org .junit .jupiter .api .Test ;
4552import org .junit .jupiter .api .Timeout ;
4653import org .junit .runner .RunWith ;
@@ -52,13 +59,21 @@ public class InstanceOperationsIT {
5259
5360 private static final String PROJECT_ID = System .getenv ("GOOGLE_CLOUD_PROJECT" );
5461 private static final String ZONE = getZone ();
62+ private static final String REGION = ZONE .substring (0 , ZONE .length () - 2 );
5563 private static String MACHINE_NAME ;
5664 private static String MACHINE_NAME_ENCRYPTED ;
5765 private static String DISK_NAME ;
5866 private static String ENCRYPTED_DISK_NAME ;
5967 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 ));
6277
6378 // Check if the required environment variables are set.
6479 public static void requireEnvVar (String envVarName ) {
@@ -72,47 +87,42 @@ public static void setUp()
7287 requireEnvVar ("GOOGLE_APPLICATION_CREDENTIALS" );
7388 requireEnvVar ("GOOGLE_CLOUD_PROJECT" );
7489
75- final PrintStream out = System .out ;
76- ByteArrayOutputStream stdOut = new ByteArrayOutputStream ();
77- System .setOut (new PrintStream (stdOut ));
78-
7990 MACHINE_NAME = "test-instance-operation-" + UUID .randomUUID ();
8091 MACHINE_NAME_ENCRYPTED = "test-instance-encrypted-" + UUID .randomUUID ();
8192 DISK_NAME = "test-clone-disk-enc-" + UUID .randomUUID ();
8293 ENCRYPTED_DISK_NAME = "test-disk-enc-" + UUID .randomUUID ();
8394 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 ];
8998
9099 compute .CreateInstance .createInstance (PROJECT_ID , ZONE , MACHINE_NAME );
91100 compute .CreateEncryptedInstance
92101 .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 );
93105
94106 TimeUnit .SECONDS .sleep (30 );
95-
96- stdOut .close ();
97- System .setOut (out );
98107 }
99108
100-
101109 @ AfterAll
102110 public static void cleanup ()
103111 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 );
107118
108119 // Delete all instances created for testing.
109120 compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , MACHINE_NAME_ENCRYPTED );
110121 compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , MACHINE_NAME );
122+ compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , INSTANCE_NAME );
111123 DeleteDisk .deleteDisk (PROJECT_ID , ZONE , DISK_NAME );
112124 DeleteDisk .deleteDisk (PROJECT_ID , ZONE , ENCRYPTED_DISK_NAME );
113-
114- stdOut .close ();
115- System .setOut (out );
125+ DeleteSnapshot .deleteSnapshot (PROJECT_ID , SNAPSHOT_NAME );
116126 }
117127
118128 private static Instance getInstance (String machineName ) throws IOException {
@@ -121,16 +131,28 @@ private static Instance getInstance(String machineName) throws IOException {
121131 }
122132 }
123133
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+ }
134156 }
135157
136158 @ Test
@@ -204,14 +226,17 @@ public void testEncryptedInstanceOperations()
204226 @ Test
205227 public void testCloneEncryptedDisk ()
206228 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+
209232 Instance instance = getInstance (MACHINE_NAME_ENCRYPTED );
210233 String diskType = String .format ("zones/%s/diskTypes/pd-standard" , ZONE );
211234 CloneEncryptedDisk .createDiskFromCustomerEncryptedKey (PROJECT_ID , ZONE , DISK_NAME , diskType , 10 ,
212235 instance .getDisks (0 ).getSource (), RAW_KEY .getBytes (
213236 StandardCharsets .UTF_8 ));
214237 assertThat (stdOut .toString ()).contains ("Disk cloned with customer encryption key." );
238+
239+ stdOut .close ();
215240 }
216241
217242 @ Test
@@ -228,4 +253,15 @@ public void testCreateEncryptedDisk()
228253 Assert .assertNotNull (encryptedDisk .getDiskEncryptionKey ());
229254 Assert .assertNotNull (encryptedDisk .getDiskEncryptionKey ().getSha256 ());
230255 }
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+ }
231267}
0 commit comments