code refactoring for the test

This commit is contained in:
Alex Yarmula 2010-02-19 10:47:52 -08:00
parent 338257ea98
commit 6786db2738
2 changed files with 37 additions and 40 deletions

View File

@ -21,6 +21,7 @@ package org.jclouds.tools.ebsresize;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.inject.Module; import com.google.inject.Module;
@ -77,7 +78,8 @@ public class InstanceVolumeManager {
this(accessKeyId, secretKey, new Properties()); this(accessKeyId, secretKey, new Properties());
} }
public InstanceVolumeManager(String accessKeyId, String secretKey, Properties overridesForContext) { @VisibleForTesting
InstanceVolumeManager(String accessKeyId, String secretKey, Properties overridesForContext) {
try { try {
context = new ComputeServiceContextFactory() context = new ComputeServiceContextFactory()
.createContext("ec2", accessKeyId, secretKey, .createContext("ec2", accessKeyId, secretKey,

View File

@ -44,8 +44,6 @@ import java.util.concurrent.TimeUnit;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static org.easymock.classextension.EasyMock.*;
/** /**
* Tests the resizing of instance's root EBS device. * Tests the resizing of instance's root EBS device.
@ -55,9 +53,11 @@ import static org.easymock.classextension.EasyMock.*;
* *
* @author Oleksiy Yarmula * @author Oleksiy Yarmula
*/ */
@Test(groups = {"live" }, enabled = false, testName = "ec2.InstanceVolumeManagerLiveTest") @Test(groups = {"live" }, enabled = true, testName = "ec2.demo.InstanceVolumeManagerLiveTest")
public class InstanceVolumeManagerLiveTest { public class InstanceVolumeManagerLiveTest {
private final int NEW_SIZE = 6;
private String tag; private String tag;
private String secret; private String secret;
private InstanceVolumeManager manager; private InstanceVolumeManager manager;
@ -112,22 +112,21 @@ public class InstanceVolumeManagerLiveTest {
AvailabilityZone availabilityZone = AvailabilityZone.fromValue(launchedNode.getLocationId()); AvailabilityZone availabilityZone = AvailabilityZone.fromValue(launchedNode.getLocationId());
Region region = getRegionNameForAvailabilityZone(availabilityZone); Region region = getRegionNameForAvailabilityZone(availabilityZone);
waitForInstanceInRunningState(launchedNode.getId(), region); instanceCreated = getOnlyInstance(launchedNode.getId(), region);
waitForInstanceInRunningState();
manager.resizeVolume(launchedNode.getId(), region, new Credentials("ubuntu", ""), manager.resizeVolume(launchedNode.getId(), region, new Credentials("ubuntu", ""),
secret, 6); secret, NEW_SIZE);
// re-fetch the instance after the resize
// NOTE: this step is essential
instanceCreated = getOnlyInstance(launchedNode.getId(), region);
instanceCreated =
Iterables.getOnlyElement(
Iterables.getOnlyElement(
manager.getApi().
getInstanceServices().
describeInstancesInRegion(region, launchedNode.getId()
)
)
);
Volume volumeAttached = manager.getEbsApi().getRootVolumeForInstance(instanceCreated); Volume volumeAttached = manager.getEbsApi().getRootVolumeForInstance(instanceCreated);
assert volumeAttached.getSize() == 6; checkState(volumeAttached.getSize() == NEW_SIZE,
String.format("The size of the new volume expected: " +
"%d. Found: %d", NEW_SIZE, volumeAttached.getSize()));
} }
@AfterTest @AfterTest
@ -162,38 +161,34 @@ public class InstanceVolumeManagerLiveTest {
} }
/** /**
* Blocks until the instance with given id and region is * Blocks until {@link #instanceCreated} transitions
* in 'running' state. * into 'running' state.
* * NOTE: {@link #instanceCreated} can not be null.
* @param id
* id of the instance being run
* @param region
* region where the instance is launched
*/ */
private void waitForInstanceInRunningState(String id, Region region) { private void waitForInstanceInRunningState() {
RunningInstance instanceMock = checkState(instanceRunning.apply(instanceCreated),
createInstanceFromIdAndRegion(id, region);
checkState(instanceRunning.apply(instanceMock),
/*or throw*/ "Couldn't run the instance"); /*or throw*/ "Couldn't run the instance");
} }
/** /**
* Uses EasyMock to create a mock {@link RunningInstance}. * Retrieves a {@link RunningInstance} object by instanceId and
* region.
* *
* @param id * @param instanceId
* id of the instance * id of launched instance
* @param region * @param region
* region where it's launched * region where the instance was launched
* @return * @return corresponding {@link RunningInstance} object
* instance of mock {@link org.jclouds.aws.ec2.domain.RunningInstance}
*/ */
private RunningInstance createInstanceFromIdAndRegion(String id, Region region) { private RunningInstance getOnlyInstance(String instanceId, Region region) {
RunningInstance instanceMock = createMock(RunningInstance.class); return Iterables.getOnlyElement(
expect(instanceMock.getId()).andStubReturn(id); Iterables.getOnlyElement(
expect(instanceMock.getRegion()).andStubReturn(region); manager.getApi().
replay(instanceMock); getInstanceServices().
return instanceMock; describeInstancesInRegion(region, instanceId
)
)
);
} }
} }