blob: d02b4f787654b2466bae9055d598bbf59f04fceb [file] [log] [blame]
Vijay VK339ca1c2018-03-27 02:05:36 +01001import os
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -04002import pytest
3import unittest
Vijay VK339ca1c2018-03-27 02:05:36 +01004import snmptrapd
5import datetime
6
7import trapd_settings as tds
8import trapd_http_session
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -04009import trapd_runtime_pid
Vijay VK339ca1c2018-03-27 02:05:36 +010010import trapd_io
Vijay VK339ca1c2018-03-27 02:05:36 +010011import trapd_get_cbs_config
12
13class test_snmptrapd(unittest.TestCase):
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040014 """
15 Test the save_pid mod
16 """
Lusheng Ji1ee28592018-03-28 02:39:54 -040017
18 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\" }"
19
20 # create copy of snmptrapd.json for pytest
21 pytest_json_config = "/tmp/opt/app/snmptrap/etc/snmptrapd.json"
22 with open(pytest_json_config, 'w') as outfile:
23 outfile.write(pytest_json_data)
24
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040025
Vijay VK339ca1c2018-03-27 02:05:36 +010026 def test_usage_err(self):
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040027 """
Vijay VK339ca1c2018-03-27 02:05:36 +010028 Test usage error
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040029 """
Vijay VK339ca1c2018-03-27 02:05:36 +010030
31 with pytest.raises(SystemExit) as pytest_wrapped_sys_exit:
32 result = snmptrapd.usage_err()
33 assert pytest_wrapped_sys_exit.type == SystemExit
34 assert pytest_wrapped_sys_exit.value.code == 1
35
36
37 def test_load_all_configs(self):
38 """
39 Test load of all configs
40 """
41
42 # init vars
43 tds.init()
44
45 # request load of CBS data
Lusheng Ji1ee28592018-03-28 02:39:54 -040046 os.environ.update(CBS_SIM_JSON='/tmp/opt/app/snmptrap/etc/snmptrapd.json')
Vijay VK339ca1c2018-03-27 02:05:36 +010047 result = trapd_get_cbs_config.get_cbs_config()
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040048 self.assertEqual(result, True)
Vijay VK339ca1c2018-03-27 02:05:36 +010049
50 # request load of CBS data
51 result = snmptrapd.load_all_configs(0, 1)
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040052 self.assertEqual(result, True)
Vijay VK339ca1c2018-03-27 02:05:36 +010053
Ladue, David (dl3158)6702b562018-03-29 15:00:54 -040054 def test_load_all_configs_signal(self):
55 """
56 Test load of all configs via runtime signal
57 """
58
59 # init vars
60 tds.init()
61
62 # request load of CBS data
63 os.environ.update(CBS_SIM_JSON='/tmp/opt/app/snmptrap/etc/snmptrapd.json')
64 result = trapd_get_cbs_config.get_cbs_config()
65 self.assertEqual(result, True)
66
67 # request load of CBS data
68 result = snmptrapd.load_all_configs(1, 1)
69 self.assertEqual(result, True)
70
Vijay VK339ca1c2018-03-27 02:05:36 +010071 def test_log_all_arriving_traps(self):
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040072 """
Vijay VK339ca1c2018-03-27 02:05:36 +010073 Test logging of traps
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040074 """
Vijay VK339ca1c2018-03-27 02:05:36 +010075
76 # init vars
77 tds.init()
78
79 # request load of CBS data
Lusheng Ji1ee28592018-03-28 02:39:54 -040080 os.environ.update(CBS_SIM_JSON='/tmp/opt/app/snmptrap/etc/snmptrapd.json')
Vijay VK339ca1c2018-03-27 02:05:36 +010081 result = trapd_get_cbs_config.get_cbs_config()
82
83 # set last day to current
84 tds.last_day = datetime.datetime.now().day
85
86 # trap dict for logging
87 tds.trap_dict = {'uuid': '06f6e91c-3236-11e8-9953-005056865aac', 'agent address': '1.2.3.4', 'agent name': 'test-agent.nodomain.com', 'cambria.partition': 'test-agent.nodomain.com', 'community': '', 'community len': 0, 'epoch_serno': 15222068260000, 'protocol version': 'v2c', 'time received': 1522206826.2938566, 'trap category': 'ONAP-COLLECTOR-SNMPTRAP', 'sysUptime': '218567736', 'notify OID': '1.3.6.1.4.1.9999.9.9.999', 'notify OID len': 10}
88
89 # open eelf logs
90 trapd_io.open_eelf_logs()
91
92 # open trap logs
93 tds.arriving_traps_filename = tds.c_config['files.runtime_base_dir'] + "/" + \
94 tds.c_config['files.log_dir'] + "/" + \
95 (tds.c_config['files.arriving_traps_log'])
96 tds.arriving_traps_fd = trapd_io.open_file(tds.arriving_traps_filename)
97
98 # name and open json trap log
99 tds.json_traps_filename = tds.c_config['files.runtime_base_dir'] + "/" + tds.c_config['files.log_dir'] + "/" + "DMAAP_" + (
100 tds.c_config['streams_publishes']['sec_fault_unsecure']['dmaap_info']['topic_url'].split('/')[-1]) + ".json"
101 tds.json_traps_fd = trapd_io.open_file(tds.json_traps_filename)
102 msg = ("published traps logged to: %s" % tds.json_traps_filename)
103 trapd_io.stdout_logger(msg)
Ladue, David (dl3158)6702b562018-03-29 15:00:54 -0400104 trapd_io.ecomp_logger(tds.LOG_TYPE_DEBUG, tds.SEV_INFO, tds.CODE_GENERAL, msg)
Vijay VK339ca1c2018-03-27 02:05:36 +0100105
106 # don't open files, but try to log - should raise exception
107 with pytest.raises(Exception) as pytest_wrapped_exception:
108 result = snmptrapd.log_all_arriving_traps()
109 assert pytest_wrapped_exception.type == AttributeError
110
Ladue, David (dl3158)6702b562018-03-29 15:00:54 -0400111 def test_log_all_incorrect_log_type(self):
112 """
113 Test logging of traps
114 """
115
116 # init vars
117 tds.init()
118
119 # request load of CBS data
120 os.environ.update(CBS_SIM_JSON='/tmp/opt/app/snmptrap/etc/snmptrapd.json')
121 trapd_get_cbs_config.get_cbs_config()
122
123 # open eelf logs
124 trapd_io.open_eelf_logs()
125
126 def test_post_dmaap(self):
127 """
128 Test publish of traps
129 """
130
131 # init vars
132 tds.init()
133
134 # request load of CBS data
135 os.environ.update(CBS_SIM_JSON='/tmp/opt/app/snmptrap/etc/snmptrapd.json')
136 result = trapd_get_cbs_config.get_cbs_config()
137
138 # set last day to current
139 tds.last_day = datetime.datetime.now().day
140
141 # trap dict for publish
142 tds.trap_dict = {'uuid': '06f6e91c-3236-11e8-9953-005056865aac', 'agent address': '1.2.3.4', 'agent name': 'test-agent.nodomain.com', 'cambria.partition': 'test-agent.nodomain.com', 'community': '', 'community len': 0, 'epoch_serno': 15222068260000, 'protocol version': 'v2c', 'time received': 1522206826.2938566, 'trap category': 'ONAP-COLLECTOR-SNMPTRAP', 'sysUptime': '218567736', 'notify OID': '1.3.6.1.4.1.9999.9.9.999', 'notify OID len': 10}
143
144 # open eelf logs
145 trapd_io.open_eelf_logs()
146
147 # open trap logs
148 tds.arriving_traps_filename = tds.c_config['files.runtime_base_dir'] + "/" + \
149 tds.c_config['files.log_dir'] + "/" + \
150 (tds.c_config['files.arriving_traps_log'])
151 tds.arriving_traps_fd = trapd_io.open_file(tds.arriving_traps_filename)
152
153 # name and open json trap log
154 tds.json_traps_filename = tds.c_config['files.runtime_base_dir'] + "/" + tds.c_config['files.log_dir'] + "/" + "DMAAP_" + (
155 tds.c_config['streams_publishes']['sec_fault_unsecure']['dmaap_info']['topic_url'].split('/')[-1]) + ".json"
156 tds.json_traps_fd = trapd_io.open_file(tds.json_traps_filename)
157 msg = ("published traps logged to: %s" % tds.json_traps_filename)
158 trapd_io.stdout_logger(msg)
159 trapd_io.ecomp_logger(tds.LOG_TYPE_DEBUG, tds.SEV_INFO, tds.CODE_GENERAL, msg)
160
161 result = snmptrapd.post_dmaap()
162 print("result from post_dmaap -> %s" % result)
163
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -0400164if __name__ == '__main__':
165 unittest.main()