From 1002e423a67cc4c22846ffc333b8027230e55dcb Mon Sep 17 00:00:00 2001 From: Edyta Krukowska Date: Wed, 12 May 2021 12:00:07 +0200 Subject: Add env service for configuration Issue-ID: DCAEGEN2-2630 Signed-off-by: Edyta Krukowska Change-Id: I86b4a84fd49bfe41afd39a9a2096d72d8263e63a --- onap-dcae-cbs-docker-client/tests/conftest.py | 76 ++++++++++++++++++++++++ onap-dcae-cbs-docker-client/tests/test_client.py | 36 +++++++++++ 2 files changed, 112 insertions(+) (limited to 'onap-dcae-cbs-docker-client/tests') diff --git a/onap-dcae-cbs-docker-client/tests/conftest.py b/onap-dcae-cbs-docker-client/tests/conftest.py index b927412..0f34ff1 100644 --- a/onap-dcae-cbs-docker-client/tests/conftest.py +++ b/onap-dcae-cbs-docker-client/tests/conftest.py @@ -1,5 +1,6 @@ # ================================================================================ # Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. +# Copyright (C) 2021 Nokia. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -44,6 +45,30 @@ good_resp_all = FakeResponse( ) good_resp_conf = FakeResponse(status_code=200, thejson={"key_to_your_heart": 666}) +good_resp_conf_env = FakeResponse(status_code=200, thejson={"key_to_your_heart": "${TEST_ENV}"}) + +good_resp_all_env = FakeResponse( + status_code=200, + thejson={ + "config": {"key_to_your_heart": "${TEST_ENV}"}, + "dti": {"some amazing": "dti stuff"}, + "policies": {"event": {"foo": "bar"}, "items": [{"foo2": "bar2"}]}, + "otherkey": {"foo3": "bar3"}, + }, +) + +good_resp_conf_wrong_env = FakeResponse(status_code=200, thejson={"key_to_your_heart": "${WRONG_TEST_ENV}"}) + +good_resp_all_wrong_env = FakeResponse( + status_code=200, + thejson={ + "config": {"key_to_your_heart": "${WRONG_TEST_ENV}"}, + "dti": {"some amazing": "dti stuff"}, + "policies": {"event": {"foo": "bar"}, "items": [{"foo2": "bar2"}]}, + "otherkey": {"foo3": "bar3"}, + }, +) + @pytest.fixture def monkeyed_requests_get(): @@ -79,6 +104,57 @@ def monkeyed_requests_get_https(): return _monkeyed_requests_get_https +@pytest.fixture +def monkeyed_requests_get_with_env(): + """ + mock for the CBS get + """ + + def _monkeyed_requests_get(url): + if url == "http://config-binding-service:10000/service_component_all/testhostname": + return good_resp_all_env + elif url == "http://config-binding-service:10000/service_component/testhostname": + return good_resp_conf_env + else: + raise Exception("Unexpected URL {0}!".format(url)) + + return _monkeyed_requests_get + + +@pytest.fixture +def monkeyed_requests_get_https_env(): + """ + mock for the CBS get + """ + + def _monkeyed_requests_get_https(url, verify=""): + if url == "https://config-binding-service:10443/service_component_all/testhostname": + return good_resp_all_env + elif url == "https://config-binding-service:10443/service_component/testhostname": + return good_resp_conf_env + else: + raise Exception("Unexpected URL {0}!".format(url)) + + return _monkeyed_requests_get_https + + +@pytest.fixture +def monkeyed_requests_get_http_with_wrong_env(): + """ + mock for the CBS get + """ + + def _monkeyed_requests_get(url): + if url == "http://config-binding-service:10000/service_component_all/testhostname": + return good_resp_all_wrong_env + elif url == "http://config-binding-service:10000/service_component/testhostname": + return good_resp_conf_wrong_env + else: + raise Exception("Unexpected URL {0}!".format(url)) + + return _monkeyed_requests_get + + @pytest.fixture def monkeyed_requests_get_404(): def _monkeyed_requests_get_404(url): diff --git a/onap-dcae-cbs-docker-client/tests/test_client.py b/onap-dcae-cbs-docker-client/tests/test_client.py index 132ab33..3b10923 100644 --- a/onap-dcae-cbs-docker-client/tests/test_client.py +++ b/onap-dcae-cbs-docker-client/tests/test_client.py @@ -1,5 +1,6 @@ # ================================================================================ # Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. +# Copyright (C) 2021 Nokia. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -77,3 +78,38 @@ def test_badenv(monkeypatch): get_config() with pytest.raises(ENVsMissing): get_all() + + +def test_http_with_env(monkeypatch, monkeyed_requests_get_with_env): + monkeypatch.setattr("requests.get", monkeyed_requests_get_with_env) + + assert get_config() == {"key_to_your_heart": "test_env"} + + assert get_all() == { + "config": {"key_to_your_heart": "test_env"}, + "dti": {"some amazing": "dti stuff"}, + "policies": {"event": {"foo": "bar"}, "items": [{"foo2": "bar2"}]}, + "otherkey": {"foo3": "bar3"}, + } + + +def test_https_with_env(monkeypatch, monkeyed_requests_get_https_env): + monkeypatch.setattr("requests.get", monkeyed_requests_get_https_env) + monkeypatch.setenv("DCAE_CA_CERTPATH", "1") + + assert get_config() == {"key_to_your_heart": "test_env"} + + assert get_all() == { + "config": {"key_to_your_heart": "test_env"}, + "dti": {"some amazing": "dti stuff"}, + "policies": {"event": {"foo": "bar"}, "items": [{"foo2": "bar2"}]}, + "otherkey": {"foo3": "bar3"}, + } + + +def test_http_with_wrong_env(monkeypatch, monkeyed_requests_get_http_with_wrong_env): + monkeypatch.setattr("requests.get", monkeyed_requests_get_http_with_wrong_env) + with pytest.raises(ENVsMissing): + get_config() + with pytest.raises(ENVsMissing): + get_all() -- cgit 1.2.3-korg