aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGokul Singaraju <gs244f@att.com>2018-05-21 15:32:52 -0400
committerGokul Singaraju <gs244f@att.com>2018-05-22 22:40:18 -0400
commit678e65fa7938114bf7d66f212b16cef8633db214 (patch)
treeb41462748c9101ea99ff3d3fa36296b159510a0c /tests
parent4571d99c799412d22e140ea8396ba9ad105626a8 (diff)
Miss HB CBS integration
Issue-ID: DCAEGEN2-279 Change-Id: I3f78e0870d4c620a304fb8d218f1d30187b4fbef Signed-off-by: Gokul Singaraju <gs244f@att.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/test_binding.py45
-rw-r--r--tests/test_trapd_exit.py37
-rw-r--r--tests/test_trapd_get_cbs_config.py70
-rw-r--r--tests/test_trapd_http_session.py20
-rw-r--r--tests/test_trapd_runtime_pid.py49
-rw-r--r--tests/test_trapd_settings.py72
6 files changed, 269 insertions, 24 deletions
diff --git a/tests/test_binding.py b/tests/test_binding.py
index f0faac0..24c7b61 100644
--- a/tests/test_binding.py
+++ b/tests/test_binding.py
@@ -23,11 +23,16 @@ import httpretty
#import miss_htbt_service
from miss_htbt_service import htbtworker
#from miss_htbt_service.htbtworker import get_collector_uri,get_policy_uri
+import subprocess
import pytest
import json
import base64
import errno
import imp
+import time
+from onap_dcae_cbs_docker_client.client import get_config
+
+
MODULE_EXTENSIONS = ('.py', '.pyc', '.pyo')
def package_contents(package_name):
@@ -44,14 +49,14 @@ def package_contents(package_name):
#####
#mr_url = 'http://127.0.0.1:3904'
-mr_url = 'http://mrrouter.att.com:3904'
+mr_url = 'http://mrrouter.onap.org:3904'
intopic = 'VESCOLL-VNFNJ-SECHEARTBEAT-OUTPUT'
outopic = 'POLICY-HILOTCA-EVENT-OUTPUT'
@httpretty.activate
def test_resolve_all(monkeypatch):
#htbtmsg = "Find the best daily deals"
- htbtmsg = '{"event":{"commonEventHeader":{"startEpochMicrosec":1518616063564475,"sourceId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","eventId":"10048640","reportingEntityId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","priority":"Normal","version":3,"reportingEntityName":"SWMSVM","sequence":10048640,"domain":"heartbeat","lastEpochMicrosec":1518616063564476,"eventName":"Heartbeat_vMrf","sourceName":"SWMSVM","nfNamingCode":"vMRF"}}}'
+ htbtmsg = '{"event":{"commonEventHeader":{"startEpochMicrosec":1518616063564475,"sourceId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","eventId":"10048640","reportingEntityId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","priority":"Normal","version":3,"reportingEntityName":"TESTVM","sequence":10048640,"domain":"heartbeat","lastEpochMicrosec":1518616063564476,"eventName":"Heartbeat_vVnf","sourceName":"TESTVM","nfNamingCode":"vVNF"}}}'
send_url = mr_url+'/events/'+intopic+'/DefaultGroup/1?timeout=15000'
print(send_url)
httpretty.register_uri(httpretty.GET, send_url, body=htbtmsg)
@@ -60,33 +65,25 @@ def test_resolve_all(monkeypatch):
print(response)
print(response.text)
assert(response.text == htbtmsg)
- try:
- os.makedirs('/tmp/config')
- except OSError as e:
- if e.errno != errno.EEXIST:
- raise
- with open("/tmp/config/coll_ip.txt", "w") as file:
- #file.write('127.0.0.1')
- file.write('mytest.onap.org')
- file.close()
- with open("/tmp/config/coll_port.txt", "w") as file2:
- file2.write('3904')
- file2.close()
- #print(package_contents('miss_htbt_service'))
- #response = requests.get(send_url)
- #print(response)
- #print(response.text)
- #assert(response.text == htbtmsg)
- htbtmsg = json.dumps({"event":{"commonEventHeader":{"startEpochMicrosec":1518616063564475,"sourceId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","eventId":"10048640","reportingEntityId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","priority":"Normal","version":3,"reportingEntityName":"SWMSVM","sequence":10048640,"domain":"heartbeat","lastEpochMicrosec":1518616063564476,"eventName":"Heartbeat_vMrf","sourceName":"SWMSVM","nfNamingCode":"vMRF"}}})
- send_url = htbtworker.get_collector_uri()+'/events/'+intopic+'/DefaultGroup/1?timeout=15000'
+ htbtmsg = json.dumps({"event":{"commonEventHeader":{"startEpochMicrosec":1518616063564475,"sourceId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","eventId":"10048640","reportingEntityId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","priority":"Normal","version":3,"reportingEntityName":"TESTVM","sequence":10048640,"domain":"heartbeat","lastEpochMicrosec":1518616063564476,"eventName":"Heartbeat_vVnf","sourceName":"TESTVM","nfNamingCode":"vVNF"}}})
+ send_url = mr_url+'/events/'+intopic+'/DefaultGroup/1?timeout=15000'
print("Send URL : "+send_url)
httpretty.register_uri(httpretty.GET, send_url, body=htbtmsg, content_type="application/json")
- pol_url = htbtworker.get_policy_uri()+'/events/'+outopic+'/DefaultGroup/1?timeout=15000'
- pol_body = json.dumps({"event":{"commonEventHeader":{"startEpochMicrosec":1518616063564475,"sourceId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","eventId":"10048640","reportingEntityId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","priority":"Normal","version":3,"reportingEntityName":"SWMSVM","sequence":10048640,"domain":"heartbeat","lastEpochMicrosec":1518616063564476,"eventName":"Heartbeat_vMrf","sourceName":"SWMSVM","nfNamingCode":"vMRF"}}})
+ pol_url = mr_url+'/events/'+outopic+'/DefaultGroup/1?timeout=15000'
+ pol_body = json.dumps({"event":{"commonEventHeader":{"startEpochMicrosec":1518616063564475,"sourceId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","eventId":"10048640","reportingEntityId":"587c14b3-72c0-4581-b5cb-6567310b9bb7","priority":"Normal","version":3,"reportingEntityName":"TESTVM","sequence":10048640,"domain":"heartbeat","lastEpochMicrosec":1518616063564476,"eventName":"Heartbeat_vVnf","sourceName":"TESTVM","nfNamingCode":"vVNF"}}})
print("Policy URL : "+pol_url)
httpretty.register_uri(httpretty.POST, pol_url, body=pol_body, status=200, content_type='text/json')
- htbtworker.test_setup([send_url,send_url,3,60,intopic,outopic])
+ htbtworker.main([send_url,intopic,send_url,outopic,"vVNF",3,60,"internal_test"])
ret = htbtworker.periodic_event()
print("Returned",ret)
assert(ret == 1)
+def test_full():
+ p = subprocess.Popen(['./miss_htbt_service/misshtbtd.py'],stdout=subprocess.PIPE)
+ time.sleep(30)
+ r = requests.get('http://127.0.0.1:10001')
+ print(r.status_code)
+ assert(r.status_code == 200)
+ #r = requests.post('http://127.0.0.1:10001',data={'number': 12524, 'health': 'good', 'action': 'show'})
+ #print(r.status_code)
+ #assert(r.status_code == 200)
diff --git a/tests/test_trapd_exit.py b/tests/test_trapd_exit.py
new file mode 100644
index 0000000..594624f
--- /dev/null
+++ b/tests/test_trapd_exit.py
@@ -0,0 +1,37 @@
+import pytest
+import unittest
+import trapd_exit
+
+pid_file="/tmp/test_pid_file"
+pid_file_dne="/tmp/test_pid_file_NOT"
+
+class test_cleanup_and_exit(unittest.TestCase):
+ """
+ Test the cleanup_and_exit mod
+ """
+
+ def test_normal_exit(self):
+ """
+ Test normal exit works as expected
+ """
+ open(pid_file, 'w')
+
+ with pytest.raises(SystemExit) as pytest_wrapped_sys_exit:
+ result = trapd_exit.cleanup_and_exit(0,pid_file)
+ assert pytest_wrapped_sys_exit.type == SystemExit
+ assert pytest_wrapped_sys_exit.value.code == 0
+
+ # compare = str(result).startswith("SystemExit: 0")
+ # self.assertEqual(compare, True)
+
+ def test_abnormal_exit(self):
+ """
+ Test exit with missing PID file exits non-zero
+ """
+ with pytest.raises(SystemExit) as pytest_wrapped_sys_exit:
+ result = trapd_exit.cleanup_and_exit(0,pid_file_dne)
+ assert pytest_wrapped_sys_exit.type == SystemExit
+ assert pytest_wrapped_sys_exit.value.code == 1
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/test_trapd_get_cbs_config.py b/tests/test_trapd_get_cbs_config.py
new file mode 100644
index 0000000..bd8d082
--- /dev/null
+++ b/tests/test_trapd_get_cbs_config.py
@@ -0,0 +1,70 @@
+import pytest
+import unittest
+import os
+
+from onap_dcae_cbs_docker_client.client import get_config
+from trapd_exit import cleanup_and_exit
+from trapd_io import stdout_logger, ecomp_logger
+import trapd_settings as tds
+import trapd_get_cbs_config
+
+class test_get_cbs_config(unittest.TestCase):
+ """
+ Test the trapd_get_cbs_config mod
+ """
+
+ pytest_json_data = "{ \"snmptrap.version\": \"1.3.0\", \"snmptrap.title\": \"ONAP SNMP Trap Receiver\" , \"protocols.transport\": \"udp\", \"protocols.ipv4_interface\": \"0.0.0.0\", \"protocols.ipv4_port\": 6164, \"protocols.ipv6_interface\": \"::1\", \"protocols.ipv6_port\": 6164, \"cache.dns_cache_ttl_seconds\": 60, \"publisher.http_timeout_milliseconds\": 1500, \"publisher.http_retries\": 3, \"publisher.http_milliseconds_between_retries\": 750, \"publisher.http_primary_publisher\": \"true\", \"publisher.http_peer_publisher\": \"unavailable\", \"publisher.max_traps_between_publishes\": 10, \"publisher.max_milliseconds_between_publishes\": 10000, \"streams_publishes\": { \"sec_measurement\": { \"type\": \"message_router\", \"aaf_password\": \"aaf_password\", \"dmaap_info\": { \"location\": \"mtl5\", \"client_id\": \"111111\", \"client_role\": \"com.att.dcae.member\", \"topic_url\": null }, \"aaf_username\": \"aaf_username\" }, \"sec_fault_unsecure\": { \"type\": \"message_router\", \"aaf_password\": null, \"dmaap_info\": { \"location\": \"mtl5\", \"client_id\": null, \"client_role\": null, \"topic_url\": \"http://uebsb93kcdc.it.att.com:3904/events/ONAP-COLLECTOR-SNMPTRAP\" }, \"aaf_username\": null } }, \"files.runtime_base_dir\": \"/tmp/opt/app/snmptrap\", \"files.log_dir\": \"logs\", \"files.data_dir\": \"data\", \"files.pid_dir\": \"/tmp/opt/app/snmptrap/tmp\", \"files.arriving_traps_log\": \"snmptrapd_arriving_traps.log\", \"files.snmptrapd_diag\": \"snmptrapd_prog_diag.log\", \"files.traps_stats_log\": \"snmptrapd_stats.csv\", \"files.perm_status_file\": \"snmptrapd_status.log\", \"files.eelf_base_dir\": \"/tmp/opt/app/snmptrap/logs\", \"files.eelf_error\": \"error.log\", \"files.eelf_debug\": \"debug.log\", \"files.eelf_audit\": \"audit.log\", \"files.eelf_metrics\": \"metrics.log\", \"files.roll_frequency\": \"hour\", \"files.minimum_severity_to_log\": 2, \"trap_def.1.trap_oid\" : \".1.3.6.1.4.1.74.2.46.12.1.1\", \"trap_def.1.trap_category\": \"DCAE-SNMP-TRAPS\", \"trap_def.2.trap_oid\" : \"*\", \"trap_def.2.trap_category\": \"DCAE-SNMP-TRAPS\", \"stormwatch.1.stormwatch_oid\" : \".1.3.6.1.4.1.74.2.46.12.1.1\", \"stormwatch.1.low_water_rearm_per_minute\" : \"5\", \"stormwatch.1.high_water_arm_per_minute\" : \"100\", \"stormwatch.2.stormwatch_oid\" : \".1.3.6.1.4.1.74.2.46.12.1.2\", \"stormwatch.2.low_water_rearm_per_minute\" : \"2\", \"stormwatch.2.high_water_arm_per_minute\" : \"200\", \"stormwatch.3.stormwatch_oid\" : \".1.3.6.1.4.1.74.2.46.12.1.2\", \"stormwatch.3.low_water_rearm_per_minute\" : \"2\", \"stormwatch.3.high_water_arm_per_minute\" : \"200\" }"
+
+ # create copy of snmptrapd.json for pytest
+ pytest_json_config = "/tmp/opt/app/miss_htbt_service/etc/config.json"
+ with open(pytest_json_config, 'w') as outfile:
+ outfile.write(pytest_json_data)
+
+
+ def test_cbs_env_present(self):
+ """
+ Test that CONSUL_HOST env variable exists but fails to
+ respond
+ """
+ os.environ.update(CONSUL_HOST='nosuchhost')
+ # del os.environ['CBS_HTBT_JSON']
+ # result = trapd_get_cbs_config.get_cbs_config()
+ # print("result: %s" % result)
+ # compare = str(result).startswith("{'snmptrap': ")
+ # self.assertEqual(compare, False)
+
+ with pytest.raises(Exception) as pytest_wrapped_sys_exit:
+ result = trapd_get_cbs_config.get_cbs_config()
+ assert pytest_wrapped_sys_exit.type == SystemExit
+ # assert pytest_wrapped_sys_exit.value.code == 1
+
+
+ def test_cbs_override_env_invalid(self):
+ """
+ """
+ os.environ.update(CBS_HTBT_JSON='/tmp/opt/app/miss_htbt_service/etc/nosuchfile.json')
+ # result = trapd_get_cbs_config.get_cbs_config()
+ # print("result: %s" % result)
+ # compare = str(result).startswith("{'snmptrap': ")
+ # self.assertEqual(compare, False)
+
+ with pytest.raises(SystemExit) as pytest_wrapped_sys_exit:
+ result = trapd_get_cbs_config.get_cbs_config()
+ assert pytest_wrapped_sys_exit.type == SystemExit
+ assert pytest_wrapped_sys_exit.value.code == 1
+
+
+ def test_cbs_fallback_env_present(self):
+ """
+ Test that CBS fallback env variable exists and we can get config
+ from fallback env var
+ """
+ os.environ.update(CBS_HTBT_JSON='/tmp/opt/app/miss_htbt_service/etc/config.json')
+ result = trapd_get_cbs_config.get_cbs_config()
+ print("result: %s" % result)
+ # compare = str(result).startswith("{'snmptrap': ")
+ # self.assertEqual(compare, True)
+ self.assertEqual(result, True)
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/test_trapd_http_session.py b/tests/test_trapd_http_session.py
new file mode 100644
index 0000000..8f61d08
--- /dev/null
+++ b/tests/test_trapd_http_session.py
@@ -0,0 +1,20 @@
+import pytest
+import unittest
+import trapd_http_session
+
+class test_init_session_obj(unittest.TestCase):
+ """
+ Test the init_session_obj mod
+ """
+
+ def test_correct_usage(self):
+ """
+ Test that attempt to create http session object works
+ """
+ result = trapd_http_session.init_session_obj()
+ compare = str(result).startswith("<requests.sessions.Session object at")
+ self.assertEqual(compare, True)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/test_trapd_runtime_pid.py b/tests/test_trapd_runtime_pid.py
new file mode 100644
index 0000000..b9010e1
--- /dev/null
+++ b/tests/test_trapd_runtime_pid.py
@@ -0,0 +1,49 @@
+import pytest
+import unittest
+import trapd_runtime_pid
+import trapd_io
+
+class test_save_pid(unittest.TestCase):
+ """
+ Test the save_pid mod
+ """
+
+ def test_correct_usage(self):
+ """
+ Test that attempt to create pid file in standard location works
+ """
+ result = trapd_runtime_pid.save_pid('/tmp/snmptrap_test_pid_file')
+ self.assertEqual(result, True)
+
+ def test_missing_directory(self):
+ """
+ Test that attempt to create pid file in missing dir fails
+ """
+ result = trapd_runtime_pid.save_pid('/bogus/directory/for/snmptrap_test_pid_file')
+ self.assertEqual(result, False)
+
+class test_rm_pid(unittest.TestCase):
+ """
+ Test the rm_pid mod
+ """
+
+ def test_correct_usage(self):
+ """
+ Test that attempt to remove pid file in standard location works
+ """
+ # must create it before removing it
+ result = trapd_runtime_pid.save_pid('/tmp/snmptrap_test_pid_file')
+ self.assertEqual(result, True)
+ result = trapd_runtime_pid.rm_pid('/tmp/snmptrap_test_pid_file')
+ self.assertEqual(result, True)
+
+ def test_missing_file(self):
+ """
+ Test that attempt to rm non-existent pid file fails
+ """
+ result = trapd_runtime_pid.rm_pid('/tmp/snmptrap_test_pid_file_9999')
+ self.assertEqual(result, False)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/test_trapd_settings.py b/tests/test_trapd_settings.py
new file mode 100644
index 0000000..17b20a8
--- /dev/null
+++ b/tests/test_trapd_settings.py
@@ -0,0 +1,72 @@
+import pytest
+import unittest
+import trapd_exit
+
+pid_file="/tmp/test_pid_file"
+pid_file_dne="/tmp/test_pid_file_NOT"
+
+import trapd_settings as tds
+
+class test_cleanup_and_exit(unittest.TestCase):
+ """
+ Test for presense of required vars
+ """
+
+
+ def test_nonexistent_dict(self):
+ """
+ Test nosuch var
+ """
+ tds.init()
+ try:
+ tds.no_such_var
+ result = True
+ except:
+ result = False
+
+ self.assertEqual(result, False)
+
+ def test_config_dict(self):
+ """
+ Test config dict
+ """
+ tds.init()
+ try:
+ tds.c_config
+ result = True
+ except:
+ result = False
+
+ self.assertEqual(result, True)
+
+ def test_dns_cache_ip_to_name(self):
+ """
+ Test dns cache name dict
+ """
+
+ tds.init()
+ try:
+ tds.dns_cache_ip_to_name
+ result = True
+ except:
+ result = False
+
+ self.assertEqual(result, True)
+
+ def test_dns_cache_ip_expires(self):
+ """
+ Test dns cache ip expires dict
+ """
+
+ tds.init()
+ try:
+ tds.dns_cache_ip_expires
+ result = True
+ except:
+ result = False
+
+ self.assertEqual(result, True)
+
+if __name__ == '__main__':
+ # tds.init()
+ unittest.main()