示例#1
0
    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)
示例#2
0
    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)
示例#3
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://*****:*****@git-lab1.mtl.mnubo.com/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)
示例#4
0
    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)
示例#5
0
    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_rollback_no_args(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.desired_replicas = 3
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         self.assertEqual(image1, dep.containers[0].image)
         dep.container_image = (name, image2)
         dep.update()
         self.assertIn('deployment.kubernetes.io/revision', dep.annotations)
         rev_before = dep.get_annotation(
             'deployment.kubernetes.io/revision')
         self.assertNotEqual(image1, dep.containers[0].image)
         self.assertEqual(image2, dep.containers[0].image)
         dep.rollback()
         self.assertIn('deployment.kubernetes.io/revision', dep.annotations)
         rev_after = dep.get_annotation('deployment.kubernetes.io/revision')
         self.assertNotEqual(rev_before, rev_after)
         self.assertGreater(rev_after, rev_before)
         self.assertEqual(image1, dep.containers[0].image)
         self.assertNotEqual(image2, dep.containers[0].image)
示例#7
0
    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_add_env(self):
     cont = Container(utils.myweb_container())
     k8s_container = utils.create_container(name=cont.name)
     k8s_container.model = cont
     envs = utils.myweb_envs()
     for k, v in envs.items():
         k8s_container.add_env(k, v)
     self.assertEqual(4, len(k8s_container.env))
示例#9
0
    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_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.desired_replicas = 1
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         with self.assertRaises(AlreadyExistsException):
             dep.create()
 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))
示例#12
0
 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_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(NotFoundException):
             dep.update()
示例#14
0
 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_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.namespace = nspace
         with self.assertRaises(BadRequestException):
             dep.update()
示例#16
0
 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))
示例#17
0
 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_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])
示例#19
0
 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])
示例#20
0
def _run(name,
         volume_name,
         command,
         data_dir,
         data_file_name,
         time_limit,
         memory_limit,
         file_size_limit=10 * 1024 * 1024,
         use_time=False):

    box_name = name.split('-')[0] + '-run-box'

    command = command + ' < /data/{}.in > /crazybox/{}.out'.format(
        data_file_name, name)

    # /usr/bin/time -v sh -c 'exec ./a < 2.in > 2.out' |& grep 'Maximum resident'
    # 如果不加sh -c参数,会导致获取内存不正确的情况,似乎这种情况下获取到的内存是重定向这个命令的内存?
    if use_time:
        command = "/usr/bin/time -v sh -c 'exec {}' |& grep 'Maximum resident'".format(
            command)

    command = '/bin/bash -c "{}"'.format(command)

    crazybox, real_time_limit = create_container(box_name, command,
                                                 volume_name, time_limit,
                                                 memory_limit, file_size_limit,
                                                 data_dir)

    ret = run_container(crazybox, real_time_limit)

    if use_time:
        crazybox.remove(force=True)
        if ret['exit_code'] == 0:
            return ret['stdout'].decode().split(':')[1].strip()  # KB
        else:
            logger.warning('/usr/bin/time function error: %s |-| %s',
                           ret['stdout'].decode(), ret['stderr'].decode())
            return None
    else:
        try:
            data = crazybox.get_archive('/crazybox/{}.out'.format(name))
            tar_path = os.path.join(TEMP_DIR,
                                    str(name).split('-')[0] + '-out.tar')
            with open(tar_path, 'w') as file:
                file.write(data[0].read().decode())
        except APIError:
            tar_path = None
        crazybox.remove(force=True)
        return ret, tar_path
 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.desired_replicas = 3
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         labels = dep.pod_labels
         labels['newkey'] = 'newvalue'
         dep.pod_labels = labels
         updated = dep.update()
         self.assertEqual(labels, updated.pod_labels)
示例#22
0
 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())
示例#23
0
 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_one_replica(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.desired_replicas = 1
     if utils.is_reachable(dep.config.api_host):
         d = dep.create()
         self.assertIsNotNone(d)
         self.assertIsInstance(d, K8sDeployment)
         self.assertEqual(d, dep)
         self.assertEqual(1, d.desired_replicas)
         self.assertEqual(1, d.available_replicas)
         self.assertEqual(1, d.updated_replicas)
 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.desired_replicas = 3
     if utils.is_reachable(dep.config.api_host):
         dep.create()
         self.assertEqual(3, dep.desired_replicas)
         self.assertEqual(3, dep.updated_replicas)
         self.assertEqual(3, dep.available_replicas)
         dep.scale(5)
         self.assertEqual(5, dep.desired_replicas)
         self.assertEqual(5, dep.updated_replicas)
         self.assertEqual(5, dep.available_replicas)
示例#26
0
 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'])
示例#27
0
 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_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.desired_replicas)
         self.assertIsNone(d.available_replicas)
         self.assertIsNone(d.current_replicas)
         self.assertIsNone(d.unavailable_replicas)
         self.assertIsNone(d.updated_replicas)
示例#29
0
def _compile(name: str,
             volume_name: str,
             command,
             time_limit,
             memory_limit,
             file_size_limit=128 * 1024 * 1024):
    box_name = name.split('-')[0] + '-compile-box'
    crazybox, real_time_limit = create_container(box_name, command,
                                                 volume_name, time_limit,
                                                 memory_limit, file_size_limit)

    tar_file_path = os.path.join(TEMP_DIR, name + '.tar')
    file = open(tar_file_path).read().encode()
    crazybox.put_archive(WORKING_DIR, file)

    ret = run_container(crazybox, real_time_limit)
    crazybox.remove(force=True)
    return ret
示例#30
0
 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'])
示例#31
0
    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)
示例#32
0
 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_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))