Clean up volume tests

This commit is contained in:
Andrew Donald Kennedy 2011-11-21 17:44:09 +00:00
parent 192956705b
commit 4095ebd4ab
1 changed files with 50 additions and 48 deletions

View File

@ -18,31 +18,39 @@
*/
package org.jclouds.cloudstack.features;
import static com.google.common.collect.Iterables.find;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNotSame;
import static com.google.common.collect.Iterables.*;
import static org.testng.AssertJUnit.*;
import java.util.Set;
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.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
/**
* Tests behavior of {@code SSHKeyPairClient}
* Tests behavior of {@code VolumeClient}
*
* @author Vijay Kiran
*/
@Test(groups = "live", singleThreaded = true, testName = "VolumeClientLiveTest")
public class VolumeClientLiveTest extends BaseCloudStackClientLiveTest {
protected String prefix = System.getProperty("user.name");
private long zoneId;
private long diskOfferingId;
private long snapshotId;
private Volume volume;
@BeforeMethod(groups = "live")
public void setZoneId() {
Set<Zone> zones = client.getZoneClient().listZones();
assertNotNull(zones);
assertFalse(zones.isEmpty());
zoneId = Iterables.get(zones, 0).getId();
}
public void testListVolumes() {
final Set<Volume> volumes = client.getVolumeClient().listVolumes();
@ -52,17 +60,15 @@ public class VolumeClientLiveTest extends BaseCloudStackClientLiveTest {
}
public void testCreateVolumeFromDiskofferingInZoneAndDeleteVolume() {
zoneId = Iterables.getFirst(client.getZoneClient().listZones(), null).getId();
// Pick some disk offering
diskOfferingId = Iterables.getFirst(client.getOfferingClient().listDiskOfferings(), null).getId();
long diskOfferingId = Iterables.get(client.getOfferingClient().listDiskOfferings(), 0).getId();
Volume volume = null;
while (volume == null) {
try {
AsyncCreateResponse job = client.getVolumeClient().createVolumeFromDiskOfferingInZone(prefix + "-jclouds-volume",
diskOfferingId, zoneId);
assert jobComplete.apply(job.getJobId());
assertTrue(jobComplete.apply(job.getJobId()));
volume = findVolumeWithId(job.getId());
} catch (IllegalStateException e) {
// TODO volume creation failed - retry?
@ -70,22 +76,21 @@ public class VolumeClientLiveTest extends BaseCloudStackClientLiveTest {
}
checkVolume(volume);
// Delete the volume
client.getVolumeClient().deleteVolume(volume.getId());
}
public void testCreateVolumeFromDiskofferingInZoneAndAttachVolumeToVirtualMachineAndDetachAndDelete() {
zoneId = Iterables.getFirst(client.getZoneClient().listZones(), null).getId();
// Pick some disk offering
diskOfferingId = Iterables.getFirst(client.getOfferingClient().listDiskOfferings(), null).getId();
long diskOfferingId = Iterables.get(client.getOfferingClient().listDiskOfferings(), 0).getId();
Volume volume = null;
while (volume == null) {
try {
AsyncCreateResponse job = client.getVolumeClient().createVolumeFromDiskOfferingInZone(prefix + "-jclouds-volume",
diskOfferingId, zoneId);
assert jobComplete.apply(job.getJobId());
assertTrue(jobComplete.apply(job.getJobId()));
volume = findVolumeWithId(job.getId());
} catch (IllegalStateException e) {
// TODO volume creation failed - retry?
@ -93,16 +98,17 @@ public class VolumeClientLiveTest extends BaseCloudStackClientLiveTest {
}
checkVolume(volume);
long virtualMachineId = Iterables.getFirst(client.getVirtualMachineClient().listVirtualMachines(), null).getId();
long virtualMachineId = Iterables.get(client.getVirtualMachineClient().listVirtualMachines(), 0).getId();
// Attach Volume
Volume attachedVolume = null;
while (attachedVolume == null) {
try {
AsyncCreateResponse job = client.getVolumeClient().attachVolume(volume.getId(), virtualMachineId);
assert jobComplete.apply(job.getJobId());
assertTrue(jobComplete.apply(job.getJobId()));
attachedVolume = findVolumeWithId(volume.getId());
assert attachedVolume.getVirtualMachineId() == virtualMachineId;
assert attachedVolume.getAttached() != null;
assertEquals(virtualMachineId, attachedVolume.getVirtualMachineId());
assertNotNull(attachedVolume.getAttached());
} catch (IllegalStateException e) {
// TODO volume creation failed - retry?
}
@ -113,7 +119,7 @@ public class VolumeClientLiveTest extends BaseCloudStackClientLiveTest {
while (detachedVolume == null) {
try {
AsyncCreateResponse job = client.getVolumeClient().detachVolume(volume.getId());
assert jobComplete.apply(job.getJobId());
assertTrue(jobComplete.apply(job.getJobId()));
detachedVolume = findVolumeWithId(volume.getId());
checkVolume(detachedVolume);
} catch (IllegalStateException e) {
@ -123,24 +129,22 @@ public class VolumeClientLiveTest extends BaseCloudStackClientLiveTest {
// Cleanup
client.getVolumeClient().deleteVolume(volume.getId());
}
/*
TODO Uncomment this test after SnapshotClient has test coverage.
// TODO Uncomment this test after SnapshotClient has test coverage.
public void testCreateVolumeFromSnapshotInZoneAndDeleteVolume() {
zoneId = Iterables.getFirst(client.getZoneClient().listZones(), null).getId();
final Set<Snapshot> snapshots = client.getSnapshotClient().listSnapshots();
Set<Snapshot> snapshots = client.getSnapshotClient().listSnapshots();
assertNotNull(snapshots);
assertNotSame(0, snapshots.size() );
snapshotId = Iterables.getFirst(snapshots, null).getId();
assertFalse(snapshots.isEmpty());
long snapshotId = Iterables.get(snapshots, 0).getId();
Volume volume = null;
while (volume == null) {
try {
AsyncCreateResponse job = client.getVolumeClient().createVolumeFromSnapshotInZone(prefix + "-jclouds-volume",
snapshotId, zoneId);
assert jobComplete.apply(job.getJobId());
assertTrue(jobComplete.apply(job.getJobId()));
volume = findVolumeWithId(job.getId());
} catch (IllegalStateException e) {
// TODO volume creation failed - retry?
@ -148,6 +152,7 @@ public class VolumeClientLiveTest extends BaseCloudStackClientLiveTest {
}
checkVolume(volume);
// Delete the volume
client.getVolumeClient().deleteVolume(volume.getId());
}
@ -161,13 +166,10 @@ public class VolumeClientLiveTest extends BaseCloudStackClientLiveTest {
private Volume findVolumeWithId(final long id) {
return find(client.getVolumeClient().listVolumes(), new Predicate<Volume>() {
@Override
public boolean apply(Volume arg0) {
return arg0.getId() == id;
}
});
}
}