def _create_fake_core_v1_with_pods(self): fake_core_v1 = MagicMock() fake_pods = MagicMock() fake_pods.items = [ utils.create_pod(self._POD_1_NAME), utils.create_pod(self._POD_2_NAME), ] fake_core_v1.list_namespaced_pod.return_value = fake_pods return fake_core_v1
def test_set_annotations(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) anns_in = {'key': 'value'} pod.set_annotations(anns_in) anns_out = pod.get_annotations() self.assertEqual(anns_in, anns_out)
def test_get_label_doesnt_exist(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) k = "yonotexists" l = pod.get_label(k) self.assertIsNone(l)
def test_get_annotation_doesnt_exist(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) k = "yonotexists" ann = pod.get_annotation(k) self.assertIsNone(ann)
def test_del_label_none_arg(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) try: pod.del_label() except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_set_labels(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) labels_in = {'key': 'value'} pod.set_labels(labels_in) labels_out = pod.get_labels() self.assertEqual(labels_in, labels_out)
def test_del_label_none_yet(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) k = "yokey" self.assertNotIn(k, pod.model.metadata.labels) pod.del_label(k) self.assertNotIn(k, pod.model.metadata.labels)
def test_pod_set_termination_grace_period(self): name = "yoname" obj = utils.create_pod(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)
def test_list_nonexistent(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) if utils.is_reachable(pod.config.api_host): p = pod.list() self.assertIsInstance(p, list) self.assertEqual(0, len(p))
def test_struct_k8spod(self): name = "yomama" pod = utils.create_pod(name=name) self.assertIsNotNone(pod) self.assertIsInstance(pod, K8sPod) self.assertIsNotNone(pod.model) self.assertIsInstance(pod.model, Pod)
def test_init_with_config_and_pull_secrets(self): ps = "yomama" name = "sofat" cfg = K8sConfig(kubeconfig=utils.kubeconfig_fallback, pull_secret=ps) pod = utils.create_pod(config=cfg, name=name) self.assertIsNotNone(pod.config) self.assertEqual(ps, pod.config.pull_secret)
def test_get_node_selector(self): name = "yoname" s_in = {"disktype": "ssd"} pod = utils.create_pod(name=name) pod.node_selector = s_in s_out = pod.node_selector self.assertEqual(s_in, s_out)
def test_add_annotation_none_args(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) try: pod.add_annotation() except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_pod_add_image_pull_secrets(self): name = "yoname" pod = utils.create_pod(name=name) secret = [{'name': 'yosecret'}] pod.add_image_pull_secrets(secret) self.assertEqual(1, len(pod.model.spec.image_pull_secrets)) self.assertEqual(secret, pod.model.spec.image_pull_secrets)
def test_add_label_invalid_args(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) k = object() v = object() with self.assertRaises(SyntaxError): pod.add_label(k, v)
def test_struct_pod(self): name = "yomama" pod = utils.create_pod(name=name) self.assertIsInstance(pod.model, Pod) self.assertIsInstance(pod.model.metadata, ObjectMeta) self.assertIsInstance(pod.model.spec, PodSpec) self.assertIsInstance(pod.model.status, PodStatus)
def test_pod_set_service_account(self): name = "yoname" obj = utils.create_pod(name=name) name_in = "yoservice" obj.set_service_account(name_in) name_out = obj.get_service_account() self.assertEqual(name_in, name_out)
def test_pod_set_pod_restart_policy(self): name = "yoname" obj = utils.create_pod(name=name) policy = "Always" obj.set_pod_restart_policy(policy=policy) p = obj.get_pod_restart_policy() self.assertEqual(policy, p)
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.api_host): 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_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.api_host): 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_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.api_host): 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.api_host): 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_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.api_host): 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_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-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.api_host): 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_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.api_host): secret.create() pod.create() volnames = [x.name for x in pod.volumes] self.assertIn(vol_name, volnames)
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.api_host): pod.create() volnames = [x.name for x in pod.volumes] self.assertIn(vol_name, volnames)
def test_del_annotation_none_yet(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) k = "yokey" self.assertEqual({}, pod.annotations) pod.del_annotation(k) self.assertEqual({}, pod.annotations)
def test_pod_get_pod_restart_policy(self): name = "yoname" obj = utils.create_pod(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)
def test_is_ready_nonexistent(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) if utils.is_reachable(pod.config.api_host): with self.assertRaises(NotFoundException): pod.is_ready() self.fail("Should not fail.")
def test_pod_get_pod_node_name(self): name = "yoname" obj = utils.create_pod(name=name) nodename = "yonodename" obj.set_pod_node_name(name=nodename) name = obj.get_pod_node_name() self.assertEqual(name, nodename)
def test_init_with_config_and_pull_secrets(self): ps = [{'name': 'yomama'}] name = "sofat" cfg = K8sConfig(kubeconfig=utils.kubeconfig_fallback, pull_secret=ps) pod = utils.create_pod(config=cfg, name=name) self.assertIsNotNone(pod.config) self.assertEqual(ps, pod.config.pull_secret)
def test_pod_get_service_account(self): name = "yoname" pod = utils.create_pod(name=name) name_in = "yoservice" pod.service_account_name = name_in name_out = pod.service_account_name self.assertEqual(name_in, name_out)
def test_struct_pod_model(self): name = "yomama" pod = utils.create_pod(name=name) model = pod.model.model self.assertIsNotNone(model) self.assertIsInstance(model, dict) self.assertIn('apiVersion', model) self.assertIsInstance(model['apiVersion'], str) self.assertIn('kind', model) self.assertIsInstance(model['kind'], str) self.assertIn('metadata', model) self.assertIsInstance(model['metadata'], dict) self.assertIn('labels', model['metadata']) self.assertIsInstance(model['metadata']['labels'], dict) self.assertIn('name', model['metadata']['labels']) self.assertEqual(model['metadata']['labels']['name'], name) self.assertIn('name', model['metadata']) self.assertIsInstance(model['metadata']['name'], str) self.assertEqual(model['metadata']['name'], name) self.assertIn('namespace', model['metadata']) self.assertIsInstance(model['metadata']['namespace'], str) self.assertIn('spec', model) self.assertIsInstance(model['spec'], dict) self.assertIn('containers', model['spec']) self.assertIsInstance(model['spec']['containers'], list) self.assertIn('dnsPolicy', model['spec']) self.assertIsInstance(model['spec']['dnsPolicy'], str) self.assertIn('restartPolicy', model['spec']) self.assertIsInstance(model['spec']['restartPolicy'], str) self.assertIn('volumes', model['spec']) self.assertIsInstance(model['spec']['volumes'], list)
def test_set_namespace(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) nspace_in = "yonamespace" pod.set_namespace(nspace_in) nspace_out = pod.get_namespace() self.assertEqual(nspace_in, nspace_out)
def test_get_node_name(self): name = "yoname" nodename = "yonodename" pod = utils.create_pod(name=name) pod.node_name = nodename self.assertEqual(nodename, pod.node_name) self.assertEqual(nodename, pod.model.spec.node_name)
def test_pod_get_pod_node_selector(self): name = "yoname" obj = utils.create_pod(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)
def test_get_label(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) k = "yokey" v_in = "yovalue" pod.add_label(k, v_in) v_out = pod.get_label(k) self.assertEqual(v_in, v_out)
def test_add_label(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) k = "yokey" v_in = "yovalue" pod.add_label(k, v_in) v_out = pod.get_label(k) self.assertEqual(v_in, v_out)
def test_set_namespace_none_args(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) try: pod.set_namespace() self.fail("Should not fail.") except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_del_annotation_invalid_arg(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) k = object() try: pod.del_annotation(k) except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_pod_set_pod_node_name_invalid_arg(self): name = "yoname" obj = utils.create_pod(name=name) nodename = 666 try: obj.set_pod_node_name(name=nodename) except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_struct_pod(self): name = "yomama" pod = utils.create_pod(name=name) model = pod.model self.assertIsInstance(model.model, dict) self.assertIsInstance(model.pod_metadata, ObjectMeta) self.assertIsInstance(model.pod_spec, PodSpec) self.assertIsNone(model.pod_status)
def test_pod_set_termination_grace_period_none_arg(self): name = "yoname" obj = utils.create_pod(name=name) try: obj.set_termination_grace_period() self.fail("Should not fail.") except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_pod_set_service_account_none_arg(self): name = "yoname" obj = utils.create_pod(name=name) try: obj.set_service_account() self.fail("Should not fail.") except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_pod_set_pod_node_selector_invalid_arg(self): name = "yoname" obj = utils.create_pod(name=name) s_in = "yoselector" try: obj.set_pod_node_selector(selector=s_in) except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_init_with_model_pod(self): name = "yoname" obj = utils.create_pod(name=name) self.assertIsInstance(obj.model, Pod) 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)
def test_pod_set_active_deadline_invalid_arg(self): name = "yoname" obj = utils.create_pod(name=name) d = "yodeadline" try: obj.set_active_deadline(d) except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_init_object_type_pod(self): ot = "Pod" name = "yopod" obj = utils.create_pod(name=name) self.assertIsNotNone(obj) self.assertIsInstance(obj, K8sPodBasedObject) self.assertEqual(ot, obj.obj_type) self.assertEqual(name, obj.name)
def test_add_annotation(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) k = "yokey" v = "yovalue" pod.add_annotation(k, v) self.assertEqual(1, len(pod.model.metadata.annotations)) self.assertIn(k, pod.model.metadata.annotations) self.assertEqual(v, pod.model.metadata.annotations[k])
def test_pod_set_pod_restart_policy_invalid_arg(self): name = "yoname" obj = utils.create_pod(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)
def test_pod_add_image_pull_secrets_invalid_arg(self): name = "yoname" obj = utils.create_pod(name=name) secretname = 666 try: obj.add_image_pull_secrets(secretname) self.fail("Should not fail.") except Exception as err: self.assertIsInstance(err, SyntaxError)
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.api_host): p = pod.create() self.assertTrue(p.is_ready())
def test_pod_add_volume_hostpath_no_path_specified(self): name = "yoname" obj = utils.create_pod(name=name) config = utils.create_config() vol = K8sVolume(config=config, name=name, mount_path="/var/test", type="hostPath") if utils.is_reachable(config.api_host): with self.assertRaises(UnprocessableEntityException): obj.add_volume(vol) obj.create()
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.api_host): pod.create() _list = pod.list() for x in _list: self.assertIsInstance(x, K8sPod)
def test_pod_add_container(self): name = "yoname" pod = utils.create_pod(name=name) self.assertEqual(0, len(pod.model.spec.containers)) name = "yopod" image = "redis" c = K8sContainer(name=name, image=image) pod.add_container(c) self.assertEqual(1, len(pod.model.spec.containers)) self.assertEqual(c.model, pod.model.spec.containers[0])
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.api_host): pod.create() pods = K8sPod.get_by_name(config=pod.config, name=name) self.assertIsInstance(pods, list) self.assertEqual(1, len(pods))
def test_del_label(self): name = "yopod-{0}".format(str(uuid.uuid4())) pod = utils.create_pod(name=name) k = "yokey" v = "yovalue" pod.add_label(k, v) self.assertTrue(hasattr(pod.model.metadata, 'labels')) self.assertIn(k, pod.model.metadata.labels) pod.del_label(k) self.assertTrue(hasattr(pod.model.metadata, 'labels')) self.assertNotIn(k, pod.model.metadata.labels)