def test_file_find_with_function_and_match_type(self): self.maxDiff = None tmp_dir = self._make_temp_content([ 'file fruit/kiwi.fruit', 'file fruit/lemon.fruit', 'file fruit/strawberry.fruit', 'file fruit/blueberry.fruit', 'file cheese/brie.cheese', 'file cheese/cheddar.cheese', ]) self.assertEqual( self.native_filename_list([ 'cheese/brie.cheese', 'cheese/cheddar.cheese', ]), file_find.find(tmp_dir, match_function=lambda f: f.endswith('.cheese'), match_type='ANY')) self.assertEqual( self.native_filename_list([ 'fruit/blueberry.fruit', 'fruit/kiwi.fruit', 'fruit/lemon.fruit', 'fruit/strawberry.fruit', ]), file_find.find(tmp_dir, match_function=lambda f: f.endswith('.cheese'), match_type='NONE'))
def test_file_find_with_patterns_and_basename(self): self.maxDiff = None tmp_dir = self._make_temp_content([ 'file fruit/kiwi.fruit', 'file fruit/lemon.fruit', 'file fruit/strawberry.fruit', 'file fruit/blueberry.fruit', 'file cheese/brie.cheese', 'file cheese/cheddar.cheese', ]) self.assertEqual([], file_find.find(tmp_dir, match_patterns=['f*'], match_basename=True)) self.assertEqual( self.native_filename_list([ 'fruit/blueberry.fruit', 'fruit/kiwi.fruit', 'fruit/lemon.fruit', 'fruit/strawberry.fruit', ]), file_find.find(tmp_dir, match_patterns=['f*'], match_basename=False))
def test_move_files(self): tmp_dir = self.make_temp_dir() src_dir = path.join(tmp_dir, 'src') dst_dir = path.join(tmp_dir, 'dst') file_util.mkdir(dst_dir) temp_content.write_items([ 'file foo.txt "This is foo.txt\n" 644', 'file bar.txt "This is bar.txt\n" 644', 'file sub1/sub2/baz.txt "This is baz.txt\n" 644', 'file yyy/zzz/vvv.txt "This is vvv.txt\n" 644', 'file .hidden "this is .hidden\n" 644', 'file script.sh "#!/bin/bash\necho script.sh\nexit 0\n" 755', 'file .hushlogin "" 644', ], src_dir) expected = [ self.native_filename('.hidden'), self.native_filename('.hushlogin'), self.native_filename('bar.txt'), self.native_filename('foo.txt'), self.native_filename('script.sh'), self.native_filename('sub1/sub2/baz.txt'), self.native_filename('yyy/zzz/vvv.txt'), ] self.assertEqual(expected, file_find.find(src_dir, relative=True)) file_copy.move_files(src_dir, dst_dir) self.assertEqual(expected, file_find.find(dst_dir, relative=True)) self.assertEqual([], file_find.find(src_dir, relative=True))
def test_rename_dirs_wont_leak_above_root_dir(self): 'Test that we only rename dirs starting at root_dir' tmp_dir = self._make_temp_content([ temp_content('file', 'fruit/fruit/lib/fruit/kiwi.py', 'this is kiwi', 0o0644), temp_content('file', 'fruit/fruit/lib/fruit/lemon.py', 'this is lemon', 0o0644), temp_content('file', 'fruit/fruit/lib/fruit/constants.py', 'this is constants', 0o0644), temp_content('file', 'fruit/fruit/lib/fruit/constants2.py', 'this is constants2', 0o0644), temp_content('file', 'fruit/fruit/lib/fruity/constants2b.py', 'this is constants2', 0o0644), temp_content('file', 'fruit/fruit/tests/lib/fruit/test_kiwi.py', 'this is test kiwi', 0o0644), temp_content('file', 'fruit/fruit/tests/lib/fruit/test_lemon.py', 'this is test lemon', 0o0644), temp_content('file', 'fruit/fruit/tests/lib/fruity/test_lemonb.py', 'this is test lemon', 0o0644), ]) options = refactor_options(word_boundary = False) refactor_files.rename_dirs(path.join(tmp_dir, 'fruit'), 'fruit', 'cheese', options = options) self.assert_filename_list_equal( [ 'fruit', 'fruit/cheese', 'fruit/cheese/lib', 'fruit/cheese/lib/cheese', 'fruit/cheese/lib/cheese/constants.py', 'fruit/cheese/lib/cheese/constants2.py', 'fruit/cheese/lib/cheese/kiwi.py', 'fruit/cheese/lib/cheese/lemon.py', 'fruit/cheese/lib/cheesey', 'fruit/cheese/lib/cheesey/constants2b.py', 'fruit/cheese/tests', 'fruit/cheese/tests/lib', 'fruit/cheese/tests/lib/cheese', 'fruit/cheese/tests/lib/cheese/test_kiwi.py', 'fruit/cheese/tests/lib/cheese/test_lemon.py', 'fruit/cheese/tests/lib/cheesey', 'fruit/cheese/tests/lib/cheesey/test_lemonb.py', ], file_find.find(tmp_dir, file_type = file_find.ANY) )
def _find(clazz, root_dir, base_dir, extra_items, include, exclude): files = file_find.find(root_dir, relative=True, file_type=file_find.FILE | file_find.LINK) items = [] if include: include_matcher = matcher_multiple_filename(include) else: include_matcher = matcher_always_true() if exclude: exclude_matcher = matcher_multiple_filename(exclude) else: exclude_matcher = matcher_always_false() for f in files: filename = path.join(root_dir, f) if base_dir: arcname = path.join(base_dir, f) else: arcname = f should_include = include_matcher.match(f) should_exclude = exclude_matcher.match(f) if should_include and not should_exclude: items.append(clazz.item(filename, arcname)) return items + (extra_items or [])
def test_file_sync_change_one(self): tmp_src_dir1 = self._make_temp_content([ 'file foo.txt "first foo.txt\n"', 'file subdir/bar.txt "bar.txt\n"', 'file subdir/subberdir/baz.txt "baz.txt\n"', 'file emptyfile.txt', 'dir emptydir', ]) tmp_src_dir2 = self._make_temp_content([ 'file foo.txt "second foo.txt\n"', 'file subdir/bar.txt "bar.txt\n"', 'file subdir/subberdir/baz.txt "baz.txt\n"', 'file emptyfile.txt', 'dir emptydir', ]) tmp_dst_dir = temp_file.make_temp_dir() file_sync.sync(tmp_src_dir1, tmp_dst_dir) file_sync.sync(tmp_src_dir2, tmp_dst_dir) expected = [ self.native_filename('emptyfile.txt'), self.native_filename('foo.txt'), self.native_filename('subdir/bar.txt'), self.native_filename('subdir/subberdir/baz.txt'), ] self.assertEqual( expected, file_find.find(tmp_dst_dir, relative = True) ) self.assertEqual( 'second foo.txt\n', file_util.read(path.join(tmp_dst_dir, 'foo.txt'), codec = 'utf8') )
def test_copy_tree_spaces_in_filenames(self): self.maxDiff = None src_tmp_dir = self.make_temp_dir(prefix='src-', suffix='-has 2 spaces-') dst_tmp_dir = self.make_temp_dir(prefix='dst-', suffix='-has 2 spaces-') file_util.remove(dst_tmp_dir) with tarfile.open(self.data_path('test.tar'), mode='r') as f: f.extractall(path=src_tmp_dir) file_copy.copy_tree(src_tmp_dir, dst_tmp_dir) expected_files = [ self.native_filename('1'), self.native_filename('1/2'), self.native_filename('1/2/3'), self.native_filename('1/2/3/4'), self.native_filename('1/2/3/4/5'), self.native_filename('1/2/3/4/5/apple.txt'), self.native_filename('1/2/3/4/5/kiwi.txt'), self.native_filename('bar.txt'), self.native_filename('empty'), self.native_filename('foo.txt'), self.native_filename('kiwi_link.txt'), ] actual_files = file_find.find(dst_tmp_dir, file_type=file_find.ANY) self.assertEqual(expected_files, actual_files)
def __enter__(self): if path.exists(self.src_dir): self.src_files_before = file_find.find(self.src_dir, relative=True, file_type=file_find.ANY) else: self.src_files_before = [] return self
def test_upload_file_new_file(self): tester = self._make_tester() self.assert_filename_list_equal([ 'emptyfile.txt', 'foo.txt', 'subdir/bar.txt', 'subdir/subberdir/baz.txt', ], file_find.find(tester.local_root_dir)) tmp_file = self.make_temp_file(content='this is kiwi.txt') tester.upload_file(tmp_file, 'kiwi.txt') self.assert_filename_list_equal([ 'emptyfile.txt', 'foo.txt', 'kiwi.txt', 'subdir/bar.txt', 'subdir/subberdir/baz.txt', ], file_find.find(tester.local_root_dir))
def _handle_post_extract(clazz, dest_dir, include, exclude): all_files = file_find.find(dest_dir, relative=True, file_type=file_find.FILE | file_find.LINK) wanted_files = self._find(dest_dir, None, None, include, exclude) delta = set(all_files) - set(wanted_files) for f in delta: print('clobber: %s' % (f))
def _collect_side_effects(table, test, where, label, keep_side_effects): droppings = file_find.find(where, relative=False, file_type=file_find.ANY) for next_dropping in droppings: if not test in table: table[test] = [] se = side_effect(where, next_dropping, label) table[test].append(se) if not keep_side_effects: file_util.remove(next_dropping)
def test_update(self): test = self._make_test_shell_framework() test.repo.tag('1.2.3', push=True) test.framework.update('1.2.3') self.assertEqual('1.2.3', test.framework.current_revision) files = file_find.find(test.tmp_dir) self.assertEqual([ 'bes_shell_framework/bes_bash.bash', 'bes_shell_framework_revision.txt', ], files)
def test_extract_all(self): tmp_dir = temp_file.make_temp_dir() info1 = dmg.info() dmg.extract(self._tmp_example_dmg('example.dmg'), tmp_dir) info2 = dmg.info() files = file_find.find(tmp_dir, relative=True, file_type=file_find.FILE_OR_LINK) self.assertEqual(['foo.txt', 'link_to_foo.sh', 'subdir/bar.txt'], files)
def _resolve_one(clazz, filename): filename = path.abspath(filename) if path.isfile(filename): return [filename] elif path.isdir(filename): return file_find.find(filename, relative=False, file_type=file_find.FILE) if path.exists(filename): raise RuntimeError('File not a file or dir: {}'.format(filename)) raise RuntimeError('File not found: {}'.format(filename))
def __exit__(self, type, value, traceback): if path.exists(self.src_dir): self.src_files = file_find.find(self.src_dir, relative=True, file_type=file_find.ANY) self.src_dirs = file_find.find(self.src_dir, relative=True, file_type=file_find.DIR) else: self.src_files = [] self.src_dirs = [] if path.exists(self.dst_dir): self.dst_files = file_find.find(self.dst_dir, relative=True, file_type=file_find.ANY) self.dst_dirs = file_find.find(self.dst_dir, relative=True, file_type=file_find.DIR) else: self.dst_files = [] self.dst_dirs = []
def find_archives(clazz, where, relative=True): 'Return valid archives found recursively in where.' files = file_find.find(where, relative=relative, file_type=file_find.FILE) if relative: files = [ f for f in files if archiver.is_valid(path.join(where, f)) ] else: files = [f for f in files if archiver.is_valid(f)] return files
def test_rename_dirs_one_file(self): tmp_dir = self._make_temp_content([ temp_content('file', 'kiwi/xdata2/kiwi_stuff2/kiwi2.txt', 'foo.txt', 0o0644), ]) options = refactor_options(word_boundary = False) refactor_files.rename_dirs(tmp_dir, 'kiwi', 'chocolate', options = options) self.assert_filename_list_equal( [ 'chocolate', 'chocolate/xdata2', 'chocolate/xdata2/chocolate_stuff2', 'chocolate/xdata2/chocolate_stuff2/kiwi2.txt', ], file_find.find(tmp_dir, file_type = file_find.ANY) )
def test_rename_files_with_word_boundary_and_underscore(self): tmp_dir = self._make_temp_content([ temp_content('dir', 'empty_rootdir', None, 0o0755), temp_content('dir', 'lib/fruit/emptydir', None, 0o0755), temp_content('file', 'lib/fruit/constants.py', 'this is constants', 0o0644), temp_content('file', 'lib/fruit/constants2.py', 'this is constants2', 0o0644), temp_content('file', 'lib/fruit/kiwi.py', 'this is kiwi', 0o0644), temp_content('file', 'lib/fruit/kiwifruit.py', 'this is kiwi', 0o0644), temp_content('file', 'lib/fruit/kiwi_fruit.py', 'this is kiwi', 0o0644), temp_content('file', 'lib/fruit/lemon.py', 'this is lemon', 0o0644), temp_content('file', 'lib/fruity/constants2b.py', 'this is constants2', 0o0644), temp_content('file', 'tests/lib/fruit/test_kiwi.py', 'this is test kiwi', 0o0644), temp_content('file', 'tests/lib/fruit/test_kiwifruit.py', 'this is test kiwi', 0o0644), temp_content('file', 'tests/lib/fruit/test_kiwi_fruit.py', 'this is test kiwi', 0o0644), temp_content('file', 'tests/lib/fruit/test_lemon.py', 'this is test lemon', 0o0644), temp_content('file', 'tests/lib/fruity/test_lemonb.py', 'this is test lemon', 0o0644), temp_content('file', 'xdata/kiwi_stuff/kiwi.txt', 'foo.txt', 0o0644), temp_content('file', 'kiwi/xdata2/kiwi_stuff2/kiwi2.txt', 'foo.txt', 0o0644), ]) options = refactor_options(word_boundary = True, word_boundary_chars = word_boundary.CHARS_UNDERSCORE) refactor_files.rename_files(tmp_dir, 'kiwi', 'chocolate', options = options) self.assert_filename_list_equal( [ 'chocolate', 'chocolate/xdata2', 'chocolate/xdata2/chocolate_stuff2', 'chocolate/xdata2/chocolate_stuff2/kiwi2.txt', 'empty_rootdir', 'lib', 'lib/fruit', 'lib/fruit/chocolate.py', 'lib/fruit/chocolate_fruit.py', 'lib/fruit/constants.py', 'lib/fruit/constants2.py', 'lib/fruit/emptydir', 'lib/fruit/kiwifruit.py', 'lib/fruit/lemon.py', 'lib/fruity', 'lib/fruity/constants2b.py', 'tests', 'tests/lib', 'tests/lib/fruit', 'tests/lib/fruit/test_chocolate.py', 'tests/lib/fruit/test_chocolate_fruit.py', 'tests/lib/fruit/test_kiwifruit.py', 'tests/lib/fruit/test_lemon.py', 'tests/lib/fruity', 'tests/lib/fruity/test_lemonb.py', 'xdata', 'xdata/chocolate_stuff', 'xdata/chocolate_stuff/chocolate.txt', ], file_find.find(tmp_dir, file_type = file_find.ANY) )
def test_file_find_min_and_max_depth(self): self.maxDiff = None tmp_dir = self._make_temp_content([ 'file 1a.f', 'file 1b.f', 'file 1.d/2a.f', 'file 1.d/2b.f', 'file 1.d/2.d/3a.f', 'file 1.d/2.d/3b.f', 'file 1.d/2.d/3.d/4a.f', 'file 1.d/2.d/3.d/4b.f', 'file 1.d/2.d/3.d/4.d/5a.f', 'file 1.d/2.d/3.d/4.d/5b.f', ]) self.assertEqual( sorted([ self.native_filename('1a.f'), self.native_filename('1b.f'), self.native_filename('1.d/2a.f'), self.native_filename('1.d/2b.f'), ]), file_find.find(tmp_dir, min_depth=1, max_depth=2)) self.assertEqual( sorted([ self.native_filename('1.d/2a.f'), self.native_filename('1.d/2b.f'), self.native_filename('1.d/2.d/3a.f'), self.native_filename('1.d/2.d/3b.f'), ]), file_find.find(tmp_dir, min_depth=2, max_depth=3)) self.assertEqual( sorted([ self.native_filename('1.d/2a.f'), self.native_filename('1.d/2b.f'), ]), file_find.find(tmp_dir, min_depth=2, max_depth=2)) self.assertEqual( sorted([ self.native_filename('1a.f'), self.native_filename('1b.f'), ]), file_find.find(tmp_dir, min_depth=1, max_depth=1))
def local_vms(self): local_vms = {} vmx_files = file_find.find(self._vm_dir, relative=False, match_patterns=['*.vmx']) for vmx_filename in vmx_files: nickname = vmware_vmx_file(vmx_filename).nickname login_credentials = self._options.resolve_login_credentials( nickname) local_vm = vmware_local_vm(self._runner, vmx_filename, login_credentials) local_vms[vmx_filename] = local_vm return local_vms
def test_rename_dirs_with_word_boundary(self): tmp_dir = self._make_temp_content([ temp_content('dir', 'empty_rootdir', None, 0o0755), temp_content('dir', 'lib/fruit/emptydir', None, 0o0755), temp_content('file', 'lib/fruit/constants.py', 'this is constants', 0o0644), temp_content('file', 'lib/fruit/constants2.py', 'this is constants2', 0o0644), temp_content('file', 'lib/fruit/kiwi.py', 'this is kiwi', 0o0644), temp_content('file', 'lib/fruit/kiwifruit.py', 'this is kiwi', 0o0644), temp_content('file', 'lib/fruit/kiwi_fruit.py', 'this is kiwi', 0o0644), temp_content('file', 'lib/fruit/lemon.py', 'this is lemon', 0o0644), temp_content('file', 'lib/fruity/constants2b.py', 'this is constants2', 0o0644), temp_content('file', 'tests/lib/fruit/test_kiwi.py', 'this is test kiwi', 0o0644), temp_content('file', 'tests/lib/fruit/test_kiwifruit.py', 'this is test kiwi', 0o0644), temp_content('file', 'tests/lib/fruit/test_kiwi_fruit.py', 'this is test kiwi', 0o0644), temp_content('file', 'tests/lib/fruit/test_lemon.py', 'this is test lemon', 0o0644), temp_content('file', 'tests/lib/fruity/test_lemonb.py', 'this is test lemon', 0o0644), temp_content('file', 'xdata/kiwi_stuff/kiwi.txt', 'foo.txt', 0o0644), temp_content('file', 'kiwi/xdata2/kiwi_stuff2/kiwi2.txt', 'foo.txt', 0o0644), ]) options = refactor_options(word_boundary = True) refactor_files.rename_dirs(tmp_dir, 'fruit', 'cheese', options = options) self.assert_filename_list_equal( [ 'empty_rootdir', 'kiwi', 'kiwi/xdata2', 'kiwi/xdata2/kiwi_stuff2', 'kiwi/xdata2/kiwi_stuff2/kiwi2.txt', 'lib', 'lib/cheese', 'lib/cheese/constants.py', 'lib/cheese/constants2.py', 'lib/cheese/emptydir', 'lib/cheese/kiwi.py', 'lib/cheese/kiwi_fruit.py', 'lib/cheese/kiwifruit.py', 'lib/cheese/lemon.py', 'lib/fruity', 'lib/fruity/constants2b.py', 'tests', 'tests/lib', 'tests/lib/cheese', 'tests/lib/cheese/test_kiwi.py', 'tests/lib/cheese/test_kiwi_fruit.py', 'tests/lib/cheese/test_kiwifruit.py', 'tests/lib/cheese/test_lemon.py', 'tests/lib/fruity', 'tests/lib/fruity/test_lemonb.py', 'xdata', 'xdata/kiwi_stuff', 'xdata/kiwi_stuff/kiwi.txt', ], file_find.find(tmp_dir, file_type = file_find.ANY) )
def test_match_files_with_word_boundary(self): tmp_dir = self._make_temp_content([ temp_content('file', 'lib/fruit/kiwi.py', 'this is kiwi', 0o0644), temp_content('file', 'lib/fruit/lemon.py', 'this is lemon', 0o0644), temp_content('file', 'lib/fruit/constants.py', 'this is constants', 0o0644), temp_content('file', 'lib/fruit/constants2.py', 'this is constants2', 0o0644), temp_content('file', 'tests/lib/fruit/test_kiwi.py', 'this is test kiwi', 0o0644), temp_content('file', 'tests/lib/fruit/test_lemon.py', 'this is test lemon', 0o0644), ]) options = refactor_options(word_boundary = True) self.assert_filename_list_equal( [ f'{tmp_dir}/lib/fruit/kiwi.py', f'{tmp_dir}/tests/lib/fruit/test_kiwi.py', ], refactor_files.match_files( file_find.find(tmp_dir, relative = False), 'kiwi', options = options) )
def test_file_find_with_function_and_basename(self): self.maxDiff = None tmp_dir = self._make_temp_content([ 'file fruit/kiwi.fruit', 'file fruit/lemon.fruit', 'file fruit/strawberry.fruit', 'file fruit/blueberry.fruit', 'file cheese/brie.cheese', 'file cheese/cheddar.cheese', ]) self.assertEqual([], file_find.find( tmp_dir, match_function=lambda f: f.startswith('cheese'), match_basename=True)) self.assertEqual( self.native_filename_list([ 'cheese/brie.cheese', 'cheese/cheddar.cheese', ]), file_find.find(tmp_dir, match_function=lambda f: f.startswith('cheese'), match_basename=False))
def test_file_find_with_re(self): self.maxDiff = None tmp_dir = self._make_temp_content([ 'file fruit/kiwi.fruit', 'file fruit/lemon.fruit', 'file fruit/strawberry.fruit', 'file fruit/blueberry.fruit', 'file cheese/brie.cheese', 'file cheese/cheddar.cheese', ]) self.assertEqual( self.native_filename_list([ 'cheese/brie.cheese', 'cheese/cheddar.cheese', ]), file_find.find(tmp_dir, match_re=r'^.*\.cheese$'))
def test_add_known_host_clean_slate(self): tmp_dir = self.make_temp_dir(suffix='.ssh') cm = ssh_config_manager(tmp_dir) cm.add_known_host(['example.com'], 'ssh-rsa', 'key1') files = file_find.find(tmp_dir, relative=True) self.assertEqual([ 'known_hosts', ], files) expected_known_hosts = ''' example.com ssh-rsa key1 ''' self.assertMultiLineEqual( expected_known_hosts.strip(), file_util.read(path.join(tmp_dir, 'known_hosts'), codec='utf-8').strip())
def test_add_authorized_key_clean_slate(self): tmp_dir = self.make_temp_dir(suffix='.ssh') cm = ssh_config_manager(tmp_dir) cm.add_authorized_key('ssh-rsa', 'key1', 'fred@bedrock') files = file_find.find(tmp_dir, relative=True) self.assertEqual([ 'authorized_keys', ], files) expected_authorized_keys = ''' ssh-rsa key1 fred@bedrock ''' self.assertMultiLineEqual( expected_authorized_keys.strip(), file_util.read(path.join(tmp_dir, 'authorized_keys'), codec='utf-8').strip())
def test_file_sync_basic(self): tmp_src_dir = self._make_temp_content([ 'file foo.txt "foo.txt\n"', 'file subdir/bar.txt "bar.txt\n"', 'file subdir/subberdir/baz.txt "baz.txt\n"', 'file emptyfile.txt', 'dir emptydir', ]) tmp_dst_dir = temp_file.make_temp_dir() file_sync.sync(tmp_src_dir, tmp_dst_dir) expected = [ self.native_filename('emptyfile.txt'), self.native_filename('foo.txt'), self.native_filename('subdir/bar.txt'), self.native_filename('subdir/subberdir/baz.txt'), ] self.assertEqual( expected, file_find.find(tmp_dst_dir, relative = True) )
def test_extract(self): tmp_dir = self.make_temp_dir() tar_util.extract(self.data_path('test.tar'), tmp_dir) expected_files = [ self.native_filename('1'), self.native_filename('1/2'), self.native_filename('1/2/3'), self.native_filename('1/2/3/4'), self.native_filename('1/2/3/4/5'), self.native_filename('1/2/3/4/5/apple.txt'), self.native_filename('1/2/3/4/5/kiwi.txt'), self.native_filename('bar.txt'), self.native_filename('empty'), self.native_filename('foo.txt'), self.native_filename('kiwi_link.txt'), ] actual_files = file_find.find(tmp_dir, file_type = file_find.ANY) self.assertEqual( expected_files, actual_files )
def test_file_find(self): tmp_dir = self._make_temp_content([ 'file foo.txt "foo.txt\n"', 'file subdir/bar.txt "bar.txt\n"', 'file subdir/subberdir/baz.txt "baz.txt\n"', 'file emptyfile.txt', 'dir emptydir', ]) expected_relative = [ self.native_filename('emptyfile.txt'), self.native_filename('foo.txt'), self.native_filename('subdir/bar.txt'), self.native_filename('subdir/subberdir/baz.txt'), ] self.assertEqual(expected_relative, file_find.find(tmp_dir, relative=True))
def test_install_key_pair_for_host(self): tmp_dir = self.make_temp_dir(suffix='.ssh') cm = ssh_config_manager(tmp_dir) tmp_public_key = 'publickey' tmp_private_key = 'privatekey' tmp_hostname = 'bitbucket.org' installed = cm.install_key_pair_for_host('publickey', 'privatekey', 'bitbucket.org', username='******', include_ip_address=False, include_comment=False) files = file_find.find(tmp_dir, relative=True) self.assertEqual([ 'config', 'id_rsa_bitbucket_org', 'id_rsa_bitbucket_org.pub', 'known_hosts', ], files) expected_config = ''' Host bitbucket.org Hostname bitbucket.org IdentityFile {private_key} User fred '''.format(private_key=installed.private_key_filename) self.assertMultiLineEqual( expected_config.strip(), file_util.read(path.join(tmp_dir, 'config'), codec='utf-8').strip()) expected_known_hosts = ''' bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw== ''' self.assertMultiLineEqual( expected_known_hosts.strip(), file_util.read(path.join(tmp_dir, 'known_hosts'), codec='utf-8').strip()) abs_files = [path.join(tmp_dir, f) for f in files] for filename in abs_files: self.assertEqual(0o0600, file_util.mode(filename))