Ejemplo n.º 1
0
 def test_rc_get_pod_node_name(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     nodename = "yonodename"
     obj.set_pod_node_name(name=nodename)
     name = obj.get_pod_node_name()
     self.assertEqual(name, nodename)
Ejemplo n.º 2
0
 def test_rc_set_service_account(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     name_in = "yoservice"
     obj.set_service_account(name_in)
     name_out = obj.get_service_account()
     self.assertEqual(name_in, name_out)
Ejemplo n.º 3
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"
        hostpath = "/var/lib/docker"
        volume = utils.create_volume(name=vol_name, type=vol_type)
        volume.path = hostpath

        mount_name = vol_name
        mount_path = '/test-hostpath'
        mount = K8sVolumeMount(name=mount_name, mount_path=mount_path)
        container_nginx.add_volume_mount(mount)
        container_redis.add_volume_mount(mount)

        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.desired_replicas = 1

        if utils.is_reachable(rc.config.api_host):
            rc.create()
            volnames = [x.name for x in rc.volumes]
            self.assertIn(vol_name, volnames)
Ejemplo n.º 4
0
 def test_rc_set_termination_grace_period(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     secs_in = 1234
     obj.set_termination_grace_period(secs_in)
     secs_out = obj.get_termination_grace_period()
     self.assertEqual(secs_in, secs_out)
Ejemplo n.º 5
0
    def test_myweb_start(self):

        # create a myweb service
        svc = utils.create_service(name="myweb")
        self.assertIsInstance(svc, K8sService)
        svc.selector = {'name': 'myweb'}
        svc.type = 'NodePort'
        svc.add_port(
            name="tcp31030",
            port=31030,
            target_port="tcp31030",
            protocol="tcp",
            node_port=8030
        )

        # create a myweb replication controller
        container = utils.create_container(name="myweb", image="nginx:latest")
        container.add_port(
            container_port=80,
            host_port=31030,
            name="tcp31030",
            protocol="tcp"
        )
        rc = utils.create_rc(name="myweb", replicas=2)
        rc.add_container(container)

        # create the API resources
        if utils.is_reachable(rc.config.api_host):
            svc.create()
            rc.create()
            pass
Ejemplo n.º 6
0
 def test_rc_get_pod_restart_policy(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     p_in = "OnFailure"
     obj.set_pod_restart_policy(p_in)
     p_out = obj.get_pod_restart_policy()
     self.assertEqual(p_in, p_out)
Ejemplo n.º 7
0
 def test_rc_set_pod_restart_policy(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     policy = "Always"
     obj.set_pod_restart_policy(policy=policy)
     p = obj.get_pod_restart_policy()
     self.assertEqual(policy, p)
Ejemplo n.º 8
0
 def test_rc_get_pod_node_selector(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     s_in = {"disktype": "ssd"}
     obj.set_pod_node_selector(selector=s_in)
     s_out = obj.get_pod_node_selector()
     self.assertEqual(s_in, s_out)
Ejemplo n.º 9
0
 def test_rc_set_pod_node_selector_invalid_arg(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     s_in = "yoselector"
     try:
         obj.set_pod_node_selector(selector=s_in)
     except Exception as err:
         self.assertIsInstance(err, SyntaxError)
Ejemplo n.º 10
0
 def test_init_object_type_rc(self):
     ot = "ReplicationController"
     name = "yorc"
     obj = utils.create_rc(name=name)
     self.assertIsNotNone(obj)
     self.assertIsInstance(obj, K8sPodBasedObject)
     self.assertEqual(ot, obj.obj_type)
     self.assertEqual(name, obj.name)
Ejemplo n.º 11
0
 def test_init_with_model_rc(self):
     name = "yoname"
     obj = utils.create_rc(name=name)
     self.assertIsInstance(obj.model, ReplicationController)
     self.assertIsInstance(obj.model.model, dict)
     self.assertIsInstance(obj.model.pod_metadata, ObjectMeta)
     self.assertIsInstance(obj.model.pod_spec, PodSpec)
     self.assertIsNone(obj.model.pod_status)
Ejemplo n.º 12
0
 def test_rc_set_pod_node_name_invalid_arg(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     nodename = 666
     try:
         obj.set_pod_node_name(name=nodename)
     except Exception as err:
         self.assertIsInstance(err, SyntaxError)
Ejemplo n.º 13
0
 def test_rc_set_active_deadline_invalid_arg(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     d = "yodeadline"
     try:
         obj.set_active_deadline(d)
     except Exception as err:
         self.assertIsInstance(err, SyntaxError)
Ejemplo n.º 14
0
 def test_rc_set_service_account_none_arg(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     try:
         obj.set_service_account()
         self.fail("Should not fail.")
     except Exception as err:
         self.assertIsInstance(err, SyntaxError)
Ejemplo n.º 15
0
 def test_rc_set_termination_grace_period_none_arg(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     try:
         obj.set_termination_grace_period()
         self.fail("Should not fail.")
     except Exception as err:
         self.assertIsInstance(err, SyntaxError)
Ejemplo n.º 16
0
 def test_rc_add_image_pull_secrets_invalid_arg(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     secretname = 666
     try:
         obj.add_image_pull_secrets(name=secretname)
         self.fail("Should not fail.")
     except Exception as err:
         self.assertIsInstance(err, SyntaxError)
Ejemplo n.º 17
0
 def test_rc_add_container_invalid(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     c = object()
     try:
         obj.add_container(c)
         self.fail("Should not fail.")
     except Exception as err:
         self.assertIsInstance(err, AssertionError)
Ejemplo n.º 18
0
 def test_rc_set_pod_restart_policy_invalid_arg(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     policy = "yopolicy"
     try:
         obj.set_pod_restart_policy(policy=policy)
         self.fail("Should not fail.")
     except Exception as err:
         self.assertIsInstance(err, SyntaxError)
Ejemplo n.º 19
0
    def test_rc_add_image_pull_secrets(self):
        name = "yorc"
        obj = utils.create_rc(name=name)
        secretname = "yosecret"
        obj.add_image_pull_secrets(name=secretname)

        podspec = obj.model.model["spec"]["template"]["spec"]
        self.assertIn("imagePullSecrets", podspec)
        self.assertEqual(1, len(podspec["imagePullSecrets"]))
        self.assertEqual(secretname, podspec["imagePullSecrets"][0]["name"])
        podspec = obj.model.pod_spec
        self.assertIn("imagePullSecrets", podspec.model)
        self.assertEqual(1, len(podspec.model["imagePullSecrets"]))
        self.assertEqual(secretname, podspec.model["imagePullSecrets"][0]["name"])
Ejemplo n.º 20
0
    def test_rc_set_pod_node_name(self):
        name = "yorc"
        obj = utils.create_rc(name=name)
        nodename = "yonodename"
        obj.set_pod_node_name(name=nodename)

        podspec = obj.model.model["spec"]["template"]["spec"]
        self.assertIn("nodeName", podspec)
        self.assertIsInstance(podspec["nodeName"], str)
        self.assertEqual(nodename, podspec["nodeName"])
        podspec = obj.model.pod_spec
        self.assertIn("nodeName", podspec.model)
        self.assertIsInstance(podspec.model["nodeName"], str)
        self.assertEqual(nodename, podspec.model["nodeName"])
Ejemplo n.º 21
0
    def test_rc_get_pod_containers(self):
        name = "yorc"
        obj = utils.create_rc(name=name)
        count = 3
        for i in range(0, 3):
            name = "yocontainer_{0}".format(i)
            image = "redis"
            c = K8sContainer(name=name, image=image)
            obj.add_container(c)

        containers = obj.get_pod_containers()
        self.assertIsNotNone(containers)
        self.assertEqual(count, len(containers))
        [self.assertIsInstance(x, dict) for x in containers]
Ejemplo n.º 22
0
    def test_rc_set_pod_node_selector(self):
        name = "yorc"
        obj = utils.create_rc(name=name)
        s = {"disktype": "ssd"}
        obj.set_pod_node_selector(selector=s)

        podspec = obj.model.model["spec"]["template"]["spec"]
        self.assertIn("nodeSelector", podspec)
        self.assertIsInstance(podspec["nodeSelector"], dict)
        self.assertEqual(s, podspec["nodeSelector"])
        podspec = obj.model.pod_spec
        self.assertIn("nodeSelector", podspec.model)
        self.assertIsInstance(podspec.model["nodeSelector"], dict)
        self.assertEqual(s, podspec.model["nodeSelector"])
Ejemplo n.º 23
0
    def test_rc_set_active_deadline(self):
        name = "yorc"
        obj = utils.create_rc(name=name)
        d = 600
        obj.set_active_deadline(d)

        podspec = obj.model.model["spec"]["template"]["spec"]
        self.assertIn("activeDeadlineSeconds", podspec)
        self.assertIsInstance(podspec["activeDeadlineSeconds"], int)
        self.assertEqual(d, podspec["activeDeadlineSeconds"])
        podspec = obj.model.pod_spec
        self.assertNotIn("nodeName", podspec.model)
        self.assertIn("activeDeadlineSeconds", podspec.model)
        self.assertIsInstance(podspec.model["activeDeadlineSeconds"], int)
        self.assertEqual(d, podspec.model["activeDeadlineSeconds"])
Ejemplo n.º 24
0
 def test_struct_with_model_rc_check_rcmeta(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     meta = obj.model.rc_metadata
     self.assertIsNotNone(meta)
     self.assertIsInstance(meta, ObjectMeta)
     self.assertIsInstance(meta.model, dict)
     self.assertEqual(len(meta.model), 3)
     self.assertIn("labels", meta.model)
     self.assertIsInstance(meta.model["labels"], dict)
     self.assertIn("name", meta.model["labels"])
     self.assertEqual(meta.model["labels"]["name"], name)
     self.assertIn("name", meta.model)
     self.assertIsInstance(meta.model["name"], str)
     self.assertEqual(meta.model["name"], name)
     self.assertIn("namespace", meta.model)
     self.assertIsInstance(meta.model["namespace"], str)
Ejemplo n.º 25
0
 def test_struct_with_model_rc_check_podspec(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     spec = obj.model.pod_spec
     self.assertIsNotNone(spec)
     self.assertIsInstance(spec, PodSpec)
     self.assertIsInstance(spec.containers, list)
     self.assertEqual(0, len(spec.containers))
     self.assertIsInstance(spec.model, dict)
     self.assertEqual(len(spec.model), 4)
     self.assertIn("containers", spec.model)
     self.assertIsInstance(spec.model["containers"], list)
     self.assertIn("dnsPolicy", spec.model)
     self.assertIsInstance(spec.model["dnsPolicy"], str)
     self.assertIn("restartPolicy", spec.model)
     self.assertIsInstance(spec.model["restartPolicy"], str)
     self.assertIn("volumes", spec.model)
     self.assertIsInstance(spec.model["volumes"], list)
Ejemplo n.º 26
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 = "mnubo-disk1"
        vol_name = "persistent"
        vol_type = "gcePersistentDisk"
        volume = utils.create_volume(name=vol_name, type=vol_type)
        volume.pd_name = pd_name
        volume.read_only = True  # HTTP 422: GCE PD can only be mounted on multiple machines if it is read-only

        mount_name = vol_name
        mount_path = '/test-gce'
        mount = K8sVolumeMount(name=mount_name, mount_path=mount_path)
        container_nginx.add_volume_mount(mount)
        container_redis.add_volume_mount(mount)

        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.desired_replicas = 3

        if utils.is_reachable(rc.config.api_host):
            try:
                rc.create()
                volnames = [x.name for x in rc.volumes]
                self.assertIn(vol_name, volnames)
            except Exception as err:
                self.assertIsInstance(err, TimedOutException)
Ejemplo n.º 27
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"
        volume = utils.create_volume(name=vol_name, type=vol_type)
        volume.volume_id = volume_id

        mount_name = vol_name
        mount_path = '/test-aws'
        mount = K8sVolumeMount(name=mount_name, mount_path=mount_path)
        container_nginx.add_volume_mount(mount)
        container_redis.add_volume_mount(mount)

        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.desired_replicas = 3

        if utils.is_reachable(rc.config.api_host):
            try:
                rc.create()
                volnames = [x.name for x in rc.volumes]
                self.assertIn(vol_name, volnames)
            except Exception as err:
                self.assertIsInstance(err, TimedOutException)
Ejemplo n.º 28
0
    def test_rc_add_container(self):
        name = "yoname"
        obj = utils.create_rc(name=name)

        podspec = obj.model.model["spec"]["template"]["spec"]
        self.assertEqual(0, len(podspec["containers"]))
        podspec = obj.model.pod_spec
        self.assertEqual(0, len(podspec.containers))
        self.assertEqual(0, len(podspec.model["containers"]))

        name = "yopod"
        image = "redis"
        c = K8sContainer(name=name, image=image)
        obj.add_container(c)

        podspec = obj.model.model["spec"]["template"]["spec"]
        self.assertEqual(1, len(podspec["containers"]))
        podspec = obj.model.pod_spec
        self.assertEqual(1, len(podspec.containers))
        self.assertEqual(1, len(podspec.model["containers"]))
Ejemplo n.º 29
0
    def test_rc_hostpath_list(self):
        volumes = [{
            'hostPath': {
                'path': '/root/.dockercfg'
            },
            'name': 'dockercred'
        }, {
            'hostPath': {
                'path': '/usr/bin/docker'
            },
            'name': 'dockerbin'
        }, {
            'hostPath': {
                'path': '/var/run/docker.sock'
            },
            'name': 'dockersock'
        }, {
            'hostPath': {
                'path': '/root/.docker'
            },
            'name': 'dockerconfig'
        }]
        rc = utils.create_rc(name="admintool")

        for vol in volumes:
            keys = list(filter(lambda x: x != 'name', vol.keys()))
            v = K8sVolume(
                name=vol['name'],
                type=keys[0],
            )
            dico = vol[keys[0]]
            if dico is not None:
                v.path = dico['path']
            rc.add_volume(v)

        self.assertEqual(len(volumes), len(rc.volumes))
        for i in range(0, len(volumes)):
            self.assertEqual(volumes[i]['name'], rc.volumes[i].name)
            self.assertEqual(volumes[i]['hostPath']['path'],
                             rc.volumes[i].hostPath.path)
Ejemplo n.º 30
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.data = {k: v}

        vol_name = "secret"
        vol_type = "secret"
        volume = utils.create_volume(name=vol_name, type=vol_type)
        volume.secret_name = secret_name

        mount_name = vol_name
        mount_path = '/test-secret'
        mount = K8sVolumeMount(name=mount_name, mount_path=mount_path)
        container_nginx.add_volume_mount(mount)
        container_redis.add_volume_mount(mount)

        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.desired_replicas = 1

        if utils.is_reachable(rc.config.api_host):
            secret.create()
            rc.create()
            volnames = [x.name for x in rc.volumes]
            self.assertIn(vol_name, volnames)
Ejemplo n.º 31
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"
        server = "howard.mtl.mnubo.com"
        path = "/fs1/test-nfs"
        volume = utils.create_volume(name=vol_name, type=vol_type)
        volume.nfs_server = server
        volume.nfs_path = path

        mount_name = vol_name
        mount_path = '/test-nfs'
        mount = K8sVolumeMount(name=mount_name, mount_path=mount_path)
        container_nginx.add_volume_mount(mount)
        container_redis.add_volume_mount(mount)

        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.desired_replicas = 3

        if utils.is_reachable(rc.config.api_host):
            try:
                rc.create()
                volnames = [x.name for x in rc.volumes]
                self.assertIn(vol_name, volnames)
            except Exception as err:
                self.assertIsInstance(err, TimedOutException)
Ejemplo n.º 32
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"
        repo = "https://*****:*****@somewhere/repo.git"
        revision = "e42d3dca1541ba085f34ce282feda1109a707c7b"
        volume = utils.create_volume(name=vol_name, type=vol_type)
        volume.git_repository = repo
        volume.git_revision = revision

        mount_name = vol_name
        mount_path = '/test-git'
        mount = K8sVolumeMount(name=mount_name, mount_path=mount_path)
        container_nginx.add_volume_mount(mount)
        container_redis.add_volume_mount(mount)

        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.desired_replicas = 3

        if utils.is_reachable(rc.config.api_host):
            try:
                rc.create()
                volnames = [x.name for x in rc.volumes]
                self.assertIn(vol_name, volnames)
            except Exception as err:
                self.assertIsInstance(err, TimedOutException)
Ejemplo n.º 33
0
 def test_rc_get_pod_containers_empty(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     containers = obj.get_pod_containers()
     self.assertIsNotNone(containers)
     self.assertEqual(0, len(containers))
Ejemplo n.º 34
0
 def test_rc_get_pod_node_name_none(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     name = obj.get_pod_node_name()
     self.assertIsNone(name)
Ejemplo n.º 35
0
 def test_rc_get_pod_restart_policy_none(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     rp = obj.get_pod_restart_policy()
     self.assertEqual("Always", rp)  # set to 'Always' by default
Ejemplo n.º 36
0
 def test_rc_get_service_account_none(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     acct = obj.get_service_account()
     self.assertIsNone(acct)
Ejemplo n.º 37
0
 def test_rc_get_pod_node_selector_none(self):
     name = "yorc"
     obj = utils.create_rc(name=name)
     s = obj.get_pod_node_selector()
     self.assertIsNone(s)
Ejemplo n.º 38
0
 def test_init_with_invalid_name(self):
     name = object()
     with self.assertRaises(SyntaxError):
         utils.create_rc(name=name)