stratos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From im...@apache.org
Subject [09/12] stratos git commit: Introducing generic methods for creating kubernetes replication controllers, services and pods
Date Wed, 24 Dec 2014 17:36:32 GMT
http://git-wip-us.apache.org/repos/asf/stratos/blob/11f9cbd1/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/KubernetesApiClientLiveTest.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/KubernetesApiClientLiveTest.java
b/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/KubernetesApiClientLiveTest.java
index 649032d..9ee1aef 100644
--- a/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/KubernetesApiClientLiveTest.java
+++ b/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/live/KubernetesApiClientLiveTest.java
@@ -24,323 +24,200 @@ import junit.framework.TestCase;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.kubernetes.client.KubernetesApiClient;
+import org.apache.stratos.kubernetes.client.KubernetesConstants;
 import org.apache.stratos.kubernetes.client.exceptions.KubernetesClientException;
-import org.apache.stratos.kubernetes.client.model.*;
-import org.junit.Before;
-import org.junit.Test;
+import org.apache.stratos.kubernetes.client.model.Labels;
+import org.apache.stratos.kubernetes.client.model.Pod;
+import org.apache.stratos.kubernetes.client.model.ReplicationController;
+import org.apache.stratos.kubernetes.client.model.Service;
+import org.junit.*;
 import org.junit.experimental.categories.Category;
 
 import java.net.InetAddress;
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 
+/**
+ * Notes:
+ * - Pull the docker image before running the live test
+ */
 @Category(org.apache.stratos.kubernetes.client.LiveTests.class)
 public class KubernetesApiClientLiveTest extends TestCase{
 
     private static final Log log = LogFactory.getLog(KubernetesApiClientLiveTest.class);
+
+    private static final int CONTAINER_PORT = 6379;
+    private static final int SERVICE_PORT = 4500;
+    private static final String DEFAULT_KUBERNETES_MASTER_IP = "172.17.8.100";
+    private static final String DEFAULT_DOCKER_IMAGE =  "gurpartap/redis";
+    private static final int POD_ACTIVATION_WAIT_TIME = 15000; // 15 seconds
+
     private KubernetesApiClient client;
     private String dockerImage;
     private String endpoint;
-    
-	@Before
-	public void setUp() {
-	    endpoint = System.getProperty("kubernetes.api.endpoint");
-	    if (endpoint == null) {
-	        endpoint = "http://192.168.1.100:8080/api/v1beta1/";
-	    }
-        log.info("Provided Kubernetes endpoint using system property [kubernetes.api.endpoint]
: " +endpoint);
-	    client = new KubernetesApiClient(endpoint);
-	    
-	    // image should be pre-downloaded for ease of testing.
-	    dockerImage = System.getProperty("docker.image");
-	    if (dockerImage == null) {
-	        dockerImage = "gurpartap/redis";
-	    }
-	}
-	
-	@Test
-	public void testPods() throws Exception { 
-	    log.info("Testing Pods ....");
-	    String podId = "nirmal-test-pod";
-        Pod pod = new Pod();
-        pod.setApiVersion("v1beta1");
-        pod.setId(podId);
-        pod.setKind("Pod");
-        Labels l = new Labels();
-        l.setName("nirmal");
-        pod.setLabels(l);
-        State desiredState = new State();
-        Manifest m = new Manifest();
-        m.setId(podId);
-        m.setVersion("v1beta1");
-        Container c = new Container();
-        c.setName("master");
-        c.setImage(dockerImage);
-        Port p = new Port();
-        p.setContainerPort(8379);
-        p.setHostPort(8379);
-        c.setPorts(new Port[] { p });
-        m.addContainer(c);
-        desiredState.setManifest(m);
-        pod.setState(desiredState);
-        if (log.isDebugEnabled()) {
-            log.debug("Creating a Pod "+pod);
-        }
-        client.createPod(pod);
-	    assertNotNull(client.getPod(podId));
-	    
-	    // give 2s to download the image
-	    Thread.sleep(2000);
-	    
-	    // test recreation from same id
-	    client.createPod(pod);
-	    assertNotNull(client.getPod(podId));
-	    
-	    String bogusPodId = "nirmal";
-	    // create an invalid Pod
-	    Pod pod3 = new Pod();
-	    pod3.setId(bogusPodId);
-	    try {
-	        client.createPod(pod3);
-	    } catch (Exception e) {
-	        assertEquals(true, e instanceof KubernetesClientException);
-	    }
-	    
-	    try {
-	        client.getPod(bogusPodId);
-	    } catch (Exception e) {
-	        assertEquals(true, e instanceof KubernetesClientException);
-	        assertEquals("Pod ["+bogusPodId+"] doesn't exist.", e.getMessage());
-	    }
-	    
-	    if (log.isDebugEnabled()) {
-            log.debug("Get all Pods ");
-        }
-	    Pod[] currentPods = client.getAllPods();
-	    boolean match = false;
-	    for (Pod pod2 : currentPods) {
-            if (podId.equals(pod2.getId())) {
-                match = true;
-                break;
-            }
-        }
-	    assertEquals(true, match);
-	    
-	    Pod[] selectedPods = client.queryPods(new Labels[]{l});
-	    assertEquals(1, selectedPods.length);
-	    
-	    if (log.isDebugEnabled()) {
-	        log.debug("Deleting a Pod "+pod);
-	    }
-	    client.deletePod(podId);
-	    try {
-	        client.getPod(podId);
-	    } catch(Exception e) {
-	        assertEquals(true, e instanceof KubernetesClientException);
-	    }
-	    
-	    // delete a non-existing pod
-	    try {
-	        client.deletePod(bogusPodId);
-	    } catch (Exception e) {
-	        assertEquals(true, e instanceof KubernetesClientException);
-	    }
-	    
-	    selectedPods = client.queryPods(new Labels[]{l});
-        assertEquals(0, selectedPods.length);
-        
-        Labels ll = new Labels();
-        ll.setName("nirmal2");
-        selectedPods = client.queryPods(new Labels[]{l, ll});
-        assertEquals(0, selectedPods.length);
-        
-        selectedPods = client.queryPods(new Labels[]{});
-        assertEquals(0, selectedPods.length);
-	}
-	
-	@Test
-    public void testReplicationControllers() throws Exception { 
-	    String id = "nirmalController";
-	    int replicas = 2;
-	    
-        ReplicationController contr = new ReplicationController();
-        contr.setId(id);
-        contr.setKind("ReplicationController");
-        contr.setApiVersion("v1beta1");
-        State desiredState = new State();
-        desiredState.setReplicas(replicas);
-        Selector selector = new Selector();
-        selector.setName("nirmal");
-        desiredState.setReplicaSelector(selector);
-
-        Pod podTemplate = new Pod();
-        State podState = new State();
-        Manifest manifest = new Manifest();
-        manifest.setVersion("v1beta1");
-        manifest.setId(id);
-        Container container = new Container();
-        container.setName("nirmal-php");
-        container.setImage(dockerImage);
-        Port p = new Port();
-        p.setContainerPort(80);
-        container.setPorts(new Port[] { p });
-        manifest.addContainer(container);
-        podState.setManifest(manifest);
-        podTemplate.setState(podState);
-        Labels l1 = new Labels();
-        l1.setName("nirmal");
-        podTemplate.setLabels(l1);
-
-        desiredState.setPodTemplate(podTemplate);
-        contr.setDesiredState(desiredState);
-        Labels l2 = new Labels();
-        l2.setName("nirmal");
-        contr.setLabels(l2);
-        if (log.isDebugEnabled()) {
-            log.debug("Creating a Replication Controller: "+contr);
-        }
-        client.createReplicationController(contr);
-        assertNotNull(client.getReplicationController(id));
-        
-        // wait 10s for Pods to be created
-        Thread.sleep(10000);
-        
-        // test recreation using same id
-        client.createReplicationController(contr);
-        assertNotNull(client.getReplicationController(id));
-        
-        assertEquals(1, client.getAllReplicationControllers().length);
-        
-        Pod[] pods = client.queryPods(new Labels[]{l1});
-        assertEquals(replicas, pods.length);
-        
-        // test incorrect replica count
-        replicas = -1;
-        try {
-            ReplicationController replicationController = client.getReplicationController(id);
-            replicationController.getDesiredState().setReplicas(replicas);
-            client.updateReplicationController(replicationController);
-        } catch (Exception e) {
-            assertEquals(true, e instanceof KubernetesClientException);
-            assertEquals(true, e.getMessage().contains("Could not update kubernetes replication
controller"));
-        }
-        
-        replicas = 0;
-        ReplicationController replicationController = client.getReplicationController(id);
-        replicationController.getDesiredState().setReplicas(replicas);
-        client.updateReplicationController(replicationController);
-
-        Thread.sleep(30000);
-        
-        pods = client.queryPods(new Labels[]{l1});
-        assertEquals(replicas, pods.length);
-        
-        client.deleteReplicationController(id);
-        try {
-            client.getReplicationController(id);
-        } catch (Exception e) {
-            assertEquals(true, e instanceof KubernetesClientException);
-        }
-        
-        String bogusContrId = "nirmal";
-        // create an invalid Controller
-        ReplicationController bogusContr = new ReplicationController();
-        bogusContr.setId(bogusContrId);
-        try {
-            client.createReplicationController(bogusContr);
-        } catch (Exception e) {
-            assertEquals(true, e instanceof KubernetesClientException);
-        }
-        
-        try {
-            client.getReplicationController(bogusContrId);
-        } catch (Exception e) {
-            assertEquals(true, e instanceof KubernetesClientException);
-            assertEquals("Replication Controller ["+bogusContrId+"] doesn't exist.", e.getMessage());
+
+    @BeforeClass
+    public void setUp() {
+        endpoint = System.getProperty("kubernetes.api.endpoint");
+        if (endpoint == null) {
+            endpoint = "http://" + DEFAULT_KUBERNETES_MASTER_IP + ":8080/api/" + KubernetesConstants.KUBERNETES_API_VERSION
+ "/";
         }
-        
-        try {
-            replicationController = client.getReplicationController(bogusContrId);
-            replicationController.getDesiredState().setReplicas(3);
-            client.updateReplicationController(replicationController);
-        } catch (Exception e) {
-            assertEquals(true, e instanceof KubernetesClientException);
-            assertEquals("Replication Controller ["+bogusContrId+"] doesn't exist.", e.getMessage());
+        log.info("Provided Kubernetes endpoint using system property [kubernetes.api.endpoint]
: " +endpoint);
+        client = new KubernetesApiClient(endpoint);
+
+        dockerImage = System.getProperty("docker.image");
+        if (dockerImage == null) {
+            dockerImage = DEFAULT_DOCKER_IMAGE;
         }
-        
+    }
+
+    @AfterClass
+    public void cleanup() {
+        deleteReplicationControllers();
+        deletePods();
+        deleteServices();
+    }
+
+    @Test
+    public void testPodCreationAndDeletion() throws Exception {
+        log.info("Testing pod creation...");
+
+        String podId = "stratos-test-pod-1";
+        String podName = "stratos-test-pod";
+
+        List<Integer> ports = new ArrayList<Integer>();
+        ports.add(CONTAINER_PORT);
+        client.createPod(podId, podName, dockerImage, ports);
+
+        Thread.sleep(2000);
+        Pod pod = client.getPod(podId);
+        assertNotNull(pod);
+
+        Thread.sleep(POD_ACTIVATION_WAIT_TIME);
+        pod = client.getPod(podId);
+        assertNotNull(pod);
+        assertEquals(pod.getCurrentState().getStatus(), KubernetesConstants.POD_STATUS_RUNNING);
+        log.info("Pod state changed to running: " + pod.getId());
+
+        log.info("Deleting pod: " + pod.getId());
+        client.deletePod(pod.getId());
+        assertNull(client.getPod(pod.getId()));
+        log.info("Pod deleted successfully: " + pod.getId());
+    }
+
+    @Test
+    public void testDeletingAnNonExistingPod() {
         try {
-            client.deleteReplicationController(bogusContrId);
+            client.deletePod("-1234");
         } catch (Exception e) {
             assertEquals(true, e instanceof KubernetesClientException);
-            assertEquals("Replication controller does not exist: [replication-controller-id]
"+bogusContrId, e.getMessage());
         }
-	}
-	
-	@Test
-    public void testServices() throws Exception { 
-	    String serviceId = "nirmal-service";
-	    Service serv = new Service();
-	    serv.setApiVersion("v1beta1");
-	    serv.setContainerPort("8379");
-	    serv.setPort(5000);
-	    serv.setId(serviceId);
-	    serv.setKind("Service");
-	    InetAddress address = InetAddress.getByName(new URL(endpoint).getHost()); 
-	    String publicIp = address.getHostAddress();
-	    serv.setPublicIPs(new String[]{publicIp});
-	    
-	    Labels l = new Labels();
-	    l.setName("nirmal");
-	    
-	    serv.setLabels(l);
-	    serv.setName("nirmal-service");
-	    Selector selector = new Selector();
-	    selector.setName(l.getName());
-	    serv.setSelector(selector);
-	    
-	    // get the services count before creation
-	    int count = client.getAllServices().length;
-	    
-	    if (log.isDebugEnabled()) {
-            log.debug("Creating a Service Proxy: "+serv);
+    }
+
+    @Test
+    public void testReplicationControllerCreationAndDeletion() throws Exception {
+        String replicationControllerId = "stratos-test-rc-1";
+        String replicationControllerName = "stratos-test-rc";
+        int replicas = 2;
+
+        List<Integer> ports = new ArrayList<Integer>();
+        ports.add(CONTAINER_PORT);
+        client.createReplicationController(replicationControllerId, replicationControllerName,
+                dockerImage, ports, null, replicas);
+
+        // Wait 5s for Pods to be created
+        Thread.sleep(5000);
+        ReplicationController replicationController = client.getReplicationController(replicationControllerId);
+
+        // Validate recreation using same id
+        log.info("Testing replication controller re-creation with an existing id: " + replicationController.getId());
+        client.createReplicationController(replicationControllerId, replicationControllerName,
+                dockerImage, ports, null, replicas);
+        log.info("Replication controller re-creation with an existing id was successful");
+
+        // Validate pod count
+        Labels label = new Labels();
+        label.setName(replicationControllerName);
+        List<Pod> pods = client.queryPods(new Labels[]{label});
+        assertEquals(replicas, pods.size());
+
+        // Delete replication controller
+        client.deleteReplicationController(replicationControllerId);
+        assertNull(client.getReplicationController(replicationControllerId));
+
+        // Delete pods
+        for(Pod pod : pods) {
+            client.deletePod(pod.getId());
+            assertNull(client.getPod(pod.getId()));
         }
-        client.createService(serv);
-        assertNotNull(client.getService(serviceId));
-        
+    }
+
+    @Test
+    public void testServiceCreationAndDeletion() throws Exception {
+        String podId = "stratos-test-pod-1";
+        String podName = "stratos-test-pod";
+        String serviceId = "stratos-test-service-1";
+        String serviceName = "stratos-test-service";
+        InetAddress address = InetAddress.getByName(new URL(endpoint).getHost());
+        String publicIp = address.getHostAddress();
+
+        List<Integer> ports = new ArrayList<Integer>();
+        ports.add(CONTAINER_PORT);
+        client.createPod(podId, podName, dockerImage, ports);
+
+        Thread.sleep(2000);
+        Pod podCreated = client.getPod(podId);
+        assertNotNull(podCreated);
+
+        client.createService(serviceId, serviceName, SERVICE_PORT, CONTAINER_PORT, publicIp);
+
+        Thread.sleep(2000);
+        Service service = client.getService(serviceId);
+
         // test recreation using same id
-        client.createService(serv);
-        assertNotNull(client.getService(serviceId));
-        
-        assertEquals(count+1, client.getAllServices().length);
-        
+        client.createService(serviceId, serviceName, SERVICE_PORT, CONTAINER_PORT, publicIp);
+
+        Thread.sleep(2000);
+        service = client.getService(serviceId);
+        assertNotNull(service);
+
         client.deleteService(serviceId);
+        assertNull(client.getService(serviceId));
+    }
+
+    public void deleteReplicationControllers() {
         try {
-            client.getService(serviceId);
-        } catch (Exception e) {
-            assertEquals(true, e instanceof KubernetesClientException);
-        }
-        
-        String bogusServId = "nirmal";
-        // create an invalid Service
-        Service bogusServ = new Service();
-        bogusServ.setId(bogusServId);
-        try {
-            client.createService(bogusServ);
-        } catch (Exception e) {
-            assertEquals(true, e instanceof KubernetesClientException);
+            List<ReplicationController> replicationControllers = client.getReplicationControllers();
+            for(ReplicationController replicationController : replicationControllers) {
+                client.deleteReplicationController(replicationController.getId());
+            }
+        } catch (KubernetesClientException e) {
+            log.error("Could not delete replication controllers", e);
         }
-        
+    }
+
+    public void deleteServices() {
         try {
-            client.getService(bogusServId);
-        } catch (Exception e) {
-            assertEquals(true, e instanceof KubernetesClientException);
-            assertEquals("Service ["+bogusServId+"] doesn't exist.", e.getMessage());
+            List<Service> services = client.getServices();
+            for(Service service : services) {
+                if(!service.getId().contains("kubernetes")) {
+                    client.deleteService(service.getId());
+                }
+            }
+        } catch (KubernetesClientException e) {
+            log.error("Could not delete services", e);
         }
-        
+    }
+
+    public void deletePods() {
         try {
-            client.deleteService(bogusServId);
-        } catch (Exception e) {
-            assertEquals(true, e instanceof KubernetesClientException);
-            assertEquals("Service ["+bogusServId+"] doesn't exist.", e.getMessage());
+            List<Pod> pods = client.getPods();
+            for(Pod replicationController : pods) {
+                client.deletePod(replicationController.getId());
+            }
+        } catch (KubernetesClientException e) {
+            log.error("Could not delete pods", e);
         }
-	}
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/11f9cbd1/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/unit/PodUnitTest.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/unit/PodUnitTest.java
b/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/unit/PodUnitTest.java
index 65d2108..8c374ac 100644
--- a/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/unit/PodUnitTest.java
+++ b/components/org.apache.stratos.kubernetes.client/src/test/java/org/apache/stratos/kubernetes/client/unit/PodUnitTest.java
@@ -66,10 +66,10 @@ public class PodUnitTest extends TestCase{
         Port p = new Port();
         p.setContainerPort(8379);
         p.setHostPort(8379);
-        c.setPorts(new Port[] { p });
+        c.addPort(p);
         m.addContainer(c);
         desiredState.setManifest(m);
-        pod.setState(desiredState);
+        pod.setDesiredState(desiredState);
         State currentState = desiredState;
         pod.setCurrentState(currentState);
         


Mime
View raw message