NFVBENCH-8 config checking fails with Exception TypeError: string indices must be...
[nfvbench.git] / test / test_nfvbench.py
index be80033..1d985b8 100644 (file)
 #
 
 from attrdict import AttrDict
+import logging
+from nfvbench.config import config_loads
 from nfvbench.connection import SSH
 from nfvbench.credentials import Credentials
+from nfvbench.fluentd import FluentLogHandler
+import nfvbench.log
 from nfvbench.network import Interface
 from nfvbench.network import Network
 from nfvbench.specs import Encaps
@@ -631,6 +635,9 @@ def test_ndr_pdr_search(traffic_client):
 # Other tests
 # =========================================================================
 
+def setup_module(module):
+    nfvbench.log.setup(mute_stdout=True)
+
 def test_no_credentials():
     cred = Credentials('/completely/wrong/path/openrc', None, False)
     if cred.rc_auth_url:
@@ -638,3 +645,44 @@ def test_no_credentials():
         assert False
     else:
         assert True
+
+def test_config():
+    refcfg = {1: 100, 2: {21: 100, 22: 200}, 3: None}
+    res1 = {1: 10, 2: {21: 100, 22: 200}, 3: None}
+    res2 = {1: 100, 2: {21: 1000, 22: 200}, 3: None}
+    res3 = {1: 100, 2: {21: 100, 22: 200}, 3: "abc"}
+    assert(config_loads("{}", refcfg) == refcfg)
+    assert(config_loads("{1: 10}", refcfg) == res1)
+    assert(config_loads("{2: {21: 1000}}", refcfg) == res2)
+    assert(config_loads('{3: "abc"}', refcfg) == res3)
+
+    # correctly fails
+    # pairs of input string and expected subset (None if identical)
+    fail_pairs = [
+        ["{4: 0}", None],
+        ["{2: {21: 100, 30: 50}}", "{2: {30: 50}}"],
+        ["{2: {0: 1, 1: 2}, 5: 5}", None],
+        ["{1: 'abc', 2: {21: 0}}", "{1: 'abc'}"],
+        ["{2: 100}", None]
+    ]
+    for fail_pair in fail_pairs:
+        with pytest.raises(Exception) as e_info:
+            config_loads(fail_pair[0], refcfg)
+        expected = fail_pair[1]
+        if expected is None:
+            expected = fail_pair[0]
+        assert expected in e_info.value.message
+
+
+
+def test_fluentd():
+    logger = logging.getLogger('fluent-logger')
+    handler = FluentLogHandler('nfvbench', fluentd_port=7081)
+    logger.addHandler(handler)
+    logger.setLevel(logging.INFO)
+    logger.info('test')
+    logger.warning('test %d', 100)
+    try:
+        raise Exception("test")
+    except Exception:
+        logger.exception("got exception")