From 3a2660ad5deb91d2b95edaf1888b5e54474c5c6a Mon Sep 17 00:00:00 2001 From: Andrew Donald Kennedy Date: Tue, 22 Nov 2011 11:10:34 +0000 Subject: [PATCH] Added Snapshot live tests --- .../features/SnapshotAsyncClient.java | 2 +- .../features/SnapshotClientLiveTest.java | 149 ++++++++++++++++++ .../features/VolumeClientLiveTest.java | 3 - 3 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SnapshotClientLiveTest.java diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SnapshotAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SnapshotAsyncClient.java index 7e57cacb99..ca1d993dee 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SnapshotAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/SnapshotAsyncClient.java @@ -80,6 +80,7 @@ public interface SnapshotAsyncClient { @GET @Consumes(MediaType.APPLICATION_JSON) @QueryParams(keys = "command", values = "listSnapshots") + @SelectJson("snapshot") @Unwrap @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) ListenableFuture> listSnapshots(ListSnapshotsOptions... options); @@ -162,5 +163,4 @@ public interface SnapshotAsyncClient { @Unwrap @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) ListenableFuture> listSnapshotPolicies(@QueryParam("volumeid") long volumeId, ListSnapshotPoliciesOptions... options); - } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SnapshotClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SnapshotClientLiveTest.java new file mode 100644 index 0000000000..9aed65b862 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SnapshotClientLiveTest.java @@ -0,0 +1,149 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.cloudstack.features; + +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import org.jclouds.cloudstack.domain.AsyncCreateResponse; +import org.jclouds.cloudstack.domain.Snapshot; +import org.jclouds.cloudstack.domain.Volume; +import org.jclouds.cloudstack.domain.Zone; +import org.jclouds.cloudstack.options.ListSnapshotsOptions; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import javax.annotation.Nullable; +import java.util.Set; + +import static com.google.common.collect.Iterables.find; +import static org.testng.AssertJUnit.*; + +/** + * Tests behavior of {@code SnapshotClient} + * + * @author grkvlt@apache.org + */ +@Test(groups = "live", singleThreaded = true, testName = "SnapshotClientLiveTest") +public class SnapshotClientLiveTest extends BaseCloudStackClientLiveTest { + protected String prefix = System.getProperty("user.name"); + + private long zoneId; + + @BeforeMethod(groups = "live") + public void setZoneId() { + Set zones = client.getZoneClient().listZones(); + assertNotNull(zones); + assertFalse(zones.isEmpty()); + zoneId = Iterables.get(zones, 0).getId(); + } + + public void testListSnapshots() { + Set snapshots = client.getSnapshotClient().listSnapshots(); + assertNotNull(snapshots); + assertFalse(snapshots.isEmpty()); + + for (Snapshot snapshot : snapshots) { + checkSnapshot(snapshot); + } + } + + public void testListSnapshotsById() { + Iterable snapshotIds = Iterables.transform(client.getSnapshotClient().listSnapshots(), new Function() { + public Long apply(Snapshot input) { + return input.getId(); + } + }); + assertNotNull(snapshotIds); + assertFalse(Iterables.isEmpty(snapshotIds)); + + for (Long id : snapshotIds) { + Set found = client.getSnapshotClient().listSnapshots(ListSnapshotsOptions.Builder.id(id)); + assertNotNull(found); + assertEquals(1, found.size()); + Snapshot snapshot = Iterables.getOnlyElement(found); + assertEquals(id.longValue(), snapshot.getId()); + checkSnapshot(snapshot); + } + } + + public void testListSnapshotsNonexistantId() { + Set found = client.getSnapshotClient().listSnapshots(ListSnapshotsOptions.Builder.id(-1)); + assertNotNull(found); + assertTrue(found.isEmpty()); + } + + public void testGetSnapshotById() { + Iterable snapshotIds = Iterables.transform(client.getSnapshotClient().listSnapshots(), new Function() { + public Long apply(Snapshot input) { + return input.getId(); + } + }); + assertNotNull(snapshotIds); + assertFalse(Iterables.isEmpty(snapshotIds)); + + for (Long id : snapshotIds) { + Snapshot found = client.getSnapshotClient().getSnapshot(id); + assertNotNull(found); + assertEquals(id.longValue(), found.getId()); + checkSnapshot(found); + } + } + + public void testGetSnapshotNonexistantId() { + Snapshot found = client.getSnapshotClient().getSnapshot(-1); + assertNull(found); + } + + public void testCreateSnapshotFromVolume() { + // Pick some volume + long volumeId = Iterables.get(client.getVolumeClient().listVolumes(), 0).getId(); + + Snapshot snapshot = null; + while (snapshot == null) { + try { + AsyncCreateResponse job = client.getSnapshotClient().createSnapshot(volumeId); + assertTrue(jobComplete.apply(job.getJobId())); + snapshot = findSnapshotWithId(job.getId()); + } catch (IllegalStateException e) { + // TODO snapshot creation failed - retry? + } + } + + checkSnapshot(snapshot); + + // Delete the snapshot + client.getSnapshotClient().deleteSnapshot(snapshot.getId()); + } + + private void checkSnapshot(final Snapshot snapshot) { + assertNotNull(snapshot.getId()); + assertNotNull(snapshot.getName()); + assertNotSame(Snapshot.Type.UNRECOGNIZED, snapshot.getSnapshotType()); + } + + private Snapshot findSnapshotWithId(final long id) { + return find(client.getSnapshotClient().listSnapshots(), new Predicate() { + @Override + public boolean apply(Snapshot arg0) { + return arg0.getId() == id; + } + }); + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VolumeClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VolumeClientLiveTest.java index b1f25d2519..1051b6847a 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VolumeClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VolumeClientLiveTest.java @@ -183,8 +183,6 @@ public class VolumeClientLiveTest extends BaseCloudStackClientLiveTest { client.getVolumeClient().deleteVolume(volume.getId()); } - /* - // TODO Uncomment this test after SnapshotClient has test coverage. public void testCreateVolumeFromSnapshotInZoneAndDeleteVolume() { Set snapshots = client.getSnapshotClient().listSnapshots(); assertNotNull(snapshots); @@ -208,7 +206,6 @@ public class VolumeClientLiveTest extends BaseCloudStackClientLiveTest { // Delete the volume client.getVolumeClient().deleteVolume(volume.getId()); } - */ private void checkVolume(final Volume volume) { assertNotNull(volume.getId());