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_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_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_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_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_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_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_add_volume_mount_invalid_args(self): name = "yomama" image = "redis" vname = object() vmount = object() with self.assertRaises(SyntaxError): mount = K8sVolumeMount(name=vname, mount_path=vmount) c = K8sContainer(name=name, image=image) c.add_volume_mount(mount)
def test_serialize(self): name = "redis" image = "redis:3.0.7" c = K8sContainer(name=name, image=image) volname = "vol1" volpath = "/path/on/container" mount = K8sVolumeMount(name=volname, mount_path=volpath) c.add_volume_mount(mount) data = c.serialize() self.assertIsInstance(data, dict)
def test_add_volume_mount(self): name = "redis" image = "redis:3.0.7" c = K8sContainer(name=name, image=image) volname = "vol1" volpath = "/path/on/container" mount = K8sVolumeMount(name=volname, mount_path=volpath) c.add_volume_mount(mount) self.assertEqual(1, len(c.volume_mounts)) self.assertIn(mount.model, c.volume_mounts)
def test_as_json(self): name = "redis" image = "redis:3.0.7" c = K8sContainer(name=name, image=image) volname = "vol1" volpath = "/path/on/container" mount = K8sVolumeMount(name=volname, mount_path=volpath) c.add_volume_mount(mount) j = c.as_json() self.assertIsInstance(j, str) d = json.loads(j) self.assertIsInstance(d, dict)
def test_as_yaml(self): name = "redis" image = "redis:3.0.7" c = K8sContainer(name=name, image=image) volname = "vol1" volpath = "/path/on/container" mount = K8sVolumeMount(name=volname, mount_path=volpath) c.add_volume_mount(mount) y = c.as_yaml() self.assertIsInstance(y, str) d = yaml.load(y) self.assertIsInstance(d, dict)
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_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_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): secret.create() rc.create() volnames = [x.name for x in rc.volumes] self.assertIn(vol_name, volnames)
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" volume = _utils.create_volume(name=vol_name, type=vol_type) mount_name = vol_name mount_path = '/test-emptydir' 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_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): 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_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): 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 create_volume_mount(name=None, mount_path=None): obj = K8sVolumeMount( name=name, mount_path=mount_path, ) return obj