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): rc.create() volnames = [x.name for x in rc.volumes] self.assertIn(vol_name, volnames)
def test_revision(self): c_redis = _utils.create_container(name="redis", image="redis") c_nginx = _utils.create_container(name="nginx", image="nginx") c_postgres = _utils.create_container(name="postgres", image="postgres:alpine") name = "yodep-{0}".format(str(uuid.uuid4())) dep = _utils.create_deployment(name=name) dep.add_container(c_redis) dep.desired_replicas = 2 if _utils.is_reachable(dep.config): dep.create() self.assertEqual(1, dep.revision) dep.add_container(c_nginx) dep.update() self.assertEqual(2, dep.revision) dep.add_container(c_postgres) dep.update() self.assertEqual(3, dep.revision) dep.rollback(revision=1) self.assertEqual(4, dep.revision) dep.add_container(c_nginx) dep.update() self.assertEqual(5, dep.revision)
def test_replicaset_creation_timestamp(self): c_redis = _utils.create_container(name="redis", image="redis") c_nginx_1 = _utils.create_container(name="nginx", image="nginx") c_nginx_2 = _utils.create_container(name="postgres", image="postgres:alpine") name = "yodep-{0}".format(str(uuid.uuid4())) dep = _utils.create_deployment(name=name) dep.add_container(c_redis) dep.desired_replicas = 2 if _utils.is_reachable(dep.config): dep.create() self.assertEqual(1, dep.revision) dep.add_container(c_nginx_1) dep.update() dep.get() self.assertEqual(2, dep.revision) dep.add_container(c_nginx_2) dep.update() dep.get() self.assertEqual(3, dep.revision) rsets = K8sReplicaSet(config=dep.config, name="yo").list() for i in range(0, len(rsets) - 1): self.assertGreaterEqual(rsets[i].creation_timestamp, rsets[i + 1].creation_timestamp)
def test_update_add_container_fails(self): cont_names = ["yocontainer", "yocontainer2"] container = _utils.create_container(name=cont_names[0]) pod_name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=pod_name) pod.add_container(container) if _utils.is_reachable(pod.config): pod.create() container = _utils.create_container(name=cont_names[1]) pod.add_container(container) with self.assertRaises(UnprocessableEntityException): pod.update()
def test_api_create_nfs(self): pvname = "yopv" pvcname = "yopvc" volname = "yovolume" podname = "yopod" contname = "yocontainer" pvtype = "nfs" pv = _utils.create_pv(name=pvname, type=pvtype) pv.nfs_server = "nfs.company.com" pv.nfs_path = "/fs1/test-nfs" pvc = _utils.create_pvc(name=pvcname) vol = _utils.create_volume(name=volname, type='persistentVolumeClaim') vol.claim_name = pvcname container = _utils.create_container(name=contname, image="nginx:latest") volmount = _utils.create_volume_mount(name=volname, mount_path='/test-persistent') container.add_volume_mount(volmount) pod = _utils.create_pod(name=podname) pod.add_volume(vol) pod.add_container(container) if _utils.is_reachable(pvc.config): try: pv.create() pvc.create() pod.create() self.assertIsInstance(pv, K8sPersistentVolume) self.assertIsInstance(pvc, K8sPersistentVolumeClaim) except Exception as err: self.assertIsInstance(err, TimedOutException)
def test_hpa_update(self): c_nginx = _utils.create_container(name="yo", image="nginx:latest") deploy = _utils.create_deployment(name="yo") deploy.add_container(c_nginx) deploy.desired_replicas = 3 hpa = _utils.create_hpa(name="yo") hpa.min_replicas = 1 hpa.max_replicas = 10 hpa.cpu_percent = 50 hpa.scale_ref = ("Deployment", "yo") if _utils.is_reachable(hpa.config): deploy.create() hpa.create() hpa.get() self.assertEqual(50, hpa.cpu_percent) hpa.cpu_percent = 70 hpa.min_replicas = 3 hpa.max_replicas = 5 hpa.update() hpa.get() self.assertEqual(70, hpa.cpu_percent) self.assertEqual(3, hpa.min_replicas) self.assertEqual(5, hpa.max_replicas)
def test_pod_hostpath(self): container_name = "nginx" container_image = "nginx:1.7.9" container = _utils.create_container(name=container_name, image=container_image) vol_name = "hostpath" vol_type = "hostPath" host_path = "/var/lib/docker" volume = _utils.create_volume(name=vol_name, type=vol_type) volume.path = host_path mount_name = vol_name mount_path = '/test-hostpath' mount = K8sVolumeMount(name=mount_name, mount_path=mount_path) container.add_volume_mount(mount) pod_name = "nginx" pod = _utils.create_pod(name=pod_name) pod.add_volume(volume) pod.add_container(container) if _utils.is_reachable(pod.config): pod.create() volnames = [x.name for x in pod.volumes] self.assertIn(vol_name, volnames)
def test_api_create_gce_pd(self): pvname = "yopv" pvcname = "yopvc" volname = "yovolume" podname = "yopod" contname = "yocontainer" pvtype = "gcePersistentDisk" pv = _utils.create_pv(name=pvname, type=pvtype) pv.pd_name = "mnubo-disk1" pv.fs_type = "xfs" pvc = _utils.create_pvc(name=pvcname) vol = _utils.create_volume(name=volname, type="persistentVolumeClaim") vol.claim_name = pvcname container = _utils.create_container(name=contname, image="nginx:latest") volmount = _utils.create_volume_mount(name=volname, mount_path="/test-persistent") container.add_volume_mount(volmount) pod = _utils.create_pod(name=podname) pod.add_volume(vol) pod.add_container(container) if _utils.is_reachable(pvc.config): try: pv.create() pvc.create() pod.create() self.assertIsInstance(pv, K8sPersistentVolume) self.assertIsInstance(pvc, K8sPersistentVolumeClaim) except Exception as err: self.assertIsInstance(err, TimedOutException)
def test_update_container_image_keep_env_vars(self): name = "yodep-{0}".format(str(uuid.uuid4())) dep = _utils.create_deployment(name=name) cont_name = "nginx" cont_image = "nginx:1.7.9" new_image = "nginx:1.10.3" env_var_name = "YoVariable" cont = _utils.create_container(name=cont_name, image=cont_image) cont.add_env(name=env_var_name, value=name) dep.add_container(container=cont) dep.desired_replicas = 1 if _utils.is_reachable(dep.config): dep.create() with self.assertRaises(AlreadyExistsException): dep.create() # Change the container image dep.container_image = (cont_name, new_image) # Update the deployment dep.update() # Refresh dep.get() for c in dep.containers: self.assertIsInstance(c, K8sContainer) if c.name == cont_name: self.assertEqual(c.image, new_image) self.assertEqual(c.env[0].name, env_var_name) self.assertEqual(c.env[0].value, name)
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.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.add_volume_mount(mount) pod_name = "nginx" pod = _utils.create_pod(name=pod_name) pod.add_volume(volume) pod.add_container(container) if _utils.is_reachable(pod.config): secret.create() pod.create() volnames = [x.name for x in pod.volumes] self.assertIn(vol_name, volnames)
def test_pod_gce_pd(self): container_name = "nginx" container_image = "nginx:1.7.9" container = _utils.create_container(name=container_name, image=container_image) pd_name = "kubernetes_py-py-test-pd" vol_name = "persistent" vol_type = "gcePersistentDisk" volume = _utils.create_volume(name=vol_name, type=vol_type) volume.pd_name = pd_name mount_name = vol_name mount_path = '/test-gce' mount = K8sVolumeMount(name=mount_name, mount_path=mount_path) container.add_volume_mount(mount) pod_name = "nginx-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=pod_name) pod.add_volume(volume) pod.add_container(container) if _utils.is_reachable(pod.config): try: pod.create() volnames = [x.name for x in pod.volumes] self.assertIn(vol_name, volnames) except Exception as err: self.assertIsInstance(err, TimedOutException)
def test_pod_nfs(self): container_name = "nginx" container_image = "nginx:1.7.9" container = _utils.create_container(name=container_name, image=container_image) vol_name = "nfs" vol_type = "nfs" server = "howard.mtl.mnubo.com" nfs_path = "/fs1/test-nfs" volume = _utils.create_volume(name=vol_name, type=vol_type) volume.nfs_server = server volume.nfs_path = nfs_path mount_name = vol_name mount_path = '/test-nfs' mount = K8sVolumeMount(name=mount_name, mount_path=mount_path) container.add_volume_mount(mount) pod_name = "nginx-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=pod_name) pod.add_volume(volume) pod.add_container(container) if _utils.is_reachable(pod.config): try: pod.create() volnames = [x.name for x in pod.volumes] self.assertIn(vol_name, volnames) except Exception as err: self.assertIsInstance(err, TimedOutException)
def test_pod_git_repo(self): container_name = "nginx" container_image = "nginx:1.7.9" container = _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.add_volume_mount(mount) pod_name = "nginx-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=pod_name) pod.add_volume(volume) pod.add_container(container) if _utils.is_reachable(pod.config): try: pod.create() volnames = [x.name for x in pod.volumes] self.assertIn(vol_name, volnames) except Exception as err: self.assertIsInstance(err, TimedOutException)
def test_api_create_aws_ebs(self): pvname = "yopv" pvcname = "yopvc" volname = "yovolume" podname = "yopod" contname = "yocontainer" pvtype = "awsElasticBlockStore" pv = _utils.create_pv(name=pvname, type=pvtype) pv.volume_id = "vol-0e3056a2" pv.fs_type = "xfs" pvc = _utils.create_pvc(name=pvcname) vol = _utils.create_volume(name=volname, type='persistentVolumeClaim') vol.claim_name = pvcname container = _utils.create_container(name=contname, image="nginx:latest") volmount = _utils.create_volume_mount(name=volname, mount_path='/test-persistent') container.add_volume_mount(volmount) pod = _utils.create_pod(name=podname) pod.add_volume(vol) pod.add_container(container) if _utils.is_reachable(pvc.config): try: pv.create() pvc.create() pod.create() self.assertIsInstance(pv, K8sPersistentVolume) self.assertIsInstance(pvc, K8sPersistentVolumeClaim) except Exception as err: self.assertIsInstance(err, TimedOutException)
def test_pod_aws_ebs(self): container_name = "nginx" container_image = "nginx:1.7.9" container = _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.add_volume_mount(mount) pod_name = "nginx-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=pod_name) pod.add_volume(volume) pod.add_container(container) if _utils.is_reachable(pod.config): try: pod.create() volnames = [x.name for x in pod.volumes] self.assertIn(vol_name, volnames) except Exception as err: self.assertIsInstance(err, TimedOutException)
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): 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)
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): 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)
def test_containers(self): c_name = "redis" c_image = "redis:latest" c_image_2 = "redis:3.2.3" container = _utils.create_container(name=c_name, image=c_image) name = "job-{}".format(uuid.uuid4()) cj = _utils.create_cronjob(name=name) cj.add_container(container) self.assertEqual(1, len(cj.containers)) self.assertIn(c_name, cj.container_image) self.assertEqual(c_image, cj.container_image[c_name]) container = _utils.create_container(name=c_name, image=c_image_2) cj.add_container(container) self.assertEqual(1, len(cj.containers)) self.assertEqual(c_image_2, cj.container_image[c_name])
def test_add_env(self): cont = Container(_constants.myweb_container()) k8s_container = _utils.create_container(name=cont.name) k8s_container.model = cont envs = _constants.myweb_envs() for k, v in envs.items(): k8s_container.add_env(k, v) self.assertEqual(4, len(k8s_container.env))
def test_server_version_no_kubeconfig(self): api_host = "127.0.0.1:8001" cfg = K8sConfig(kubeconfig=None, api_host=api_host) if _utils.is_reachable(cfg.api_host): container = _utils.create_container(name="nginx", image="nginx:latest") cj = _utils.create_cronjob(config=cfg, name="test") cj.add_container(container) cj.create() self.assertIsInstance(cj, K8sCronJob)
def test_create_without_selector(self): k8s_container = _utils.create_container(name="nginx", image="nginx:latest") k8s_ds = _utils.create_daemonset(name="yo") k8s_ds.add_container(k8s_container) if _utils.is_reachable(k8s_ds.config): k8s_ds.create() self.assertIsInstance(k8s_ds, K8sDaemonSet) self.assertEqual(k8s_ds.name, "yo")
def test_is_ready(self): name = "yocontainer" container = _utils.create_container(name=name) name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=name) pod.add_container(container) if _utils.is_reachable(pod.config): p = pod.create() self.assertTrue(p.is_ready())
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): try: rc.create() volnames = [x.name for x in rc.volumes] self.assertIn(vol_name, volnames) except Exception as err: self.assertIsInstance(err, TimedOutException)
def test_node_selectors(self): node = _utils.create_node(name="yo") c_nginx = _utils.create_container("yo-nginx", "nginx:latest") c_pg = _utils.create_container("yo-pg", "postgres:alpine") c_redis = _utils.create_container("yo-redis", "redis:latest") d_nginx = _utils.create_deployment(name="yo-nginx") d_nginx.desired_replicas = 1 d_nginx.add_container(c_nginx) d_pg = _utils.create_deployment(name="yo-pg") d_pg.desired_replicas = 1 d_pg.add_container(c_pg) d_redis = _utils.create_deployment(name="yo-redis") d_redis.desired_replicas = 1 d_redis.add_container(c_redis) if _utils.is_reachable(node.config): nodes = node.list() for i in range(len(nodes)): node = nodes[i] labels = node.labels labels.update({'mnubo.com/selector': str(i)}) node.labels = labels node.update() d_nginx.node_selector = {"mnubo.com/selector": "0"} d_pg.node_selector = {"mnubo.com/selector": "1"} d_redis.node_selector = {"mnubo.com/selector": "2"} d_nginx.create() d_pg.create() d_redis.create() self.assertEqual(d_nginx.node_selector, {"mnubo.com/selector": "0"}) self.assertEqual(d_pg.node_selector, {"mnubo.com/selector": "1"}) self.assertEqual(d_redis.node_selector, {"mnubo.com/selector": "2"}) pass # set breakpoint; play around with killing pods
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): try: rc.create() volnames = [x.name for x in rc.volumes] self.assertIn(vol_name, volnames) except Exception as err: self.assertIsInstance(err, TimedOutException)
def test_list(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=name) container = _utils.create_container(name=name) pod.add_container(container) if _utils.is_reachable(pod.config): pod.create() _list = pod.list() for x in _list: self.assertIsInstance(x, K8sPod)
def test_get_by_name(self): name = "yocontainer" container = _utils.create_container(name=name) name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=name) pod.add_container(container) if _utils.is_reachable(pod.config): pod.create() pods = K8sPod.get_by_name(config=pod.config, name=name) self.assertIsInstance(pods, list) self.assertEqual(1, len(pods))
def test_get_pod_logs(self): cname = "yocontainer" container = _utils.create_container(name=cname) name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=name) pod.add_container(container) if _utils.is_reachable(pod.config): with self.assertRaises(NotFoundException): pod.get_log() pod.get_log(container=cname)
def test_get_pod_status(self): name = "yocontainer" container = _utils.create_container(name=name) name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=name) pod.add_container(container) if _utils.is_reachable(pod.config): p = pod.create() result = p.status self.assertIsInstance(result, PodStatus) for i in ['conditions', 'container_statuses', 'host_ip', 'phase', 'pod_ip', 'reason', 'start_time']: self.assertTrue(hasattr(result, i))
def test_tolerations_default(self): config = _utils.create_config() container = _utils.create_container(name="nginx", image="nginx:latest") pod = _utils.create_pod(config=config, name="yo") pod.add_container(container) if _utils.is_reachable(config): pod.create() pod.get() # default 'NoExecute' tolerations # 'node.alpha.kubernetes.io/notReady' && 'node.alpha.kubernetes.io/unreachable' self.assertEqual(2, len(pod.tolerations))
def test_create_already_exists(self): name = "yocontainer" c = _utils.create_container(name=name) name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=name) pod.add_container(c) if _utils.is_reachable(pod.config): with self.assertRaises(AlreadyExistsException): result = pod.create() self.assertIsInstance(result, K8sPod) self.assertEqual(pod, result) pod.create()
def test_list(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): dep.create() objs = dep.list() for x in objs: self.assertIsInstance(x, K8sDeployment)
def test_get(self): name = "yocontainer" container = _utils.create_container(name=name) name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=name) pod.add_container(container) if _utils.is_reachable(pod.config): from_create = pod.create() from_get = pod.get() self.assertIsInstance(from_create, K8sPod) self.assertIsInstance(from_get, K8sPod) self.assertEqual(from_create, from_get)
def test_purge_replica_set(self): c_redis = _utils.create_container(name="redis", image="redis") c_nginx = _utils.create_container(name="nginx", image="nginx") c_postgres = _utils.create_container(name="postgres", image="postgres:alpine") # c_memcached = utils.create_container(name="memcached", image="memcached:alpine") name = "yodep-{0}".format(str(uuid.uuid4())) dep = _utils.create_deployment(name=name) dep.add_container(c_redis) dep.desired_replicas = 2 if _utils.is_reachable(dep.config): dep.create() dep.add_container(c_nginx) dep.update() dep.add_container(c_postgres) dep.update() rsets = K8sReplicaSet(config=dep.config, name="yo").list() self.assertEqual(3, len(rsets)) dep.purge_replica_sets(keep=2) rsets = K8sReplicaSet(config=dep.config, name="yo").list() self.assertEqual(2, len(rsets))
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): dep.create() with self.assertRaises(AlreadyExistsException): dep.create()
def test_delete(self): name = "yocontainer" container = _utils.create_container(name=name) name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=name) pod.add_container(container) if _utils.is_reachable(pod.config): pod.create() _utils.cleanup_pods() result = pod.list() self.assertIsInstance(result, list) self.assertEqual(0, len(result))
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): dep.create() dep.name = name2 with self.assertRaises(NotFoundException): 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): dep.create() dep.namespace = nspace 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): 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_replicaset_creation_timestamp(self): c_redis = _utils.create_container(name="redis", image="redis") c_nginx_1 = _utils.create_container(name="nginx", image="nginx") c_nginx_2 = _utils.create_container(name="postgres", image="postgres:alpine") name = "yodep-{0}".format(str(uuid.uuid4())) dep = _utils.create_deployment(name=name) dep.add_container(c_redis) dep.desired_replicas = 2 if _utils.is_reachable(dep.config): dep.create() self.assertEqual(1, dep.revision) dep.add_container(c_nginx_1) dep.update() self.assertEqual(2, dep.revision) dep.add_container(c_nginx_2) dep.update() self.assertEqual(3, dep.revision) rsets = K8sReplicaSet(config=dep.config, name="yo").list() for i in range(0, len(rsets) - 1): self.assertGreater(rsets[i].creation_timestamp, rsets[i + 1].creation_timestamp)
def test_delete_cascade(self): c_redis = _utils.create_container(name="redis", image="redis") c_nginx = _utils.create_container(name="nginx", image="nginx") name = "yodep-{0}".format(str(uuid.uuid4())) dep = _utils.create_deployment(name=name) dep.add_container(c_redis) dep.desired_replicas = 3 if _utils.is_reachable(dep.config): dep.create() dep.add_container(c_nginx) dep.update() repsets = K8sReplicaSet(config=dep.config, name="yo").list() self.assertEqual(2, len(repsets)) pods = K8sPod(config=dep.config, name="yo").list() self.assertLessEqual(3, len(pods)) # rollout burst dep.delete(cascade=True) result = dep.list() self.assertIsInstance(result, list) self.assertEqual(0, len(result)) repsets = K8sReplicaSet(config=dep.config, name="yo").list() self.assertEqual(0, len(repsets)) pods = K8sPod(config=dep.config, name="yo").list() self.assertEqual(0, len(pods))
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): dep.create() labels = dep.pod_labels labels['newkey'] = 'newvalue' dep.pod_labels = labels updated = dep.update() self.assertEqual(labels, updated.pod_labels)
def test_get_by_labels_without_name(self): name = "yocontainer" container = _utils.create_container(name=name) name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(name=name) pod.add_container(container) pod.add_label("test.label", "hello") if _utils.is_reachable(pod.config): pod.create() pods = K8sPod.get_by_labels(config=pod.config, labels={'test.label': "hello"}) self.assertIsInstance(pods, list) self.assertEqual(1, len(pods)) self.assertEqual(pod, pods[0]) pods = K8sPod.get_by_labels(config=pod.config, labels={'test.label': "world"}) self.assertEqual(0, len(pods))
def test_list_multiple(self): name = "yocontainer" container = _utils.create_container(name=name) config = K8sConfig(kubeconfig=_utils.kubeconfig_fallback) pods = [] count = 3 if _utils.is_reachable(config): for i in range(0, count): name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(config, name) pod.add_container(container) result = pod.create() self.assertIsInstance(result, K8sPod) self.assertEqual(pod, result) pods.append(pod) self.assertEqual(count, len(pods))
def test_update_name_fails(self): name = "yocontainer" container = _utils.create_container(name=name) name1 = "yopod1" name2 = "yopod2" pod = _utils.create_pod(name=name1) pod.add_container(container) if _utils.is_reachable(pod.config): pod.create() result = K8sPod.get_by_name(config=pod.config, name=name1) self.assertIsInstance(result, list) self.assertEqual(1, len(result)) self.assertIsInstance(result[0], K8sPod) result[0].name = name2 with self.assertRaises(NotFoundException): result[0].update()
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): 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)