From 914d8ccb42befc04ec5b759dd84b242591bb5e91 Mon Sep 17 00:00:00 2001 From: Tommy Carpenter Date: Mon, 5 Mar 2018 11:25:44 -0500 Subject: Enforce most pep8 compliance in tox Also fix some pep8 failings Change-Id: I6904c171c898d510ac2b7e2435b5a80dcdf1be13 Issue-ID: DCAEGEN2-348 Signed-off-by: Tommy Carpenter --- config_binding_service/__init__.py | 10 +- config_binding_service/client.py | 48 +++++--- config_binding_service/controller.py | 9 +- pom.xml | 2 +- setup.py | 2 +- tests/test_binding.py | 226 ++++++++++++++++++++--------------- tox-local.ini | 12 +- tox.ini | 12 +- 8 files changed, 197 insertions(+), 124 deletions(-) diff --git a/config_binding_service/__init__.py b/config_binding_service/__init__.py index 5f09f1e..2835511 100644 --- a/config_binding_service/__init__.py +++ b/config_binding_service/__init__.py @@ -19,7 +19,7 @@ import os import logging -'''Configures the module root logger''' +# Configures the module root logger root = logging.getLogger() if root.handlers: root.handlers.clear() @@ -29,13 +29,19 @@ handler.setFormatter(formatter) root.addHandler(handler) root.setLevel("DEBUG") + class BadEnviornmentENVNotFound(Exception): + """ + Specific exception to be raised when a required ENV varaible is missing + """ pass + def get_logger(module=None): '''Returns a module-specific logger or global logger if the module is None''' return root if module is None else root.getChild(module) + def get_consul_uri(): """ This method waterfalls reads an envioronmental variable called CONSUL_HOST @@ -48,5 +54,3 @@ def get_consul_uri(): return "http://{0}:{1}".format(os.environ["CONSUL_HOST"], 8500) else: raise BadEnviornmentENVNotFound("CONSUL_HOST") - - diff --git a/config_binding_service/client.py b/config_binding_service/client.py index 8facf35..2c04684 100644 --- a/config_binding_service/client.py +++ b/config_binding_service/client.py @@ -40,6 +40,7 @@ class CantGetConfig(Exception): """ Represents an exception where a required key in consul isn't there """ + def __init__(self, code, response): self.code = code self.response = response @@ -49,6 +50,7 @@ class BadRequest(Exception): """ Exception to be raised when the user tried to do something they shouldn't """ + def __init__(self, response): self.code = 400 self.response = response @@ -84,7 +86,8 @@ def _consul_get_all_as_transaction(service_component_name): new_res = {} for res in result: key = res["KV"]["Key"] - new_res[key] = json.loads(base64.b64decode(res["KV"]["Value"]).decode("utf-8")) + new_res[key] = json.loads(base64.b64decode( + res["KV"]["Value"]).decode("utf-8")) if service_component_name not in new_res: raise CantGetConfig(404, "") @@ -108,18 +111,23 @@ def _get_connection_info_from_consul(service_component_name): TODO: WARNING: FIXTHIS: CALLINTHENATIONALARMY: This tries to determine that a service_component_name is a cdap application by inspecting service_component_name and name munging. However, this would force all CDAP applications to have cdap_app in their name. A much better way to do this is to do some kind of catalog_lookup here, OR MAYBE change this API so that the component_type is passed in somehow. THis is a gaping TODO. """ - _logger.info("Retrieving connection information for {0}".format(service_component_name)) - res = requests.get("{0}/v1/catalog/service/{1}".format(CONSUL, service_component_name)) + _logger.info("Retrieving connection information for {0}".format( + service_component_name)) + res = requests.get( + "{0}/v1/catalog/service/{1}".format(CONSUL, service_component_name)) res.raise_for_status() services = res.json() if services == []: - _logger.info("Warning: config and rels keys were both valid, but there is no component named {0} registered in Consul!".format(service_component_name)) - return None #later will get filtered out + _logger.info("Warning: config and rels keys were both valid, but there is no component named {0} registered in Consul!".format( + service_component_name)) + return None # later will get filtered out ip_addr = services[0]["ServiceAddress"] port = services[0]["ServicePort"] if "cdap_app" in service_component_name: - redirectish_url = "http://{0}:{1}/application/{2}".format(ip_addr, port, service_component_name) - _logger.info("component is a CDAP application; trying the broker redirect on {0}".format(redirectish_url)) + redirectish_url = "http://{0}:{1}/application/{2}".format( + ip_addr, port, service_component_name) + _logger.info("component is a CDAP application; trying the broker redirect on {0}".format( + redirectish_url)) res = requests.get(redirectish_url) res.raise_for_status() details = res.json() @@ -138,7 +146,7 @@ def _replace_rels_template(rels, template_identifier): for rel in rels: if template_identifier in rel and template_identifier is not "": returnl.append(rel) - #returnl now contains a list of DNS names (possible empty), now resolve them (or not if they are not regustered) + # returnl now contains a list of DNS names (possible empty), now resolve them (or not if they are not regustered) return list(filter(lambda x: x is not None, map(_get_connection_info_from_consul, returnl))) @@ -159,10 +167,11 @@ def _replace_value(v, rels, dmaap): - the split below sees if we have v = x,y,z... so we can support {{x,y,z,....}} - the lambda is because we can't fold operators in Python, wanted fold(+, L) where + when applied to lists in python is list concatenation """ - if isinstance(v, six.string_types): #do not try to replace anything that is not a string + if isinstance(v, six.string_types): # do not try to replace anything that is not a string match_on_rels = re.match(template_match_rels, v) if match_on_rels: - template_identifier = match_on_rels.groups()[0].strip() #now holds just x,.. of {{x,...}} + # now holds just x,.. of {{x,...}} + template_identifier = match_on_rels.groups()[0].strip() rtpartial = partial(_replace_rels_template, rels) return reduce(lambda a, b: a + b, map(rtpartial, template_identifier.split(",")), []) match_on_dmaap = re.match(template_match_dmaap, v) @@ -176,7 +185,7 @@ def _replace_value(v, rels, dmaap): So now component developers have to possible handle dicts and [], and we have to communicate that to them """ return _replace_dmaap_template(dmaap, template_identifier) - return v #was not a match or was not a string, return value as is + return v # was not a match or was not a string, return value as is def _recurse(config, rels, dmaap): @@ -195,7 +204,7 @@ def _recurse(config, rels, dmaap): return config if isinstance(config, six.string_types): return _replace_value(config, rels, dmaap) - #not a dict, not a list, not a string, nothing to do. + # not a dict, not a list, not a string, nothing to do. return config @@ -217,7 +226,7 @@ def resolve_override(config, rels=[], dmaap={}): Explicitly take in a config, rels, dmaap and try to resolve it. Useful for testing where you dont want to put the test values in consul """ - #use deepcopy to make sure that config is not touched + # use deepcopy to make sure that config is not touched return _recurse(copy.deepcopy(config), rels, dmaap) @@ -228,10 +237,10 @@ def resolve_all(service_component_name): allk = _consul_get_all_as_transaction(service_component_name) returnk = {} - #replace the config with the resolved config + # replace the config with the resolved config returnk["config"] = resolve(service_component_name) - #concatenate the items + # concatenate the items for k in allk: if "policies" in k: if "policies" not in returnk: @@ -245,7 +254,8 @@ def resolve_all(service_component_name): returnk["policies"]["items"].append(allk[k]) else: if not(k == service_component_name or k.endswith(":rels") or k.endswith(":dmaap")): - suffix = k.split(":")[1] #this would blow up if you had a key in consul without a : but this shouldnt happen + # this would blow up if you had a key in consul without a : but this shouldnt happen + suffix = k.split(":")[1] returnk[suffix] = allk[k] return returnk @@ -256,8 +266,10 @@ def get_key(key, service_component_name): Try to fetch a key k from Consul of the form service_component_name:k """ if key == "policies": - raise BadRequest(":policies is a complex folder and should be retrieved using the service_component_all API") - response = requests.get("{0}/v1/kv/{1}:{2}".format(CONSUL, service_component_name, key)) + raise BadRequest( + ":policies is a complex folder and should be retrieved using the service_component_all API") + response = requests.get( + "{0}/v1/kv/{1}:{2}".format(CONSUL, service_component_name, key)) try: response.raise_for_status() except requests.exceptions.HTTPError as exc: diff --git a/config_binding_service/controller.py b/config_binding_service/controller.py index dbdb57b..7bb5f51 100644 --- a/config_binding_service/controller.py +++ b/config_binding_service/controller.py @@ -48,7 +48,7 @@ def bind_config_for_scn(service_component_name): except client.CantGetConfig as e: return Response(status=e.code, response=e.response) - except Exception as e: #should never happen... + except Exception as e: # should never happen... _logger.error(e) return Response(response="Please report this error", status=500) @@ -67,16 +67,17 @@ def get_key(key, service_component_name): return Response(status=exc.code, response=exc.response, mimetype="text/plain") - except Exception as e: #should never happen... + except Exception as e: # should never happen... _logger.error(e) return Response(response="Please report this error", status=500) def healthcheck(): - #got this far, I must be alive... check my connection to Consul by checking myself + # got this far, I must be alive... check my connection to Consul by checking myself CONSUL = get_consul_uri() - res = requests.get("{0}/v1/catalog/service/config_binding_service".format(CONSUL)) + res = requests.get( + "{0}/v1/catalog/service/config_binding_service".format(CONSUL)) if res.status_code == 200: return Response(response="CBS is alive and Consul connection OK", status=200) diff --git a/pom.xml b/pom.xml index be972e7..1ceef1f 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property. org.onap.dcaegen2.platform configbinding dcaegen2-platform-configbinding - 2.1.0 + 2.1.1 http://maven.apache.org UTF-8 diff --git a/setup.py b/setup.py index 2c4fd2e..7f73b93 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ from setuptools import setup, find_packages setup( name='config_binding_service', - version='2.1.0', + version='2.1.1', packages=find_packages(exclude=["tests.*", "tests"]), author="Tommy Carpenter", author_email="tommy@research.att.com", diff --git a/tests/test_binding.py b/tests/test_binding.py index 3f51285..1ed4b0a 100644 --- a/tests/test_binding.py +++ b/tests/test_binding.py @@ -29,7 +29,7 @@ from config_binding_service import client, controller def monkeyed_get_connection_info_from_consul(service_component_name): - #shared monkeypatch. probably somewhat lazy because the function htis patches can be broken up. + # shared monkeypatch. probably somewhat lazy because the function htis patches can be broken up. if service_component_name == "cdap": return '666.666.666.666:666' elif service_component_name == "testing_bravo.somedomain.com": @@ -39,7 +39,7 @@ def monkeyed_get_connection_info_from_consul(service_component_name): elif service_component_name == "testing_charlie.somedomain.com": return '5.5.5.5:555' elif service_component_name == "nonexistent_hope": - return None #the real function returns None here + return None # the real function returns None here elif service_component_name == "cdap_serv.dcae.ecomp.somedomain.com": broker_ip = '1.1.1.1' broker_port = 444 @@ -85,7 +85,8 @@ def monkeyed_requests_put(url, json): def test_consul_get_all_as_transaction(monkeypatch): monkeypatch.setattr('requests.put', monkeyed_requests_put) - allk = client._consul_get_all_as_transaction("test_service_component_name.unknown.unknown.unknown.dcae.onap.org") + allk = client._consul_get_all_as_transaction( + "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") assert allk == { 'test_service_component_name.unknown.unknown.unknown.dcae.onap.org': {'my': 'amazing config'}, 'test_service_component_name.unknown.unknown.unknown.dcae.onap.org:dti': {'my': 'dti'}, @@ -99,8 +100,10 @@ def test_consul_get_all_as_transaction(monkeypatch): def test_get_config_rels_dmaap(monkeypatch): monkeypatch.setattr('requests.put', monkeyed_requests_put) - assert ({"foo3": "bar3"}, ["foo"], {"foo4": "bar4"}) == client._get_config_rels_dmaap("scn_exists") - assert ({"foo5": "bar5"}, [], {}) == client._get_config_rels_dmaap("scn_exists_nord") + assert ({"foo3": "bar3"}, ["foo"], {"foo4": "bar4"} + ) == client._get_config_rels_dmaap("scn_exists") + assert ({"foo5": "bar5"}, [], {}) == client._get_config_rels_dmaap( + "scn_exists_nord") def test_bind_config_for_scn(monkeypatch): @@ -111,7 +114,7 @@ def test_bind_config_for_scn(monkeypatch): client.resolve("scn_NOTexists") R = controller.bind_config_for_scn("scn_exists") - assert(json.loads(R.data) == {"foo3" : "bar3"}) + assert(json.loads(R.data) == {"foo3": "bar3"}) assert(R.status_code == 200) R = controller.bind_config_for_scn("scn_NOTexists") @@ -124,153 +127,187 @@ def test_bind_config_for_scn(monkeypatch): def test_generic(monkeypatch): monkeypatch.setattr('requests.put', monkeyed_requests_put) monkeypatch.setattr('requests.get', monkeyed_requests_get) - assert client.get_key("dti", "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") == json.loads('{"my": "dti"}') + assert client.get_key( + "dti", "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") == json.loads('{"my": "dti"}') with pytest.raises(client.CantGetConfig): - client.get_key("nokeyforyou", "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") + client.get_key( + "nokeyforyou", "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") - R = controller.get_key("dti", "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") + R = controller.get_key( + "dti", "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") assert(json.loads(R.data) == {"my": "dti"}) assert(R.status_code == 200) - R = controller.get_key("nokeyforyou", "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") + R = controller.get_key( + "nokeyforyou", "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") assert(R.status_code == 404) - R = controller.get_key("policies", "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") + R = controller.get_key( + "policies", "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") assert(R.status_code == 400) def test_bad_config_http(): - test_config = {'yeahhhhh' : "{{}}"} + test_config = {'yeahhhhh': "{{}}"} test_rels = ["testing_bravo.somedomain.com"] - assert {'yeahhhhh' : []} == client.resolve_override(test_config, test_rels) + assert {'yeahhhhh': []} == client.resolve_override(test_config, test_rels) + def test_bad_config_dmaap(): - test_config = {'darkness' : "<<>>"} - test_dmaap = {"WHO?" : "darkness"} - assert {'darkness' : {}} == client.resolve_override(test_config, test_dmaap) + test_config = {'darkness': "<<>>"} + test_dmaap = {"WHO?": "darkness"} + assert {'darkness': {}} == client.resolve_override(test_config, test_dmaap) + def test_config(monkeypatch): - #test config override - monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', monkeyed_get_connection_info_from_consul) - test_config = {"autoderegisterafter": "10m", "cdap_to_manage": {'some_nested_thing' : "{{cdap}}"}, "bindingttw": 5, "hcinterval": "5s"} + # test config override + monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', + monkeyed_get_connection_info_from_consul) + test_config = {"autoderegisterafter": "10m", "cdap_to_manage": { + 'some_nested_thing': "{{cdap}}"}, "bindingttw": 5, "hcinterval": "5s"} test_rels = ["cdap"] test_bind_1 = client.resolve_override(test_config, test_rels) - assert test_bind_1 == {'autoderegisterafter': '10m', 'cdap_to_manage': {'some_nested_thing': ['666.666.666.666:666']}, 'bindingttw': 5, 'hcinterval': '5s'} + assert test_bind_1 == {'autoderegisterafter': '10m', 'cdap_to_manage': { + 'some_nested_thing': ['666.666.666.666:666']}, 'bindingttw': 5, 'hcinterval': '5s'} + def test_config_with_list(monkeypatch): - monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', monkeyed_get_connection_info_from_consul) - test_config_1 = {"dcae_target_type": ["vhss-ems", "pcrf-oam"], "downstream-laika": "{{ laika }}", "some-param": "Lorem ipsum dolor sit amet"} - test_rels_1 = ["3df5292249ae4a949f173063617cea8d_docker-snmp-polling-firstnet-m"] + monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', + monkeyed_get_connection_info_from_consul) + test_config_1 = {"dcae_target_type": [ + "vhss-ems", "pcrf-oam"], "downstream-laika": "{{ laika }}", "some-param": "Lorem ipsum dolor sit amet"} + test_rels_1 = [ + "3df5292249ae4a949f173063617cea8d_docker-snmp-polling-firstnet-m"] test_bind_1 = client.resolve_override(test_config_1, test_rels_1, {}) - assert(test_bind_1 == {'dcae_target_type': ['vhss-ems', 'pcrf-oam'], 'downstream-laika': [], 'some-param': 'Lorem ipsum dolor sit amet'}) + assert(test_bind_1 == {'dcae_target_type': [ + 'vhss-ems', 'pcrf-oam'], 'downstream-laika': [], 'some-param': 'Lorem ipsum dolor sit amet'}) - test_config_2 = {"foo" : ["{{cdap}}", "notouching", "<>"]} + test_config_2 = {"foo": ["{{cdap}}", "notouching", "<>"]} test_rels_2 = ["cdap"] - test_dmaap_2={"yo" : "im here"} - test_bind_2 = client.resolve_override(test_config_2, test_rels_2, test_dmaap_2) - assert(test_bind_2 == {"foo" : [['666.666.666.666:666'], "notouching", "im here"]}) + test_dmaap_2 = {"yo": "im here"} + test_bind_2 = client.resolve_override( + test_config_2, test_rels_2, test_dmaap_2) + assert(test_bind_2 == { + "foo": [['666.666.666.666:666'], "notouching", "im here"]}) + def test_non_existent(monkeypatch): - #test a valid config-rels but the key is not in Consul - monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', monkeyed_get_connection_info_from_consul) - test_config = {"you shall not be fufilled" : "{{nonexistent_hope}}"} - test_rels = ["nonexistent_hope.rework-central.ecomp.somedomain.com"] #hopefully not registered in Consul.. + # test a valid config-rels but the key is not in Consul + monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', + monkeyed_get_connection_info_from_consul) + test_config = {"you shall not be fufilled": "{{nonexistent_hope}}"} + # hopefully not registered in Consul.. + test_rels = ["nonexistent_hope.rework-central.ecomp.somedomain.com"] test_bind_1 = client.resolve_override(test_config, test_rels, {}) - assert(test_bind_1 == {"you shall not be fufilled" : []}) + assert(test_bind_1 == {"you shall not be fufilled": []}) + def test_cdap(monkeypatch): - #user override to test CDAP functionality - monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', monkeyed_get_connection_info_from_consul) - test_rels = ["testing_alpha.somedomain.com", "testing_bravo.somedomain.com", "testing_charlie.somedomain.com", "testing_charlie.somedomain.com", "cdap"] - test_config = { "streams_publishes" : "{{alpha}}", - "services_calls" : [{"somekey" : "{{charlie}}"}], #should be dumped - "cdap_to_manage": {'some_nested_thing' : "{{cdap}}"} #no dumps - } + # user override to test CDAP functionality + monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', + monkeyed_get_connection_info_from_consul) + test_rels = ["testing_alpha.somedomain.com", "testing_bravo.somedomain.com", + "testing_charlie.somedomain.com", "testing_charlie.somedomain.com", "cdap"] + test_config = {"streams_publishes": "{{alpha}}", + # should be dumped + "services_calls": [{"somekey": "{{charlie}}"}], + "cdap_to_manage": {'some_nested_thing': "{{cdap}}"}} # no dumps test_bind_1 = client.resolve_override(test_config, test_rels) - assert test_bind_1 == {'services_calls': [{"somekey": ["5.5.5.5:555", "5.5.5.5:555"]}], 'streams_publishes': ["6.6.6.6:666"], 'cdap_to_manage': {'some_nested_thing': ['666.666.666.666:666']}} - assert test_bind_1['services_calls'] == [{"somekey" : ["5.5.5.5:555", "5.5.5.5:555"]}] + assert test_bind_1 == {'services_calls': [{"somekey": ["5.5.5.5:555", "5.5.5.5:555"]}], 'streams_publishes': [ + "6.6.6.6:666"], 'cdap_to_manage': {'some_nested_thing': ['666.666.666.666:666']}} + assert test_bind_1['services_calls'] == [ + {"somekey": ["5.5.5.5:555", "5.5.5.5:555"]}] assert test_bind_1['streams_publishes'] == ["6.6.6.6:666"] + def test_broker_redirect(monkeypatch): - #test the broker redirect - monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', monkeyed_get_connection_info_from_consul) - test_config = {"gimmie_dat_cdap" : "{{cdap_serv}}"} + # test the broker redirect + monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', + monkeyed_get_connection_info_from_consul) + test_config = {"gimmie_dat_cdap": "{{cdap_serv}}"} test_rels = ["cdap_serv.dcae.ecomp.somedomain.com"] - assert {"gimmie_dat_cdap" : ['http://1.1.1.1:444/application/cdap_serv.dcae.ecomp.somedomain.com']} == client.resolve_override(test_config, test_rels) + assert {"gimmie_dat_cdap": ['http://1.1.1.1:444/application/cdap_serv.dcae.ecomp.somedomain.com'] + } == client.resolve_override(test_config, test_rels) -def test_multiple_service_types(monkeypatch): - #test {{x,y,z}} - monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', monkeyed_get_connection_info_from_consul) - #test 1: they all resovle - test_rels = ["testing_alpha.somedomain.com", "testing_bravo.somedomain.com", "testing_charlie.somedomain.com", "testing_charlie.somedomain.com"] - config = {"ALL YOUR SERVICE BELONG TO US" : "{{alpha,bravo,charlie}}"} +def test_multiple_service_types(monkeypatch): + # test {{x,y,z}} + monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', + monkeyed_get_connection_info_from_consul) + + # test 1: they all resovle + test_rels = ["testing_alpha.somedomain.com", "testing_bravo.somedomain.com", + "testing_charlie.somedomain.com", "testing_charlie.somedomain.com"] + config = {"ALL YOUR SERVICE BELONG TO US": "{{alpha,bravo,charlie}}"} test_bind_1 = client.resolve_override(config, test_rels) - assert(test_bind_1 == {"ALL YOUR SERVICE BELONG TO US" : ['6.6.6.6:666', '7.7.7.7:777', '5.5.5.5:555', '5.5.5.5:555']}) + assert(test_bind_1 == {"ALL YOUR SERVICE BELONG TO US": [ + '6.6.6.6:666', '7.7.7.7:777', '5.5.5.5:555', '5.5.5.5:555']}) - #test 2: two resolve, one is missing from rels key - config2 = {"two there one not exist" : "{{alpha,bravo,notexist}}"} + # test 2: two resolve, one is missing from rels key + config2 = {"two there one not exist": "{{alpha,bravo,notexist}}"} test_bind_2 = client.resolve_override(config2, test_rels) - assert(test_bind_2 == {"two there one not exist" : ['6.6.6.6:666', '7.7.7.7:777']}) + assert(test_bind_2 == {"two there one not exist": [ + '6.6.6.6:666', '7.7.7.7:777']}) - #test 3: two resolve, one is in rels key but not registered - config3 = {"two there one unregistered" : "{{alpha,bravo,unregistered}}"} - test_rels3 = ["testing_alpha.somedomain.com", "testing_bravo.somedomain.com", "unregistered.somedomain.com"] + # test 3: two resolve, one is in rels key but not registered + config3 = {"two there one unregistered": "{{alpha,bravo,unregistered}}"} + test_rels3 = ["testing_alpha.somedomain.com", + "testing_bravo.somedomain.com", "unregistered.somedomain.com"] test_bind_3 = client.resolve_override(config3, test_rels3) - assert(test_bind_3 == {"two there one unregistered" : ['6.6.6.6:666', '7.7.7.7:777']}) + assert(test_bind_3 == {"two there one unregistered": [ + '6.6.6.6:666', '7.7.7.7:777']}) + def test_dmaap(monkeypatch): - #test resolving dmaap key - config = {"TODAY IS YOUR LUCKY DAY" : "<>"} - #does not match - test_bind = client.resolve_override(config, dmaap={"XX" : "ABSOLVEME"}) #XX != XXX - assert(test_bind == {"TODAY IS YOUR LUCKY DAY" : {}}) - #matches - test_bind_2 = client.resolve_override(config, dmaap={"XXX" : "ABSOLVEME"}) - assert(test_bind_2 == {"TODAY IS YOUR LUCKY DAY" : "ABSOLVEME"}) - -expected_config = { - "deep" : { - "ALL YOUR SERVICE BELONG TO US" : ['6.6.6.6:666', '7.7.7.7:777', '5.5.5.5:555', '5.5.5.5:555']}, - "doubledeep" : { - "sodeep" : {"hello" : "darkness"}} - } + # test resolving dmaap key + config = {"TODAY IS YOUR LUCKY DAY": "<>"} + # does not match + test_bind = client.resolve_override( + config, dmaap={"XX": "ABSOLVEME"}) # XX != XXX + assert(test_bind == {"TODAY IS YOUR LUCKY DAY": {}}) + # matches + test_bind_2 = client.resolve_override(config, dmaap={"XXX": "ABSOLVEME"}) + assert(test_bind_2 == {"TODAY IS YOUR LUCKY DAY": "ABSOLVEME"}) + + +expected_config = {"deep": {"ALL YOUR SERVICE BELONG TO US": ['6.6.6.6:666', '7.7.7.7:777', '5.5.5.5:555', '5.5.5.5:555']}, + "doubledeep": {"sodeep": {"hello": "darkness"}}} + def test_both(monkeypatch): - #test rels and http - monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', monkeyed_get_connection_info_from_consul) - test_rels = ["testing_alpha.somedomain.com", "testing_bravo.somedomain.com", "testing_charlie.somedomain.com", "testing_charlie.somedomain.com"] - test_dmaap = {"WHO?" : "darkness"} - config = { - "deep" : { - "ALL YOUR SERVICE BELONG TO US" : "{{alpha,bravo,charlie}}"}, - "doubledeep" : { - "sodeep" : {"hello" : "<>"}} - } + # test rels and http + monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', + monkeyed_get_connection_info_from_consul) + test_rels = ["testing_alpha.somedomain.com", "testing_bravo.somedomain.com", + "testing_charlie.somedomain.com", "testing_charlie.somedomain.com"] + test_dmaap = {"WHO?": "darkness"} + config = {"deep": {"ALL YOUR SERVICE BELONG TO US": "{{alpha,bravo,charlie}}"}, + "doubledeep": {"sodeep": {"hello": "<>"}}} test_bind_1 = client.resolve_override(config, test_rels, test_dmaap) assert(test_bind_1 == expected_config) def test_resolve_all(monkeypatch): monkeypatch.setattr('requests.put', monkeyed_requests_put) - allk = client.resolve_all("test_service_component_name.unknown.unknown.unknown.dcae.onap.org") + allk = client.resolve_all( + "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") withstuff = {'config': {'my': 'amazing config'}, - 'dti': {'my': 'dti'}, - 'policies': {'items': [{'policyName': 'DCAE_alex.Config_MS_alex_microservice.132.xml', 'policyConfigMessage': 'Config Retrieved! ', 'responseAttributes': {}, 'policyConfigStatus': 'CONFIG_RETRIEVED', 'matchingConditions': {'ONAPName': 'DCAE', 'Name': 'DCAE', 'ConfigName': 'alex_config_name'}, 'config': {'policyScope': 'alex_policy_scope', 'configName': 'alex_config_name', 'description': 'test DCAE policy-handler', 'service': 'alex_service', 'policyName': 'alex_policy_name', 'riskLevel': '3', 'key1': 'value1', 'policy_hello': 'world!', 'content': {'foo': 'microservice3', 'foo_updated': '2018-01-30T13:25:33.222Z'}, 'riskType': '1712_ETE', 'guard': 'False', 'version': '0.0.1', 'location': 'Central', 'policy_updated_ts': '2018-02-19T15:09:55.217Z', 'updated_policy_id': 'DCAE_alex.Config_MS_alex_microservice', 'policy_updated_to_ver': '132', 'priority': '4', 'policy_updated_from_ver': '131', 'templateVersion': '2', 'uuid': '5e87d7c5-0daf-4b6b-ab92-5365cf5db1ef'}, 'property': None, 'type': 'JSON', 'policyVersion': '132'}, {'policyName': 'DCAE_alex.Config_db_client_policy_id_value.133.xml', 'policyConfigMessage': 'Config Retrieved! ', 'responseAttributes': {}, 'policyConfigStatus': 'CONFIG_RETRIEVED', 'matchingConditions': {'ONAPName': 'DCAE', 'Name': 'DCAE', 'ConfigName': 'alex_config_name'}, 'config': {'db_client_ts': '2017-11-21T12:12:13.696Z', 'db_client': 'ipsum', 'policy_hello': 'world!', 'policy_updated_from_ver': '132', 'updated_policy_id': 'DCAE_alex.Config_db_client_policy_id_value', 'policy_updated_ts': '2018-02-19T15:09:55.812Z', 'policy_updated_to_ver': '133'}, 'property': None, 'type': 'JSON', 'policyVersion': '133'}], 'event': {'action': 'gathered', 'timestamp': '2018-02-19 15:36:44.877380', 'update_id': 'bb73c20a-5ff8-450f-8223-da6720ade267', 'policies_count': 2}} - } + 'dti': {'my': 'dti'}, + 'policies': {'items': [{'policyName': 'DCAE_alex.Config_MS_alex_microservice.132.xml', 'policyConfigMessage': 'Config Retrieved! ', 'responseAttributes': {}, 'policyConfigStatus': 'CONFIG_RETRIEVED', 'matchingConditions': {'ONAPName': 'DCAE', 'Name': 'DCAE', 'ConfigName': 'alex_config_name'}, 'config': {'policyScope': 'alex_policy_scope', 'configName': 'alex_config_name', 'description': 'test DCAE policy-handler', 'service': 'alex_service', 'policyName': 'alex_policy_name', 'riskLevel': '3', 'key1': 'value1', 'policy_hello': 'world!', 'content': {'foo': 'microservice3', 'foo_updated': '2018-01-30T13:25:33.222Z'}, 'riskType': '1712_ETE', 'guard': 'False', 'version': '0.0.1', 'location': 'Central', 'policy_updated_ts': '2018-02-19T15:09:55.217Z', 'updated_policy_id': 'DCAE_alex.Config_MS_alex_microservice', 'policy_updated_to_ver': '132', 'priority': '4', 'policy_updated_from_ver': '131', 'templateVersion': '2', 'uuid': '5e87d7c5-0daf-4b6b-ab92-5365cf5db1ef'}, 'property': None, 'type': 'JSON', 'policyVersion': '132'}, {'policyName': 'DCAE_alex.Config_db_client_policy_id_value.133.xml', 'policyConfigMessage': 'Config Retrieved! ', 'responseAttributes': {}, 'policyConfigStatus': 'CONFIG_RETRIEVED', 'matchingConditions': {'ONAPName': 'DCAE', 'Name': 'DCAE', 'ConfigName': 'alex_config_name'}, 'config': {'db_client_ts': '2017-11-21T12:12:13.696Z', 'db_client': 'ipsum', 'policy_hello': 'world!', 'policy_updated_from_ver': '132', 'updated_policy_id': 'DCAE_alex.Config_db_client_policy_id_value', 'policy_updated_ts': '2018-02-19T15:09:55.812Z', 'policy_updated_to_ver': '133'}, 'property': None, 'type': 'JSON', 'policyVersion': '133'}], 'event': {'action': 'gathered', 'timestamp': '2018-02-19 15:36:44.877380', 'update_id': 'bb73c20a-5ff8-450f-8223-da6720ade267', 'policies_count': 2}}} assert allk == withstuff - monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', monkeyed_get_connection_info_from_consul) + monkeypatch.setattr('config_binding_service.client._get_connection_info_from_consul', + monkeyed_get_connection_info_from_consul) allk = client.resolve_all("test_resolve_scn") - print(allk) - assert allk == {"config" : expected_config} + assert allk == {"config": expected_config} - R = controller.bind_all("test_service_component_name.unknown.unknown.unknown.dcae.onap.org") + R = controller.bind_all( + "test_service_component_name.unknown.unknown.unknown.dcae.onap.org") assert(json.loads(R.data) == withstuff) assert(R.status_code == 200) R = controller.bind_all("test_resolve_scn") - assert(json.loads(R.data) == {"config" : expected_config}) + assert(json.loads(R.data) == {"config": expected_config}) assert(R.status_code == 200) R = controller.bind_all("scn_NOTexists") @@ -278,4 +315,3 @@ def test_resolve_all(monkeypatch): R = controller.bind_all("asdfasdf") assert(R.status_code == 500) - diff --git a/tox-local.ini b/tox-local.ini index 52b7916..8a309b1 100644 --- a/tox-local.ini +++ b/tox-local.ini @@ -1,6 +1,6 @@ # content of: tox.ini , put in same dir as setup.py [tox] -envlist = py36 +envlist = py36,flake8 [testenv] deps= @@ -12,3 +12,13 @@ setenv = HOSTNAME = config_binding_service commands= pytest --verbose --cov config_binding_service --cov-report=html + +[testenv:flake8] +basepython = python3.6 +skip_install = true +deps = flake8 +commands = flake8 setup.py config_binding_service tests + +[flake8] +ignore = E501,E265,E262,E261 + diff --git a/tox.ini b/tox.ini index 418de62..a14f591 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ # content of: tox.ini , put in same dir as setup.py [tox] -envlist = py36 +envlist = py36,flake8 [testenv] deps= @@ -14,3 +14,13 @@ setenv = commands= pytest --junitxml xunit-results.xml --cov config_binding_service --cov-report xml coverage xml + +[testenv:flake8] +basepython = python3.6 +skip_install = true +deps = flake8 +commands = flake8 setup.py config_binding_service tests + +[flake8] +ignore = E501 + -- cgit 1.2.3-korg