def test_list_nonexistent(self):
     name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     if utils.is_reachable(dep.config.api_host):
         objs = dep.list()
         self.assertIsInstance(objs, list)
         self.assertEqual(0, len(objs))
 def test_get_by_name_nonexistent(self):
     name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     if utils.is_reachable(dep.config.api_host):
         result = K8sDeployment.get_by_name(config=dep.config, name=name)
         self.assertIsInstance(result, list)
         self.assertEqual(0, len(result))
    def test_pod_emptydir(self):
        container_name = "nginx"
        container_image = "nginx:1.7.9"
        container = utils.create_container(name=container_name, image=container_image)

        vol_name = "emptydir"
        vol_type = "emptyDir"
        vol_mount = "/test-emptydir"
        volume = utils.create_volume(name=vol_name, type=vol_type, mount_path=vol_mount)
        container.add_volume_mount(volume)

        pod_name = "nginx"
        pod = utils.create_pod(name=pod_name)
        pod.add_volume(volume)
        pod.add_container(container)

        if utils.is_reachable(pod.config.api_host):
            pod.create()
            vols = pod.model.model['spec']['volumes']
            volnames = [x['name'] for x in vols]
            self.assertIn(vol_name, volnames)

            vols = pod.model.pod_spec.model['volumes']
            volnames = [x['name'] for x in vols]
            self.assertIn(vol_name, volnames)
            self.assertEqual(1, len(pod.model.model['spec']['containers']))

            mounts = pod.model.model['spec']['containers'][0]['volumeMounts']
            mountnames = [x['name'] for x in mounts]
            self.assertIn(vol_name, mountnames)
            self.assertEqual(1, len(pod.model.pod_spec.model['containers']))

            mounts = pod.model.pod_spec.model['containers'][0]['volumeMounts']
            mountnames = [x['name'] for x in mounts]
            self.assertIn(vol_name, mountnames)
 def test_delete_nonexistent(self):
     name = "yorc-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     if utils.is_reachable(dep.config.api_host):
         try:
             dep.delete()
             self.fail("Should not fail.")
         except Exception as err:
             self.assertIsInstance(err, NotFoundException)
 def test_delete(self):
     name = "yoname"
     rs = utils.create_rs(name=name)
     config = utils.create_config()
     if utils.is_reachable(config.api_host):
         utils.cleanup_rs()
         result = rs.list()
         self.assertIsInstance(result, list)
         self.assertEqual(0, len(result))
    def test_rc_aws_ebs(self):
        # http://kubernetes.io/docs/user-guide/volumes/#awselasticblockstore
        # - the nodes on which pods are running must be AWS EC2 instances
        # - those instances need to be in the same region and availability-zone as the EBS volume
        # - EBS only supports a single EC2 instance mounting a volume

        # Pod creation will timeout waiting for readiness if not on AWS; unschedulable.

        container_name = "nginx"
        container_image = "nginx:1.7.9"
        container_nginx = utils.create_container(name=container_name, image=container_image)

        container_name = "redis"
        container_image = "redis:3.0.7"
        container_redis = utils.create_container(name=container_name, image=container_image)

        volume_id = "vol-0e3056a2"
        vol_name = "ebs"
        vol_type = "awsElasticBlockStore"
        vol_mount = "/test-aws-ebs"
        volume = utils.create_volume(name=vol_name, type=vol_type, mount_path=vol_mount)
        volume.set_volume_id(volume_id)
        container_nginx.add_volume_mount(volume)
        container_redis.add_volume_mount(volume)

        rc_name = "nginx-{0}".format(str(uuid.uuid4()))
        rc = utils.create_rc(name=rc_name)
        rc.add_volume(volume)
        rc.add_container(container_nginx)
        rc.add_container(container_redis)
        rc.set_replicas(3)

        if utils.is_reachable(rc.config.api_host):
            try:
                rc.create()

                vols = rc.model.model['spec']['template']['spec']['volumes']
                volnames = [x['name'] for x in vols]
                self.assertIn(vol_name, volnames)

                vols = rc.model.pod_spec.model['volumes']
                volnames = [x['name'] for x in vols]
                self.assertIn(vol_name, volnames)
                self.assertEqual(2, len(rc.model.model['spec']['template']['spec']['containers']))

                mounts = rc.model.model['spec']['template']['spec']['containers'][0]['volumeMounts']
                mountnames = [x['name'] for x in mounts]
                self.assertIn(vol_name, mountnames)
                self.assertEqual(2, len(rc.model.pod_spec.model['containers']))

                mounts = rc.model.pod_spec.model['containers'][0]['volumeMounts']
                mountnames = [x['name'] for x in mounts]
                self.assertIn(vol_name, mountnames)

            except Exception as err:
                self.assertIsInstance(err, TimedOutException)
    def test_rc_gce_pd(self):
        # http://kubernetes.io/docs/user-guide/volumes/#gcepersistentdisk
        # - the nodes on which pods are running must be GCE VMs
        # - those VMs need to be in the same GCE project and zone as the PD

        # Pod creation will timeout waiting for readiness if not on GCE; unschedulable.

        container_name = "nginx"
        container_image = "nginx:1.7.9"
        container_nginx = utils.create_container(name=container_name, image=container_image)

        container_name = "redis"
        container_image = "redis:3.0.7"
        container_redis = utils.create_container(name=container_name, image=container_image)

        pd_name = "kubernetes-py-test-pd"
        vol_name = "persistent"
        vol_type = "gcePersistentDisk"
        vol_mount = "/test-gce-pd"
        volume = utils.create_volume(name=vol_name, type=vol_type, mount_path=vol_mount, read_only=True)
        volume.set_pd_name(pd_name)
        container_nginx.add_volume_mount(volume)
        container_redis.add_volume_mount(volume)

        rc_name = "nginx-{0}".format(str(uuid.uuid4()))
        rc = utils.create_rc(name=rc_name)
        rc.add_volume(volume)
        rc.add_container(container_nginx)
        rc.add_container(container_redis)
        rc.set_replicas(3)

        if utils.is_reachable(rc.config.api_host):
            try:
                rc.create()

                vols = rc.model.model['spec']['volumes']
                volnames = [x['name'] for x in vols]
                self.assertIn(vol_name, volnames)

                vols = rc.model.pod_spec.model['volumes']
                volnames = [x['name'] for x in vols]
                self.assertIn(vol_name, volnames)
                self.assertEqual(1, len(rc.model.model['spec']['containers']))

                mounts = rc.model.model['spec']['containers'][0]['volumeMounts']
                mountnames = [x['name'] for x in mounts]
                self.assertIn(vol_name, mountnames)
                self.assertEqual(1, len(rc.model.pod_spec.model['containers']))

                mounts = rc.model.pod_spec.model['containers'][0]['volumeMounts']
                mountnames = [x['name'] for x in mounts]
                self.assertIn(vol_name, mountnames)

            except Exception as err:
                self.assertIsInstance(err, TimedOutException)
 def test_delete(self):
     name = "yocontainer"
     container = utils.create_container(name=name)
     name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     dep.add_container(container)
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         utils.cleanup_deployments()
         result = dep.list()
         self.assertIsInstance(result, list)
         self.assertEqual(0, len(result))
 def test_update_namespace_fails(self):
     name = "yocontainer"
     container = utils.create_container(name=name)
     name = "yorc-{0}".format(str(uuid.uuid4()))
     nspace = "yonamespace"
     dep = utils.create_deployment(name=name)
     dep.add_container(container)
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         dep.set_namespace(nspace)
         with self.assertRaises(BadRequestException):
             dep.update()
 def test_create_already_exists(self):
     name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     cont_name = "nginx"
     cont_image = "nginx:1.7.9"
     cont = utils.create_container(name=cont_name, image=cont_image)
     dep.add_container(container=cont)
     dep.set_replicas(1)
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         with self.assertRaises(AlreadyExistsException):
             dep.create()
 def test_update_name_fails(self):
     name = "yocontainer"
     container = utils.create_container(name=name)
     name1 = "yodep1"
     name2 = "yodep2"
     dep = utils.create_deployment(name=name1)
     dep.add_container(container)
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         dep.name = name2
         with self.assertRaises(BadRequestException):
             dep.update()
 def test_get_by_name(self):
     cont_name = "yocontainer"
     container = utils.create_container(name=cont_name)
     name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     dep.add_container(container)
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         result = K8sDeployment.get_by_name(config=dep.config, name=name)
         self.assertIsInstance(result, list)
         self.assertEqual(1, len(result))
         self.assertIsInstance(result[0], K8sDeployment)
         self.assertEqual(dep, result[0])
    def test_rc_git_repo(self):
        container_name = "nginx"
        container_image = "nginx:1.7.9"
        container_nginx = utils.create_container(name=container_name, image=container_image)

        container_name = "redis"
        container_image = "redis:3.0.7"
        container_redis = utils.create_container(name=container_name, image=container_image)

        vol_name = "git-repo"
        vol_type = "gitRepo"
        vol_mount = "/test-git-repo"
        repo = "https://user:[email protected]/ops/traffic-analyzer.git"
        revision = "e42d3dca1541ba085f34ce282feda1109a707c7b"
        volume = utils.create_volume(name=vol_name, type=vol_type, mount_path=vol_mount)
        volume.set_git_repository(repo)
        volume.set_git_revision(revision)
        container_nginx.add_volume_mount(volume)
        container_redis.add_volume_mount(volume)

        rc_name = "nginx-{0}".format(str(uuid.uuid4()))
        rc = utils.create_rc(name=rc_name)
        rc.add_volume(volume)
        rc.add_container(container_nginx)
        rc.add_container(container_redis)
        rc.set_replicas(3)

        if utils.is_reachable(rc.config.api_host):
            try:
                rc.create()

                vols = rc.model.model['spec']['volumes']
                volnames = [x['name'] for x in vols]
                self.assertIn(vol_name, volnames)

                vols = rc.model.pod_spec.model['volumes']
                volnames = [x['name'] for x in vols]
                self.assertIn(vol_name, volnames)
                self.assertEqual(1, len(rc.model.model['spec']['containers']))

                mounts = rc.model.model['spec']['containers'][0]['volumeMounts']
                mountnames = [x['name'] for x in mounts]
                self.assertIn(vol_name, mountnames)
                self.assertEqual(1, len(rc.model.pod_spec.model['containers']))

                mounts = rc.model.pod_spec.model['containers'][0]['volumeMounts']
                mountnames = [x['name'] for x in mounts]
                self.assertIn(vol_name, mountnames)

            except Exception as err:
                self.assertIsInstance(err, TimedOutException)
    def test_rc_nfs(self):
        container_name = "nginx"
        container_image = "nginx:1.7.9"
        container_nginx = utils.create_container(name=container_name, image=container_image)

        container_name = "redis"
        container_image = "redis:3.0.7"
        container_redis = utils.create_container(name=container_name, image=container_image)

        vol_name = "nfs"
        vol_type = "nfs"
        vol_mount = "/test-nfs"
        server = "howard.mtl.mnubo.com"
        path = "/fs1/test-nfs"
        volume = utils.create_volume(name=vol_name, type=vol_type, mount_path=vol_mount)
        volume.set_server(server)
        volume.set_path(path)
        container_nginx.add_volume_mount(volume)
        container_redis.add_volume_mount(volume)

        rc_name = "nginx-{0}".format(str(uuid.uuid4()))
        rc = utils.create_rc(name=rc_name)
        rc.add_volume(volume)
        rc.add_container(container_nginx)
        rc.add_container(container_redis)
        rc.set_replicas(3)

        if utils.is_reachable(rc.config.api_host):
            try:
                rc.create()

                vols = rc.model.model['spec']['volumes']
                volnames = [x['name'] for x in vols]
                self.assertIn(vol_name, volnames)

                vols = rc.model.pod_spec.model['volumes']
                volnames = [x['name'] for x in vols]
                self.assertIn(vol_name, volnames)
                self.assertEqual(1, len(rc.model.model['spec']['containers']))

                mounts = rc.model.model['spec']['containers'][0]['volumeMounts']
                mountnames = [x['name'] for x in mounts]
                self.assertIn(vol_name, mountnames)
                self.assertEqual(1, len(rc.model.pod_spec.model['containers']))

                mounts = rc.model.pod_spec.model['containers'][0]['volumeMounts']
                mountnames = [x['name'] for x in mounts]
                self.assertIn(vol_name, mountnames)

            except Exception as err:
                self.assertIsInstance(err, TimedOutException)
    def test_rc_secret(self):
        container_name = "nginx"
        container_image = "nginx:1.7.9"
        container_nginx = utils.create_container(name=container_name, image=container_image)

        container_name = "redis"
        container_image = "redis:3.0.7"
        container_redis = utils.create_container(name=container_name, image=container_image)

        secret_name = "yosecret"
        secret = utils.create_secret(name=secret_name)
        k = ".secret-file"
        v = "dmFsdWUtMg0KDQo="
        secret.set_data(k, v)

        vol_name = "secret"
        vol_type = "secret"
        vol_mount = "/test-secret"
        volume = utils.create_volume(name=vol_name, type=vol_type, mount_path=vol_mount)
        volume.set_secret_name(secret)
        container_nginx.add_volume_mount(volume)
        container_redis.add_volume_mount(volume)

        rc_name = "app"
        rc = utils.create_rc(name=rc_name)
        rc.add_volume(volume)
        rc.add_container(container_nginx)
        rc.add_container(container_redis)
        rc.set_replicas(1)

        if utils.is_reachable(rc.config.api_host):
            secret.create()
            rc.create()
            vols = rc.model.model['spec']['template']['spec']['volumes']
            volnames = [x['name'] for x in vols]
            self.assertIn(vol_name, volnames)

            vols = rc.model.pod_spec.model['volumes']
            volnames = [x['name'] for x in vols]
            self.assertIn(vol_name, volnames)
            self.assertEqual(2, len(rc.model.model['spec']['template']['spec']['containers']))

            mounts = rc.model.model['spec']['template']['spec']['containers'][0]['volumeMounts']
            mountnames = [x['name'] for x in mounts]
            self.assertIn(vol_name, mountnames)
            self.assertEqual(2, len(rc.model.pod_spec.model['containers']))

            mounts = rc.model.pod_spec.model['containers'][0]['volumeMounts']
            mountnames = [x['name'] for x in mounts]
            self.assertIn(vol_name, mountnames)
 def test_update_pod_labels(self):
     name = "nginx"
     image = "nginx:1.7.9"
     container = utils.create_container(name=name, image=image)
     dep_name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=dep_name)
     dep.add_container(container)
     dep.set_replicas(3)
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         labels = dep.get_pod_labels()
         labels['newkey'] = 'newvalue'
         dep.set_pod_labels(labels)
         updated = dep.update()
         self.assertEqual(labels, updated.get_pod_labels())
 def test_create_three_replicas(self):
     name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     cont_name = "nginx"
     cont_image = "nginx:1.7.9"
     cont = utils.create_container(name=cont_name, image=cont_image)
     dep.add_container(container=cont)
     dep.set_replicas(3)
     if utils.is_reachable(dep.config.api_host):
         d = dep.create()
         self.assertIsNotNone(d)
         self.assertIsInstance(d, K8sDeployment)
         self.assertEqual(d, dep)
         self.assertEqual(3, d.model.model['status']['replicas'])
         self.assertEqual(3, d.model.model['status']['availableReplicas'])
         self.assertEqual(3, d.model.model['status']['updatedReplicas'])
 def test_create_zero_replicas(self):
     name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     cont_name = "redis"
     cont_image = "redis:3.2.3"
     cont = utils.create_container(name=cont_name, image=cont_image)
     dep.add_container(container=cont)
     if utils.is_reachable(dep.config.api_host):
         d = dep.create()
         self.assertIsNotNone(d)
         self.assertIsInstance(d, K8sDeployment)
         self.assertEqual(d, dep)
         self.assertEqual(0, d.model.model['spec']['replicas'])
         self.assertNotIn('replicas', d.model.model['status'])
         self.assertNotIn('availableReplicas', d.model.model['status'])
         self.assertNotIn('updatedReplicas', d.model.model['status'])
 def test_scale(self):
     cont_name = "yocontainer"
     container = utils.create_container(name=cont_name)
     name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     dep.add_container(container)
     dep.set_replicas(3)
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         replicas = dep.get_replicas()
         for k in replicas:
             self.assertEqual(3, replicas[k])
         dep.scale(5)
         replicas = dep.get_replicas()
         for k in replicas:
             self.assertEqual(5, replicas[k])
 def test_update_container_image(self):
     name = "nginx"
     image1 = "nginx:1.7.9"
     image2 = "nginx:1.9.1"
     container = utils.create_container(name=name, image=image1)
     dep_name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=dep_name)
     dep.add_container(container)
     dep.set_replicas(3)
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         self.assertEqual(image1, dep.model.model['spec']['template']['spec']['containers'][0]['image'])
         dep.set_container_image(name=name, image=image2)
         dep.update()
         self.assertIn('annotations', dep.model.deployment_metadata.model)
         self.assertIn('deployment.kubernetes.io/revision', dep.model.deployment_metadata.model['annotations'])
         self.assertNotEqual(image1, dep.model.model['spec']['template']['spec']['containers'][0]['image'])
         self.assertEqual(image2, dep.model.model['spec']['template']['spec']['containers'][0]['image'])
    def test_rc_hostpath(self):
        container_name = "nginx"
        container_image = "nginx:1.7.9"
        container_nginx = utils.create_container(name=container_name, image=container_image)

        container_name = "redis"
        container_image = "redis:3.0.7"
        container_redis = utils.create_container(name=container_name, image=container_image)

        vol_name = "hostpath"
        vol_type = "hostPath"
        vol_mount = "/test-hostpath"
        hostpath = "/var/lib/docker"
        volume = utils.create_volume(name=vol_name, type=vol_type, mount_path=vol_mount)
        volume.set_path(hostpath)
        container_nginx.add_volume_mount(volume)
        container_redis.add_volume_mount(volume)

        rc_name = "app"
        rc = utils.create_rc(name=rc_name)
        rc.add_volume(volume)
        rc.add_container(container_nginx)
        rc.add_container(container_redis)
        rc.set_replicas(1)

        if utils.is_reachable(rc.config.api_host):
            rc.create()
            vols = rc.model.model['spec']['template']['spec']['volumes']
            volnames = [x['name'] for x in vols]
            self.assertIn(vol_name, volnames)

            vols = rc.model.pod_spec.model['volumes']
            volnames = [x['name'] for x in vols]
            self.assertIn(vol_name, volnames)
            self.assertEqual(2, len(rc.model.model['spec']['template']['spec']['containers']))

            mounts = rc.model.model['spec']['template']['spec']['containers'][0]['volumeMounts']
            mountnames = [x['name'] for x in mounts]
            self.assertIn(vol_name, mountnames)
            self.assertEqual(2, len(rc.model.pod_spec.model['containers']))

            mounts = rc.model.pod_spec.model['containers'][0]['volumeMounts']
            mountnames = [x['name'] for x in mounts]
            self.assertIn(vol_name, mountnames)
    def test_pod_secret(self):
        container_name = "nginx"
        container_image = "nginx:1.7.9"
        container = utils.create_container(name=container_name, image=container_image)

        secret_name = "yosecret"
        secret = utils.create_secret(name=secret_name)
        k = ".secret-file"
        v = "dmFsdWUtMg0KDQo="
        secret.set_data(k, v)

        vol_name = "secret"
        vol_type = "secret"
        vol_mount = "/test-secret"
        volume = utils.create_volume(name=vol_name, type=vol_type, mount_path=vol_mount)
        volume.set_secret_name(secret)
        container.add_volume_mount(volume)

        pod_name = "nginx"
        pod = utils.create_pod(name=pod_name)
        pod.add_volume(volume)
        pod.add_container(container)

        if utils.is_reachable(pod.config.api_host):
            secret.create()
            pod.create()
            vols = pod.model.model['spec']['volumes']
            volnames = [x['name'] for x in vols]
            self.assertIn(vol_name, volnames)

            vols = pod.model.pod_spec.model['volumes']
            volnames = [x['name'] for x in vols]
            self.assertIn(vol_name, volnames)
            self.assertEqual(1, len(pod.model.model['spec']['containers']))

            mounts = pod.model.model['spec']['containers'][0]['volumeMounts']
            mountnames = [x['name'] for x in mounts]
            self.assertIn(vol_name, mountnames)
            self.assertEqual(1, len(pod.model.pod_spec.model['containers']))

            mounts = pod.model.pod_spec.model['containers'][0]['volumeMounts']
            mountnames = [x['name'] for x in mounts]
            self.assertIn(vol_name, mountnames)
 def test_list_multiple(self):
     name = "yocontainer"
     container = utils.create_container(name=name)
     config = K8sConfig(kubeconfig=utils.kubeconfig_fallback)
     deployments = []
     count = 3
     objs = []
     if utils.is_reachable(config.api_host):
         for i in range(0, count):
             name = "yodep-{0}".format(str(uuid.uuid4()))
             dep = utils.create_deployment(config, name)
             dep.add_container(container)
             result = dep.create()
             self.assertIsInstance(result, K8sDeployment)
             self.assertEqual(dep, result)
             deployments.append(dep)
             objs = dep.list()
         self.assertEqual(count, len(deployments))
         self.assertEqual(count, len(objs))
 def test_create_no_args(self):
     name = "yodep-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     if utils.is_reachable(dep.config.api_host):
         with self.assertRaises(UnprocessableEntityException):
             dep.create()
 def test_delete_nonexistent(self):
     name = "yorc-{0}".format(str(uuid.uuid4()))
     dep = utils.create_deployment(name=name)
     if utils.is_reachable(dep.config.api_host):
         with self.assertRaises(NotFoundException):
             dep.delete()