def test_delete(self): name = "yo-{0}".format(str(uuid.uuid4().get_hex()[:16])) svc = utils.create_service(name=name) svc.add_port(name="redis", port=5432, target_port=5432, protocol="tcp") if utils.is_reachable(svc.config.api_host): svc.create() from_get = K8sService.get_by_name(svc.config, svc.name) self.assertIsInstance(from_get, list) self.assertIn(svc, from_get) svc.delete() from_get = K8sService.get_by_name(svc.config, svc.name) self.assertNotIn(svc, from_get)
def test_delete(self): name = "yo-{0}".format(str(uuid.uuid4().hex[:16])) svc = _utils.create_service(name=name) svc.add_port(name="redis", port=5432, target_port=5432, protocol="tcp") if _utils.is_reachable(svc.config): svc.create() from_get = K8sService.get_by_name(svc.config, svc.name) self.assertIsInstance(from_get, list) self.assertIn(svc, from_get) svc.delete() from_get = K8sService.get_by_name(svc.config, svc.name) self.assertNotIn(svc, from_get)
def create_service(self, name, port=None, debug_port=None): service = K8sService(config=self.k8s_config, name='svc-' + name) if port: service.add_port(name='web', port='8080', target_port='8080', node_port=str(port), protocol='TCP') if debug_port: service.add_port(name='debug', port='8087', target_port='8087', node_port=str(debug_port), protocol='TCP') service.add_selector(selector=dict(name=name)) service.type = 'NodePort' is_run = False last_port = "" if self.has_service(name='svc-' + name): last_port = self.get_nodePort(name, "web") is_run = True if port and str(last_port) != str(port): if is_run: model = self.get_service(name) service.cluster_ip = model["spec"]["clusterIP"] service.model.metadata.resource_version = model['metadata'][ 'resourceVersion'] return service.update() else: return service.create() else: return False
def test_get_by_name_nonexistent(self): name = "yo-{0}".format(str(uuid.uuid4().get_hex()[:16])) svc = utils.create_service(name=name) svc.add_port(name="redis", port=5432, target_port=5432, protocol="tcp") if utils.is_reachable(svc.config.api_host): _list = K8sService.get_by_name(config=svc.config, name=name) self.assertIsInstance(_list, list) self.assertEqual(0, len(_list))
def test_get_by_name_nonexistent(self): name = "yo-{0}".format(str(uuid.uuid4().hex[:16])) svc = _utils.create_service(name=name) svc.add_port(name="redis", port=5432, target_port=5432, protocol="tcp") if _utils.is_reachable(svc.config): _list = K8sService.get_by_name(config=svc.config, name=name) self.assertIsInstance(_list, list) self.assertEqual(0, len(_list))
def has_service(self, name): o = K8sService(config=self.k8s_config, name=name) res = False for ds in o.list(): if ds.name == name: res = True break else: continue return res
def test_init_no_args(self): try: K8sService() self.fail("Should not fail.") except SyntaxError: pass except IOError: pass except Exception as err: self.fail("Unhandled exception: [ {0} ]".format(err.__class__.__name__))
def test_get_by_name(self): name = "yo-{0}".format(str(uuid.uuid4().hex[:16])) svc = _utils.create_service(name=name) svc.add_port(name="redis", port=5432, target_port=5432, protocol="tcp") if _utils.is_reachable(svc.config): svc.create() _list = K8sService.get_by_name(config=svc.config, name=name) self.assertIsInstance(_list, list) self.assertEqual(1, len(_list)) from_get = _list[0] self.assertIsInstance(from_get, K8sService) self.assertEqual(from_get, svc)
def test_get_by_name(self): name = "yo-{0}".format(unicode(uuid.uuid4().get_hex()[:16])) svc = utils.create_service(name=name) svc.add_port(name="redis", port=5432, target_port=5432, protocol="tcp") if utils.is_reachable(svc.config.api_host): svc.create() _list = K8sService.get_by_name(config=svc.config, name=name) self.assertIsInstance(_list, list) self.assertEqual(1, len(_list)) from_get = _list[0] self.assertIsInstance(from_get, K8sService) self.assertEqual(from_get, svc)
def service_action(self): self.k8s_svc_name = self.project_name+'-'+self.project_status cfg_cert = K8sConfig(kubeconfig=None,auth=(self.k8s_username,self.k8s_password), api_host=self.k8s_host) service = K8sService(config=cfg_cert, name=self.k8s_svc_name) if not service._exists(): service.add_selector(selector={"name": self.k8s_deployment_name}) service.type = 'NodePort' service.add_port(name=self.k8s_svc_name, port=self.project_port, target_port=self.project_port, protocol='TCP') service.create() else: service.add_selector(selector={"name": self.k8s_deployment_name}) service.type = 'NodePort' if not service._exists_port(self.project_port): service.del_port() service.add_port(name=self.k8s_svc_name, port=self.project_port, target_port=self.project_port, protocol='TCP') service.update()
def del_service(self, name): service = K8sService(config=self.k8s_config, name='svc-' + name) return service.delete()
def get_service(self, name): service = K8sService(config=self.k8s_config, name='svc-' + name) return service.get_model()
def test_init_with_invalid_config(self): config = object() with self.assertRaises(SyntaxError): K8sService(config=config)