blob: 2cc11d7f664b295c4c0448b63176da327296c2ec [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
11import trapd_logging
12import trapd_get_cbs_config
13
14class test_snmptrapd(unittest.TestCase):
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040015 """
16 Test the save_pid mod
17 """
18
Vijay VK339ca1c2018-03-27 02:05:36 +010019 def test_usage_err(self):
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040020 """
Vijay VK339ca1c2018-03-27 02:05:36 +010021 Test usage error
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040022 """
Vijay VK339ca1c2018-03-27 02:05:36 +010023
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)abb283c2018-03-14 14:01:36 -040041 self.assertEqual(result, True)
Vijay VK339ca1c2018-03-27 02:05:36 +010042
43 # request load of CBS data
44 result = snmptrapd.load_all_configs(0, 1)
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040045 self.assertEqual(result, True)
Vijay VK339ca1c2018-03-27 02:05:36 +010046
47 def test_log_all_arriving_traps(self):
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040048 """
Vijay VK339ca1c2018-03-27 02:05:36 +010049 Test logging of traps
Ladue, David (dl3158)abb283c2018-03-14 14:01:36 -040050 """
Vijay VK339ca1c2018-03-27 02:05:36 +010051
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)abb283c2018-03-14 14:01:36 -040087if __name__ == '__main__':
88 unittest.main()