fix name 'recon_container' to 'rcache'
self.recon_container is called before assigned. Accounding the context codes, this is used to store somehing like self.rcache. So I guess the name of 'recon_container' is a mistake, and change it to 'rcache' (we could look at other places using dump_recon_cache) Also add unit tests fixes bug #1201958 Change-Id: I3a6e3d22ba1dbffc4309bc22ff37873b4a3f09b3
This commit is contained in:
@ -102,7 +102,7 @@ class ContainerAuditor(Daemon):
|
||||
self.logger.info(
|
||||
_('Container audit "once" mode completed: %.02fs'), elapsed)
|
||||
dump_recon_cache({'container_auditor_pass_completed': elapsed},
|
||||
self.recon_container)
|
||||
self.rcache, self.logger)
|
||||
|
||||
def container_audit(self, path):
|
||||
"""
|
||||
|
@ -13,15 +13,108 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# TODO: Tests
|
||||
|
||||
import unittest
|
||||
import time
|
||||
import os
|
||||
import random
|
||||
from tempfile import mkdtemp
|
||||
from shutil import rmtree
|
||||
|
||||
from swift.container import auditor
|
||||
from test.unit import FakeLogger
|
||||
|
||||
|
||||
class TestReaper(unittest.TestCase):
|
||||
def test_placeholder(self):
|
||||
pass
|
||||
class FakeContainerBroker(object):
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
self.db_file = path
|
||||
self.file = os.path.basename(path)
|
||||
|
||||
def is_deleted(self):
|
||||
return False
|
||||
|
||||
def get_info(self):
|
||||
if self.file.startswith('fail'):
|
||||
raise ValueError
|
||||
if self.file.startswith('true'):
|
||||
return 'ok'
|
||||
|
||||
|
||||
class TestAuditor(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.testdir = os.path.join(mkdtemp(), 'tmp_test_container_auditor')
|
||||
self.logger = FakeLogger()
|
||||
rmtree(self.testdir, ignore_errors=1)
|
||||
os.mkdir(self.testdir)
|
||||
fnames = ['true1.db', 'true2.db', 'true3.db',
|
||||
'fail1.db', 'fail2.db']
|
||||
for fn in fnames:
|
||||
with open(os.path.join(self.testdir, fn), 'w+') as f:
|
||||
f.write(' ')
|
||||
|
||||
def tearDown(self):
|
||||
rmtree(os.path.dirname(self.testdir), ignore_errors=1)
|
||||
|
||||
def test_run_forever(self):
|
||||
sleep_times = random.randint(5, 10)
|
||||
call_times = sleep_times - 1
|
||||
|
||||
class FakeTime(object):
|
||||
def __init__(self):
|
||||
self.times = 0
|
||||
|
||||
def sleep(self, sec):
|
||||
self.times += 1
|
||||
if self.times < sleep_times:
|
||||
time.sleep(0.1)
|
||||
else:
|
||||
# stop forever by an error
|
||||
raise ValueError()
|
||||
|
||||
def time(self):
|
||||
return time.time()
|
||||
|
||||
conf = {}
|
||||
test_auditor = auditor.ContainerAuditor(conf)
|
||||
auditor.ContainerBroker = FakeContainerBroker
|
||||
auditor.time = FakeTime()
|
||||
|
||||
def fake_audit_location_generator(*args, **kwargs):
|
||||
files = os.listdir(self.testdir)
|
||||
return [(os.path.join(self.testdir, f), '', '') for f in files]
|
||||
|
||||
auditor.audit_location_generator = fake_audit_location_generator
|
||||
|
||||
self.assertRaises(ValueError, test_auditor.run_forever)
|
||||
self.assertEquals(test_auditor.container_failures, 2 * call_times)
|
||||
self.assertEquals(test_auditor.container_passes, 3 * call_times)
|
||||
|
||||
def test_run_once(self):
|
||||
conf = {}
|
||||
test_auditor = auditor.ContainerAuditor(conf)
|
||||
auditor.ContainerBroker = FakeContainerBroker
|
||||
|
||||
def fake_audit_location_generator(*args, **kwargs):
|
||||
files = os.listdir(self.testdir)
|
||||
return [(os.path.join(self.testdir, f), '', '') for f in files]
|
||||
|
||||
auditor.audit_location_generator = fake_audit_location_generator
|
||||
|
||||
test_auditor.run_once()
|
||||
self.assertEquals(test_auditor.container_failures, 2)
|
||||
self.assertEquals(test_auditor.container_passes, 3)
|
||||
|
||||
def test_container_auditor(self):
|
||||
conf = {}
|
||||
test_auditor = auditor.ContainerAuditor(conf)
|
||||
auditor.ContainerBroker = FakeContainerBroker
|
||||
files = os.listdir(self.testdir)
|
||||
for f in files:
|
||||
path = os.path.join(self.testdir, f)
|
||||
test_auditor.container_audit(path)
|
||||
self.assertEquals(test_auditor.container_failures, 2)
|
||||
self.assertEquals(test_auditor.container_passes, 3)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Reference in New Issue
Block a user