private ReplicationController getReplicationController() {
    ReplicationController contr = new ReplicationController();
    contr.setId("kubernetes-test-controller");
    State desiredState = new State();
    desiredState.setReplicas(2);

    Selector selector = new Selector();
    selector.setName("kubernetes-test-controller-selector");
    desiredState.setReplicaSelector(selector);

    Pod podTemplate = new Pod();
    State podState = new State();
    Manifest manifest = new Manifest();
    manifest.setId(contr.getId());
    Container container = new Container();
    container.setName("kubernetes-test");
    container.setImage(dockerImage);
    Port p = new Port();
    p.setContainerPort(80);
    container.setPorts(Collections.singletonList(p));
    container.setCommand("tail", "-f", "/dev/null");
    manifest.setContainers(Collections.singletonList(container));
    podState.setManifest(manifest);
    podTemplate.setDesiredState(podState);
    podTemplate.setLabels(ImmutableMap.of("name", selector.getName()));

    desiredState.setPodTemplate(podTemplate);
    contr.setDesiredState(desiredState);
    contr.setLabels(podTemplate.getLabels());
    return contr;
  }
 @Test
 public void testDeletePod() throws Exception {
   if (log.isDebugEnabled()) {
     log.debug("Deleting a Pod " + pod);
   }
   getClient().createPod(pod);
   getClient().deletePod(pod.getId());
   assertNull(getClient().getPod(pod.getId()));
 }
 @Test
 public void testGetSelectedPodsEmpty() {
   PodList selectedPods = getClient().getSelectedPods(pod.getLabels());
   assertNotNull(selectedPods);
   assertNotNull(selectedPods.getItems());
   assertEquals(0, selectedPods.getItems().size());
 }
 @Test
 public void testGetSelectedPods() throws Exception {
   getClient().createPod(pod);
   PodList selectedPods = getClient().getSelectedPods(pod.getLabels());
   assertNotNull(selectedPods);
   assertNotNull(selectedPods.getItems());
   assertEquals(1, selectedPods.getItems().size());
 }
 private Pod getPod() {
   Pod pod = new Pod();
   pod.setId("kubernetes-test-pod");
   pod.setLabels(ImmutableMap.of("name", "kubernetes-test-pod-label", "label1", "value1"));
   State desiredState = new State();
   Manifest m = new Manifest();
   m.setId(pod.getId());
   Container c = new Container();
   c.setName("master");
   c.setImage(dockerImage);
   c.setCommand("tail", "-f", "/dev/null");
   Port p = new Port(8379, new Random().nextInt((65535 - 49152) + 1) + 49152, "0.0.0.0");
   c.setPorts(Collections.singletonList(p));
   m.setContainers(Collections.singletonList(c));
   desiredState.setManifest(m);
   pod.setDesiredState(desiredState);
   return pod;
 }
 @Test
 public void testGetAllPods() throws Exception {
   if (log.isDebugEnabled()) {
     log.debug("Get all Pods ");
   }
   getClient().createPod(pod);
   PodList podList = getClient().getAllPods();
   assertNotNull(podList);
   List<Pod> currentPods;
   assertNotNull(currentPods = podList.getItems());
   boolean match = false;
   for (Pod pod2 : currentPods) {
     if (pod.getId().equals(pod2.getId())) {
       match = true;
       break;
     }
   }
   assertEquals(true, match);
 }
  @Test
  public void testCreatePod() throws Exception {
    log.info("Testing Pods ....");

    if (log.isDebugEnabled()) {
      log.debug("Creating a Pod " + pod);
    }
    Pod createPod = getClient().createPod(pod);
    assertEquals(pod.getId(), createPod.getId());
    assertNotNull(getClient().getPod(pod.getId()));
    assertEquals("Waiting", createPod.getCurrentState().getStatus());

    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Pod> future =
        executor.submit(
            new Callable<Pod>() {
              public Pod call() throws Exception {
                Pod newPod;
                do {
                  log.info("Waiting for Pod to be ready: " + pod.getId());
                  Thread.sleep(1000);
                  newPod = getClient().getPod(pod.getId());
                  StateInfo info = newPod.getCurrentState().getInfo("master");
                  if (info.getState("waiting") != null) {
                    throw new RuntimeException("Pod is waiting due to " + info.getState("waiting"));
                  }
                } while (!"Running".equals(newPod.getCurrentState().getStatus()));
                return newPod;
              }
            });

    try {
      createPod = future.get(90, TimeUnit.SECONDS);
    } finally {
      executor.shutdownNow();
    }
    assertNotNull(createPod.getCurrentState().getInfo("master").getState("running"));
    assertNotNull(createPod.getCurrentState().getNetInfo().getState("running"));

    // test recreation from same id
    try {
      getClient().createPod(pod);
      fail("Should have thrown exception");
    } catch (Exception e) {
      // ignore
    }
    assertNotNull(getClient().getPod(pod.getId()));
  }
  @Test
  public void testCreateReplicationController() throws Exception {
    if (log.isDebugEnabled()) {
      log.debug("Creating a Replication Controller: " + contr);
    }
    getClient().createReplicationController(contr);
    assertNotNull(getClient().getReplicationController(contr.getId()));

    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<PodList> future =
        executor.submit(
            new Callable<PodList>() {
              public PodList call() throws Exception {
                PodList pods;
                do {
                  log.info("Waiting for Pods to be ready");
                  Thread.sleep(1000);
                  pods =
                      getClient()
                          .getSelectedPods(
                              ImmutableMap.of("name", "kubernetes-test-controller-selector"));
                  if (pods.isEmpty()) {
                    continue;
                  }

                  StateInfo info = pods.get(0).getCurrentState().getInfo("kubernetes-test");
                  if ((info != null) && info.getState("waiting") != null) {
                    throw new RuntimeException("Pod is waiting due to " + info.getState("waiting"));
                  }
                } while (pods.isEmpty()
                    || !FluentIterable.from(pods)
                        .allMatch(
                            new Predicate<Pod>() {
                              public boolean apply(Pod pod) {
                                return "Running".equals(pod.getCurrentState().getStatus());
                              }
                            }));
                return pods;
              }
            });

    PodList pods;
    try {
      pods = future.get(90, TimeUnit.SECONDS);
    } finally {
      executor.shutdownNow();
    }
    for (Pod pod : pods) {
      assertNotNull(pod.getCurrentState().getInfo("kubernetes-test").getState("running"));
      assertNotNull(pod.getCurrentState().getNetInfo().getState("running"));
    }

    // test recreation using same id
    try {
      getClient().createReplicationController(contr);
      fail("Should have thrown exception");
    } catch (Exception e) {
      // ignore
    }
    assertNotNull(getClient().getReplicationController(contr.getId()));

    PodList podList = getClient().getSelectedPods(contr.getLabels());
    assertNotNull(podList);
    assertNotNull(podList.getItems());
    assertEquals(contr.getDesiredState().getReplicas(), podList.getItems().size());
  }