private RunningInstance blockUntilWeCanSshIntoInstance(RunningInstance instance)
      throws UnknownHostException {
    System.out.printf(
        "%d: %s awaiting instance to run %n", System.currentTimeMillis(), instance.getId());
    assert runningTester.apply(instance);

    // search my identity for the instance I just created
    Set<? extends Reservation<? extends RunningInstance>> reservations =
        client
            .getInstanceServices()
            .describeInstancesInRegion(instance.getRegion(), instance.getId()); // last
    // parameter
    // (ids)
    // narrows
    // the
    // search

    instance = Iterables.getOnlyElement(Iterables.getOnlyElement(reservations));

    System.out.printf(
        "%d: %s awaiting ssh service to start%n",
        System.currentTimeMillis(), instance.getIpAddress());
    assert socketTester.apply(new IPSocket(instance.getIpAddress(), 22));
    System.out.printf(
        "%d: %s ssh service started%n", System.currentTimeMillis(), instance.getDnsName());
    sshPing(instance);
    System.out.printf("%d: %s ssh connection made%n", System.currentTimeMillis(), instance.getId());
    return instance;
  }
  private void _sshAndDf(IPSocket socket, Credentials credentials) {
    RetryablePredicate<IPSocket> socketOpen =
        new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 180, 5, TimeUnit.SECONDS);

    socketOpen.apply(socket);

    SshClient ssh =
        new JschSshClient(
            new BackoffLimitedRetryHandler(),
            socket,
            60000,
            credentials.identity,
            null,
            credentials.credential.getBytes());
    try {
      ssh.connect();
      ExecResponse hello = ssh.exec("echo hello");
      assertEquals(hello.getOutput().trim(), "hello");
      ExecResponse exec = ssh.exec("df");
      assertTrue(
          exec.getOutput().contains("Filesystem"),
          "The output should've contained filesystem information, but it didn't. Output: " + exec);
    } finally {
      if (ssh != null) ssh.disconnect();
    }
  }
 private void verifyPlacementGroup(String groupName) {
   assert availableTester.apply(
           new PlacementGroup(Region.US_EAST_1, groupName, "cluster", State.PENDING))
       : group;
   Set<PlacementGroup> oneResult =
       client.getPlacementGroupServices().describePlacementGroupsInRegion(null, groupName);
   assertNotNull(oneResult);
   assertEquals(oneResult.size(), 1);
   group = oneResult.iterator().next();
   assertEquals(group.getName(), groupName);
   assertEquals(group.getStrategy(), "cluster");
   assert availableTester.apply(group) : group;
 }
  @Test
  public void testCreateEmail() {
    client.createAccount(
        "test@" + testDomain,
        "password",
        CreateAccountOptions.Builder.antiVirus(true)
            .autorespond(true)
            .autorespondMessage("out of office"));

    assertTrue(emailAccountCounter.apply(1));

    client.createAccount("test1@" + testDomain, "password");

    assertTrue(emailAccountCounter.apply(2));
  }
  @Test(
      enabled = false,
      dependsOnMethods = {"testAMIFromBundle"})
  public void testInstanceFromEBS() throws Exception {
    System.out.printf(
        "%d: %s creating instance from ebs-backed ami%n",
        System.currentTimeMillis(), ebsImage.getId());

    ebsInstance = createInstance(ebsImage.getId());

    client
        .getInstanceServices()
        .stopInstancesInRegion(ebsInstance.getRegion(), true, ebsInstance.getId());

    System.out.printf(
        "%d: %s awaiting instance to stop %n", System.currentTimeMillis(), ebsInstance.getId());
    stoppedTester.apply(ebsInstance);
    tryToChangeStuff();
    System.out.printf(
        "%d: %s awaiting instance to start %n", System.currentTimeMillis(), ebsInstance.getId());
    client
        .getInstanceServices()
        .startInstancesInRegion(ebsInstance.getRegion(), ebsInstance.getId());
    ebsInstance = blockUntilWeCanSshIntoInstance(ebsInstance);
  }
 @AfterTest
 public void shutdown() {
   if (group != null) {
     client
         .getPlacementGroupServices()
         .deletePlacementGroupInRegion(group.getRegion(), group.getName());
     assert deletedTester.apply(group) : group;
   }
 }
 @Override
 public NodeMetadata createNodeWithGroupEncodedIntoName(
     String group, String name, Template template) {
   Server addedServer = null;
   boolean notStarted = true;
   int numOfRetries = 20;
   GetIpListOptions unassignedIps =
       new GetIpListOptions()
           .onlyUnassigned()
           .inDatacenter(template.getLocation().getId())
           .onlyWithType(IpType.PUBLIC);
   // lock-free consumption of a shared resource: IP address pool
   while (notStarted) { // TODO: replace with Predicate-based thread
     // collision avoidance for simplicity
     Set<Ip> availableIps = client.getIpServices().getIpList(unassignedIps);
     if (availableIps.isEmpty()) throw new RuntimeException("No IPs available on this identity.");
     int ipIndex = new SecureRandom().nextInt(availableIps.size());
     Ip availableIp = Iterables.get(availableIps, ipIndex);
     try {
       addedServer = addServer(name, template, availableIp);
       notStarted = false;
     } catch (Exception e) {
       if (--numOfRetries == 0) Throwables.propagate(e);
       notStarted = true;
     }
   }
   if (template.getOptions().shouldBlockUntilRunning()) {
     serverLatestJobCompleted.apply(addedServer);
     client.getServerServices().power(addedServer.getName(), PowerCommand.START);
     serverLatestJobCompletedShort.apply(addedServer);
     addedServer =
         Iterables.getOnlyElement(
             client.getServerServices().getServersByName(addedServer.getName()));
   }
   Credentials credentials =
       client.getServerServices().getServerCredentialsList().get(addedServer.getName());
   if (credentials != null) credentialStore.put("node#" + addedServer.getId(), credentials);
   else logger.warn("couldn't get credentials for server %s", addedServer.getName());
   return serverToNodeMetadata.apply(addedServer);
 }
  @Test(enabled = false, dependsOnMethods = "testCreateAndAttachVolume")
  void testBundleInstance() {
    SshClient ssh =
        sshFactory.create(
            new IPSocket(instance.getIpAddress(), 22),
            new Credentials("ubuntu", keyPair.getKeyMaterial()));
    try {
      ssh.connect();
    } catch (SshException e) { // try twice in case there is a network timeout
      try {
        Thread.sleep(10 * 1000);
      } catch (InterruptedException e1) {
      }
      ssh.connect();
    }
    try {
      System.out.printf(
          "%d: %s writing ebs script%n", System.currentTimeMillis(), instance.getId());
      String script = "/tmp/mkebsboot-init.sh";
      ssh.put(script, Payloads.newStringPayload(mkEbsBoot));

      System.out.printf(
          "%d: %s launching ebs script%n", System.currentTimeMillis(), instance.getId());
      ssh.exec("chmod 755 " + script);
      ssh.exec(script + " init");
      ExecResponse output = ssh.exec("sudo " + script + " start");
      System.out.println(output);
      output = ssh.exec(script + " status");

      assert !output.getOutput().trim().equals("") : output;

      RetryablePredicate<String> scriptTester =
          new RetryablePredicate<String>(
              new ScriptTester(ssh, SCRIPT_END), 600, 10, TimeUnit.SECONDS);
      scriptTester.apply(script);
    } finally {
      if (ssh != null) ssh.disconnect();
    }
  }
  // takes a few minutes and requires an extra server (using 2 already)
  @Test(enabled = false)
  public void testCloneServer() throws Exception {
    ServerDetails testServer2 =
        client.cloneServer(testServerId, testHostName2, CloneServerOptions.Builder.cpucores(1));

    assertNotNull(testServer2.getId());
    assertEquals(testServer2.getHostname(), "jclouds-test2");
    assertTrue(testServer2.getIps().isEmpty());

    testServerId2 = testServer2.getId();

    RetryablePredicate<Server.State> cloneChecker =
        new ServerStatusChecker(client, testServerId2, 300, 10, TimeUnit.SECONDS);
    assertTrue(cloneChecker.apply(Server.State.STOPPED));

    client.startServer(testServer2.getId());

    // TODO ServerStatus==STOPPED suggests the previous call to start should have worked
    cloneChecker =
        new RetryablePredicate<Server.State>(
            new Predicate<Server.State>() {

              public boolean apply(Server.State value) {
                ServerStatus status =
                    client.getServerStatus(testServerId2, ServerStatusOptions.Builder.state());
                if (status.getState() == value) {
                  return true;
                }

                client.startServer(testServerId2);
                return false;
              }
            },
            300,
            10,
            TimeUnit.SECONDS);

    assertTrue(cloneChecker.apply(Server.State.RUNNING));
  }
  @Test
  public void testCreateArchive() throws Exception {
    try {
      client.deleteArchive(archiveUser);
    } catch (Exception ex) {
    }

    int before = client.listArchives().size();

    client.createArchive(archiveUser, "password", 10);

    assertTrue(archiveCounter.apply(before + 1));
  }
  @Test(enabled = false, dependsOnMethods = "testCreateRunningInstance")
  void testCreateAndAttachVolume() {
    volume =
        client
            .getElasticBlockStoreServices()
            .createVolumeInAvailabilityZone(instance.getAvailabilityZone(), VOLUME_SIZE);
    System.out.printf(
        "%d: %s awaiting volume to become available%n", System.currentTimeMillis(), volume.getId());

    assert volumeTester.apply(volume);

    Attachment attachment =
        client
            .getElasticBlockStoreServices()
            .attachVolumeInRegion(
                instance.getRegion(), volume.getId(), instance.getId(), "/dev/sdh");

    System.out.printf(
        "%d: %s awaiting attachment to complete%n", System.currentTimeMillis(), attachment.getId());

    assert attachTester.apply(attachment);
    System.out.printf(
        "%d: %s attachment complete%n", System.currentTimeMillis(), attachment.getId());
  }
 protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node) throws IOException {
   IPSocket socket = new IPSocket(Iterables.get(node.getPublicAddresses(), 0), 22);
   socketTester.apply(socket); // TODO add transitionTo option that accepts
   // a socket conection
   // state.
   SshClient ssh =
       sshFactory.create(socket, node.getCredentials().account, keyPair.get("private").getBytes());
   try {
     ssh.connect();
     ExecResponse hello = ssh.exec("echo hello");
     assertEquals(hello.getOutput().trim(), "hello");
     ExecResponse exec = ssh.exec("java -version");
     assert exec.getError().indexOf("OpenJDK") != -1 : exec;
   } finally {
     if (ssh != null) ssh.disconnect();
   }
 }
  @Override
  public NodeAndInitialCredentials<VM> createNodeWithGroupEncodedIntoName(
      String tag, String name, Template template) {
    String networkTierName = template.getLocation().getId();
    String vpdcId = template.getLocation().getParent().getId();
    String billingSiteId = template.getLocation().getParent().getParent().getId();

    VMSpec.Builder specBuilder = VMSpec.builder();
    specBuilder.name(name);
    specBuilder.networkTierName(networkTierName);
    specBuilder.operatingSystem(
        CIMOperatingSystem.class.cast(template.getImage().getOperatingSystem()));
    specBuilder.processorCount(template.getHardware().getProcessors().size());
    specBuilder.memoryInGig(template.getHardware().getRam() / 1024);

    for (Volume volume : template.getHardware().getVolumes()) {
      if (volume.isBootDevice())
        specBuilder.bootDeviceName(volume.getDevice()).bootDiskSize(volume.getSize().intValue());
      else specBuilder.addDataDrive(volume.getDevice(), volume.getSize().intValue());
    }

    Task task = client.getVMClient().addVMIntoVDC(billingSiteId, vpdcId, specBuilder.build());
    // make sure there's no error
    if (task.getError() != null)
      throw new RuntimeException("cloud not add vm: " + task.getError().toString());

    if (taskTester.apply(task.getId())) {
      try {
        VM returnVal = this.getNode(task.getResult().getHref().toASCIIString());
        return new NodeAndInitialCredentials<VM>(returnVal, returnVal.getId(), null);
      } finally {
        // TODO: get the credentials relevant to the billingSiteId/Org
        // credentialStore.put(id, new Credentials(orgId, orgUser));
      }
    } else {
      throw new RuntimeException("task timed out: " + task);
    }
  }
 @Test(dependsOnMethods = "testCreateMachine")
 protected void testSsh() {
   String publicAddress =
       Iterables.find(machine.getIps(), not(InetAddresses2.IsPrivateIPAddress.INSTANCE));
   HostAndPort socket = HostAndPort.fromParts(publicAddress, 22);
   assertTrue(socketTester.apply(socket), socket.toString());
   SshClient api =
       context
           .utils()
           .injector()
           .getInstance(SshClient.Factory.class)
           .create(
               socket,
               LoginCredentials.builder().user("root").privateKey(key.get("private")).build());
   try {
     api.connect();
     ExecResponse exec = api.exec("echo hello");
     System.out.println(exec);
     assertEquals(exec.getOutput().trim(), "hello");
   } finally {
     if (api != null) api.disconnect();
   }
 }
  @AfterTest
  void cleanup() {
    if (ebsInstance != null) {
      try {
        client
            .getInstanceServices()
            .terminateInstancesInRegion(ebsInstance.getRegion(), ebsInstance.getId());
        terminatedTester.apply(ebsInstance);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (ebsImage != null) {
      try {
        client.getAMIServices().deregisterImageInRegion(ebsImage.getRegion(), ebsImage.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if (snapshot != null) {
      try {
        client
            .getElasticBlockStoreServices()
            .deleteSnapshotInRegion(snapshot.getRegion(), snapshot.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (attachment != null) {
      try {
        client
            .getElasticBlockStoreServices()
            .detachVolumeInRegion(volume.getRegion(), volume.getId(), true);
        assert volumeTester.apply(volume);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (instance != null) {
      try {
        client
            .getInstanceServices()
            .terminateInstancesInRegion(instance.getRegion(), instance.getId());
        terminatedTester.apply(instance);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (volume != null) {
      try {
        client
            .getElasticBlockStoreServices()
            .deleteVolumeInRegion(volume.getRegion(), volume.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (keyPair != null) {
      try {
        client
            .getKeyPairServices()
            .deleteKeyPairInRegion(keyPair.getRegion(), keyPair.getKeyName());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (securityGroupName != null) {
      try {
        client.getSecurityGroupServices().deleteSecurityGroupInRegion(null, securityGroupName);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
 @Override
 public void destroyNode(String id) {
   taskTester.apply(client.getVMClient().removeVM(URI.create(checkNotNull(id, "id"))).getId());
 }
 @Override
 public void suspendNode(String id) {
   taskTester.apply(
       client.getServiceManagementClient().powerOffVM(URI.create(checkNotNull(id, "id"))).getId());
 }
  @Test(enabled = false, dependsOnMethods = "testBundleInstance")
  void testAMIFromBundle() {
    volume =
        Iterables.getOnlyElement(
            client
                .getElasticBlockStoreServices()
                .describeVolumesInRegion(volume.getRegion(), volume.getId()));
    if (volume.getAttachments().size() > 0) {
      // should be cleanly unmounted, so force is not necessary.
      client
          .getElasticBlockStoreServices()
          .detachVolumeInRegion(instance.getRegion(), volume.getId(), false);
      System.out.printf(
          "%d: %s awaiting detachment to complete%n", System.currentTimeMillis(), volume.getId());
      assert volumeTester.apply(volume);
    } else {
      attachment = null; // protect test closer so that it doesn't try to
      // detach
    }
    snapshot =
        client
            .getElasticBlockStoreServices()
            .createSnapshotInRegion(
                volume.getRegion(), volume.getId(), withDescription("EBS Ubuntu Hardy"));

    System.out.printf(
        "%d: %s awaiting snapshot to complete%n", System.currentTimeMillis(), snapshot.getId());

    assert snapshotTester.apply(snapshot);
    Image image =
        Iterables.getOnlyElement(
            client
                .getAMIServices()
                .describeImagesInRegion(snapshot.getRegion(), imageIds(IMAGE_ID)));
    String description = image.getDescription() == null ? "jclouds" : image.getDescription();

    System.out.printf(
        "%d: %s creating ami from snapshot%n", System.currentTimeMillis(), snapshot.getId());

    String amiId =
        client
            .getAMIServices()
            .registerUnixImageBackedByEbsInRegion(
                snapshot.getRegion(),
                "ebsboot-" + image.getId(),
                snapshot.getId(),
                withKernelId(image.getKernelId())
                    .withRamdisk(image.getRamdiskId())
                    .withDescription(description)
                    .asArchitecture(Architecture.I386));
    try {
      ebsImage =
          Iterables.getOnlyElement(
              client
                  .getAMIServices()
                  .describeImagesInRegion(snapshot.getRegion(), imageIds(amiId)));
    } catch (AWSResponseException e) {
      // TODO add a retry handler for this HTTP code 400 and the below error
      if (e.getError().getClass().equals("InvalidAMIID.NotFound"))
        ebsImage =
            Iterables.getOnlyElement(
                client
                    .getAMIServices()
                    .describeImagesInRegion(snapshot.getRegion(), imageIds(amiId)));
      else throw e;
    }
    verifyImage();
  }