5
5
6
6
import com .azure .core .http .HttpPipeline ;
7
7
import com .azure .core .http .rest .PagedIterable ;
8
+ import com .azure .core .management .Region ;
9
+ import com .azure .core .management .profile .AzureProfile ;
8
10
import com .azure .resourcemanager .compute .models .CreationSourceType ;
9
11
import com .azure .resourcemanager .compute .models .Disk ;
10
12
import com .azure .resourcemanager .compute .models .DiskCreateOption ;
13
15
import com .azure .resourcemanager .compute .models .SnapshotSkuType ;
14
16
import com .azure .resourcemanager .resources .models .ResourceGroup ;
15
17
import com .azure .resourcemanager .test .utils .TestUtilities ;
16
- import com .azure .core .management .Region ;
17
- import com .azure .core .management .profile .AzureProfile ;
18
18
import org .junit .jupiter .api .Assertions ;
19
19
import org .junit .jupiter .api .Test ;
20
20
21
+ import java .time .Duration ;
22
+
21
23
public class ManagedDiskOperationsTests extends ComputeManagementTest {
22
24
private String rgName = "" ;
25
+ private String rgName2 = null ;
23
26
private Region region = Region .US_WEST_CENTRAL ;
27
+ private Region region2 = Region .US_EAST ;
24
28
25
29
@ Override
26
30
protected void initializeClients (HttpPipeline httpPipeline , AzureProfile profile ) {
@@ -31,6 +35,9 @@ protected void initializeClients(HttpPipeline httpPipeline, AzureProfile profile
31
35
@ Override
32
36
protected void cleanUpResources () {
33
37
resourceManager .resourceGroups ().beginDeleteByName (rgName );
38
+ if (rgName2 != null ) {
39
+ resourceManager .resourceGroups ().beginDeleteByName (rgName2 );
40
+ }
34
41
}
35
42
36
43
@ Test
@@ -237,4 +244,121 @@ public void canOperateOnManagedDiskFromSnapshot() {
237
244
Assertions .assertEquals (fromSnapshotDisk .source ().type (), CreationSourceType .COPIED_FROM_SNAPSHOT );
238
245
Assertions .assertTrue (fromSnapshotDisk .source ().sourceId ().equalsIgnoreCase (snapshot .id ()));
239
246
}
247
+
248
+ @ Test
249
+ public void canCopyStartIncrementalSnapshot () {
250
+ rgName2 = generateRandomResourceName ("rg" , 15 );
251
+ final String emptyDiskName = generateRandomResourceName ("md-empty-" , 20 );
252
+ final String snapshotName = generateRandomResourceName ("snp-" , 20 );
253
+ final String snapshotName2 = generateRandomResourceName ("snp-" , 20 );
254
+ final String newRegionSnapshotName = generateRandomResourceName ("snp-newregion-" , 20 );
255
+ final String snapshotBasedDiskName = generateRandomResourceName ("md-snp-newregion-" , 20 );
256
+
257
+ ResourceGroup resourceGroup = resourceManager .resourceGroups ().define (rgName ).withRegion (region ).create ();
258
+ ResourceGroup resourceGroup2 = resourceManager .resourceGroups ().define (rgName2 ).withRegion (region2 ).create ();
259
+
260
+ // create disk to copy
261
+ Disk emptyDisk =
262
+ computeManager
263
+ .disks ()
264
+ .define (emptyDiskName )
265
+ .withRegion (region )
266
+ .withExistingResourceGroup (resourceGroup )
267
+ .withData ()
268
+ .withSizeInGB (100 )
269
+ .create ();
270
+
271
+ // create incremental snapshot from the disk
272
+ Snapshot snapshot =
273
+ computeManager
274
+ .snapshots ()
275
+ .define (snapshotName )
276
+ .withRegion (region )
277
+ .withExistingResourceGroup (resourceGroup )
278
+ .withDataFromDisk (emptyDisk )
279
+ .withSku (SnapshotSkuType .STANDARD_LRS )
280
+ .withIncremental (true )
281
+ .create ();
282
+
283
+ Assertions .assertTrue (snapshot .incremental ());
284
+ Assertions .assertEquals (CreationSourceType .COPIED_FROM_DISK , snapshot .source ().type ());
285
+ Assertions .assertEquals (DiskCreateOption .COPY , snapshot .creationMethod ());
286
+ Assertions .assertThrows (IllegalStateException .class , snapshot ::awaitCopyStartCompletion );
287
+
288
+ // copy the snapshot to the same region
289
+ Snapshot snapshotSameRegion =
290
+ computeManager
291
+ .snapshots ()
292
+ .define (snapshotName2 )
293
+ .withRegion (region )
294
+ .withExistingResourceGroup (resourceGroup )
295
+ .withDataFromSnapshot (snapshot )
296
+ .withCopyStart ()
297
+ .withIncremental (true )
298
+ .create ();
299
+
300
+ Assertions .assertTrue (snapshotSameRegion .incremental ());
301
+ Assertions .assertEquals (CreationSourceType .COPIED_FROM_SNAPSHOT , snapshotSameRegion .source ().type ());
302
+ Assertions .assertEquals (DiskCreateOption .COPY_START , snapshotSameRegion .creationMethod ());
303
+ Assertions .assertNull (snapshotSameRegion .copyCompletionError ());
304
+ // we don't wait for CopyStart to finish, so it should be in progress
305
+ Assertions .assertNotEquals (100 , snapshotSameRegion .copyCompletionPercent ());
306
+
307
+ computeManager
308
+ .snapshots ()
309
+ .deleteById (snapshotSameRegion .id ());
310
+
311
+ Snapshot snapshotSameRegion2 = computeManager
312
+ .snapshots ()
313
+ .define (snapshotName2 )
314
+ .withRegion (region )
315
+ .withExistingResourceGroup (resourceGroup )
316
+ .withDataFromSnapshot (snapshot )
317
+ .withCopyStart ()
318
+ .withIncremental (true )
319
+ .create ();
320
+ Assertions .assertFalse (snapshotSameRegion2 .awaitCopyStartCompletion (Duration .ofMillis (1 )));
321
+ Assertions .assertTrue (snapshotSameRegion2 .awaitCopyStartCompletion (Duration .ofHours (24 )));
322
+
323
+ // copy the snapshot to a new region
324
+ Snapshot snapshotNewRegion =
325
+ computeManager
326
+ .snapshots ()
327
+ .define (newRegionSnapshotName )
328
+ .withRegion (region2 )
329
+ .withExistingResourceGroup (resourceGroup2 )
330
+ .withDataFromSnapshot (snapshot )
331
+ .withCopyStart ()
332
+ .withIncremental (true )
333
+ .create ();
334
+ snapshotNewRegion .awaitCopyStartCompletion ();
335
+
336
+ Assertions .assertTrue (snapshotNewRegion .incremental ());
337
+ Assertions .assertEquals (CreationSourceType .COPIED_FROM_SNAPSHOT , snapshotNewRegion .source ().type ());
338
+ Assertions .assertEquals (DiskCreateOption .COPY_START , snapshotNewRegion .creationMethod ());
339
+ Assertions .assertEquals (100 , snapshotNewRegion .copyCompletionPercent ());
340
+ Assertions .assertNull (snapshotNewRegion .copyCompletionError ());
341
+
342
+ // create disk from snapshot in the new region
343
+ Disk fromSnapshotDisk =
344
+ computeManager
345
+ .disks ()
346
+ .define (snapshotBasedDiskName )
347
+ .withRegion (region2 )
348
+ .withExistingResourceGroup (resourceGroup2 )
349
+ .withData ()
350
+ .fromSnapshot (snapshotNewRegion )
351
+ .withSizeInGB (300 )
352
+ .create ();
353
+
354
+ Assertions .assertNotNull (fromSnapshotDisk .id ());
355
+ Assertions .assertTrue (fromSnapshotDisk .name ().equalsIgnoreCase (snapshotBasedDiskName ));
356
+ Assertions .assertEquals (fromSnapshotDisk .sku (), DiskSkuTypes .STANDARD_LRS );
357
+ Assertions .assertEquals (fromSnapshotDisk .creationMethod (), DiskCreateOption .COPY );
358
+ Assertions .assertEquals (fromSnapshotDisk .sizeInGB (), 300 );
359
+ Assertions .assertNull (fromSnapshotDisk .osType ());
360
+ Assertions .assertNotNull (fromSnapshotDisk .source ());
361
+ Assertions .assertEquals (fromSnapshotDisk .source ().type (), CreationSourceType .COPIED_FROM_SNAPSHOT );
362
+ Assertions .assertTrue (fromSnapshotDisk .source ().sourceId ().equalsIgnoreCase (snapshotNewRegion .id ()));
363
+ }
240
364
}
0 commit comments