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 |
| 11 | import trapd_logging |
| 12 | import trapd_get_cbs_config |
| 13 | |
| 14 | class test_snmptrapd(unittest.TestCase): |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 15 | """ |
| 16 | Test the save_pid mod |
| 17 | """ |
| 18 | |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame^] | 19 | def test_usage_err(self): |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 20 | """ |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame^] | 21 | Test usage error |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 22 | """ |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame^] | 23 | |
| 24 | with pytest.raises(SystemExit) as pytest_wrapped_sys_exit: |
| 25 | result = snmptrapd.usage_err() |
| 26 | assert pytest_wrapped_sys_exit.type == SystemExit |
| 27 | assert pytest_wrapped_sys_exit.value.code == 1 |
| 28 | |
| 29 | |
| 30 | def test_load_all_configs(self): |
| 31 | """ |
| 32 | Test load of all configs |
| 33 | """ |
| 34 | |
| 35 | # init vars |
| 36 | tds.init() |
| 37 | |
| 38 | # request load of CBS data |
| 39 | os.environ.update(CBS_SIM_JSON='/opt/app/snmptrap/etc/snmptrapd.json') |
| 40 | result = trapd_get_cbs_config.get_cbs_config() |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 41 | self.assertEqual(result, True) |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame^] | 42 | |
| 43 | # request load of CBS data |
| 44 | result = snmptrapd.load_all_configs(0, 1) |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 45 | self.assertEqual(result, True) |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame^] | 46 | |
| 47 | def test_log_all_arriving_traps(self): |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 48 | """ |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame^] | 49 | Test logging of traps |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 50 | """ |
Vijay VK | 339ca1c | 2018-03-27 02:05:36 +0100 | [diff] [blame^] | 51 | |
| 52 | # init vars |
| 53 | tds.init() |
| 54 | |
| 55 | # request load of CBS data |
| 56 | os.environ.update(CBS_SIM_JSON='/opt/app/snmptrap/etc/snmptrapd.json') |
| 57 | result = trapd_get_cbs_config.get_cbs_config() |
| 58 | |
| 59 | # set last day to current |
| 60 | tds.last_day = datetime.datetime.now().day |
| 61 | |
| 62 | # trap dict for logging |
| 63 | 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} |
| 64 | |
| 65 | # open eelf logs |
| 66 | trapd_io.open_eelf_logs() |
| 67 | |
| 68 | # open trap logs |
| 69 | tds.arriving_traps_filename = tds.c_config['files.runtime_base_dir'] + "/" + \ |
| 70 | tds.c_config['files.log_dir'] + "/" + \ |
| 71 | (tds.c_config['files.arriving_traps_log']) |
| 72 | tds.arriving_traps_fd = trapd_io.open_file(tds.arriving_traps_filename) |
| 73 | |
| 74 | # name and open json trap log |
| 75 | tds.json_traps_filename = tds.c_config['files.runtime_base_dir'] + "/" + tds.c_config['files.log_dir'] + "/" + "DMAAP_" + ( |
| 76 | tds.c_config['streams_publishes']['sec_fault_unsecure']['dmaap_info']['topic_url'].split('/')[-1]) + ".json" |
| 77 | tds.json_traps_fd = trapd_io.open_file(tds.json_traps_filename) |
| 78 | msg = ("published traps logged to: %s" % tds.json_traps_filename) |
| 79 | trapd_io.stdout_logger(msg) |
| 80 | trapd_logging.ecomp_logger(tds.LOG_TYPE_DEBUG, tds.SEV_INFO, tds.CODE_GENERAL, msg) |
| 81 | |
| 82 | # don't open files, but try to log - should raise exception |
| 83 | with pytest.raises(Exception) as pytest_wrapped_exception: |
| 84 | result = snmptrapd.log_all_arriving_traps() |
| 85 | assert pytest_wrapped_exception.type == AttributeError |
| 86 | |
Ladue, David (dl3158) | abb283c | 2018-03-14 14:01:36 -0400 | [diff] [blame] | 87 | if __name__ == '__main__': |
| 88 | unittest.main() |