@AfterClass
    public void tearDownClass() throws Throwable {
      listeningExecutorService.shutdown();
      FileUtils.deleteDirectory(storageDirectory);

      if (sourceCloudStore != null) {
        sourceCloudStore.stop();
        sourceCloudStore = null;
      }

      if (destinationCloudStore != null) {
        destinationCloudStore.stop();
        destinationCloudStore = null;
      }
    }
    @BeforeClass
    public void setUpClass() throws Throwable {
      listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));

      startState =
          buildValidStartState(
              InitializeDeploymentMigrationWorkflowService.TaskState.TaskStage.CREATED, null);
      startState.controlFlags = null;
      startState.taskPollDelay = 10;

      sourceCloudStore =
          com.vmware.photon.controller.cloudstore.dcp.helpers.TestEnvironment.create(1);
      destinationCloudStore =
          com.vmware.photon.controller.cloudstore.dcp.helpers.TestEnvironment.create(1);

      FileUtils.deleteDirectory(storageDirectory);
      vibDirectory.mkdirs();
      TestHelper.createSourceFile(null, vibDirectory);
    }
    private void createTestEnvironment() throws Throwable {
      ZookeeperClientFactory zkFactory = mock(ZookeeperClientFactory.class);
      sourceEnvironment =
          new TestEnvironment.Builder()
              .listeningExecutorService(listeningExecutorService)
              .apiClientFactory(apiClientFactory)
              .cloudServerSet(sourceCloudStore.getServerSet())
              .hostCount(1)
              .build();

      destinationEnvironment =
          new TestEnvironment.Builder()
              .hostCount(1)
              .apiClientFactory(apiClientFactory)
              .cloudServerSet(destinationCloudStore.getServerSet())
              .httpFileServiceClientFactory(httpFileServiceClientFactory)
              .zookeeperServersetBuilderFactory(zkFactory)
              .build();

      ZookeeperClient zkBuilder = mock(ZookeeperClient.class);
      doReturn(zkBuilder).when(zkFactory).create();
      doReturn(
              Collections.singleton(
                  new InetSocketAddress(
                      "127.0.0.1", sourceEnvironment.getHosts()[0].getState().httpPort)))
          .when(zkBuilder)
          .getServers(Matchers.startsWith("127.0.0.1:2181"), eq("cloudstore"));

      ServiceHost sourceHost = sourceEnvironment.getHosts()[0];
      startState.sourceLoadBalancerAddress = sourceHost.getPublicUri().toString();

      TestHelper.createHostService(sourceCloudStore, Collections.singleton(UsageTag.MGMT.name()));
      TestHelper.createHostService(sourceCloudStore, Collections.singleton(UsageTag.CLOUD.name()));
      DeploymentService.State deploymentService =
          TestHelper.createDeploymentService(destinationCloudStore);
      startState.destinationDeploymentId =
          ServiceUtils.getIDFromDocumentSelfLink(deploymentService.documentSelfLink);
    }
 /**
  * Creates datastore documents in cloudstore.
  *
  * <p>This method creates datastore documents with datastore IDs
  * 00000000-0000-0000-0000-000000000000, 00000000-0000-0000-0000-000000000001, and so on.
  *
  * @param numDatastores The number of datastore documents to create.
  */
 public static void loadDatastores(TestEnvironment cloudstore, int numDatastores)
     throws Throwable {
   for (int i = 0; i < numDatastores; i++) {
     DatastoreService.State datastore = new DatastoreService.State();
     String datastoreId = new UUID(0, i).toString();
     datastore.id = datastoreId;
     datastore.name = datastoreId;
     datastore.documentSelfLink = datastoreId;
     datastore.type = "SHARED_VMFS";
     // TODO(mmutsuzaki) Support datastore tags.
     datastore.tags = new HashSet<>();
     Operation result = cloudstore.sendPostAndWait(DatastoreServiceFactory.SELF_LINK, datastore);
     assertThat(result.getStatusCode(), is(200));
     logger.debug("Created a datastore document: {}", Utils.toJson(datastore));
   }
 }
 /**
  * Creates host documents in cloudstore.
  *
  * @param cloudstore CloudStore test environment to create documents in.
  * @param numHosts The number of host documents to create.
  * @param hostConfigurations A map from {@link HostConfiguration} to the probability that this
  *     host configuration is used in the deployment. The sum of all the values of this map must be
  *     1.
  * @param numDatastores The number of datastores.
  * @param numDatastoresDistribution Distribution for number of datastores on each host. This
  *     distribution is expected to generate samples in the range [0, numDatastores].
  * @throws Throwable
  */
 public static void loadHosts(
     TestEnvironment cloudstore,
     int numHosts,
     Map<HostConfiguration, Double> hostConfigurations,
     int numDatastores,
     IntegerDistribution numDatastoresDistribution)
     throws Throwable {
   int[] indices = new int[hostConfigurations.size()];
   HostConfiguration[] configs = new HostConfiguration[hostConfigurations.size()];
   double[] probabilities = new double[hostConfigurations.size()];
   int i = 0;
   for (Map.Entry<HostConfiguration, Double> entry : hostConfigurations.entrySet()) {
     indices[i] = i;
     configs[i] = entry.getKey();
     probabilities[i] = entry.getValue();
     i++;
   }
   EnumeratedIntegerDistribution configDistribution =
       new EnumeratedIntegerDistribution(indices, probabilities);
   for (i = 0; i < numHosts; i++) {
     HostService.State host = new HostService.State();
     host.hostAddress = "host" + i;
     host.state = HostState.READY;
     host.userName = "******";
     host.password = "******";
     host.reportedDatastores = new HashSet<>();
     int numDatastoresPerHost = numDatastoresDistribution.sample();
     assertThat(numDatastoresPerHost >= 0, is(true));
     assertThat(numDatastoresPerHost <= numDatastores, is(true));
     while (host.reportedDatastores.size() < numDatastoresPerHost) {
       int randomInt = random.nextInt(numDatastores);
       host.reportedDatastores.add(new UUID(0, randomInt).toString());
     }
     host.reportedNetworks = new HashSet<>();
     host.usageTags = new HashSet<>(Arrays.asList(UsageTag.CLOUD.name()));
     int configIndex = configDistribution.sample();
     host.cpuCount = configs[configIndex].numCpus;
     host.memoryMb = configs[configIndex].memoryMb;
     host.documentSelfLink = new UUID(0, i).toString();
     // TODO(mmutsuzaki) Support availability zones.
     Operation result = cloudstore.sendPostAndWait(HostServiceFactory.SELF_LINK, host);
     assertThat(result.getStatusCode(), is(200));
     logger.debug("Created a host document: {}", Utils.toJson(host));
   }
 }