def test_base(self): depl = chkit.Deployment( name="functional-test-depl", replicas=1, containers=[ chkit.Container(image="nginx", name="first", limits=chkit.Resources(cpu=10, memory=10)) ], ) try: chkit.create_deployment(depl) got_depl = chkit.get_deployment(depl.name) self.assertEqual(depl.name, got_depl.name) attempts = 1 while attempts <= 40: pods = chkit.get_pods() deployment_pods = [ pod for pod in pods if pod.deploy == depl.name ] not_running_pods = [ pod for pod in deployment_pods if pod.status.phase != "Running" ] if len(not_running_pods) == 0 and len(deployment_pods) > 0: break time.sleep(15) attempts += 1 self.assertLessEqual(attempts, 40) finally: chkit.delete_deployment(name=depl.name) time.sleep(5) self.assertNotIn( depl.name, [deploy.name for deploy in chkit.get_deployments()])
class TestConfigMap(unittest.TestCase): __default_cm = chkit.ConfigMap(name="test-configmap", data=dict(TESTKEY="TESTVALUE")) @timeout_decorator.timeout(seconds=30) @chkit.test_account def test_base(self): configmap = chkit.ConfigMap(name="test-configmap", data=dict(TESTKEY="TESTVALUE")) try: chkit.create_configmap(configmap) self.assertIn(configmap.name, [cm.name for cm in chkit.get_configmaps()]) got_cm = chkit.get_configmap(configmap.name) self.assertEqual(configmap.name, got_cm.name) self.assertEqual(configmap.data["TESTKEY"], got_cm.data["TESTKEY"]) finally: chkit.delete_configmap(configmap.name) time.sleep(1) self.assertNotIn(configmap.name, [cm.name for cm in chkit.get_configmaps()]) @timeout_decorator.timeout(seconds=30) @chkit.test_account @chkit.with_cm(configmap=__default_cm) def test_update(self, cm: chkit.ConfigMap): new_configmap = chkit.ConfigMap(name=cm.name, data=dict(TESTKEY1="TESTVALUE1")) chkit.replace_configmap(new_configmap) got_cm = chkit.get_configmap(new_configmap.name) self.assertEqual(got_cm.name, new_configmap.name) self.assertEqual(got_cm.data["TESTKEY1"], new_configmap.data["TESTKEY1"]) __default_cm_deployment = chkit.Deployment( name="cm-test-deploy", replicas=1, containers=[ chkit.Container( name="first", limits=chkit.Resources(cpu=15, memory=15), image="nginx", config_maps=[chkit.DeploymentConfigMap(name="test-configmap")], ) ], ) @timeout_decorator.timeout(seconds=650 * 2) @chkit.test_account @chkit.with_cm(configmap=__default_cm) @chkit.with_deployment(deployment=__default_cm_deployment) @chkit.ensure_pods_running(deployment=__default_cm_deployment.name) def test_deploy_mount(self, cm: chkit.ConfigMap, depl: chkit.Deployment): chkit.get_configmap(cm.name) depl_created = chkit.get_deployment(depl.name) self.assertEqual(depl_created.containers[0].config_maps[0].name, cm.name)
def test_replace_container(self, depl: chkit.Deployment): new_container = chkit.Container( name=depl.containers[0].name, limits=chkit.Resources(cpu=16, memory=16), image="redis:3-alpine", env=[chkit.EnvVariable("HELLOHELLO", "worldworld")], ) chkit.replace_container(deployment=depl.name, container=new_container) got_depl = chkit.get_deployment(depl.name) needed_containers = [ container for container in got_depl.containers if container.name == new_container.name ] self.assertGreater(len(needed_containers), 0) self.assertEqual(needed_containers[0].env, new_container.env) self.assertEqual(needed_containers[0].limits.cpu, new_container.limits.cpu) self.assertEqual(needed_containers[0].limits.memory, new_container.limits.memory) self.assertEqual(needed_containers[0].image, new_container.image)
def test_add_container(self, depl: chkit.Deployment): new_container = chkit.Container( name="additional-container", limits=chkit.Resources(cpu=15, memory=15), image="redis", env=[chkit.EnvVariable("HELLO", "world")], ) chkit.add_container(deployment=depl.name, container=new_container) got_depl = chkit.get_deployment(depl.name) self.assertEqual(len(got_depl.containers), len(depl.containers) + 1) needed_containers = [ container for container in got_depl.containers if container.name == new_container.name ] self.assertGreater(len(needed_containers), 0) self.assertEqual(needed_containers[0].name, new_container.name) self.assertEqual(needed_containers[0].env, new_container.env) self.assertEqual(needed_containers[0].limits.cpu, new_container.limits.cpu) self.assertEqual(needed_containers[0].limits.memory, new_container.limits.memory) self.assertEqual(needed_containers[0].image, new_container.image)
class TestPod(unittest.TestCase): __test_deployment = chkit.Deployment( name="pod-log-test", replicas=1, containers=[ chkit.Container(image="twentydraft/shibainfo", name="shiba", limits=chkit.Resources(cpu=50, memory=50)) ]) @timeout_decorator.timeout(seconds=650) @chkit.test_account @chkit.with_deployment(deployment=__test_deployment) @chkit.ensure_pods_running(deployment=__test_deployment.name) def test_pod_logs(self, depl: chkit.Deployment): pod = [pod for pod in chkit.get_pods() if pod.deploy == depl.name][0] time.sleep(30) log_lines = chkit.pod_logs(pod=pod.name, tail=10) print("\ngot log lines:") print("\n".join(log_lines)) self.assertGreaterEqual(len(log_lines), 3) self.assertLessEqual(len(log_lines), 10)
class TestExternalService(unittest.TestCase): __default_services_deployment = chkit.Deployment( name="default-services-test-depl", replicas=1, containers=[ chkit.Container(image="nginx", name="first", limits=chkit.Resources(cpu=10, memory=10)), ], ) @timeout_decorator.timeout(seconds=650 * 2) @chkit.test_account @chkit.with_deployment(deployment=__default_services_deployment) @chkit.ensure_pods_running(deployment=__default_services_deployment.name) def test_base(self, depl: chkit.Deployment): svc = chkit.Service(name="test-external-service", deploy=depl.name, ports=[ chkit.ServicePort(name="test-external-port", target_port=80) ]) try: chkit.create_service(svc) self.assertIn(svc.name, [service.name for service in chkit.get_services()]) got_svc = chkit.get_service(svc.name) self.assertEqual(svc.name, got_svc.name) self.assertTrue(got_svc.is_external()) attempts, max_attempts = 1, 40 while attempts <= max_attempts: try: response = requests.get( f"http://{got_svc.ips[0]}:{got_svc.ports[0].port}", headers={"Host": got_svc.domain}, timeout=1) response.raise_for_status() if response.status_code == 200: break except requests.exceptions.ConnectionError: pass time.sleep(15) attempts += 1 self.assertLessEqual(attempts, max_attempts) finally: chkit.delete_service(svc.name) time.sleep(1) self.assertNotIn( svc.name, [service.name for service in chkit.get_services()]) __default_external_service = chkit.Service( name="test-external-service", deploy=__default_services_deployment.name, ports=[chkit.ServicePort(name="test-external-port", target_port=80)]) @timeout_decorator.timeout(seconds=30) @chkit.test_account @chkit.with_deployment(deployment=__default_services_deployment) @chkit.with_service(service=__default_external_service) def test_update(self, depl: chkit.Deployment, svc: chkit.Service): new_svc = chkit.Service(name=svc.name, deploy=depl.name, ports=[ chkit.ServicePort( name="test-external-port-update", target_port=443) ]) chkit.replace_service(service=new_svc, file=True) got_svc = chkit.get_service(service=new_svc.name) self.assertEqual(new_svc.name, got_svc.name) self.assertEqual(len(got_svc.ports), 1) self.assertEqual(got_svc.ports[0].name, new_svc.ports[0].name) self.assertEqual(got_svc.ports[0].target_port, new_svc.ports[0].target_port)
class TestInternalService(unittest.TestCase): __default_services_deployment = chkit.Deployment( name="default-services-test-depl", replicas=1, containers=[ chkit.Container(image="nginx", name="first", limits=chkit.Resources(cpu=10, memory=10)), ], ) @timeout_decorator.timeout(seconds=30) @chkit.test_account @chkit.with_deployment(deployment=__default_services_deployment) def test_base(self, depl: chkit.Deployment): svc = chkit.Service(name="test-internal-service", deploy=depl.name, ports=[ chkit.ServicePort(name="test-internal-port", target_port=80, port=8888) ]) try: chkit.create_service(svc) self.assertIn(svc.name, [service.name for service in chkit.get_services()]) got_svc = chkit.get_service(svc.name) self.assertEqual(svc.name, got_svc.name) self.assertFalse(got_svc.is_external()) finally: chkit.delete_service(svc.name) time.sleep(1) self.assertNotIn( svc.name, [service.name for service in chkit.get_services()]) __default_internal_service = chkit.Service( name="test-internal-service", deploy=__default_services_deployment.name, ports=[ chkit.ServicePort(name="test-internal-port", target_port=80, port=8888) ]) @timeout_decorator.timeout(seconds=30) @chkit.test_account @chkit.with_deployment(deployment=__default_services_deployment) @chkit.with_service(service=__default_internal_service) def test_update(self, depl: chkit.Deployment, svc: chkit.Service): new_svc = chkit.Service( name=svc.name, deploy=depl.name, ports=[ chkit.ServicePort(name="test-internal-port-update", target_port=443, port=9999) ], ) chkit.replace_service(new_svc) got_svc = chkit.get_service(svc.name) self.assertEqual(got_svc.name, new_svc.name) self.assertEqual(got_svc.deploy, new_svc.deploy) self.assertEqual(got_svc.ports[0].name, new_svc.ports[0].name) self.assertEqual(got_svc.ports[0].target_port, new_svc.ports[0].target_port) self.assertEqual(got_svc.ports[0].port, new_svc.ports[0].port)
class TestIngress(unittest.TestCase): __default_services_deployment = chkit.Deployment( name="default-services-test-depl", replicas=1, containers=[ chkit.Container(image="nginx", name="first", limits=chkit.Resources(cpu=10, memory=10)), ], ) __default_external_service = chkit.Service( name="test-external-service", deploy=__default_services_deployment.name, ports=[ chkit.ServicePort(name="test-external-port", target_port=80), chkit.ServicePort(name="test-external-port-2", target_port=443) ]) @timeout_decorator.timeout(seconds=650 * 2) @chkit.test_account @chkit.with_deployment(deployment=__default_services_deployment) @chkit.ensure_pods_running(deployment=__default_services_deployment.name) @chkit.with_service(service=__default_external_service) def test_base(self, depl: chkit.Deployment, svc: chkit.Service): ingr = chkit.Ingress(name="test-ingress", rules=[ chkit.IngressRules( host="test-host", path=[ chkit.IngressPath( path="/", service_name=svc.name, service_port=svc.ports[0].port) ]) ]) try: got_svc = chkit.get_service(svc.name) ingr.rules[0].path[0].service_port = got_svc.ports[0].port chkit.create_ingress(ingr) self.assertIn(ingr.name[0], [ingr.name[0] for ingr in chkit.get_ingresses()]) got_ingr = chkit.get_ingress(ingr.name[0]) self.assertEqual(got_ingr.name[0], ingr.name[0]) self.assertEqual(got_ingr.rules[0].host, ingr.rules[0].host + ".hub.containerum.io") self.assertEqual(got_ingr.rules[0].path[0].path, ingr.rules[0].path[0].path) self.assertEqual(got_ingr.rules[0].path[0].service_port, ingr.rules[0].path[0].service_port) self.assertEqual(got_ingr.rules[0].path[0].service_name, ingr.rules[0].path[0].service_name) url = "http://" + got_ingr.rules[0].host attempts, max_attempts = 1, 40 while attempts <= max_attempts: try: response = requests.get(url) response.raise_for_status() if response.status_code < 400: break except requests.exceptions.ConnectionError: pass time.sleep(15) attempts += 1 self.assertLessEqual(attempts, max_attempts) finally: chkit.delete_ingress(ingr.name[0]) time.sleep(1) self.assertNotIn( ingr.name[0], [ingrs.name[0] for ingrs in chkit.get_ingresses()]) __default_internal_service = chkit.Service( name="test-internal-service", deploy=__default_services_deployment.name, ports=[ chkit.ServicePort(name="test-int-port", port=80, target_port=80), chkit.ServicePort(name="test-int-port-2", port=443, target_port=443) ]) __default_update_ingress = chkit.Ingress( name="test-ingress", rules=[ chkit.IngressRules( host="test-host", path=[ chkit.IngressPath( path="/", service_name=__default_internal_service.name, service_port=__default_internal_service.ports[0].port) ]) ]) @timeout_decorator.timeout(seconds=30) @chkit.test_account @chkit.with_deployment(deployment=__default_services_deployment) @chkit.with_service(service=__default_internal_service) @chkit.with_ingress(ingress=__default_update_ingress) def test_update(self, depl: chkit.Deployment, svc: chkit.Service, ingr: chkit.Ingress): self.assertIn(ingr.name[0], [ingr.name[0] for ingr in chkit.get_ingresses()]) repl_ingr = chkit.Ingress( name=ingr.name[0], rules=[ chkit.IngressRules(path=[ chkit.IngressPath(path="/test-path", service_name=svc.name, service_port=svc.ports[1].port) ]) ]) chkit.replace_ingress(repl_ingr) got_ingr = chkit.get_ingress(ingr.name[0]) self.assertEqual(got_ingr.name[0], repl_ingr.name[0]) self.assertEqual(got_ingr.rules[0].path[0].path, repl_ingr.rules[0].path[0].path) self.assertEqual(got_ingr.rules[0].path[0].service_port, repl_ingr.rules[0].path[0].service_port) self.assertEqual(got_ingr.rules[0].path[0].service_name, repl_ingr.rules[0].path[0].service_name)