@Test
 public void testGetLocation() throws Exception {
   Set<? extends Location> response = connection.listLocations();
   assertNotNull(response);
   if (response.size() > 0) {
     location = Iterables.get(response, 0);
     assertEquals(connection.getLocation(location.getId()).getId(), location.getId());
   }
 }
  @Test(dependsOnMethods = "testResolveVolumeOffering")
  public void testCreateVolume() throws Exception {
    try {
      volume =
          connection.createVolumeInLocation(
              location.getId(), TAG, FORMAT, cheapestStorage.getName(), cheapestStorage.getId());
      // wait up to 5 minutes for this to become "unmounted"
      assert new RetryablePredicate<Volume>(
              new VolumeUnmounted(connection), 300, 5, TimeUnit.SECONDS)
          .apply(volume);
    } catch (IllegalStateException e) {
      int code = HttpResponseException.class.cast(e.getCause()).getResponse().getStatusCode();
      if (code == 409 || code == 500) {
        Set<? extends Volume> volumes = connection.listVolumes();
        try {
          volume =
              Iterables.find(
                  volumes,
                  new Predicate<Volume>() {

                    @Override
                    public boolean apply(Volume input) {
                      return input.getState() == Volume.State.UNMOUNTED;
                    }
                  });
        } catch (NoSuchElementException ex) {
          killInstance(TAG + 1);
        }
      } else {
        throw e;
      }
    }
    assertEquals(volume.getInstanceId(), null);
    assertEquals(volume.getLocation(), location.getId());

    final String id = volume.getId();
    Set<? extends Volume> allVolumes = connection.listVolumes();

    // refresh volume as it may have been just created
    volume =
        Iterables.find(
            allVolumes,
            new Predicate<Volume>() {

              @Override
              public boolean apply(Volume input) {
                return input.getId().equals(id);
              }
            });

    assert (allVolumes.contains(volume)) : String.format("volume %s not in %s", volume, volume);
  }
  @Test(
      dependsOnMethods = {
        "testAddPublicKey",
        "testAllocateIpAddress",
        "testCreateVolume",
        "resolveImageAndInstanceType"
      })
  public void testCreateInstanceWithIpAndVolume() throws Exception {
    String name = TAG + "1";
    killInstance(name);

    instance2 =
        connection.createInstanceInLocation(
            location.getId(),
            name,
            image.getId(),
            instanceType.getId(),
            attachIp(ip.getId())
                .authorizePublicKey(key.getName())
                .mountVolume(volume.getId(), "/mnt"));

    assertBeginState(instance2, name);
    assertIpHostAndStatusNEW(instance2);
    blockUntilRunning(instance2);
    instance2 = assertRunning(instance2, name);

    volume = connection.getVolume(volume.getId());
    assertEquals(volume.getInstanceId(), instance2.getId());

    refreshIpAndReturnAllAddresses();
    assertEquals(ip.getInstanceId(), instance2.getId());
    assertEquals(ip.getIp(), instance2.getIp());
    sshAndDf(
        new IPSocket(instance2.getIp(), 22), new Credentials("idcuser", keyPair.get("private")));
  }
  @Test(dependsOnMethods = "resolveImageAndInstanceType")
  public void testAllocateIpAddress() throws Exception {

    Offering offering =
        Iterables.find(
            connection.listAddressOfferings(),
            new Predicate<Offering>() {

              @Override
              public boolean apply(Offering arg0) {
                return arg0.getLocation().equals(location.getId());
              }
            });

    try {
      ip = connection.allocateAddressInLocation(location.getId(), offering.getId());
      System.out.println(ip);
      assertEquals(ip.getIp(), null);
      // wait up to 30 seconds for this to become "free"
      new RetryablePredicate<Address>(new AddressFree(connection), 30, 2, TimeUnit.SECONDS)
          .apply(ip);
      refreshIpAndReturnAllAddresses();
      assertEquals(ip.getInstanceId(), null);
    } catch (IllegalStateException e) {
      if (HttpResponseException.class.cast(e.getCause()).getResponse().getStatusCode() == 409) {
        ip =
            Iterables.find(
                connection.listAddresses(),
                new Predicate<Address>() {

                  @Override
                  public boolean apply(Address input) {
                    return input.getState() == Address.State.FREE;
                  }
                });
      } else {
        throw e;
      }
    }
    assertEquals(ip.getInstanceId(), null);
    assertEquals(ip.getLocation(), location.getId());

    Set<? extends Address> allAddresses = refreshIpAndReturnAllAddresses();

    assert (allAddresses.contains(ip)) : String.format("ip %s not in %s", ip, allAddresses);
  }
 private void assertConsistent(Instance instance, String name) {
   assertNotNull(instance.getId());
   assertEquals(instance.getName(), name);
   assertEquals(instance.getInstanceType(), instanceType.getId());
   assertEquals(instance.getLocation(), location.getId());
   assertEquals(instance.getImageId(), image.getId());
   assertEquals(instance.getSoftware(), SOFTWARE);
   assertEquals(instance.getKeyName(), key.getName());
   assertNotNull(instance.getLaunchTime());
   assertNotNull(instance.getExpirationTime());
   assertEquals(instance.getOwner(), identity);
   assertEquals(instance.getProductCodes(), ImmutableSet.<String>of());
   assertEquals(instance.getRequestName(), name);
   assertNotNull(instance.getRequestId());
 }
  @Test(dependsOnMethods = {"testAddPublicKey", "resolveImageAndInstanceType"})
  public void testCreateInstance() throws Exception {
    killInstance(TAG);

    instance =
        connection.createInstanceInLocation(
            location.getId(),
            TAG,
            image.getId(),
            instanceType.getId(),
            authorizePublicKey(key.getName()));

    assertBeginState(instance, TAG);
    assertIpHostNullAndStatusNEW(instance);
    blockUntilRunning(instance);
    instance = assertRunning(instance, TAG);
    sshAndDf(new IPSocket(instance.getIp(), 22), new Credentials("idcuser", key.getKeyMaterial()));
  }