Example #1
0
    def test_db_migration(self, tempdir, mock_recon):
        db_path = os.path.join(tempdir, 'sda', 'containers', '0', '0', '0',
                               'test.db')
        with test_backend.TestContainerBrokerBeforeSPI.old_broker() as \
                old_ContainerBroker:
            broker = old_ContainerBroker(db_path, account='a', container='c')
            broker.initialize(normalize_timestamp(0), -1)

        with broker.get() as conn:
            try:
                conn.execute('SELECT storage_policy_index '
                             'FROM container_stat')
            except Exception as err:
                self.assertTrue(
                    'no such column: storage_policy_index' in str(err))
            else:
                self.fail('TestContainerBrokerBeforeSPI broker class '
                          'was already migrated')

        conf = {'devices': tempdir, 'mount_check': False}
        test_auditor = auditor.ContainerAuditor(conf, logger=debug_logger())
        test_auditor.run_once()

        broker = auditor.ContainerBroker(db_path, account='a', container='c')
        info = broker.get_info()
        expected = {
            'account': 'a',
            'container': 'c',
            'object_count': 0,
            'bytes_used': 0,
            'storage_policy_index': 0,
        }
        for k, v in expected.items():
            self.assertEqual(info[k], v)
Example #2
0
    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, logger=self.logger)

        with mock.patch('swift.container.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]

            with mock.patch('swift.container.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)
Example #3
0
 def test_container_auditor(self):
     conf = {}
     test_auditor = auditor.ContainerAuditor(conf, logger=self.logger)
     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)
Example #4
0
    def test_run_once(self):
        conf = {}
        test_auditor = auditor.ContainerAuditor(conf, logger=self.logger)

        def fake_audit_location_generator(*args, **kwargs):
            files = os.listdir(self.testdir)
            return [(os.path.join(self.testdir, f), '', '') for f in files]

        with mock.patch('swift.container.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)
Example #5
0
    def test_run_once(self):
        conf = {}
        test_auditor = auditor.ContainerAuditor(conf)

        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)
Example #6
0
    def test_one_audit_pass(self, mock_recon):
        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)