diff options
author | Ruan HE <ruan.he@orange.com> | 2018-01-03 15:12:09 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2018-01-03 15:12:09 +0000 |
commit | ed403078a96170ca4dcb102d7357fefbe16b45c8 (patch) | |
tree | 2b50168ff4b817bd407aa82e9cd1e5ea624870c4 | |
parent | 0e678794fd2c858069fbbe0b985a0e2f064862b1 (diff) | |
parent | c7b935cd626cb93876d72f6876eda1dd5c4d5be7 (diff) |
Merge "adding more unit tests for configuration module"
5 files changed, 171 insertions, 6 deletions
diff --git a/python_moonutilities/python_moonutilities/configuration.py b/python_moonutilities/python_moonutilities/configuration.py index 9a044db7..c31432c3 100644 --- a/python_moonutilities/python_moonutilities/configuration.py +++ b/python_moonutilities/python_moonutilities/configuration.py @@ -52,10 +52,13 @@ def get_configuration(key): return {data["Key"]: json.loads(base64.b64decode(data["Value"]).decode("utf-8"))} raise exceptions.ConsulComponentContentError("error={}".format(data)) else: + for item in data: + if not all(k in item for k in ("Key", "Value")): + logger.warning("invalidate content {}".format(item)) + raise exceptions.ConsulComponentContentError("error={}".format(data)) return [ { item["Key"]: json.loads(base64.b64decode(item["Value"]).decode("utf-8")) - if all(k in item for k in ("Key", "Value")) else logger.warning("invalidate content {}".format(item)) } for item in data ] @@ -93,9 +96,12 @@ def get_plugins(): return {data["Key"].replace("plugins/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))} raise exceptions.ConsulComponentContentError("error={}".format(data)) else: + for item in data: + if not all(k in item for k in ("Key", "Value")): + logger.warning("invalidate content {}".format(item)) + raise exceptions.ConsulComponentContentError("error={}".format(data)) return { item["Key"].replace("plugins/", ""): json.loads(base64.b64decode(item["Value"]).decode("utf-8")) - if all(k in item for k in ("Key", "Value")) else logger.warning("invalidate content {}".format(item)) for item in data } @@ -112,9 +118,12 @@ def get_components(): return {data["Key"].replace("components/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))} raise exceptions.ConsulComponentContentError("error={}".format(data)) else: + for item in data: + if not all(k in item for k in ("Key", "Value")): + logger.warning("invalidate content {}".format(item)) + raise exceptions.ConsulComponentContentError("error={}".format(data)) return { item["Key"].replace("components/", ""): json.loads(base64.b64decode(item["Value"]).decode("utf-8")) - if all(k in item for k in ("Key", "Value")) else logger.warning("invalidate content {}".format(item)) for item in data } diff --git a/python_moonutilities/python_moonutilities/exceptions.py b/python_moonutilities/python_moonutilities/exceptions.py index e6c9f6de..d85cef49 100644 --- a/python_moonutilities/python_moonutilities/exceptions.py +++ b/python_moonutilities/python_moonutilities/exceptions.py @@ -507,7 +507,7 @@ class ConsulComponentNotFound(ConsulError): class ConsulComponentContentError(ConsulError): description = _("invalid content of component .") code = 500 - title = 'Consul error' + title = 'Consul Content error' logger = "WARNING" # Containers exceptions diff --git a/python_moonutilities/tests/unit_python/mock_repo/components_utilities.py b/python_moonutilities/tests/unit_python/mock_repo/components_utilities.py index 1d79d890..53c51343 100644 --- a/python_moonutilities/tests/unit_python/mock_repo/components_utilities.py +++ b/python_moonutilities/tests/unit_python/mock_repo/components_utilities.py @@ -134,3 +134,5 @@ def get_b64_conf(component=None): return base64.b64encode( json.dumps( CONF[key1][key2]).encode('utf-8')+b"\n").decode('utf-8') + else: + return base64.b64encode(component.encode('utf-8')+b"\n").decode('utf-8') diff --git a/python_moonutilities/tests/unit_python/mock_repo/urls.py b/python_moonutilities/tests/unit_python/mock_repo/urls.py index 75b55927..ff7976da 100644 --- a/python_moonutilities/tests/unit_python/mock_repo/urls.py +++ b/python_moonutilities/tests/unit_python/mock_repo/urls.py @@ -13,6 +13,19 @@ def register_components(m): json=[{'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}] ) m.register_uri( + 'GET', 'http://consul:8500/v1/kv/plugins?recurse=true', + json=[ + { + "LockIndex": 0, + "Key": "plugins/authz", + "Flags": 0, + "Value": "eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0=", + "CreateIndex": 14, + "ModifyIndex": 656 + } + ], + ) + m.register_uri( 'PUT', 'http://consul:8500/v1/kv/components/port_start', json=[] ) diff --git a/python_moonutilities/tests/unit_python/test_configuration.py b/python_moonutilities/tests/unit_python/test_configuration.py index 8ca389bf..87ebb534 100644 --- a/python_moonutilities/tests/unit_python/test_configuration.py +++ b/python_moonutilities/tests/unit_python/test_configuration.py @@ -7,6 +7,32 @@ def test_get_configuration_success(): from python_moonutilities import configuration assert configuration.get_configuration("components/port_start")["port_start"] == comp_util.CONF["components"]["port_start"] +@requests_mock.Mocker(kw='mock') +def test_get_configuration_mutliple_list_success(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', + json=[ + {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}, + {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")} + ] + ) + + assert len(configuration.get_configuration("components/port_start")) == 2 + +@requests_mock.Mocker(kw='mock') +def test_get_configuration_mutliple_list_failure(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', + json=[ + {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}, + {'invalidKey': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")} + ] + ) + with pytest.raises(Exception) as exception_info: + configuration.get_configuration("components/port_start") + assert str(exception_info.value) == '500: Consul Content error' @requests_mock.Mocker(kw='mock') def test_get_configuration_not_found(**kwargs): @@ -27,8 +53,19 @@ def test_get_configuration_invalid_response(**kwargs): ]) with pytest.raises(Exception) as exception_info: configuration.get_configuration("components/port_start") - assert str(exception_info.value) == '500: Consul error' + assert str(exception_info.value) == '500: Consul Content error' +################################ increment_port #################################### +@requests_mock.Mocker(kw='mock') +def test_put_increment_port_invalidkey_failure(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', json=[ + {'Key': 'invalidkey', 'Value': comp_util.get_b64_conf("components/port_start")} + ], status_code=200) + with pytest.raises(Exception) as exception_info: + configuration.increment_port() + assert str(exception_info.value) == '500: Consul Content error' @requests_mock.Mocker(kw='mock') def test_put_increment_port_failure(**kwargs): @@ -48,7 +85,111 @@ def test_increment_port_success(): incremented_port = configuration.increment_port() assert incremented_port == cur_port + 1 +################################ plugin #################################### +def test_get_plugins_success(): + from python_moonutilities import configuration + plugin = configuration.get_plugins() + assert plugin is not None + +@requests_mock.Mocker(kw='mock') +def test_get_plugins_mutliple_list_success(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/plugins?recurse=true', + json=[ + {'Key': 'plugins/authz', 'Value': 'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='}, + {'Key': 'plugins/authz', 'Value': 'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='} + ] + ) + + res = configuration.get_plugins() + assert bool(res) + +@requests_mock.Mocker(kw='mock') +def test_get_plugins_mutliple_list_failure(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/plugins?recurse=true', + json=[ + {'Key': 'plugins/authz', 'Value': "eyJjb250YWluZXIiOiAid3Vrb25"}, + {'invalidKey': 'plugins/authz', 'Value': "eyJjb250YWluZXIiOiAid3Vrb25"} + ] + ) + with pytest.raises(Exception) as exception_info: + configuration.get_plugins() + assert str(exception_info.value) == '500: Consul Content error' +@requests_mock.Mocker(kw='mock') +def test_get_plugins_not_found(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/plugins?recurse=true', json=[ + ], status_code=500) + with pytest.raises(Exception) as exception_info: + configuration.get_plugins() + assert str(exception_info.value) == '400: Consul error' + +@requests_mock.Mocker(kw='mock') +def test_get_plugins_invalid_response(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/plugins?recurse=true', json=[ + {"invalidKey":'invalid', 'Value': "jb250"} + ]) + with pytest.raises(Exception) as exception_info: + configuration.get_plugins() + assert str(exception_info.value) == '500: Consul Content error' + +################################ component #################################### def test_get_components(): from python_moonutilities import configuration - assert isinstance(configuration.get_components(), dict)
\ No newline at end of file + assert isinstance(configuration.get_components(), dict) + +@requests_mock.Mocker(kw='mock') +def test_get_components_mutliple_list_success(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true', + json=[ + {'Key': 'components/c1', 'Value': 'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='}, + {'Key': 'components/c2', 'Value': 'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='} + ] + ) + + res = configuration.get_components() + assert bool(res) + +@requests_mock.Mocker(kw='mock') +def test_get_components_mutliple_list_failure(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true', + json=[ + {'Key': 'components/c1', 'Value': "eyJjb250YWluZXIiOiAid3Vrb25"}, + {'invalidKey': 'components/c2', 'Value': "eyJjb250YWluZXIiOiAid3Vrb25"} + ] + ) + with pytest.raises(Exception) as exception_info: + configuration.get_components() + assert str(exception_info.value) == '500: Consul Content error' + +@requests_mock.Mocker(kw='mock') +def test_get_components_not_found(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true', json=[ + ], status_code=500) + with pytest.raises(Exception) as exception_info: + configuration.get_components() + assert str(exception_info.value) == '400: Consul error' + +@requests_mock.Mocker(kw='mock') +def test_get_components_invalid_response(**kwargs): + from python_moonutilities import configuration + + kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true', json=[ + {"invalidKey":'invalid', 'Value': "jb250"} + ]) + with pytest.raises(Exception) as exception_info: + configuration.get_components() + assert str(exception_info.value) == '500: Consul Content error' |