summaryrefslogtreecommitdiffstats
path: root/dmaap/tests
diff options
context:
space:
mode:
Diffstat (limited to 'dmaap/tests')
-rw-r--r--dmaap/tests/conftest.py88
-rw-r--r--dmaap/tests/test_consulif.py72
-rw-r--r--dmaap/tests/test_dmaapcontrollerif.py113
-rw-r--r--dmaap/tests/test_dr_lifecycle.py65
-rw-r--r--dmaap/tests/test_mr_lifecycle.py59
-rw-r--r--dmaap/tests/test_plugin.py26
-rw-r--r--dmaap/tests/test_utils.py26
7 files changed, 449 insertions, 0 deletions
diff --git a/dmaap/tests/conftest.py b/dmaap/tests/conftest.py
new file mode 100644
index 0000000..9ae7b40
--- /dev/null
+++ b/dmaap/tests/conftest.py
@@ -0,0 +1,88 @@
+# ============LICENSE_START=======================================================
+# org.onap.dcae
+# ================================================================================
+# Copyright (c) 2018-2020 AT&T Intellectual Property. 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+#
+
+import pytest
+
+import requests
+
+@pytest.fixture()
+def mockconsul(monkeypatch):
+ """ Override the regular Consul interface"""
+ def fake_get_config(self, key):
+ config={'dmaap': {
+ 'username': 'testuser@dmaaptest.example.com',
+ 'url': 'https://dmaaptest.example.com:8443/webapi',
+ 'password' : 'testpassword',
+ 'owner': 'dcaeorch'
+ }}
+ return config
+
+ def fake_get_service(self, service_name):
+ service_address = "myAddress"
+ service_port= "8443"
+ return service_address, service_port
+
+ def fake_add_to_entry(self, key, add_name, add_value):
+ return True
+
+ def fake_delete_entry(self, entry_name):
+ return True
+
+ def fake_init(self, api_url, user, password, logger):
+ pass
+
+ from consulif.consulif import ConsulHandle
+ monkeypatch.setattr(ConsulHandle, 'get_config', fake_get_config)
+ monkeypatch.setattr(ConsulHandle, 'get_service', fake_get_service)
+ monkeypatch.setattr(ConsulHandle, 'add_to_entry', fake_add_to_entry)
+ monkeypatch.setattr(ConsulHandle, 'delete_entry', fake_delete_entry)
+ monkeypatch.setattr(ConsulHandle, '__init__', fake_init)
+
+ def get_handle():
+ return ConsulHandle('mockconsul', None, None, None)
+ return get_handle
+
+
+@pytest.fixture()
+def mockdmaapbc(monkeypatch):
+
+ def fake_get(url, auth):
+ # print "fake_get: {0}, {1}".format(url, auth)
+ r = requests.Response()
+ r.status_code = 200
+ return r
+ def fake_post(url, auth, json):
+ # print "fake_post: {0}, {1}, {2}".format(url, auth, json)
+ r = requests.Response()
+ r.status_code = 200
+ return r
+ def fake_delete(url, auth):
+ # print "fake_delete: {0}, {1}".format(url, auth)
+ r = requests.Response()
+ r.status_code = 200
+ return r
+ def fake_json(self):
+ return {"fqtn":"test_fqtn"}
+
+ import requests
+ monkeypatch.setattr(requests.Response, "json", fake_json)
+ monkeypatch.setattr(requests, "get", fake_get)
+ monkeypatch.setattr(requests, "post", fake_post)
+ monkeypatch.setattr(requests, "delete", fake_delete)
+
diff --git a/dmaap/tests/test_consulif.py b/dmaap/tests/test_consulif.py
new file mode 100644
index 0000000..a45c6a4
--- /dev/null
+++ b/dmaap/tests/test_consulif.py
@@ -0,0 +1,72 @@
+# ============LICENSE_START=======================================================
+# org.onap.dcae
+# ================================================================================
+# Copyright (c) 2017-2020 AT&T Intellectual Property. 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+#
+
+
+import pytest
+from cloudify.exceptions import NonRecoverableError
+import os
+from consulif.consulif import ConsulHandle
+
+
+# No connections are actually made to this host
+CONSUL_HOST = "consul" # Should always be a local consul agent on Cloudify Manager
+#CONSUL_PORT = '8510'
+CONSUL_PORT = '8500'
+DBCL_KEY_NAME = "dmaap_dbcl_info" # Consul key containing DMaaP data bus credentials
+DBC_SERVICE_NAME= "dmaap_bus_controller" # Name under which the DMaaP bus controller is registered
+
+
+def test_get_config_service(mockconsul):
+ err_msg = "Error getting ConsulHandle when configuring dmaap plugin: {0}"
+ _ch = ConsulHandle("http://{0}:{1}".format(CONSUL_HOST, CONSUL_PORT), None, None, None)
+
+ config = _ch.get_config(DBCL_KEY_NAME)
+
+ DMAAP_USER = config['dmaap']['username']
+ DMAAP_PASS = config['dmaap']['password']
+ DMAAP_OWNER = config['dmaap']['owner']
+
+ if 'protocol' in config['dmaap']:
+ DMAAP_PROTOCOL = config['dmaap']['protocol']
+ else:
+ DMAAP_PROTOCOL = 'https' # Default to https (service discovery should give us this but doesn't
+
+ if 'path' in config['dmaap']:
+ DMAAP_PATH = config['dmaap']['path']
+ else:
+ DMAAP_PATH = 'webapi' # Should come from service discovery but Consul doesn't support it
+
+ service_address, service_port = _ch.get_service(DBC_SERVICE_NAME)
+
+ DMAAP_API_URL = '{0}://{1}:{2}/{3}'.format(DMAAP_PROTOCOL, service_address, service_port, DMAAP_PATH)
+
+
+def test_add_entry(mockconsul):
+ _ch = ConsulHandle("http://{0}:{1}".format(CONSUL_HOST, CONSUL_PORT), None, None, None)
+
+ key = 'DMAAP_TEST'
+ name = 'dmaap_test_name'
+ value = 'dmaap_test_value'
+ _ch.add_to_entry(key, name, value)
+
+ name = "dmaap_test_name_2"
+ value = 'dmaap_test_value_2'
+ _ch.add_to_entry(key, name, value)
+
+ _ch.delete_entry(key)
diff --git a/dmaap/tests/test_dmaapcontrollerif.py b/dmaap/tests/test_dmaapcontrollerif.py
new file mode 100644
index 0000000..25ddb88
--- /dev/null
+++ b/dmaap/tests/test_dmaapcontrollerif.py
@@ -0,0 +1,113 @@
+# ============LICENSE_START=======================================================
+# org.onap.dcae
+# ================================================================================
+# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2020 Pantheon.tech. 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+#
+
+import pytest
+import requests
+from cloudify.mocks import MockCloudifyContext
+from cloudify.state import current_ctx
+from cloudify import ctx
+from cloudify.decorators import operation
+from cloudify.exceptions import NonRecoverableError
+
+
+import test_consulif
+from dmaapcontrollerif.dmaap_requests import DMaaPControllerHandle
+
+import logging
+logger = logging.getLogger("test_mr_lifecycle")
+
+_goodosv2 = {
+ 'auth_url': 'https://example.com/identity/v2.0',
+ 'password': 'pw',
+ 'region': 'r',
+ 'tenant_name': 'tn',
+ 'username': 'un'
+}
+
+
+def test_dmaapc (monkeypatch, mockconsul, mockdmaapbc):
+ from dmaapplugin.dmaaputils import random_string
+
+ config = mockconsul().get_config('mockkey')['dmaap']
+ DMAAP_API_URL = config['url']
+ DMAAP_USER = config['username']
+ DMAAP_PASS = config['password']
+ DMAAP_OWNER = config['owner']
+
+ properties = {'fqdn': 'a.x.example.com', 'openstack': _goodosv2 }
+ mock_ctx = MockCloudifyContext(
+ node_id='test_node_id',
+ node_name='test_node_name',
+ properties=properties,
+ runtime_properties = {
+ "admin": { "user": "admin_user" },
+ "user": { "user": "user_user" },
+ "viewer": { "user": "viewer_user" }
+ })
+
+ current_ctx.set(mock_ctx)
+
+ kwargs = { "topic_name": "ONAP_test",
+ "topic_description": "onap dmaap plugin unit test topic"}
+
+ # Make sure there's a topic_name
+ if "topic_name" in ctx.node.properties:
+ topic_name = ctx.node.properties["topic_name"]
+ if topic_name == '' or topic_name.isspace():
+ topic_name = random_string(12)
+ else:
+ topic_name = random_string(12)
+
+ # Make sure there's a topic description
+ if "topic_description" in ctx.node.properties:
+ topic_description = ctx.node.properties["topic_description"]
+ else:
+ topic_description = "No description provided"
+
+ # ..and the truly optional setting
+ if "txenable" in ctx.node.properties:
+ txenable = ctx.node.properties["txenable"]
+ else:
+ txenable= False
+
+ if "replication_case" in ctx.node.properties:
+ replication_case = ctx.node.properties["replication_case"]
+ else:
+ replication_case = None
+
+ if "global_mr_url" in ctx.node.properties:
+ global_mr_url = ctx.node.properties["global_mr_url"]
+ else:
+ global_mr_url = None
+
+ dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger)
+ ctx.logger.info("Attempting to create topic name {0}".format(topic_name))
+ t = dmc.create_topic(topic_name, topic_description, txenable, DMAAP_OWNER, replication_case, global_mr_url)
+
+ # Capture important properties from the result
+ topic = t.json()
+ ctx.instance.runtime_properties["fqtn"] = topic["fqtn"]
+
+ # test DMaaPControllerHandle functions
+ path = "myPath"
+ url = dmc._make_url(path)
+ rc = dmc._get_resource(path)
+ rc = dmc._create_resource(path, None)
+ rc = dmc._delete_resource(path)
diff --git a/dmaap/tests/test_dr_lifecycle.py b/dmaap/tests/test_dr_lifecycle.py
new file mode 100644
index 0000000..2aa65e8
--- /dev/null
+++ b/dmaap/tests/test_dr_lifecycle.py
@@ -0,0 +1,65 @@
+# ============LICENSE_START=======================================================
+# org.onap.dcae
+# ================================================================================
+# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2020 Pantheon.tech. 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+#
+
+import pytest
+import requests
+from cloudify.mocks import MockCloudifyContext
+from cloudify.state import current_ctx
+from cloudify import ctx
+from cloudify.decorators import operation
+from cloudify.exceptions import NonRecoverableError
+from cloudify.exceptions import RecoverableError
+
+_goodosv2 = {
+ 'auth_url': 'https://example.com/identity/v2.0',
+ 'password': 'pw',
+ 'region': 'r',
+ 'tenant_name': 'tn',
+ 'username': 'un'
+}
+
+
+def test_create_feed(monkeypatch, mockconsul, mockdmaapbc):
+ import dmaapplugin
+ from dmaapplugin import dr_lifecycle
+
+ properties = {'fqdn': 'a.x.example.com', 'openstack': _goodosv2, 'feed_id': 'test_feed_id' }
+ mock_ctx = MockCloudifyContext(
+ node_id='test_node_id',
+ node_name='test_node_name',
+ properties=properties,
+ runtime_properties = {
+ "admin": { "user": "admin_user" },
+ "user": { "user": "user_user" },
+ "viewer": { "user": "viewer_user" }
+ })
+
+ current_ctx.set(mock_ctx)
+
+ kwargs = { "feed_name": "ONAP_test",
+ "feed_description": "onap dmaap plugin unit test feed"}
+
+ def fake_feed(self):
+ return {"feedId":"test_feedId", "publishURL":"test_publishURL", "logURL":"test_logURL" }
+ monkeypatch.setattr(requests.Response, "json", fake_feed)
+
+ dr_lifecycle.create_feed(**kwargs)
+ dr_lifecycle.get_existing_feed(**kwargs)
+ dr_lifecycle.delete_feed(**kwargs)
diff --git a/dmaap/tests/test_mr_lifecycle.py b/dmaap/tests/test_mr_lifecycle.py
new file mode 100644
index 0000000..4a6a583
--- /dev/null
+++ b/dmaap/tests/test_mr_lifecycle.py
@@ -0,0 +1,59 @@
+# ============LICENSE_START=======================================================
+# org.onap.dcae
+# ================================================================================
+# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2020 Pantheon.tech. 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+
+import pytest
+import requests
+from cloudify.mocks import MockCloudifyContext
+from cloudify.state import current_ctx
+from cloudify import ctx
+from cloudify.decorators import operation
+from cloudify.exceptions import NonRecoverableError
+from cloudify.exceptions import RecoverableError
+
+_goodosv2 = {
+ 'auth_url': 'https://example.com/identity/v2.0',
+ 'password': 'pw',
+ 'region': 'r',
+ 'tenant_name': 'tn',
+ 'username': 'un'
+}
+
+
+def test_create_topic(monkeypatch, mockconsul, mockdmaapbc):
+ import dmaapplugin
+ from dmaapplugin import mr_lifecycle
+ properties = {'fqdn': 'a.x.example.com', 'openstack': _goodosv2, 'fqtn': 'test_fqtn' }
+ mock_ctx = MockCloudifyContext(
+ node_id='test_node_id',
+ node_name='test_node_name',
+ properties=properties,
+ runtime_properties = {
+ "admin": { "user": "admin_user" },
+ "user": { "user": "user_user" },
+ "viewer": { "user": "viewer_user" }
+ })
+
+ current_ctx.set(mock_ctx)
+
+ kwargs = { "topic_name": "ONAP_test",
+ "topic_description": "onap dmaap plugin unit test topic"}
+
+ mr_lifecycle.create_topic(**kwargs)
+ mr_lifecycle.get_existing_topic(**kwargs)
+ mr_lifecycle.delete_topic(**kwargs)
diff --git a/dmaap/tests/test_plugin.py b/dmaap/tests/test_plugin.py
new file mode 100644
index 0000000..e2a8586
--- /dev/null
+++ b/dmaap/tests/test_plugin.py
@@ -0,0 +1,26 @@
+# ============LICENSE_START====================================================
+# org.onap.dcaegen2
+# =============================================================================
+# Copyright (c) 2017-2020 AT&T Intellectual Property. 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END======================================================
+
+import pytest
+import requests
+from cloudify.mocks import MockCloudifyContext
+from cloudify.state import current_ctx
+from cloudify.exceptions import NonRecoverableError
+
+def test_noop():
+ pass
diff --git a/dmaap/tests/test_utils.py b/dmaap/tests/test_utils.py
new file mode 100644
index 0000000..362948d
--- /dev/null
+++ b/dmaap/tests/test_utils.py
@@ -0,0 +1,26 @@
+# ============LICENSE_START=======================================================
+# org.onap.dcae
+# ================================================================================
+# Copyright (c) 2017-2020 AT&T Intellectual Property. 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+#
+
+import pytest
+
+
+def test_random_string(monkeypatch):
+ from dmaapplugin import dmaaputils
+ target_length = 10
+ assert len(dmaaputils.random_string(target_length)) == target_length