Make DMaaP tests independent of each other 19/98519/2
authorMiroslav Los <miroslav.los@pantheon.tech>
Mon, 18 Nov 2019 15:53:21 +0000 (16:53 +0100)
committerMiroslav Los <miroslav.los@pantheon.tech>
Tue, 7 Jan 2020 11:11:23 +0000 (12:11 +0100)
The tests in controllerif need not run to make the other tests work.
Use mockconsul instead of test_get_config_service to feed test_dmaapc.
Use mockconsul fixture in all other tests.
Do not wrap actual exceptions with NonRecoverableError in test code.

Signed-off-by: Miroslav Los <miroslav.los@pantheon.tech>
Issue-ID: CCSDK-1937
Change-Id: Iacb229cb8569838cc63932bcf572a6692ffd9124

dmaap/tests/conftest.py
dmaap/tests/test_consulif.py
dmaap/tests/test_dmaapcontrollerif.py
dmaap/tests/test_dr_lifecycle.py
dmaap/tests/test_mr_lifecycle.py

index 240a1af..6dd0697 100644 (file)
@@ -26,16 +26,18 @@ import requests
 def mockconsul(monkeypatch):
     """ Override the regular Consul interface"""
     def fake_get_config(self, key):
-        config={'dmaap': {'username': 'm06814@controller.dcae.ecomp.att.com', 
-                'url': 'https://dcae-dbcl.d54852.dyh1b.tci.att.com:8443/webapi', 
-                'password' : 'Dca3wr3x',
-                'owner': 'dcaeorch' }}
+        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 
+        return service_address, service_port
 
     def fake_add_to_entry(self, key, add_name, add_value):
         return True
@@ -53,9 +55,14 @@ def mockconsul(monkeypatch):
     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()
index 2d26576..e310afc 100644 (file)
@@ -25,7 +25,7 @@ import os
 from consulif.consulif import ConsulHandle
 
 
-#When run unit test, please update the consul interface parameters based on local consul configuration. 
+# 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'
@@ -33,76 +33,33 @@ DBCL_KEY_NAME = "dmaap_dbcl_info"           # Consul key containing DMaaP data b
 DBC_SERVICE_NAME= "dmaap_bus_controller"    # Name under which the DMaaP bus controller is registered
 
 
-def _fake_get_config(self, key):
-    fix ={'dmaap': {'username': 'm06814@controller.dcae.ecomp.att.com',
-                'url': 'https://dcae-dbcl.d54852.dyh1b.tci.att.com:8443/webapi',
-                'password' : 'Dca3wr3x',
-                'owner': 'dcaeorch' }}
-    return fix
-
-def _fake_service(self, key):
-    service_address = "myAddress"
-    service_port= "8443"
-    return service_address, service_port
-
-def test_get_config_service(monkeypatch):
-  monkeypatch.setattr(ConsulHandle, 'get_config', _fake_get_config)
-  monkeypatch.setattr(ConsulHandle, 'get_service', _fake_service)
-  try:
+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)
-    assert None != _ch
-    
-    err_msg = "Error getting config for '{0}' from ConsulHandle when configuring dmaap plugin: ".format(DBCL_KEY_NAME) + "{0}"
+
     config = _ch.get_config(DBCL_KEY_NAME)
 
-    err_msg = "Error setting DMAAP_USER while configuring dmaap plugin: {0}"
     DMAAP_USER = config['dmaap']['username']
-
-    err_msg = "Error setting DMAAP_PASS while configuring dmaap plugin: {0}"
     DMAAP_PASS = config['dmaap']['password']
-
-    err_msg = "Error setting DMAAP_OWNER while configuring dmaap plugin: {0}"
     DMAAP_OWNER = config['dmaap']['owner']
 
-    err_msg = "Error setting DMAAP_PROTOCOL while configuring dmaap plugin: {0}"
     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
 
-    err_msg = "Error setting DMAAP_PATH while configuring dmaap plugin: {0}"
     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
+        DMAAP_PATH = 'webapi'       # Should come from service discovery but Consul doesn't support it
 
-    err_msg = "Error getting service_address and service_port for '{0}' from ConsulHandle when configuring dmaap plugin: ".format(DBC_SERVICE_NAME) + "{0}"
     service_address, service_port = _ch.get_service(DBC_SERVICE_NAME)
 
-    err_msg = "Error setting DMAAP_API_URL while configuring dmaap plugin: {0}"
     DMAAP_API_URL = '{0}://{1}:{2}/{3}'.format(DMAAP_PROTOCOL, service_address, service_port, DMAAP_PATH)
-    assert DMAAP_API_URL != None
-    dmaap_config = {'DMAAP_USER':DMAAP_USER, 'DMAAP_API_URL':DMAAP_API_URL, 'DMAAP_PASS':DMAAP_PASS, 'DMAAP_OWNER':DMAAP_OWNER}
-    print "return dmaap config info from consul: {0}".format(dmaap_config)
-    return dmaap_config
 
-  except Exception as e:
-    raise NonRecoverableError(err_msg.format(e))
 
-def _fake_add_to_entry(self, key, add_name, add_value):
-    return True
-
-def _fake_delete_entry(self, entry_name):
-    return True
-
-def test_add_entry(monkeypatch):
-  monkeypatch.setattr(ConsulHandle, 'get_config', _fake_get_config)
-  monkeypatch.setattr(ConsulHandle, 'add_to_entry', _fake_add_to_entry)
-  monkeypatch.setattr(ConsulHandle, 'delete_entry', _fake_delete_entry)
-  try:
+def test_add_entry(mockconsul):
     _ch = ConsulHandle("http://{0}:{1}".format(CONSUL_HOST, CONSUL_PORT), None, None, None)
-    assert None != _ch
 
     key = 'DMAAP_TEST'
     name = 'dmaap_test_name'
@@ -114,7 +71,3 @@ def test_add_entry(monkeypatch):
     _ch.add_to_entry(key, name, value)
 
     _ch.delete_entry(key)
-
-  except Exception as e:
-    raise NonRecoverableError("Error in test_add_entry: {0}".format(e))
-
index f647530..0f6a5c0 100644 (file)
@@ -45,12 +45,11 @@ _goodosv2 = {
 def test_dmaapc (monkeypatch, mockconsul, mockdmaapbc):
     from dmaapplugin.dmaaputils import random_string
 
-    config = test_consulif.test_get_config_service(monkeypatch)
-    DMAAP_API_URL = config['DMAAP_API_URL']
-    DMAAP_USER = config['DMAAP_USER']
-    DMAAP_PASS = config['DMAAP_PASS']
-    DMAAP_OWNER = config['DMAAP_OWNER']
-    assert DMAAP_API_URL != None
+    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,
@@ -61,66 +60,52 @@ def test_dmaapc (monkeypatch, mockconsul, mockdmaapbc):
                                    }
                                )
 
-    try:
-        current_ctx.set(mock_ctx)
-    except Exception as e:
-        raise NonRecoverableError(e) 
-#    finally:
-#        current_ctx.clear()
-   
+    current_ctx.set(mock_ctx)
+
     kwargs = { "topic_name": "ONAP_test",
             "topic_description": "onap dmaap plugin unit test topic"}
 
-    try:
-        # 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:
+    # 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)
-
-        # 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"]
-
-    except Exception as e:
-        raise NonRecoverableError(e)
+    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
-    try:
-        path = "myPath"
-        url = dmc._make_url(path)
-       rc = dmc._get_resource(path)
-        rc = dmc._create_resource(path, None)
-        rc = dmc._delete_resource(path)
-    except Exception as e:
-        raise NonRecoverableError(e)
-
+    path = "myPath"
+    url = dmc._make_url(path)
+    rc = dmc._get_resource(path)
+    rc = dmc._create_resource(path, None)
+    rc = dmc._delete_resource(path)
index c6b6d4b..925d575 100644 (file)
@@ -36,7 +36,7 @@ _goodosv2 = {
 }
 
 
-def test_create_feed(monkeypatch, mockdmaapbc):
+def test_create_feed(monkeypatch, mockconsul, mockdmaapbc):
     import dmaapplugin
     from dmaapplugin import dr_lifecycle
 
@@ -49,13 +49,8 @@ def test_create_feed(monkeypatch, mockdmaapbc):
                                    }
                                )
 
-    try:
-        current_ctx.set(mock_ctx)
-    except Exception as e:
-        raise NonRecoverableError(e) 
-#    finally:
-#        current_ctx.clear()
-   
+    current_ctx.set(mock_ctx)
+
     kwargs = { "feed_name": "ONAP_test",
             "feed_description": "onap dmaap plugin unit test feed"}
 
@@ -63,10 +58,6 @@ def test_create_feed(monkeypatch, mockdmaapbc):
         return {"feedId":"test_feedId", "publishURL":"test_publishURL", "logURL":"test_logURL" }
     monkeypatch.setattr(requests.Response, "json", fake_feed)
 
-    try:
-        dr_lifecycle.create_feed(**kwargs)
-        dr_lifecycle.get_existing_feed(**kwargs)
-        dr_lifecycle.delete_feed(**kwargs)
-
-    except Exception as e:
-        raise NonRecoverableError(e)
+    dr_lifecycle.create_feed(**kwargs)
+    dr_lifecycle.get_existing_feed(**kwargs)
+    dr_lifecycle.delete_feed(**kwargs)
index 98a9ed5..b2ee713 100644 (file)
@@ -35,7 +35,8 @@ _goodosv2 = {
   'username': 'un'
 }
 
-def test_create_topic(monkeypatch, mockdmaapbc):
+
+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' }
@@ -47,18 +48,11 @@ def test_create_topic(monkeypatch, mockdmaapbc):
                                    }
                                )
 
-    try:
-        current_ctx.set(mock_ctx)
-    except Exception as e:
-        raise NonRecoverableError(e) 
-   
+    current_ctx.set(mock_ctx)
+
     kwargs = { "topic_name": "ONAP_test",
             "topic_description": "onap dmaap plugin unit test topic"}
 
-    try:
-        mr_lifecycle.create_topic(**kwargs)
-        mr_lifecycle.get_existing_topic(**kwargs)
-        mr_lifecycle.delete_topic(**kwargs)
-
-    except Exception as e:
-        raise NonRecoverableError(e)
+    mr_lifecycle.create_topic(**kwargs)
+    mr_lifecycle.get_existing_topic(**kwargs)
+    mr_lifecycle.delete_topic(**kwargs)