def test_check_drive_isdir(self): root = '/srv' path = 'sdb2' with mock_check_drive(isdir=True) as mocks: self.assertEqual('/srv/sdb2', constraints.check_dir(root, path)) self.assertEqual('/srv/sdb2', constraints.check_drive(root, path, False)) self.assertEqual([mock.call('/srv/sdb2'), mock.call('/srv/sdb2')], mocks['isdir'].call_args_list) self.assertEqual([], mocks['ismount'].call_args_list) with mock_check_drive(isdir=True) as mocks: with self.assertRaises(ValueError) as exc_mgr: constraints.check_mount(root, path) self.assertEqual(str(exc_mgr.exception), '/srv/sdb2 is not mounted') with self.assertRaises(ValueError) as exc_mgr: constraints.check_drive(root, path, True) self.assertEqual(str(exc_mgr.exception), '/srv/sdb2 is not mounted') self.assertEqual([], mocks['isdir'].call_args_list) self.assertEqual([mock.call('/srv/sdb2'), mock.call('/srv/sdb2')], mocks['ismount'].call_args_list)
def test_run_once(self): def prepare_data_dir(): devices_path = tempfile.mkdtemp() # will be deleted by teardown self.to_delete.append(devices_path) path = os.path.join(devices_path, 'sda1', DATADIR) os.makedirs(path) return devices_path def init_reaper(devices): r = reaper.AccountReaper({'devices': devices}) return r devices = prepare_data_dir() r = init_reaper(devices) with patch('swift.account.reaper.AccountReaper.reap_device') as foo, \ unit.mock_check_drive(ismount=True): r.run_once() self.assertEqual(foo.called, 1) with patch('swift.account.reaper.AccountReaper.reap_device') as foo, \ unit.mock_check_drive(ismount=False): r.run_once() self.assertFalse(foo.called) with patch('swift.account.reaper.AccountReaper.reap_device') as foo: r.logger = unit.debug_logger('test-reaper') r.devices = 'thisdeviceisbad' r.run_once() self.assertTrue( r.logger.get_lines_for_level('error')[-1].startswith( 'Exception in top-level account reaper'))
def test_check_drive_isdir(self): root = '/srv' path = 'sdb2' with mock_check_drive(isdir=True) as mocks: self.assertEqual('/srv/sdb2', constraints.check_dir(root, path)) self.assertEqual('/srv/sdb2', constraints.check_drive( root, path, False)) self.assertEqual([mock.call('/srv/sdb2'), mock.call('/srv/sdb2')], mocks['isdir'].call_args_list) self.assertEqual([], mocks['ismount'].call_args_list) with mock_check_drive(isdir=True) as mocks: with self.assertRaises(ValueError) as exc_mgr: constraints.check_mount(root, path) self.assertEqual(str(exc_mgr.exception), '/srv/sdb2 is not mounted') with self.assertRaises(ValueError) as exc_mgr: constraints.check_drive(root, path, True) self.assertEqual(str(exc_mgr.exception), '/srv/sdb2 is not mounted') self.assertEqual([], mocks['isdir'].call_args_list) self.assertEqual([mock.call('/srv/sdb2'), mock.call('/srv/sdb2')], mocks['ismount'].call_args_list)
def test_run_once(self): def prepare_data_dir(): devices_path = tempfile.mkdtemp() # will be deleted by teardown self.to_delete.append(devices_path) path = os.path.join(devices_path, 'sda1', DATADIR) os.makedirs(path) return devices_path def init_reaper(devices): r = reaper.AccountReaper({'devices': devices}) return r devices = prepare_data_dir() r = init_reaper(devices) with patch('swift.account.reaper.AccountReaper.reap_device') as foo, \ unit.mock_check_drive(ismount=True): r.run_once() self.assertEqual(foo.called, 1) with patch('swift.account.reaper.AccountReaper.reap_device') as foo, \ unit.mock_check_drive(ismount=False): r.run_once() self.assertFalse(foo.called) with patch('swift.account.reaper.AccountReaper.reap_device') as foo: r.logger = unit.debug_logger('test-reaper') r.devices = 'thisdeviceisbad' r.run_once() self.assertTrue(r.logger.get_lines_for_level( 'error')[-1].startswith('Exception in top-level account reaper'))
def test_check_drive_invalid_path(self): root = '/srv/' with mock_check_drive() as mocks: drive = 'foo?bar' with self.assertRaises(ValueError) as exc_mgr: constraints.check_dir(root, drive) self.assertEqual(str(exc_mgr.exception), '%s is not a valid drive name' % drive) drive = 'foo bar' with self.assertRaises(ValueError) as exc_mgr: constraints.check_mount(root, drive) self.assertEqual(str(exc_mgr.exception), '%s is not a valid drive name' % drive) drive = 'foo/bar' with self.assertRaises(ValueError) as exc_mgr: constraints.check_drive(root, drive, True) self.assertEqual(str(exc_mgr.exception), '%s is not a valid drive name' % drive) drive = 'foo%bar' with self.assertRaises(ValueError) as exc_mgr: constraints.check_drive(root, drive, False) self.assertEqual(str(exc_mgr.exception), '%s is not a valid drive name' % drive) self.assertEqual([], mocks['isdir'].call_args_list) self.assertEqual([], mocks['ismount'].call_args_list)
def test_check_drive_invalid_path(self): root = '/srv/' with mock_check_drive() as mocks: drive = 'foo?bar' with self.assertRaises(ValueError) as exc_mgr: constraints.check_dir(root, drive) self.assertEqual(str(exc_mgr.exception), '%s is not a valid drive name' % drive) drive = 'foo bar' with self.assertRaises(ValueError) as exc_mgr: constraints.check_mount(root, drive) self.assertEqual(str(exc_mgr.exception), '%s is not a valid drive name' % drive) drive = 'foo/bar' with self.assertRaises(ValueError) as exc_mgr: constraints.check_drive(root, drive, True) self.assertEqual(str(exc_mgr.exception), '%s is not a valid drive name' % drive) drive = 'foo%bar' with self.assertRaises(ValueError) as exc_mgr: constraints.check_drive(root, drive, False) self.assertEqual(str(exc_mgr.exception), '%s is not a valid drive name' % drive) self.assertEqual([], mocks['isdir'].call_args_list) self.assertEqual([], mocks['ismount'].call_args_list)
def test_check_drive_isdir(self): root = '/srv' path = 'sdb2' with mock_check_drive(isdir=True) as mocks: self.assertEqual('/srv/sdb2', constraints.check_dir(root, path)) self.assertEqual('/srv/sdb2', constraints.check_drive( root, path, False)) self.assertEqual([mock.call('/srv/sdb2'), mock.call('/srv/sdb2')], mocks['isdir'].call_args_list) self.assertEqual([], mocks['ismount'].call_args_list) with mock_check_drive(isdir=True) as mocks: self.assertIsNone(constraints.check_mount(root, path)) self.assertIsNone(constraints.check_drive(root, path, True)) self.assertEqual([], mocks['isdir'].call_args_list) self.assertEqual([mock.call('/srv/sdb2'), mock.call('/srv/sdb2')], mocks['ismount'].call_args_list)
def test_check_drive_isdir(self): root = '/srv' path = 'sdb2' with mock_check_drive(isdir=True) as mocks: self.assertEqual('/srv/sdb2', constraints.check_dir(root, path)) self.assertEqual('/srv/sdb2', constraints.check_drive(root, path, False)) self.assertEqual([mock.call('/srv/sdb2'), mock.call('/srv/sdb2')], mocks['isdir'].call_args_list) self.assertEqual([], mocks['ismount'].call_args_list) with mock_check_drive(isdir=True) as mocks: self.assertIsNone(constraints.check_mount(root, path)) self.assertIsNone(constraints.check_drive(root, path, True)) self.assertEqual([], mocks['isdir'].call_args_list) self.assertEqual([mock.call('/srv/sdb2'), mock.call('/srv/sdb2')], mocks['ismount'].call_args_list)
def test_check_drive_invalid_path(self): root = '/srv/' with mock_check_drive() as mocks: self.assertIsNone(constraints.check_dir(root, 'foo?bar')) self.assertIsNone(constraints.check_mount(root, 'foo bar')) self.assertIsNone(constraints.check_drive(root, 'foo/bar', True)) self.assertIsNone(constraints.check_drive(root, 'foo%bar', False)) self.assertEqual([], mocks['isdir'].call_args_list) self.assertEqual([], mocks['ismount'].call_args_list)
def test_check_drive_invalid_path(self): root = '/srv/' with mock_check_drive() as mocks: self.assertIsNone(constraints.check_dir(root, 'foo?bar')) self.assertIsNone(constraints.check_mount(root, 'foo bar')) self.assertIsNone(constraints.check_drive(root, 'foo/bar', True)) self.assertIsNone(constraints.check_drive(root, 'foo%bar', False)) self.assertEqual([], mocks['isdir'].call_args_list) self.assertEqual([], mocks['ismount'].call_args_list)
def test_run_once_with_device_unmounted(self, mock_sweep): cu = self._get_container_updater() containers_dir = os.path.join(self.sda1, DATADIR) os.mkdir(containers_dir) partition_dir = os.path.join(containers_dir, "a") os.mkdir(partition_dir) cu.run_once() self.assertTrue(os.path.exists(containers_dir)) # sanity check # only called if a partition dir exists self.assertTrue(mock_sweep.called) mock_sweep.reset_mock() cu = self._get_container_updater({'mount_check': 'true'}) with mock_check_drive(): cu.run_once() log_lines = self.logger.get_lines_for_level('warning') self.assertGreater(len(log_lines), 0) msg = '%s is not mounted' % self.sda1 self.assertEqual(log_lines[0], msg) # Ensure that the container_sweep did not run self.assertFalse(mock_sweep.called)
def test_run_once_with_device_unmounted(self, mock_sweep): cu = self._get_container_updater() containers_dir = os.path.join(self.sda1, DATADIR) os.mkdir(containers_dir) partition_dir = os.path.join(containers_dir, "a") os.mkdir(partition_dir) cu.run_once() self.assertTrue(os.path.exists(containers_dir)) # sanity check # only called if a partition dir exists self.assertTrue(mock_sweep.called) mock_sweep.reset_mock() cu = self._get_container_updater({'mount_check': 'true'}) with mock_check_drive(): cu.run_once() log_lines = self.logger.get_lines_for_level('warning') self.assertGreater(len(log_lines), 0) msg = 'sda1 is not mounted' self.assertEqual(log_lines[0], msg) # Ensure that the container_sweep did not run self.assertFalse(mock_sweep.called)