def test_init_with_invalid_name(self): name = object() try: utils.create_service(name=name) self.fail("Should not fail.") except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_set_service_type(self): name = "yoservice" svc = utils.create_service(name=name) for i in ['ClusterIP', 'NodePort', 'LoadBalancer']: svc.set_service_type(i) self.assertIn('type', svc.model.model['spec']) self.assertEqual(i, svc.model.model['spec']['type'])
def test_get_meta_creation_timestamp(self): name = "yoservice" svc = utils.create_service(name=name) tstamp_in = "yotimestamp" svc.set_meta_creation_timestamp(tstamp_in) tstamp_out = svc.get_meta_creation_timestamp() self.assertEqual(tstamp_in, tstamp_out)
def test_set_session_affinity(self): name = "yoservice" svc = utils.create_service(name=name) for i in ['None', 'ClientIP']: svc.set_session_affinity(i) self.assertIn('sessionAffinity', svc.model.model['spec']) self.assertEqual(i, svc.model.model['spec']['sessionAffinity'])
def test_add_selector(self): name = "yoservice" svc = utils.create_service(name=name) sel = {'abc': 'def'} svc.add_selector(selector=sel) self.assertIn('selector', svc.model.model['spec']) self.assertEqual(sel, svc.model.model['spec']['selector'])
def test_get_meta_generation(self): name = "yoservice" svc = utils.create_service(name=name) gen_in = 2 svc.set_meta_generation(gen_in) gen_out = svc.get_meta_generation() self.assertEqual(gen_in, gen_out)
def test_struct_service_model(self): name = "yoname" svc = utils.create_service(name=name) model = svc.model.model self.assertIsInstance(model, dict) self.assertEqual(4, len(model)) for i in ['apiVersion', 'kind', 'metadata', 'spec']: self.assertIn(i, model) for i in ['apiVersion', 'kind']: self.assertIsInstance(model[i], str) for i in ['metadata', 'spec']: self.assertIsInstance(model[i], dict) self.assertEqual(3, len(model['metadata'])) for i in ['labels', 'name', 'namespace']: self.assertIn(i, model['metadata']) self.assertIsInstance(model['metadata']['labels'], dict) for i in ['name', 'namespace']: self.assertIsInstance(model['metadata'][i], str) self.assertEqual(4, len(model['spec'])) for i in ['ports', 'selector', 'sessionAffinity', 'type']: self.assertIn(i, model['spec']) self.assertIsInstance(model['spec']['ports'], list) self.assertIsInstance(model['spec']['selector'], dict) for i in ['sessionAffinity', 'type']: self.assertIsInstance(model['spec'][i], str)
def test_get_meta_self_link(self): name = "yoservice" svc = utils.create_service(name=name) slink_in = "yoselflink" svc.set_meta_self_link(slink_in) slink_out = svc.get_meta_self_link() self.assertEqual(slink_in, slink_out)
def test_get_meta_resource_version(self): name = "yoservice" svc = utils.create_service(name=name) ver_in = "yoversion" svc.set_meta_resource_version(ver_in) ver_out = svc.get_meta_resource_version() self.assertEqual(ver_in, ver_out)
def test_get_external_ips(self): name = "yoservice" svc = utils.create_service(name=name) eips_in = ["192.168.99.100"] svc.set_external_ips(eips_in) eips_out = svc.get_external_ips() self.assertEqual(eips_in, eips_out)
def test_get_cluster_ip(self): name = "yoservice" svc = utils.create_service(name=name) cip_in = "192.168.99.100" svc.set_cluster_ip(cip_in) cip_out = svc.get_cluster_ip() self.assertEqual(cip_in, cip_out)
def test_struct_service(self): name = "yoname" svc = utils.create_service(name=name) self.assertIsInstance(svc, K8sService) self.assertIsInstance(svc.model, Service) self.assertIsInstance(svc.model.model, dict) self.assertIsInstance(svc.model.svc_metadata, ObjectMeta)
def test_get_meta_uid(self): name = "yoservice" svc = utils.create_service(name=name) uid_in = "yoselflink" svc.set_meta_uid(uid_in) uid_out = svc.get_meta_uid() self.assertEqual(uid_in, uid_out)
def test_set_labels(self): name = "yoservice" svc = utils.create_service(name=name) labels = {'yokey': 'yovalue'} svc.set_labels(labels) self.assertIn('labels', svc.model.model['metadata']) self.assertEqual(labels, svc.model.model['metadata']['labels'])
def test_set_external_ip(self): name = "yoservice" svc = utils.create_service(name=name) eips = ["192.168.99.100"] svc.set_external_ips(eips) self.assertIn('externalIPs', svc.model.model['spec']) self.assertEqual(eips, svc.model.model['spec']['externalIPs'])
def test_set_annotations(self): name = "yoservice" svc = utils.create_service(name=name) anns = {'yokey': 'yovalue'} svc.set_annotations(anns) self.assertIn('annotations', svc.model.model['metadata']) self.assertEqual(anns, svc.model.model['metadata']['annotations'])
def test_set_cluster_ip(self): name = "yoservice" svc = utils.create_service(name=name) cip = "192.168.99.100" svc.set_cluster_ip(cip) self.assertIn('clusterIP', svc.model.model['spec']) self.assertEqual(cip, svc.model.model['spec']['clusterIP'])
def test_set_load_balancer_ip(self): name = "yoservice" svc = utils.create_service(name=name) lbip = "192.168.99.100" svc.set_load_balancer_ip(lbip) self.assertIn('loadBalancerIP', svc.model.model['spec']) self.assertEqual(lbip, svc.model.model['spec']['loadBalancerIP'])
def test_get_labels(self): name = "yoservice" svc = utils.create_service(name=name) labels_in = {'yokey': 'yovalue'} svc.set_labels(labels_in) labels_out = svc.get_labels() self.assertEqual(labels_in, labels_out)
def test_get_nonexistent(self): name = "yoservice" svc = utils.create_service(name=name) if utils.is_reachable(svc.config.api_host): try: svc.get() except Exception as err: self.assertIsInstance(err, NotFoundException)
def test_set_load_balancer_ip_none_arg(self): name = "yoservice" svc = utils.create_service(name=name) try: svc.set_load_balancer_ip() self.fail("Should not fail.") except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_set_labels_none_arg(self): name = "yoservice" svc = utils.create_service(name=name) try: svc.set_labels() self.fail('Should not fail.') except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_init_with_name(self): name = "yoname" svc = utils.create_service(name=name) self.assertIsNotNone(svc) self.assertIsInstance(svc, K8sService) self.assertEqual('Service', svc.obj_type) self.assertEqual(svc.name, name) self.assertIsInstance(svc.config, K8sConfig)
def test_add_selector_none_arg(self): name = "yoservice" svc = utils.create_service(name=name) try: svc.add_selector() self.fail("Should not fail.") except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_set_meta_resource_version_none_arg(self): name = "yoservice" svc = utils.create_service(name=name) try: svc.set_meta_resource_version() self.fail("Should not fail.") except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_add_label(self): name = "yoservice" svc = utils.create_service(name=name) k = "yokey" v = "yovalue" svc.add_label(k, v) self.assertIn(k, svc.labels) self.assertEqual(v, svc.labels[k])
def test_get_label(self): name = "yoservice" svc = utils.create_service(name=name) k = "yokey" v_in = "yovalue" svc.add_label(k, v_in) v_out = svc.get_label(k) self.assertEqual(v_in, v_out)
def test_del_server_generated_meta_doesnt_exist(self): name = "yoservice" svc = utils.create_service(name=name) svc.del_server_generated_meta_attr() for i in ['generation', 'resourceVersion', 'creationTimestamp', 'deletionTimestamp', 'deletionGracePeriodSeconds', 'status', 'selfLink', 'uid']: self.assertNotIn(i, svc.model.model['metadata']) self.assertNotIn(i, svc.model.svc_metadata.model)
def test_get_by_name_nonexistent(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): _list = K8sService.get_by_name(config=svc.config, name=name) self.assertIsInstance(_list, list) self.assertEqual(0, len(_list))
def test_add_annotation(self): name = "yoservice" svc = utils.create_service(name=name) k = "yokey" v = "yovalue" svc.add_annotation(k, v) self.assertIn(k, svc.annotations) self.assertEqual(v, svc.annotations[k])
def test_update_nothing_changed(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): from_create = svc.create() from_update = svc.update() self.assertEqual(from_create, from_update)
def test_set_session_affinity_invalid_string(self): name = "yoservice" svc = utils.create_service(name=name) aff = 'yoaffinity' try: svc.set_session_affinity(aff) except Exception as err: self.assertIsInstance(err, SyntaxError)
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.api_host): _list = K8sService.get_by_name(config=svc.config, name=name) self.assertIsInstance(_list, list) self.assertEqual(0, len(_list))
def test_get_annotation(self): name = "yoservice" svc = utils.create_service(name=name) k = "yokey" v_in = "yovalue" svc.add_annotation(k, v_in) v_out = svc.get_annotation(k) self.assertEqual(v_in, v_out)
def test_list_without_create(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 = svc.list() self.assertIsInstance(_list, list) self.assertEqual(1, len(_list)) # api server exists already self.assertIsInstance(_list[0], dict)
def test_update_set_cluster_ip_fails(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() svc.cluster_ip = "192.168.123.123" with self.assertRaises(UnprocessableEntityException): svc.update()
def test_get(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 = svc.get() self.assertIsInstance(from_get, K8sService) self.assertEqual(svc, from_get)
def test_struct_k8s_service(self): name = "yoname" svc = utils.create_service(name=name) self.assertIsInstance(svc, K8sService) self.assertIsInstance(svc.base_url, str) self.assertIsInstance(svc.config, K8sConfig) self.assertIsInstance(svc.model, Service) self.assertIsInstance(svc.name, str) self.assertIsInstance(svc.obj_type, str)
def test_update_set_external_ips(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") ip = '192.168.123.123' if utils.is_reachable(svc.config.api_host): svc.create() svc.external_ips = [ip] svc.update() self.assertIn(ip, svc.external_ips)
def test_set_cluster_ip_invalid_ip_address(self): name = "yoservice" svc = utils.create_service(name=name) svc.add_port('http', 80, 80, 'TCP') if utils.is_reachable(svc.config.api_host): svc.create() ip = "192.168.00000.1234345" svc.cluster_ip = ip with self.assertRaises(UnprocessableEntityException): svc.update()
def test_add_annotation_invalid_args(self): name = "yoservice" svc = utils.create_service(name=name) k = object() v = object() try: svc.add_annotation(k, v) self.fail("Should not fail.") except Exception as err: self.assertIsInstance(err, SyntaxError)
def test_init_with_name_and_config(self): nspace = "yonamespace" config = K8sConfig(kubeconfig=utils.kubeconfig_fallback, namespace=nspace) name = "yoname" svc = utils.create_service(config=config, name=name) self.assertIsNotNone(svc) self.assertIsInstance(svc, K8sService) self.assertEqual(svc.name, name) self.assertEqual('Service', svc.obj_type) self.assertIsInstance(svc.config, K8sConfig) self.assertEqual(nspace, svc.config.namespace)
def test_get_by_name(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() _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_list(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() _list = svc.list() self.assertIsInstance(_list, list) self.assertEqual(2, len(_list)) # api server exists already from_query = _list[1] self.assertIsInstance(from_query, dict) self.assertEqual(name, from_query['metadata']['name'])
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.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_get_annotations(self): name = "yoservice" svc = utils.create_service(name=name) count = 4 for i in range(0, count): k = "yokey_{0}".format(i) v = "yovalue_{0}".format(i) svc.add_annotation(k, v) self.assertEqual(count, len(svc.annotations)) for i in range(0, count): k = "yokey_{0}".format(i) v = "yovalue_{0}".format(i) self.assertIn(k, svc.annotations) self.assertEqual(v, svc.annotations[k])
def test_update_with_full_model(self): data = { "kind": "Service", "apiVersion": "v1", "metadata": { "name": "frontend", "namespace": "default", "labels": { "name": "frontend" } }, "spec": { "ports": [ { "protocol": "TCP", "port": 8082, "targetPort": "feport", "nodePort": 8082 } ], "selector": { "name": "frontend" }, "clusterIP": "10.250.1.27", "type": "NodePort", "sessionAffinity": "None" }, "status": { "loadBalancer": {} } } svc = Service(data) k8s_service = utils.create_service(name=svc.name) k8s_service.model = svc k8s_service.add_port( name="frontend", port=8082, target_port="feport", node_port=8082, protocol='tcp' ) self.assertEqual(1, len(k8s_service.ports))
def test_add_port_with_all_args(self): svc_name = "yoservice" svc = utils.create_service(name=svc_name) port = 666 port_name = "yoport" target_port = 666 protocol = 'TCP' node_port = 666 svc.add_port(port=port, name=port_name, target_port=target_port, protocol=protocol, node_port=node_port) self.assertEqual(1, len(svc.ports)) self.assertEqual(port_name, svc.ports[0].name) self.assertEqual(port, svc.ports[0].port) self.assertEqual(target_port, svc.ports[0].target_port) self.assertEqual(protocol, svc.ports[0].protocol) self.assertEqual(node_port, svc.ports[0].node_port)
def test_system_service(self): config = utils.create_config() config.namespace = 'kube-system' service = utils.create_service(config, name="kubernetes-dashboard") service.type = 'ClusterIP' service.add_port(port=80, target_port="k8s-dashport", name="kubernetes-dashboard", protocol="TCP") service.selector = {'k8s-app': "kubernetes-dashboard"} service.labels = { 'k8s-app': "kubernetes-dashboard", 'kubernetes.io/cluster-service': 'true' } if utils.is_reachable(service.config.api_host): with self.assertRaises(AlreadyExistsException): service.create() service.get() service.update()
def test_set_session_affinity_none_arg(self): name = "yoservice" svc = utils.create_service(name=name) with self.assertRaises(SyntaxError): svc.session_affinity = None
def test_set_load_balancer_ip(self): name = "yoservice" svc = utils.create_service(name=name) ip = "192.168.99.100" svc.load_balancer_ip = ip self.assertEqual(ip, svc.load_balancer_ip)
def test_set_load_balancer_ip_invalid_ip_address(self): name = "yoservice" svc = utils.create_service(name=name) ip = "192.168.00000.1234345" with self.assertRaises(SyntaxError): svc.load_balancer_ip = ip
def test_set_namespace(self): name = "yoservice" svc = utils.create_service(name=name) nspace = "yonamespace" svc.namespace = nspace self.assertEqual(nspace, svc.namespace)
def test_set_namespace_invalid_arg(self): name = "yoservice" svc = utils.create_service(name=name) nspace = object() with self.assertRaises(SyntaxError): svc.namespace = nspace
def test_set_session_affinity_invalid_string(self): name = "yoservice" svc = utils.create_service(name=name) aff = 'yoaffinity' with self.assertRaises(SyntaxError): svc.session_affinity = aff
def test_set_service_type(self): name = "yoservice" svc = utils.create_service(name=name) for i in ['ClusterIP', 'NodePort', 'LoadBalancer']: svc.type = i self.assertEqual(i, svc.type)
def test_set_service_type_invalid_string(self): name = "yoservice" svc = utils.create_service(name=name) stype = "yoservicetype" with self.assertRaises(SyntaxError): svc.type = stype
def test_set_session_affinity(self): name = "yoservice" svc = utils.create_service(name=name) for i in ['None', 'ClientIP']: svc.session_affinity = i self.assertEqual(i, svc.session_affinity)
def test_set_service_type_none_arg(self): name = "yoservice" svc = utils.create_service(name=name) with self.assertRaises(SyntaxError): svc.type = None