From c4eeea7820b7bbf6f5994c9878c8838c43e61b9b Mon Sep 17 00:00:00 2001 From: Sivasathurappan Radhakrishnan Date: Thu, 29 Oct 2015 23:04:42 +0000 Subject: [PATCH] Added unit test cases for container/auditor.py Added unit test cases to cover all code path in _one_audit_pass function in container/auditor.py Change-Id: I8b89e94f1492e4366af3ac4260587e988ba29408 --- test/unit/container/test_auditor.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/unit/container/test_auditor.py b/test/unit/container/test_auditor.py index 6d9b28d640..fe004c6743 100644 --- a/test/unit/container/test_auditor.py +++ b/test/unit/container/test_auditor.py @@ -20,6 +20,7 @@ import os import random from tempfile import mkdtemp from shutil import rmtree +from eventlet import Timeout from swift.common.utils import normalize_timestamp from swift.container import auditor @@ -93,6 +94,12 @@ class TestAuditor(unittest.TestCase): self.assertEqual(test_auditor.container_failures, 2 * call_times) self.assertEqual(test_auditor.container_passes, 3 * call_times) + # now force timeout path code coverage + with mock.patch('swift.container.auditor.ContainerAuditor.' + '_one_audit_pass', side_effect=Timeout()): + with mock.patch('swift.container.auditor.time', FakeTime()): + self.assertRaises(ValueError, test_auditor.run_forever) + @mock.patch('swift.container.auditor.ContainerBroker', FakeContainerBroker) def test_run_once(self): conf = {} @@ -108,6 +115,23 @@ class TestAuditor(unittest.TestCase): self.assertEqual(test_auditor.container_failures, 2) self.assertEqual(test_auditor.container_passes, 3) + @mock.patch('swift.container.auditor.ContainerBroker', FakeContainerBroker) + def test_one_audit_pass(self): + conf = {} + test_auditor = auditor.ContainerAuditor(conf, logger=self.logger) + + def fake_audit_location_generator(*args, **kwargs): + files = sorted(os.listdir(self.testdir)) + return [(os.path.join(self.testdir, f), '', '') for f in files] + + # force code coverage for logging path + test_auditor.logging_interval = 0 + with mock.patch('swift.container.auditor.audit_location_generator', + fake_audit_location_generator): + test_auditor._one_audit_pass(test_auditor.logging_interval) + self.assertEqual(test_auditor.container_failures, 1) + self.assertEqual(test_auditor.container_passes, 3) + @mock.patch('swift.container.auditor.ContainerBroker', FakeContainerBroker) def test_container_auditor(self): conf = {}