From d0ebcd415c9df96d3de771429dfcebb4625230b7 Mon Sep 17 00:00:00 2001 From: Everett Toews Date: Tue, 13 Nov 2012 17:54:10 -0600 Subject: [PATCH] Added awaitDeleted() to VolumePredicates and SnapshotPredicates. Improved Volume.forId(). --- .../openstack/cinder/v1/domain/Volume.java | 10 +++++- .../v1/predicates/SnapshotPredicates.java | 33 ++++++++++++++++++- .../v1/predicates/VolumePredicates.java | 31 +++++++++++++++++ .../cinder/v1/domain/VolumeTest.java | 16 +++++++++ .../VolumeAndSnapshotApiLiveTest.java | 16 ++------- .../v1/features/VolumeApiExpectTest.java | 2 +- 6 files changed, 91 insertions(+), 17 deletions(-) create mode 100644 labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/domain/VolumeTest.java diff --git a/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/domain/Volume.java b/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/domain/Volume.java index 9075c2fcfb..6e5760fd0f 100644 --- a/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/domain/Volume.java +++ b/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/domain/Volume.java @@ -206,8 +206,16 @@ public class Volume { } } + /** + * Creates a dummy Volume when you need a Volume with just the volumeId. + * Several fields must be set in the returned Volume: + * + * 1. status=Status.UNRECOGNIZED + * 2. zone="nova" + * 3. created=[The Date the method was called] + */ public static Volume forId(String volumeId) { - return builder().id(volumeId).build(); + return builder().id(volumeId).status(Status.UNRECOGNIZED).zone("nova").created(new Date()).build(); } private final String id; diff --git a/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/predicates/SnapshotPredicates.java b/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/predicates/SnapshotPredicates.java index 81ba385cd4..4c001ad314 100644 --- a/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/predicates/SnapshotPredicates.java +++ b/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/predicates/SnapshotPredicates.java @@ -74,6 +74,19 @@ public class SnapshotPredicates { return new RetryablePredicate(statusPredicate, 1200, 5, 5, TimeUnit.SECONDS); } + /** + * Wait until a Snapshot no longer exists. + * + * @param snapshotApi The SnapshotApi in the zone where your Snapshot resides. + * @return RetryablePredicate That will check the whether the Snapshot exists + * every 5 seconds for a maxiumum of 20 minutes. + */ + public static RetryablePredicate awaitDeleted(SnapshotApi snapshotApi) { + DeletedPredicate deletedPredicate = new DeletedPredicate(snapshotApi); + + return new RetryablePredicate(deletedPredicate, 1200, 5, 5, TimeUnit.SECONDS); + } + public static RetryablePredicate awaitStatus( SnapshotApi snapshotApi, Volume.Status status, long maxWaitInSec, long periodInSec) { StatusUpdatedPredicate statusPredicate = new StatusUpdatedPredicate(snapshotApi, status); @@ -95,7 +108,7 @@ public class SnapshotPredicates { */ @Override public boolean apply(Snapshot snapshot) { - checkNotNull(snapshot, "snapshotId must be defined"); + checkNotNull(snapshot, "snapshot must be defined"); if (status.equals(snapshot.getStatus())) { return true; @@ -108,4 +121,22 @@ public class SnapshotPredicates { } } } + + private static class DeletedPredicate implements Predicate { + private SnapshotApi snapshotApi; + + public DeletedPredicate(SnapshotApi snapshotApi) { + this.snapshotApi = checkNotNull(snapshotApi, "snapshotApi must be defined"); + } + + /** + * @return boolean Return true when the snapshot is deleted, false otherwise + */ + @Override + public boolean apply(Snapshot snapshot) { + checkNotNull(snapshot, "snapshot must be defined"); + + return snapshotApi.get(snapshot.getId()) == null; + } + } } \ No newline at end of file diff --git a/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/predicates/VolumePredicates.java b/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/predicates/VolumePredicates.java index e6eaf82cc8..b24a595e93 100644 --- a/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/predicates/VolumePredicates.java +++ b/labs/openstack-cinder/src/main/java/org/jclouds/openstack/cinder/v1/predicates/VolumePredicates.java @@ -85,6 +85,19 @@ public class VolumePredicates { return new RetryablePredicate(statusPredicate, 600, 5, 5, TimeUnit.SECONDS); } + + /** + * Wait until a Volume no longer exists. + * + * @param volumeApi The VolumeApi in the zone where your Volume resides. + * @return RetryablePredicate That will check the whether the Volume exists + * every 5 seconds for a maxiumum of 10 minutes. + */ + public static RetryablePredicate awaitDeleted(VolumeApi volumeApi) { + DeletedPredicate deletedPredicate = new DeletedPredicate(volumeApi); + + return new RetryablePredicate(deletedPredicate, 600, 5, 5, TimeUnit.SECONDS); + } public static RetryablePredicate awaitStatus( VolumeApi volumeApi, Volume.Status status, long maxWaitInSec, long periodInSec) { @@ -120,4 +133,22 @@ public class VolumePredicates { } } } + + private static class DeletedPredicate implements Predicate { + private VolumeApi volumeApi; + + public DeletedPredicate(VolumeApi volumeApi) { + this.volumeApi = checkNotNull(volumeApi, "volumeApi must be defined"); + } + + /** + * @return boolean Return true when the snapshot is deleted, false otherwise + */ + @Override + public boolean apply(Volume volume) { + checkNotNull(volume, "volume must be defined"); + + return volumeApi.get(volume.getId()) == null; + } + } } \ No newline at end of file diff --git a/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/domain/VolumeTest.java b/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/domain/VolumeTest.java new file mode 100644 index 0000000000..519706411b --- /dev/null +++ b/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/domain/VolumeTest.java @@ -0,0 +1,16 @@ +package org.jclouds.openstack.cinder.v1.domain; + +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "VolumeTest") +public class VolumeTest { + public void testVolumeForId() { + Volume volume = Volume.forId("60761c60-0f56-4499-b522-ff13e120af10"); + + assertEquals(volume.getId(), "60761c60-0f56-4499-b522-ff13e120af10"); + assertEquals(volume.getStatus(), Volume.Status.UNRECOGNIZED); + assertEquals(volume.getZone(), "nova"); + } +} diff --git a/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/features/VolumeAndSnapshotApiLiveTest.java b/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/features/VolumeAndSnapshotApiLiveTest.java index 84979cdead..6880767158 100644 --- a/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/features/VolumeAndSnapshotApiLiveTest.java +++ b/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/features/VolumeAndSnapshotApiLiveTest.java @@ -31,13 +31,11 @@ import org.jclouds.openstack.cinder.v1.options.CreateSnapshotOptions; import org.jclouds.openstack.cinder.v1.options.CreateVolumeOptions; import org.jclouds.openstack.cinder.v1.predicates.SnapshotPredicates; import org.jclouds.openstack.cinder.v1.predicates.VolumePredicates; -import org.jclouds.predicates.RetryablePredicate; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.google.common.base.Objects; -import com.google.common.base.Predicate; import com.google.common.collect.Iterables; /** @@ -71,22 +69,12 @@ public class VolumeAndSnapshotApiLiveTest extends BaseCinderApiLiveTest { protected void tearDownContext() { if (testSnapshot != null) { assertTrue(snapshotApi.delete(testSnapshot.getId())); - assertTrue(new RetryablePredicate(new Predicate() { - @Override - public boolean apply(SnapshotApi snapshotApi) { - return snapshotApi.get(testSnapshot.getId()) == null; - } - }, 300 * 1000L).apply(snapshotApi)); + assertTrue(SnapshotPredicates.awaitDeleted(snapshotApi).apply(testSnapshot)); } if (testVolume != null) { assertTrue(volumeApi.delete(testVolume.getId())); - assertTrue(new RetryablePredicate(new Predicate() { - @Override - public boolean apply(VolumeApi volumeApi) { - return volumeApi.get(testVolume.getId()) == null; - } - }, 300 * 1000L).apply(volumeApi)); + assertTrue(VolumePredicates.awaitDeleted(volumeApi).apply(testVolume)); } super.tearDownContext(); diff --git a/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/features/VolumeApiExpectTest.java b/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/features/VolumeApiExpectTest.java index bf17c4d7a9..89c2cacf57 100644 --- a/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/features/VolumeApiExpectTest.java +++ b/labs/openstack-cinder/src/test/java/org/jclouds/openstack/cinder/v1/features/VolumeApiExpectTest.java @@ -195,7 +195,7 @@ public class VolumeApiExpectTest extends BaseCinderApiExpectTest { assertFalse(api.delete("60761c60-0f56-4499-b522-ff13e120af10")); } - + protected Volume testVolumeCreate() { return Volume.builder() .id("60761c60-0f56-4499-b522-ff13e120af10")