def test_successfully(self, mock_move, mock_umount, mock_mount, mock_chmod, mock_exists, mock_makedirs): '''Tests calling NfsBroker.move_files() successfully''' def new_exists(path): return False mock_exists.side_effect = new_exists mount = 'host:/dir' work_dir = os.path.join('the', 'work', 'dir') file_1 = 'my_file.txt' file_2 = 'my_file.json' old_workspace_path_1 = os.path.join('my_dir_1', file_1) old_workspace_path_2 = os.path.join('my_dir_2', file_1) new_workspace_path_1 = os.path.join('my_new_dir_1', file_1) new_workspace_path_2 = os.path.join('my_new_dir_2', file_1) full_old_workspace_path_1 = os.path.join(work_dir, old_workspace_path_1) full_old_workspace_path_2 = os.path.join(work_dir, old_workspace_path_2) full_new_workspace_path_1 = os.path.join(work_dir, new_workspace_path_1) full_new_workspace_path_2 = os.path.join(work_dir, new_workspace_path_2) # Call method to test broker = NfsBroker() broker.load_config({'type': NfsBroker.broker_type, 'mount': mount}) broker.move_files(work_dir, [(old_workspace_path_1, new_workspace_path_1), (old_workspace_path_2, new_workspace_path_2)]) # Check results mock_mount.assert_called_once_with(mount, work_dir, False) two_calls = [call(os.path.dirname(full_new_workspace_path_1), mode=0755), call(os.path.dirname(full_new_workspace_path_2), mode=0755)] mock_makedirs.assert_has_calls(two_calls) two_calls = [call(full_old_workspace_path_1, full_new_workspace_path_1), call(full_old_workspace_path_2, full_new_workspace_path_2)] mock_move.assert_has_calls(two_calls) mock_umount.assert_called_once_with(work_dir)
class TestNfsBrokerMoveFiles(TestCase): def setUp(self): django.setup() self.broker = NfsBroker() self.broker.load_configuration({'type': NfsBroker().broker_type, 'nfs_path': 'host:/path'}) @patch('storage.brokers.nfs_broker.os.makedirs') @patch('storage.brokers.nfs_broker.os.path.exists') @patch('storage.brokers.nfs_broker.os.chmod') @patch('storage.brokers.nfs_broker.shutil.move') def test_successfully(self, mock_move, mock_chmod, mock_exists, mock_makedirs): """Tests calling NfsBroker.move_files() successfully""" def new_exists(path): return False mock_exists.side_effect = new_exists volume_path = os.path.join('the', 'volume', 'path') file_name_1 = 'my_file.txt' file_name_2 = 'my_file.json' old_workspace_path_1 = os.path.join('my_dir_1', file_name_1) old_workspace_path_2 = os.path.join('my_dir_2', file_name_2) new_workspace_path_1 = os.path.join('my_new_dir_1', file_name_1) new_workspace_path_2 = os.path.join('my_new_dir_2', file_name_2) full_old_workspace_path_1 = os.path.join(volume_path, old_workspace_path_1) full_old_workspace_path_2 = os.path.join(volume_path, old_workspace_path_2) full_new_workspace_path_1 = os.path.join(volume_path, new_workspace_path_1) full_new_workspace_path_2 = os.path.join(volume_path, new_workspace_path_2) file_1 = storage_test_utils.create_file(file_path=old_workspace_path_1) file_2 = storage_test_utils.create_file(file_path=old_workspace_path_2) file_1_mv = FileMove(file_1, new_workspace_path_1) file_2_mv = FileMove(file_2, new_workspace_path_2) # Call method to test self.broker.move_files(volume_path, [file_1_mv, file_2_mv]) # Check results two_calls = [call(os.path.dirname(full_new_workspace_path_1), mode=0755), call(os.path.dirname(full_new_workspace_path_2), mode=0755)] mock_makedirs.assert_has_calls(two_calls) two_calls = [call(full_old_workspace_path_1, full_new_workspace_path_1), call(full_old_workspace_path_2, full_new_workspace_path_2)] mock_move.assert_has_calls(two_calls) two_calls = [call(full_new_workspace_path_1, 0644), call(full_new_workspace_path_2, 0644)] mock_chmod.assert_has_calls(two_calls) self.assertEqual(file_1.file_path, new_workspace_path_1) self.assertEqual(file_2.file_path, new_workspace_path_2)
class TestNfsBrokerMoveFiles(TestCase): def setUp(self): django.setup() self.broker = NfsBroker() self.broker.load_configuration({ 'type': NfsBroker().broker_type, 'nfs_path': 'host:/path' }) @patch('storage.brokers.nfs_broker.os.makedirs') @patch('storage.brokers.nfs_broker.os.path.exists') @patch('storage.brokers.nfs_broker.os.chmod') @patch('storage.brokers.nfs_broker.shutil.move') def test_successfully(self, mock_move, mock_chmod, mock_exists, mock_makedirs): """Tests calling NfsBroker.move_files() successfully""" def new_exists(path): return path.count('new') == 0 mock_exists.side_effect = new_exists volume_path = os.path.join('the', 'volume', 'path') file_name_1 = 'my_file.txt' file_name_2 = 'my_file.json' old_workspace_path_1 = os.path.join('my_dir_1', file_name_1) old_workspace_path_2 = os.path.join('my_dir_2', file_name_2) new_workspace_path_1 = os.path.join('my_new_dir_1', file_name_1) new_workspace_path_2 = os.path.join('my_new_dir_2', file_name_2) full_old_workspace_path_1 = os.path.join(volume_path, old_workspace_path_1) full_old_workspace_path_2 = os.path.join(volume_path, old_workspace_path_2) full_new_workspace_path_1 = os.path.join(volume_path, new_workspace_path_1) full_new_workspace_path_2 = os.path.join(volume_path, new_workspace_path_2) file_1 = storage_test_utils.create_file(file_path=old_workspace_path_1) file_2 = storage_test_utils.create_file(file_path=old_workspace_path_2) file_1_mv = FileMove(file_1, new_workspace_path_1) file_2_mv = FileMove(file_2, new_workspace_path_2) # Call method to test self.broker.move_files(volume_path, [file_1_mv, file_2_mv]) # Check results two_calls = [ call(os.path.dirname(full_new_workspace_path_1), mode=0755), call(os.path.dirname(full_new_workspace_path_2), mode=0755) ] mock_makedirs.assert_has_calls(two_calls) two_calls = [ call(full_old_workspace_path_1, full_new_workspace_path_1), call(full_old_workspace_path_2, full_new_workspace_path_2) ] mock_move.assert_has_calls(two_calls) two_calls = [ call(full_new_workspace_path_1, 0644), call(full_new_workspace_path_2, 0644) ] mock_chmod.assert_has_calls(two_calls) self.assertEqual(file_1.file_path, new_workspace_path_1) self.assertEqual(file_2.file_path, new_workspace_path_2)