def test_copy_to_output_dir(shutil_, _copy_soscleaner_files): ''' Test that shutil is called to copy the collection to the specified output dir ''' config = InsightsConfig() client = InsightsClient(config) client.copy_to_output_dir('test') shutil_.copytree.assert_called_once() _copy_soscleaner_files.assert_not_called()
def test_copy_to_output_dir_obfuscate_on(shutil_, _copy_soscleaner_files): ''' Test that shutil is called to copy the collection to the specified output dir, and soscleaner copy function is called ''' # obfuscate off, no soscleaner files config = InsightsConfig(obfuscate=True) client = InsightsClient(config) client.copy_to_output_dir('test') shutil_.copytree.assert_called_once() _copy_soscleaner_files.assert_called_once()
def test_copy_to_output_dir_other_oserror(os_, shutil_, _copy_soscleaner_files): ''' Test that any OSError != 17 is logged and we bail out before attempting to copy anything else ''' config = InsightsConfig(output_dir='dest') client = InsightsClient(config) shutil_.copytree.side_effect = [OSError(19, '???'), None, None] client.copy_to_output_dir('src') os_.listdir.assert_not_called shutil_.copytree.assert_called_once_with('src', config.output_dir) shutil_.copyfile.assert_not_called()
def test_copy_to_output_dir_exists_and_empty(os_, shutil_, _copy_soscleaner_files): ''' Test that writing to an existing but empty directory is performed ''' config = InsightsConfig(output_dir='dest') client = InsightsClient(config) # raise file exists error first, then raise for "b" and "c" below shutil_.copytree.side_effect = [OSError(17, 'File exists'), None, None] # returns empty list for destination, file list for source os_.listdir.side_effect = [[], ['a', 'b', 'c']] # os.path.join called 6 times, once for each file per src and dest os_.path.join.side_effect = [ os.path.join('src', 'a'), os.path.join(config.output_dir, 'a'), os.path.join('src', 'b'), os.path.join(config.output_dir, 'b'), os.path.join('src', 'c'), os.path.join(config.output_dir, 'c') ] # 'a' is file, 'b', 'c' are dirs os_.path.isfile.side_effect = [True, False, False] # a is file so the check for 'a' does not fall through to the elif os_.path.isdir.side_effect = [True, True] client.copy_to_output_dir('src') os_.listdir.assert_has_calls([call(config.output_dir), call('src')]) os_.path.isfile.assert_has_calls( [call('src/a'), call('src/b'), call('src/c')]) # a is file so the check for 'a' does not fall through to the elif os_.path.isdir.assert_has_calls([call('src/b'), call('src/c')]) # initial (failed) copy is part of the calls shutil_.copytree.assert_has_calls([ call('src', config.output_dir), call(os.path.join('src', 'b'), os.path.join(config.output_dir, 'b')), call(os.path.join('src', 'c'), os.path.join(config.output_dir, 'c')) ]) shutil_.copyfile.assert_has_calls( [call(os.path.join('src', 'a'), os.path.join(config.output_dir, 'a'))]) _copy_soscleaner_files.assert_not_called()
def test_copy_to_output_dir_exists_and_not_empty(os_, shutil_, _copy_soscleaner_files): ''' Test that writing to an existing and non-empty directory is NOT performed. Due to the check in config.py this should never happen, but just to be safe. ''' config = InsightsConfig(output_dir='dest') client = InsightsClient(config) shutil_.copytree.side_effect = [OSError(17, 'File exists')] os_.listdir.return_value = ['test'] client.copy_to_output_dir('src') os_.listdir.assert_called_once_with(config.output_dir) shutil_.copytree.assert_called_once_with('src', config.output_dir) shutil_.copyfile.assert_not_called()