aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_trapd_io.py
blob: f3d1a4c1fc8a0536b7aefb747016916d6d964771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# ============LICENSE_START=======================================================
# Copyright (c) 2019-2022 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=========================================================

import datetime
import glob
import io
import json
import os
from pathlib import Path
import sys
import tempfile
import unittest
from unittest.mock import patch

import snmptrapd
import trapd_settings as tds
import trapd_runtime_pid
import trapd_io


class test_trapd_io(unittest.TestCase):
    """
    Test the save_pid mod
    """

    class PseudoFile():
        """ test file-like object that does nothing """
        def write(self):
            pass
        def close(self):
            pass

    class WriteThrows():
        """ test file-like object that throws on a write """
        def write(self):
            raise RuntimeError("close() throws")


    @classmethod
    def setUpClass(cls):

        tds.init()

        snmptrap_dir = "/tmp/opt/app/snmptrap"
        try:
            Path(snmptrap_dir + "/logs").mkdir(parents=True, exist_ok=True)
            Path(snmptrap_dir + "/tmp").mkdir(parents=True, exist_ok=True)
            Path(snmptrap_dir + "/etc").mkdir(parents=True, exist_ok=True)
        except Exception as e:
            print("Error while running %s : %s" % (os.path.basename(__file__), str(e.strerror)))
            sys.exit(1)

        # fmt: off
        tds.c_config = json.loads(
            '{ "snmptrapd": { '
            '    "version": "1.4.0", '
            '    "title": "ONAP SNMP Trap Receiver" }, '
            '"protocols": { '
            '    "transport": "udp", '
            '    "ipv4_interface": "0.0.0.0", '
            '    "ipv4_port": 6162, '
            '    "ipv6_interface": "::1", '
            '    "ipv6_port": 6162 }, '
            '"cache": { '
            '    "dns_cache_ttl_seconds": 60 }, '
            '"publisher": { '
            '    "http_timeout_milliseconds": 1500, '
            '    "http_retries": 3, '
            '    "http_milliseconds_between_retries": 750, '
            '    "http_primary_publisher": "true", '
            '    "http_peer_publisher": "unavailable", '
            '    "max_traps_between_publishes": 10, '
            '    "max_milliseconds_between_publishes": 10000 }, '
            '"streams_publishes": { '
            '    "sec_fault_unsecure": { '
            '        "type": "message_router", '
            '        "aaf_password": null, '
            '        "dmaap_info": { '
            '            "location": "mtl5", '
            '            "client_id": null, '
            '            "client_role": null, '
            '            "topic_url": "http://localhost:3904/events/ONAP-COLLECTOR-SNMPTRAP" }, '
            '        "aaf_username": null } }, '
            '"files": { '
            '    "runtime_base_dir": "/tmp/opt/app/snmptrap", '
            '    "log_dir": "logs", '
            '    "data_dir": "data", '
            '    "pid_dir": "tmp", '
            '    "arriving_traps_log": "snmptrapd_arriving_traps.log", '
            '    "snmptrapd_diag": "snmptrapd_prog_diag.log", '
            '    "traps_stats_log": "snmptrapd_stats.csv", '
            '    "perm_status_file": "snmptrapd_status.log", '
            '    "eelf_base_dir": "/tmp/opt/app/snmptrap/logs", '
            '    "eelf_error": "error.log", '
            '    "eelf_debug": "debug.log", '
            '    "eelf_audit": "audit.log", '
            '    "eelf_metrics": "metrics.log", '
            '    "roll_frequency": "day", '
            '    "minimum_severity_to_log": 2 }, '
            '"trap_config": { '
            '    "sw_interval_in_seconds": 60, '
            '    "notify_oids": { '
            '        ".1.3.6.1.4.1.9.0.1": { '
            '            "sw_high_water_in_interval": 102, '
            '            "sw_low_water_in_interval": 7, '
            '            "category": "logonly" }, '
            '        ".1.3.6.1.4.1.9.0.2": { '
            '            "sw_high_water_in_interval": 101, '
            '            "sw_low_water_in_interval": 7, '
            '            "category": "logonly" }, '
            '        ".1.3.6.1.4.1.9.0.3": { '
            '            "sw_high_water_in_interval": 102, '
            '            "sw_low_water_in_interval": 7, '
            '            "category": "logonly" }, '
            '        ".1.3.6.1.4.1.9.0.4": { '
            '            "sw_high_water_in_interval": 10, '
            '            "sw_low_water_in_interval": 3, '
            '            "category": "logonly" } } }, '
            '"snmpv3_config": { '
            '    "usm_users": [ { '
            '        "user": "usr-sha-aes256", '
            '        "engineId": "8000000001020304", '
            '        "usmHMACSHAAuth": "authkey1", '
            '        "usmAesCfb256": "privkey1" }, '
            '    { "user": "user1", '
            '        "engineId": "8000000000000001", '
            '        "usmHMACMD5Auth": "authkey1", '
            '        "usmDESPriv": "privkey1" }, '
            '    { "user": "user2", '
            '        "engineId": "8000000000000002", '
            '        "usmHMACSHAAuth": "authkey2", '
            '        "usmAesCfb128": "privkey2" }, '
            '    { "user": "user3", '
            '        "engineId": "8000000000000003", '
            '        "usmHMACSHAAuth": "authkey3", '
            '        "usmAesCfb256": "privkey3" } '
            '] } }'
        )
        # fmt: on
        tds.json_traps_filename = (
            tds.c_config["files"]["runtime_base_dir"] + "/json_traps.json"
        )
        tds.arriving_traps_filename = (
            tds.c_config["files"]["runtime_base_dir"] + "/arriving_traps.log"
        )


    def test_open_eelf_error_file(self):
        """
        Test bad error file location
        """

        with patch.dict(tds.c_config["files"]):
            # open eelf error logs
            tds.c_config["files"]["eelf_error"] = "/bad_dir/error.log"

            # try to open file in non-existent dir
            with self.assertRaises(SystemExit):
                result = trapd_io.open_eelf_logs()


    def test_open_eelf_debug_file(self):
        """
        Test bad debug file location
        """

        # open eelf debug logs
        with patch.dict(tds.c_config["files"]):
            tds.c_config["files"]["eelf_debug"] = "/bad_dir/debug.log"

            # try to open file in non-existent dir
            with self.assertRaises(SystemExit):
                result = trapd_io.open_eelf_logs()


    def test_open_eelf_audit_file(self):
        """
        Test bad audit file location
        """

        with patch.dict(tds.c_config["files"]):
            # open eelf debug logs
            tds.c_config["files"]["eelf_audit"] = "/bad_dir/audit.log"

            # try to open file in non-existent dir
            with self.assertRaises(SystemExit):
                result = trapd_io.open_eelf_logs()


    def test_open_eelf_metrics_file(self):
        """
        Test bad metrics file location
        """

        with patch.dict(tds.c_config["files"]):
            # open eelf debug logs
            tds.c_config["files"]["eelf_metrics"] = "/bad_dir/metrics.log"

            # try to open file in non-existent dir
            with self.assertRaises(SystemExit):
                result = trapd_io.open_eelf_logs()


    def test_open_eelf_error_file_missing_name(self):
        """
        Test bad error file location
        """

        with patch.dict(tds.c_config["files"]):
            # open eelf error logs
            del tds.c_config["files"]["eelf_error"]

            # try to open file in non-existent dir
            with self.assertRaises(SystemExit):
                result = trapd_io.open_eelf_logs()


    def test_open_eelf_debug_file_missing_name(self):
        """
        Test bad debug file location
        """

        # open eelf debug logs
        with patch.dict(tds.c_config["files"]):
            del tds.c_config["files"]["eelf_debug"]

            # try to open file in non-existent dir
            with self.assertRaises(SystemExit):
                result = trapd_io.open_eelf_logs()


    def test_open_eelf_audit_file_missing_name(self):
        """
        Test bad audit file location
        """

        with patch.dict(tds.c_config["files"]):
            # open eelf debug logs
            del tds.c_config["files"]["eelf_audit"]

            # try to open file in non-existent dir
            with self.assertRaises(SystemExit):
                result = trapd_io.open_eelf_logs()


    def test_open_eelf_metrics_file_missing_name(self):
        """
        Test bad metrics file location
        """

        with patch.dict(tds.c_config["files"]):
            # open eelf debug logs
            del tds.c_config["files"]["eelf_metrics"]

            # try to open file in non-existent dir
            with self.assertRaises(SystemExit):
                result = trapd_io.open_eelf_logs()


    def test_roll_all_logs_not_open(self):
        """
        Test roll of logs when not open
        """

        # try to roll logs when not open. Shouldn't fail
        trapd_io.roll_all_logs()
        self.assertIsNotNone(tds.eelf_error_fd)

    def test_roll_all_logs(self):
        """
        Test rolling files that they are open
        """

        trapd_io.open_eelf_logs()
        # try to roll logs
        trapd_io.roll_all_logs()
        self.assertIsNotNone(tds.eelf_error_fd)


    def test_roll_all_logs_roll_file_throws(self):
        """
        Test rolling files that they are open
        but roll_file throws an exception
        """

        trapd_io.open_eelf_logs()
        # try to roll logs
        with patch('trapd_io.roll_file') as roll_file_throws:
            roll_file_throws.side_effect = RuntimeError("roll_file() throws")
            with self.assertRaises(SystemExit):
                trapd_io.roll_all_logs()
                self.assertIsNotNone(tds.eelf_error_fd)


    def test_roll_all_logs_open_eelf_logs_returns_false(self):
        """
        Test rolling files that they are open
        but open_eelf_logs returns false
        """

        trapd_io.open_eelf_logs()
        # try to roll logs
        with patch('trapd_io.open_eelf_logs') as open_eelf_logs_throws:
            open_eelf_logs_throws.return_value = False
            with self.assertRaises(SystemExit):
                trapd_io.roll_all_logs()
                self.assertIsNotNone(tds.eelf_error_fd)


    def test_roll_all_logs_open_file_json_traps_throws(self):
        """
        Test rolling files that they are open
        but open_file(json_traps_filename) throws an exception
        """

        def tmp_func(nm):
            if nm == tds.json_traps_filename:
                raise RuntimeError("json_traps_filename throws")
            return test_trapd_io.PseudoFile()

        trapd_io.open_eelf_logs()
        # try to roll logs
        with patch('trapd_io.open_file') as open_file_throws:
            open_file_throws.side_effect = tmp_func
            with self.assertRaises(SystemExit):
                trapd_io.roll_all_logs()
                self.assertIsNotNone(tds.eelf_error_fd)


    def test_roll_all_logs_open_file_arriving_traps_throws(self):
        """
        Test rolling files that they are open
        but open_file(arriving_traps_filename) throws an exception
        """

        def tmp_func(nm):
            if nm == tds.arriving_traps_filename:
                raise RuntimeError("arriving_traps_filename throws")
            return test_trapd_io.PseudoFile()

        trapd_io.open_eelf_logs()
        # try to roll logs
        with patch('trapd_io.open_file') as open_file_throws:
            open_file_throws.side_effect = tmp_func
            with self.assertRaises(SystemExit):
                trapd_io.roll_all_logs()
                self.assertIsNotNone(tds.eelf_error_fd)


    def test_roll_file(self):
        """
        Test roll of individual file when not present
        """

        # try to roll a valid log file
        with tempfile.TemporaryDirectory() as ntd:
            fn = ntd + "/test.log"
            with open(fn, "w") as ofp:
                self.assertTrue(trapd_io.roll_file(fn))
                # The file will be renamed to something like
                # test.log.2022-08-17T20:28:32
                self.assertFalse(os.path.exists(fn))
                # We could also add a test to see if there is a file
                # with a name like that.
                files = list(glob.glob(f"{ntd}/*"))
                print(f"files={files}")
                self.assertEqual(len(files), 1)
                self.assertTrue(files[0].startswith(fn + "."))


    def test_roll_file_not_present(self):
        """
        Test roll of individual file when not present
        """

        # try to roll logs when not open
        self.assertFalse(trapd_io.roll_file("/file/not/present"))


    def test_roll_file_no_write_perms(self):
        """
        try to roll logs when not enough perms
        """

        with tempfile.TemporaryDirectory() as no_perms_dir:
            # no_perms_dir = "/tmp/opt/app/snmptrap/no_perms"
            no_perms_file = "test.dat"
            no_perms_fp = no_perms_dir + "/" + no_perms_file

            # required directory tree
            #try:
            #    Path(no_perms_dir).mkdir(parents=True, exist_ok=True)
            #    os.chmod(no_perms_dir, 0o700)
            #except Exception as e:
            #    self.fail("Error while running %s : %s" % (os.path.basename(__file__), str(e.strerror)))

            # create empty file
            open(no_perms_fp, "w").close()
            os.chmod(no_perms_dir, 0o555)

            # try to roll file in dir with no write perms
            self.assertFalse(trapd_io.roll_file(no_perms_fp))

            # the file should still be there
            open(no_perms_fp).close()

            # allow the directory to be removed
            os.chmod(no_perms_dir, 0o700)


    def test_open_file_exists(self):
        """
        Test file open in directory present
        """

        # create copy of snmptrapd.json for pytest
        test_file = "/tmp/snmptrap_pytest"

        # try to roll logs when not open
        result = trapd_io.open_file(test_file)
        self.assertTrue(str(result).startswith("<_io.TextIOWrapper name="))
        self.assertIsInstance(result, io.TextIOWrapper)


    def test_open_file_exists_does_not_exist(self):
        """
        Test file open in directory present
        """

        # create copy of snmptrapd.json for pytest
        test_file = "/tmp/no_such_dir/snmptrap_pytest"

        # try to open file when dir not present
        with self.assertRaises(SystemExit):
            result = trapd_io.open_file(test_file)


    def test_close_file_exists(self):
        """
        Test closing a file that's present
        """

        # create copy of snmptrapd.json for pytest
        test_file_name = "/tmp/snmptrap_pytest"
        test_file = trapd_io.open_file(test_file_name)

        # close active file
        self.assertTrue(trapd_io.close_file(test_file, test_file_name))


    def test_close_file_does_not_exist(self):
        """
        Test closing non-existent file
        """

        # try to roll logs when not open
        self.assertFalse(trapd_io.close_file(None, None))


    def test_ecomp_logger_type_error(self):
        """
        test trapd_io.ecomp_logger
        """

        trapd_io.open_eelf_logs()
        msg = "this is a test"
        self.assertTrue(trapd_io.ecomp_logger(tds.LOG_TYPE_ERROR, tds.SEV_ERROR, tds.CODE_GENERAL, msg))


    def test_ecomp_logger_type_error_bad_fd(self):
        """
        test trapd_io.ecomp_logger, but write() throws
        """

        trapd_io.open_eelf_logs()
        msg = "this is a test"
        # the following SHOULD be done with a context patch
        sv_eelf_error_fd = tds.eelf_error_fd
        tds.eelf_error_fd = test_trapd_io.WriteThrows()
        self.assertTrue(trapd_io.ecomp_logger(tds.LOG_TYPE_ERROR, tds.SEV_ERROR, tds.CODE_GENERAL, msg))
        tds.eelf_error_fd = sv_eelf_error_fd


    def test_ecomp_logger_type_unknown_bad_fd(self):
        """
        test trapd_io.ecomp_logger, unknown type, but write() throws
        """

        trapd_io.open_eelf_logs()
        msg = "this is a test"
        # the following SHOULD be done with a context patch
        sv_eelf_error_fd = tds.eelf_error_fd
        tds.eelf_error_fd = test_trapd_io.WriteThrows()
        self.assertFalse(trapd_io.ecomp_logger(-1, tds.SEV_ERROR, tds.CODE_GENERAL, msg))
        tds.eelf_error_fd = sv_eelf_error_fd


    def test_ecomp_logger_type_metrics(self):
        """
        test trapd_io.ecomp_logger to metrics
        """

        trapd_io.open_eelf_logs()
        msg = "this is a test"
        self.assertTrue(trapd_io.ecomp_logger(tds.LOG_TYPE_METRICS, tds.SEV_ERROR, tds.CODE_GENERAL, msg))


    def test_ecomp_logger_type_metrics_bad_fd(self):
        """
        test trapd_io.ecomp_logger to metrics, but write() throws
        """

        trapd_io.open_eelf_logs()
        msg = "this is a test"
        # the following SHOULD be done with a context patch
        sv_eelf_metrics_fd = tds.eelf_metrics_fd
        tds.eelf_metrics_fd = test_trapd_io.WriteThrows()
        self.assertTrue(trapd_io.ecomp_logger(tds.LOG_TYPE_METRICS, tds.SEV_ERROR, tds.CODE_GENERAL, msg))
        tds.eelf_metrics_fd = sv_eelf_metrics_fd


    def test_ecomp_logger_type_audit(self):
        """
        test trapd_io.ecomp_logger to audit log
        """

        trapd_io.open_eelf_logs()
        msg = "this is a test"
        self.assertTrue(trapd_io.ecomp_logger(tds.LOG_TYPE_AUDIT, tds.SEV_ERROR, tds.CODE_GENERAL, msg))


    def test_ecomp_logger_type_audit_bad_fd(self):
        """
        test trapd_io.ecomp_logger to audit log, but write() throws
        """

        trapd_io.open_eelf_logs()
        msg = "this is a test"
        # the following SHOULD be done with a context patch
        sv_eelf_audit_fd = tds.eelf_audit_fd
        tds.eelf_audit_fd = test_trapd_io.WriteThrows()
        self.assertTrue(trapd_io.ecomp_logger(tds.LOG_TYPE_AUDIT, tds.SEV_ERROR, tds.CODE_GENERAL, msg))
        tds.eelf_audit_fd = sv_eelf_audit_fd


    def test_ecomp_logger_type_unknown(self):
        """
        test trapd_io.ecomp_logger
        """

        trapd_io.open_eelf_logs()
        msg = "this is a test"
        self.assertFalse(trapd_io.ecomp_logger(-1, tds.SEV_ERROR, tds.CODE_GENERAL, msg))


if __name__ == "__main__": # pragma: no cover
    unittest.main()