aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_trapd_runtime_pid.py
diff options
context:
space:
mode:
authorHansen, Tony (th1395) <th1395@att.com>2022-08-17 21:36:24 +0000
committerHansen, Tony (th1395) <th1395@att.com>2022-08-22 20:40:07 +0000
commit4b6b34642374103b8ccf938eb9e970232bfb63ae (patch)
tree526c304ddc9207831cc27b6b26d01e46be5c763c /tests/test_trapd_runtime_pid.py
parent5116f87391d00c04b789a59d1abcebbbf80147ad (diff)
CodeCoverage improvement (60% to 90%)
Change-Id: I579e9d3fedbd6cc2589d189b121fa7dadfb6f7f1 Signed-off-by: Hansen, Tony (th1395) <th1395@att.com> Issue-ID: DCAEGEN2-3158 Signed-off-by: Hansen, Tony (th1395) <th1395@att.com>
Diffstat (limited to 'tests/test_trapd_runtime_pid.py')
-rw-r--r--tests/test_trapd_runtime_pid.py39
1 files changed, 26 insertions, 13 deletions
diff --git a/tests/test_trapd_runtime_pid.py b/tests/test_trapd_runtime_pid.py
index c6b8601..923f730 100644
--- a/tests/test_trapd_runtime_pid.py
+++ b/tests/test_trapd_runtime_pid.py
@@ -1,5 +1,5 @@
# ============LICENSE_START=======================================================
-# Copyright (c) 2019-2021 AT&T Intellectual Property. All rights reserved.
+# 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.
@@ -14,24 +14,30 @@
# limitations under the License.
# ============LICENSE_END=========================================================
-import pytest
+import os
import unittest
-import trapd_runtime_pid
+from unittest.mock import patch
+
import trapd_io
+import trapd_runtime_pid
-class test_save_pid(unittest.TestCase):
+class test_trapd_runtime_pid(unittest.TestCase):
"""
Test the save_pid mod
"""
+ GOOD_PID_FILE = "/tmp/snmptrap_test_pid_file"
+ BAD_PID_FILE = "/tmp/snmptrap_test_pid_file_not_there"
+
def test_correct_usage(self):
"""
Test that attempt to create pid file in standard location works
"""
- result = trapd_runtime_pid.save_pid("/tmp/snmptrap_test_pid_file")
+ result = trapd_runtime_pid.save_pid(test_trapd_runtime_pid.GOOD_PID_FILE)
self.assertEqual(result, True)
+
def test_missing_directory(self):
"""
Test that attempt to create pid file in missing dir fails
@@ -40,7 +46,6 @@ class test_save_pid(unittest.TestCase):
self.assertEqual(result, False)
-class test_rm_pid(unittest.TestCase):
"""
Test the rm_pid mod
"""
@@ -50,18 +55,26 @@ class test_rm_pid(unittest.TestCase):
Test that attempt to remove pid file in standard location works
"""
# must create it before removing it
- result = trapd_runtime_pid.save_pid("/tmp/snmptrap_test_pid_file")
- self.assertEqual(result, True)
- result = trapd_runtime_pid.rm_pid("/tmp/snmptrap_test_pid_file")
- self.assertEqual(result, True)
+ self.assertTrue(trapd_runtime_pid.save_pid(test_trapd_runtime_pid.GOOD_PID_FILE))
+ self.assertTrue(trapd_runtime_pid.rm_pid(test_trapd_runtime_pid.GOOD_PID_FILE))
+
def test_missing_file(self):
"""
Test that attempt to rm non-existent pid file fails
"""
- result = trapd_runtime_pid.rm_pid("/tmp/snmptrap_test_pid_file_9999")
- self.assertEqual(result, False)
+ self.assertFalse(trapd_runtime_pid.rm_pid(test_trapd_runtime_pid.BAD_PID_FILE))
+
+
+ def test_correct_usage_but_throws(self):
+ """
+ Test that an exception while removing returns false
+ """
+ self.assertTrue(trapd_runtime_pid.save_pid(test_trapd_runtime_pid.GOOD_PID_FILE))
+ with patch('os.remove') as mock_remove:
+ mock_remove.side_effect = RuntimeError("os.remove throws")
+ self.assertFalse(trapd_runtime_pid.rm_pid(test_trapd_runtime_pid.GOOD_PID_FILE))
-if __name__ == "__main__":
+if __name__ == "__main__": # pragma: no cover
unittest.main()