From c7b935cd626cb93876d72f6876eda1dd5c4d5be7 Mon Sep 17 00:00:00 2001 From: sgdt6900 Date: Wed, 3 Jan 2018 16:54:18 +0200 Subject: adding more unit tests for configuration module undo deleted exception by mistake Change-Id: I6c0246a938035f5542634e33311ad4b02973f67b Signed-off-by: sgdt6900 --- .../unit_python/mock_repo/components_utilities.py | 2 + .../tests/unit_python/mock_repo/urls.py | 13 ++ .../tests/unit_python/test_configuration.py | 145 ++++++++++++++++++++- 3 files changed, 158 insertions(+), 2 deletions(-) (limited to 'python_moonutilities/tests') 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 @@ -12,6 +12,19 @@ def register_components(m): 'GET', 'http://consul:8500/v1/kv/components/port_start', 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' -- cgit 1.2.3-korg