@Test(dependsOnMethods = "testGetLocation")
 public void testAddPublicKey() throws Exception {
   try {
     connection.addPublicKey(TAG, keyPair.get("public"));
     key = connection.getKey(TAG);
     try {
       assert key.getInstanceIds().equals(ImmutableSet.<String>of()) : key;
     } catch (AssertionError e) {
       // inconsistency in the key api when recreating a key
       // http://www-180.ibm.com/cloud/enterprise/beta/ram/community/discussionTopic.faces?guid={DA689AEE-783C-6FE7-6F9F-DFEE9763F806}&v=1&fid=1068&tid=1528
     }
   } catch (IllegalStateException e) {
     // must have been found
     connection.updatePublicKey(TAG, keyPair.get("public"));
     key = connection.getKey(TAG);
     for (String instanceId : key.getInstanceIds()) {
       Instance instance = connection.getInstance(instanceId);
       if (instance.getStatus() == Instance.Status.FAILED
           || instance.getStatus() == Instance.Status.ACTIVE) {
         killInstance(instance.getId());
       }
     }
   }
   assertEquals(key.getName(), TAG);
   assert keyPair.get("public").indexOf(key.getKeyMaterial()) > 0;
   assertNotNull(key.getLastModifiedTime());
 }
 @Test
 public void testGetKey() throws Exception {
   Set<? extends Key> response = connection.listKeys();
   assertNotNull(response);
   if (response.size() > 0) {
     Key key = Iterables.get(response, 0);
     assertEquals(connection.getKey(key.getName()).getName(), key.getName());
   }
 }
  @AfterTest(groups = {"live"})
  void tearDown() {
    if (volume != null)
      try {
        connection.deleteVolume(volume.getId());
      } catch (Exception e) {

      }
    if (ip != null)
      try {
        connection.releaseAddress(ip.getId());
      } catch (Exception e) {

      }
    if (key != null)
      try {
        connection.deleteKey(key.getName());
      } catch (Exception e) {

      }
    if (instance != null)
      try {
        connection.deleteInstance(instance.getId());
      } catch (Exception e) {

      }
    if (instance2 != null)
      try {
        connection.deleteInstance(instance2.getId());
      } catch (Exception e) {

      }
  }
  @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 = {"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()));
  }
 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());
 }