Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 1 | import os |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 2 | import pytest |
| 3 | import unittest |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 4 | import snmptrapd |
| 5 | import datetime |
| 6 | |
| 7 | import trapd_settings as tds |
| 8 | import trapd_http_session |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 9 | import trapd_runtime_pid |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 10 | import trapd_io |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 11 | import trapd_get_cbs_config |
| 12 | |
| 13 | class test_snmptrapd(unittest.TestCase): |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 14 | """ |
| 15 | Test the save_pid mod |
| 16 | """ |
Lusheng Ji | 1ee2859 | 2018-03-28 02:39:54 -0400 | [diff] [blame] | 17 | |
| 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) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 25 | |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 26 | def test_usage_err(self): |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 27 | """ |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 28 | Test usage error |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 29 | """ |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 30 | |
| 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 Ji | 1ee2859 | 2018-03-28 02:39:54 -0400 | [diff] [blame] | 46 | os.environ.update(CBS_SIM_JSON='/tmp/opt/app/snmptrap/etc/snmptrapd.json') |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 47 | result = trapd_get_cbs_config.get_cbs_config() |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 48 | self.assertEqual(result, True) |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 49 | |
| 50 | # request load of CBS data |
| 51 | result = snmptrapd.load_all_configs(0, 1) |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 52 | self.assertEqual(result, True) |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 53 | |
Ladue, David (dl3158) | 6702b56 | 2018-03-29 15:00:54 -0400 | [diff] [blame] | 54 | 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 VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 71 | def test_log_all_arriving_traps(self): |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 72 | """ |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 73 | Test logging of traps |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 74 | """ |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 75 | |
| 76 | # init vars |
| 77 | tds.init() |
| 78 | |
| 79 | # request load of CBS data |
Lusheng Ji | 1ee2859 | 2018-03-28 02:39:54 -0400 | [diff] [blame] | 80 | os.environ.update(CBS_SIM_JSON='/tmp/opt/app/snmptrap/etc/snmptrapd.json') |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 81 | 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) | 6702b56 | 2018-03-29 15:00:54 -0400 | [diff] [blame] | 104 | trapd_io.ecomp_logger(tds.LOG_TYPE_DEBUG, tds.SEV_INFO, tds.CODE_GENERAL, msg) |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame] | 105 | |
| 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) | 6702b56 | 2018-03-29 15:00:54 -0400 | [diff] [blame] | 111 | 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) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 164 | if __name__ == '__main__': |
| 165 | unittest.main() |