aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_binding.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_binding.py')
-rw-r--r--tests/test_binding.py145
1 files changed, 70 insertions, 75 deletions
diff --git a/tests/test_binding.py b/tests/test_binding.py
index 0e5d05a..4a7dd50 100644
--- a/tests/test_binding.py
+++ b/tests/test_binding.py
@@ -17,10 +17,12 @@
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
from config_binding_service import client, controller
+from config_binding_service import get_consul_uri
import pytest
import json
from requests.exceptions import HTTPError, RequestException
from requests import Response
+import base64
#####
# MONKEYPATCHES
@@ -47,85 +49,50 @@ class FakeResponse():
def __init__(self, status_code, text):
self.text = text
self.status_code = status_code
-
-def monkeyed_consul_get(k):
- if k == "dti_exists_test:dti":
- return {"foo" : "bar"}
- elif k == "dti_NOTexists_test:dti":
- raise HTTPError(response = FakeResponse(text= "", status_code = 404))
- elif k == "policies_exists_test:policies":
- return {"foo2" : "bar2"}
- elif k == "policies_NOTexists_test:policies":
- raise HTTPError(response = FakeResponse(text= "", status_code = 404))
- else:
- raise Exception("BLOW UP IN FIRE")
-
-def monkeyed_resolve(k):
- if k == "scn_NOTexists":
- raise client.CantGetConfig(code = 404, response = "")
- elif k == "scn_exists":
- return {"foo3" : "bar3"}
- elif k == "scn_exists:rel":
- return ["foo"]
- elif k == "scn_exists:dmaap":
- return {"foo4" : "bar4"}
-
- elif k == "scn_exists_nord":
- return {"foo5" : "bar5"}
- elif k == "scn_exists_nord:rel":
- raise HTTPError(response = FakeResponse(text= "", status_code = 404))
- elif k == "scn_exists_nord:dmaap":
- raise HTTPError(response = FakeResponse(text= "", status_code = 404))
-
- else:
- raise Exception("BLOW UP IN FIRE")
+ def raise_for_status(self):
+ if self.status_code >= 400:
+ raise HTTPError(response=FakeResponse(404,""))
+
+def monkeyed_requests_put(url, json):
+ if url == "{0}/v1/txn".format(get_consul_uri()):
+ key = json[0]["KV"]["Key"]
+ if key == "test_service_component_name.unknown.unknown.unknown.dcae.onap.org":
+ return FakeResponse(status_code=200, text = '{"Results":[{"KV":{"LockIndex":0,"Key":"test_service_component_name.unknown.unknown.unknown.dcae.onap.org","Flags":0,"Value":"eyJteSIgOiAiYW1hemluZyBjb25maWcifQ==","CreateIndex":4051555,"ModifyIndex":4051555}},{"KV":{"LockIndex":0,"Key":"test_service_component_name.unknown.unknown.unknown.dcae.onap.org:dmaap","Flags":0,"Value":"eyJmb28iIDogImJhciJ9","CreateIndex":4051571,"ModifyIndex":4051571}},{"KV":{"LockIndex":0,"Key":"test_service_component_name.unknown.unknown.unknown.dcae.onap.org:dti","Flags":0,"Value":"eyJteSIgOiAiZHRpIn0=","CreateIndex":4066524,"ModifyIndex":4066524}},{"KV":{"LockIndex":0,"Key":"test_service_component_name.unknown.unknown.unknown.dcae.onap.org:policies/event","Flags":0,"Value":"eyJhY3Rpb24iOiAiZ2F0aGVyZWQiLCAidGltZXN0YW1wIjogIjIwMTgtMDItMTkgMTU6MzY6NDQuODc3MzgwIiwgInVwZGF0ZV9pZCI6ICJiYjczYzIwYS01ZmY4LTQ1MGYtODIyMy1kYTY3MjBhZGUyNjciLCAicG9saWNpZXNfY291bnQiOiAyfQ==","CreateIndex":4048564,"ModifyIndex":4048564}},{"KV":{"LockIndex":0,"Key":"test_service_component_name.unknown.unknown.unknown.dcae.onap.org:policies/items/DCAE_alex.Config_MS_alex_microservice","Flags":0,"Value":"eyJwb2xpY3lOYW1lIjogIkRDQUVfYWxleC5Db25maWdfTVNfYWxleF9taWNyb3NlcnZpY2UuMTMyLnhtbCIsICJwb2xpY3lDb25maWdNZXNzYWdlIjogIkNvbmZpZyBSZXRyaWV2ZWQhICIsICJyZXNwb25zZUF0dHJpYnV0ZXMiOiB7fSwgInBvbGljeUNvbmZpZ1N0YXR1cyI6ICJDT05GSUdfUkVUUklFVkVEIiwgIm1hdGNoaW5nQ29uZGl0aW9ucyI6IHsiT05BUE5hbWUiOiAiRENBRSIsICJOYW1lIjogIkRDQUUiLCAiQ29uZmlnTmFtZSI6ICJhbGV4X2NvbmZpZ19uYW1lIn0sICJjb25maWciOiB7InBvbGljeVNjb3BlIjogImFsZXhfcG9saWN5X3Njb3BlIiwgImNvbmZpZ05hbWUiOiAiYWxleF9jb25maWdfbmFtZSIsICJkZXNjcmlwdGlvbiI6ICJ0ZXN0IERDQUUgcG9saWN5LWhhbmRsZXIiLCAic2VydmljZSI6ICJhbGV4X3NlcnZpY2UiLCAicG9saWN5TmFtZSI6ICJhbGV4X3BvbGljeV9uYW1lIiwgInJpc2tMZXZlbCI6ICIzIiwgImtleTEiOiAidmFsdWUxIiwgInBvbGljeV9oZWxsbyI6ICJ3b3JsZCEiLCAiY29udGVudCI6IHsiZm9vIjogIm1pY3Jvc2VydmljZTMiLCAiZm9vX3VwZGF0ZWQiOiAiMjAxOC0wMS0zMFQxMzoyNTozMy4yMjJaIn0sICJyaXNrVHlwZSI6ICIxNzEyX0VURSIsICJndWFyZCI6ICJGYWxzZSIsICJ2ZXJzaW9uIjogIjAuMC4xIiwgImxvY2F0aW9uIjogIkNlbnRyYWwiLCAicG9saWN5X3VwZGF0ZWRfdHMiOiAiMjAxOC0wMi0xOVQxNTowOTo1NS4yMTdaIiwgInVwZGF0ZWRfcG9saWN5X2lkIjogIkRDQUVfYWxleC5Db25maWdfTVNfYWxleF9taWNyb3NlcnZpY2UiLCAicG9saWN5X3VwZGF0ZWRfdG9fdmVyIjogIjEzMiIsICJwcmlvcml0eSI6ICI0IiwgInBvbGljeV91cGRhdGVkX2Zyb21fdmVyIjogIjEzMSIsICJ0ZW1wbGF0ZVZlcnNpb24iOiAiMiIsICJ1dWlkIjogIjVlODdkN2M1LTBkYWYtNGI2Yi1hYjkyLTUzNjVjZjVkYjFlZiJ9LCAicHJvcGVydHkiOiBudWxsLCAidHlwZSI6ICJKU09OIiwgInBvbGljeVZlcnNpb24iOiAiMTMyIn0=","CreateIndex":4048564,"ModifyIndex":4065574}},{"KV":{"LockIndex":0,"Key":"test_service_component_name.unknown.unknown.unknown.dcae.onap.org:policies/items/DCAE_alex.Config_db_client_policy_id_value","Flags":0,"Value":"eyJwb2xpY3lOYW1lIjogIkRDQUVfYWxleC5Db25maWdfZGJfY2xpZW50X3BvbGljeV9pZF92YWx1ZS4xMzMueG1sIiwgInBvbGljeUNvbmZpZ01lc3NhZ2UiOiAiQ29uZmlnIFJldHJpZXZlZCEgIiwgInJlc3BvbnNlQXR0cmlidXRlcyI6IHt9LCAicG9saWN5Q29uZmlnU3RhdHVzIjogIkNPTkZJR19SRVRSSUVWRUQiLCAibWF0Y2hpbmdDb25kaXRpb25zIjogeyJPTkFQTmFtZSI6ICJEQ0FFIiwgIk5hbWUiOiAiRENBRSIsICJDb25maWdOYW1lIjogImFsZXhfY29uZmlnX25hbWUifSwgImNvbmZpZyI6IHsiZGJfY2xpZW50X3RzIjogIjIwMTctMTEtMjFUMTI6MTI6MTMuNjk2WiIsICJkYl9jbGllbnQiOiAiaXBzdW0iLCAicG9saWN5X2hlbGxvIjogIndvcmxkISIsICJwb2xpY3lfdXBkYXRlZF9mcm9tX3ZlciI6ICIxMzIiLCAidXBkYXRlZF9wb2xpY3lfaWQiOiAiRENBRV9hbGV4LkNvbmZpZ19kYl9jbGllbnRfcG9saWN5X2lkX3ZhbHVlIiwgInBvbGljeV91cGRhdGVkX3RzIjogIjIwMTgtMDItMTlUMTU6MDk6NTUuODEyWiIsICJwb2xpY3lfdXBkYXRlZF90b192ZXIiOiAiMTMzIn0sICJwcm9wZXJ0eSI6IG51bGwsICJ0eXBlIjogIkpTT04iLCAicG9saWN5VmVyc2lvbiI6ICIxMzMifQ==","CreateIndex":4048564,"ModifyIndex":4065570}},{"KV":{"LockIndex":0,"Key":"test_service_component_name.unknown.unknown.unknown.dcae.onap.org:rels","Flags":0,"Value":"WyJteS5hbWF6aW5nLnJlbGF0aW9uc2hpcCJd","CreateIndex":4051567,"ModifyIndex":4051567}}],"Errors":null,"Index":0,"LastContact":0,"KnownLeader":true}')
+ elif key == "scn_exists":
+ return FakeResponse(status_code = 200, text = '{"Results":[{"KV":{"LockIndex":0,"Key":"scn_exists","Flags":0,"Value":"eyJmb28zIiA6ICJiYXIzIn0=","CreateIndex":4067403,"ModifyIndex":4067403}},{"KV":{"LockIndex":0,"Key":"scn_exists:dmaap","Flags":0,"Value":"eyJmb280IiA6ICJiYXI0In0=","CreateIndex":4067410,"ModifyIndex":4067410}},{"KV":{"LockIndex":0,"Key":"scn_exists:rels","Flags":0,"Value":"WyJmb28iXQ==","CreateIndex":4067406,"ModifyIndex":4067406}},{"KV":{"LockIndex":0,"Key":"scn_exists_nord","Flags":0,"Value":"eyJmb281IiA6ICJiYXI1In0=","CreateIndex":4067340,"ModifyIndex":4067340}}],"Errors":null,"Index":0,"LastContact":0,"KnownLeader":true}')
+ elif key == "scn_exists_nord":
+ return FakeResponse(status_code = 200, text = '{"Results":[{"KV":{"LockIndex":0,"Key":"scn_exists_nord","Flags":0,"Value":"eyJmb281IiA6ICJiYXI1In0=","CreateIndex":4067340,"ModifyIndex":4067340}}],"Errors":null,"Index":0,"LastContact":0,"KnownLeader":true}')
+ elif key == "test_resolve_scn":
+ return FakeResponse(status_code = 200, text = '{"Results":[{"KV":{"LockIndex":0,"Key":"test_resolve_scn","Flags":0,"Value":"ewogICAgICAgICAgICAgICAgImRlZXAiIDogewogICAgICAgICAgICAgICAgICAgICJBTEwgWU9VUiBTRVJWSUNFIEJFTE9ORyBUTyBVUyIgOiAie3thbHBoYSxicmF2byxjaGFybGllfX0ifSwKICAgICAgICAgICAgICAgICJkb3VibGVkZWVwIiA6ICB7CiAgICAgICAgICAgICAgICAgICAgInNvZGVlcCIgOiB7ImhlbGxvIiA6ICI8PFdITz8+PiJ9fQogICAgICAgICAgICAgfQo=","CreateIndex":4068002,"ModifyIndex":4068002}},{"KV":{"LockIndex":0,"Key":"test_resolve_scn:dmaap","Flags":0,"Value":"eyJXSE8/IiA6ICJkYXJrbmVzcyJ9","CreateIndex":4068013,"ModifyIndex":4068013}},{"KV":{"LockIndex":0,"Key":"test_resolve_scn:rels","Flags":0,"Value":"WyJ0ZXN0aW5nX2FscGhhLnNvbWVkb21haW4uY29tIiwgInRlc3RpbmdfYnJhdm8uc29tZWRvbWFpbi5jb20iLCAidGVzdGluZ19jaGFybGllLnNvbWVkb21haW4uY29tIiwgInRlc3RpbmdfY2hhcmxpZS5zb21lZG9tYWluLmNvbSJd","CreateIndex":4068010,"ModifyIndex":4068010}}],"Errors":null,"Index":0,"LastContact":0,"KnownLeader":true}')
+ elif key == "scn_NOTexists":
+ return FakeResponse(status_code=404, text="")
#######
# TESTS
#######
+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")
+ 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'},
+ 'test_service_component_name.unknown.unknown.unknown.dcae.onap.org:dmaap': {'foo': 'bar'},
+ 'test_service_component_name.unknown.unknown.unknown.dcae.onap.org:policies/event': {'action': 'gathered', 'timestamp': '2018-02-19 15:36:44.877380', 'update_id': 'bb73c20a-5ff8-450f-8223-da6720ade267', 'policies_count': 2},
+ 'test_service_component_name.unknown.unknown.unknown.dcae.onap.org:policies/items/DCAE_alex.Config_MS_alex_microservice': {'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'},
+ 'test_service_component_name.unknown.unknown.unknown.dcae.onap.org:policies/items/DCAE_alex.Config_db_client_policy_id_value': {'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'},
+ 'test_service_component_name.unknown.unknown.unknown.dcae.onap.org:rels': ['my.amazing.relationship']
+ }
+
+def test_controller_allk():
+ pass
def test_get_config_rels_dmaap(monkeypatch):
- monkeypatch.setattr('config_binding_service.client._consul_get_key', monkeyed_resolve)
+ 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")
-def test_dti(monkeypatch):
- monkeypatch.setattr('config_binding_service.client._consul_get_key', monkeyed_consul_get)
-
- assert client.resolve_DTI("dti_exists_test") == {"foo" : "bar"}
- with pytest.raises(client.CantGetConfig):
- client.resolve_DTI("dti_NOTexists_test")
-
- R = controller.dtievents("dti_exists_test")
- assert(json.loads(R.data) == {"foo" : "bar"})
- assert(R.status_code == 200)
-
- R = controller.dtievents("dti_NOTexists_test")
- assert(R.status_code == 404)
-
- R = controller.dtievents("asdfasdf")
- assert(R.status_code == 500)
-
-
-def test_policies(monkeypatch):
- monkeypatch.setattr('config_binding_service.client._consul_get_key', monkeyed_consul_get)
-
- assert client.resolve_policies("policies_exists_test") == {"foo2" : "bar2"}
- with pytest.raises(client.CantGetConfig):
- client.resolve_policies("policies_NOTexists_test")
-
- R = controller.policies("policies_exists_test")
- assert(json.loads(R.data) == {"foo2" : "bar2"})
- assert(R.status_code == 200)
-
- R = controller.policies("policies_NOTexists_test")
- assert(R.status_code == 404)
-
- R = controller.policies("asdfasdf")
- assert(R.status_code == 500)
-
def test_bind_config_for_scn(monkeypatch):
- monkeypatch.setattr('config_binding_service.client.resolve', monkeyed_resolve)
+ monkeypatch.setattr('requests.put', monkeyed_requests_put)
assert(client.resolve("scn_exists") == {"foo3" : "bar3"})
with pytest.raises(client.CantGetConfig):
@@ -231,6 +198,12 @@ def test_dmaap(monkeypatch):
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
@@ -244,12 +217,34 @@ def test_both(monkeypatch):
"sodeep" : {"hello" : "<<WHO?>>"}}
}
test_bind_1 = client.resolve_override(config, test_rels, test_dmaap)
- 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"}}
- }
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")
+ 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}}
+ }
+ assert allk == withstuff
+
+ 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}
+
+ 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(R.status_code == 200)
+
+ R = controller.bind_all("scn_NOTexists")
+ assert(R.status_code == 404)
+
+ R = controller.bind_all("asdfasdf")
+ assert(R.status_code == 500)
+