def setUp(self): ''' We don't want to check in another .git dir into GH because that just gets messy. Instead, we'll create a temporary repo on the fly for the tests to examine. ''' self.integration_base_files = os.path.join(integration.FILES, 'file', 'base') self.tmp_repo_dir = os.path.join(integration.TMP, 'gitfs_root') # Create the dir if it doesn't already exist try: shutil.copytree(self.integration_base_files, self.tmp_repo_dir + '/') except OSError: # We probably caught an error because files already exist. Ignore pass try: repo = git.Repo(self.tmp_repo_dir) except git.exc.InvalidGitRepositoryError: repo = git.Repo.init(self.tmp_repo_dir) repo.index.add([x for x in os.listdir(self.tmp_repo_dir) if x != '.git']) repo.index.commit('Test') with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'], 'gitfs_remotes': ['file://' + self.tmp_repo_dir], 'sock_dir': self.master_opts['sock_dir']}): gitfs.update()
def setUp(self): ''' We don't want to check in another .git dir into GH because that just gets messy. Instead, we'll create a temporary repo on the fly for the tests to examine. :return: ''' self.integration_base_files = os.path.join(integration.FILES, 'file', 'base') self.tmp_repo_dir = os.path.join(integration.TMP, 'gitfs_root') self.tmp_repo_git = os.path.join(self.tmp_repo_dir, '.git') # Create the dir if it doesn't already exist try: shutil.copytree(self.integration_base_files, self.tmp_repo_dir + '/') except OSError as e: # We probably caught an error because files already exist. Ignore pass if not os.path.exists(self.tmp_repo_git): os.makedirs(self.tmp_repo_git) try: git_bin = git.Git(self.tmp_repo_git) git_bin.init(self.tmp_repo_dir) os.chdir(self.tmp_repo_dir) git_bin.add('.', with_keep_cwd=True) # Is there a way to pass in a .git repo? git_bin.commit('-a', '-m', 'Test', with_keep_cwd=True) except git.GitCommandError: # Will throw a command error if you try to init a repo that already exists pass with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'], 'gitfs_remotes': ['file://' + self.tmp_repo_git], 'sock_dir': self.master_opts['sock_dir']}): gitfs.update()
def test_disable_saltenv_mapping_per_remote_with_mapping_defined_per_remote(self): """ Test the per-remote disable_saltenv_mapping config option, combined with the per-saltenv mapping being defined in the remote itself via the "saltenv" per-remote option. """ opts = salt.utils.yaml.safe_load( textwrap.dedent( """\ gitfs_remotes: - {0}: - disable_saltenv_mapping: True - saltenv: - world: - ref: somebranch """.format( self.tmp_repo_dir ) ) ) with patch.dict(gitfs.__opts__, opts): gitfs.update() ret = gitfs.envs(ignore_cache=True) # Since we are restricting to tags only, the tag should appear in # the envs list, but the branches should not. self.assertEqual(ret, ["base", "world"])
def setUp(self): ''' We don't want to check in another .git dir into GH because that just gets messy. Instead, we'll create a temporary repo on the fly for the tests to examine. ''' self.integration_base_files = os.path.join(integration.FILES, 'file', 'base') self.tmp_repo_dir = os.path.join(integration.TMP, 'gitfs_root') # Create the dir if it doesn't already exist try: shutil.copytree(self.integration_base_files, self.tmp_repo_dir + '/') except OSError: # We probably caught an error because files already exist. Ignore pass try: repo = git.Repo(self.tmp_repo_dir) except git.exc.InvalidGitRepositoryError: repo = git.Repo.init(self.tmp_repo_dir) repo.index.add( [x for x in os.listdir(self.tmp_repo_dir) if x != '.git']) repo.index.commit('Test') with patch.dict( gitfs.__opts__, { 'cachedir': self.master_opts['cachedir'], 'gitfs_remotes': ['file://' + self.tmp_repo_dir], 'sock_dir': self.master_opts['sock_dir'] }): gitfs.update()
def test_find_and_serve_file_fallback(self): with patch.dict( gitfs.__opts__, {"file_buffer_size": 262144, "gitfs_fallback": "master"} ): gitfs.update() # find_file ret = gitfs.find_file("testfile", tgt_env="notexisting") self.assertEqual("testfile", ret["rel"]) full_path_to_file = salt.utils.path.join( gitfs.__opts__["cachedir"], "gitfs", "refs", "notexisting", "testfile" ) self.assertEqual(full_path_to_file, ret["path"]) # serve_file load = {"saltenv": "notexisting", "path": full_path_to_file, "loc": 0} fnd = {"path": full_path_to_file, "rel": "testfile"} ret = gitfs.serve_file(load, fnd) with salt.utils.files.fopen( os.path.join(RUNTIME_VARS.BASE_FILES, "testfile"), "r" ) as fp_: # NB: Why not 'rb'? data = fp_.read() self.assertDictEqual(ret, {"data": data, "dest": "testfile"})
def test_file_list(self): gitfs.update() ret = gitfs.file_list(LOAD) self.assertIn("testfile", ret) self.assertIn(UNICODE_FILENAME, ret) # This function does not use os.sep, the Salt fileserver uses the # forward slash, hence it being explicitly used to join here. self.assertIn("/".join((UNICODE_DIRNAME, "foo.txt")), ret)
def test_file_list_fallback(self): with patch.dict(gitfs.__opts__, {"gitfs_fallback": "master"}): gitfs.update() ret = gitfs.file_list({"saltenv": "notexisting"}) self.assertIn("testfile", ret) self.assertIn(UNICODE_FILENAME, ret) # This function does not use os.sep, the Salt fileserver uses the # forward slash, hence it being explicitly used to join here. self.assertIn("/".join((UNICODE_DIRNAME, "foo.txt")), ret)
def setUp(self): ''' We don't want to check in another .git dir into GH because that just gets messy. Instead, we'll create a temporary repo on the fly for the tests to examine. ''' if not gitfs.__virtual__(): self.skipTest("GitFS could not be loaded. Skipping GitFS tests!") self.tmp_cachedir = tempfile.mkdtemp(dir=TMP) gitfs.update()
def test_ref_types_global(self): """ Test the global gitfs_ref_types config option """ with patch.dict(gitfs.__opts__, {"gitfs_ref_types": ["branch"]}): gitfs.update() ret = gitfs.envs(ignore_cache=True) # Since we are restricting to branches only, the tag should not # appear in the envs list. self.assertIn("base", ret) self.assertIn(UNICODE_ENVNAME, ret) self.assertNotIn(TAG_NAME, ret)
def test_saltenv_whitelist(self): """ test saltenv_whitelist """ opts = salt.utils.yaml.safe_load( textwrap.dedent("""\ gitfs_saltenv_whitelist: base """)) with patch.dict(gitfs.__opts__, opts): gitfs.update() ret = gitfs.envs(ignore_cache=True) assert "base" in ret assert UNICODE_ENVNAME not in ret assert "mytag" not in ret
def test_ref_types_per_remote(self): ''' Test the per_remote ref_types config option, using a different ref_types setting than the global test. ''' remotes = [{'file://' + TMP_REPO_DIR: [{'ref_types': ['tag']}]}] with patch.dict(gitfs.__opts__, {'gitfs_remotes': remotes}): gitfs.update() ret = gitfs.envs(ignore_cache=True) # Since we are restricting to tags only, the tag should appear in # the envs list, but the branches should not. self.assertNotIn('base', ret) self.assertNotIn(UNICODE_ENVNAME, ret) self.assertIn(TAG_NAME, ret)
def test_saltenv_whitelist(self): ''' test saltenv_whitelist ''' opts = salt.utils.yaml.safe_load( textwrap.dedent('''\ gitfs_saltenv_whitelist: base ''')) with patch.dict(gitfs.__opts__, opts): gitfs.update() ret = gitfs.envs(ignore_cache=True) assert 'base' in ret assert UNICODE_ENVNAME not in ret assert 'mytag' not in ret
def test_ref_types_per_remote(self): """ Test the per_remote ref_types config option, using a different ref_types setting than the global test. """ remotes = [{"file://" + self.tmp_repo_dir: [{"ref_types": ["tag"]}]}] with patch.dict(gitfs.__opts__, {"gitfs_remotes": remotes}): gitfs.update() ret = gitfs.envs(ignore_cache=True) # Since we are restricting to tags only, the tag should appear in # the envs list, but the branches should not. self.assertNotIn("base", ret) self.assertNotIn(UNICODE_ENVNAME, ret) self.assertIn(TAG_NAME, ret)
def test_env_deprecated_opts(self): ''' ensure deprecated options gitfs_env_whitelist and gitfs_env_blacklist do not cause gitfs to not load. ''' opts = salt.utils.yaml.safe_load( textwrap.dedent('''\ gitfs_env_whitelist: base gitfs_env_blacklist: '' ''')) with patch.dict(gitfs.__opts__, opts): gitfs.update() ret = gitfs.envs(ignore_cache=True) assert 'base' in ret assert UNICODE_ENVNAME in ret assert 'mytag' in ret
def test_disable_saltenv_mapping_global_with_mapping_defined_globally(self): ''' Test the global gitfs_disable_saltenv_mapping config option, combined with the per-saltenv mapping being defined in the global gitfs_saltenv option. ''' opts = salt.utils.yaml.safe_load(textwrap.dedent('''\ gitfs_disable_saltenv_mapping: True gitfs_saltenv: - foo: - ref: somebranch ''')) with patch.dict(gitfs.__opts__, opts): gitfs.update() ret = gitfs.envs(ignore_cache=True) # Since we are restricting to tags only, the tag should appear in # the envs list, but the branches should not. self.assertEqual(ret, ['base', 'foo'])
def setUp(self): ''' We don't want to check in another .git dir into GH because that just gets messy. Instead, we'll create a temporary repo on the fly for the tests to examine. ''' self.integration_base_files = os.path.join(integration.FILES, 'file', 'base') self.tmp_repo_dir = os.path.join(integration.TMP, 'gitfs_root') # Create the dir if it doesn't already exist try: shutil.copytree(self.integration_base_files, self.tmp_repo_dir + '/') except OSError: # We probably caught an error because files already exist. Ignore pass try: repo = git.Repo(self.tmp_repo_dir) except git.exc.InvalidGitRepositoryError: repo = git.Repo.init(self.tmp_repo_dir) if 'USERNAME' not in os.environ: try: os.environ['USERNAME'] = pwd.getpwuid(os.geteuid()).pw_name except AttributeError: log.error('Unable to get effective username, falling back to ' '\'root\'.') os.environ['USERNAME'] = '******' repo.index.add( [x for x in os.listdir(self.tmp_repo_dir) if x != '.git']) repo.index.commit('Test') with patch.dict( gitfs.__opts__, { 'cachedir': self.master_opts['cachedir'], 'gitfs_remotes': ['file://' + self.tmp_repo_dir], 'sock_dir': self.master_opts['sock_dir'], '__role': self.master_opts['__role'] }): gitfs.update()
def test_disable_saltenv_mapping_global_with_mapping_defined_per_remote(self): ''' Test the global gitfs_disable_saltenv_mapping config option, combined with the per-saltenv mapping being defined in the remote itself via the "saltenv" per-remote option. ''' opts = salt.utils.yaml.safe_load(textwrap.dedent('''\ gitfs_disable_saltenv_mapping: True gitfs_remotes: - file://{0}: - saltenv: - bar: - ref: somebranch '''.format(self.tmp_repo_dir))) with patch.dict(gitfs.__opts__, opts): gitfs.update() ret = gitfs.envs(ignore_cache=True) # Since we are restricting to tags only, the tag should appear in # the envs list, but the branches should not. self.assertEqual(ret, ['bar', 'base'])
def setUp(self): ''' We don't want to check in another .git dir into GH because that just gets messy. Instead, we'll create a temporary repo on the fly for the tests to examine. ''' if not gitfs.__virtual__(): self.skipTest("GitFS could not be loaded. Skipping GitFS tests!") self.integration_base_files = os.path.join(FILES, 'file', 'base') # Create the dir if it doesn't already exist try: shutil.copytree(self.integration_base_files, self.tmp_repo_dir + '/') except OSError: # We probably caught an error because files already exist. Ignore pass try: repo = git.Repo(self.tmp_repo_dir) except git.exc.InvalidGitRepositoryError: repo = git.Repo.init(self.tmp_repo_dir) if 'USERNAME' not in os.environ: try: if salt.utils.platform.is_windows(): os.environ[ 'USERNAME'] = salt.utils.win_functions.get_current_user( ) else: os.environ['USERNAME'] = pwd.getpwuid(os.geteuid()).pw_name except AttributeError: log.error('Unable to get effective username, falling back to ' '\'root\'.') os.environ['USERNAME'] = '******' repo.index.add( [x for x in os.listdir(self.tmp_repo_dir) if x != '.git']) repo.index.commit('Test') gitfs.update()
def setUp(self): ''' We don't want to check in another .git dir into GH because that just gets messy. Instead, we'll create a temporary repo on the fly for the tests to examine. ''' self.integration_base_files = os.path.join(integration.FILES, 'file', 'base') self.tmp_repo_dir = os.path.join(integration.TMP, 'gitfs_root') # Create the dir if it doesn't already exist try: shutil.copytree(self.integration_base_files, self.tmp_repo_dir + '/') except OSError: # We probably caught an error because files already exist. Ignore pass try: repo = git.Repo(self.tmp_repo_dir) except git.exc.InvalidGitRepositoryError: repo = git.Repo.init(self.tmp_repo_dir) if 'USERNAME' not in os.environ: try: os.environ['USERNAME'] = pwd.getpwuid(os.geteuid()).pw_name except AttributeError: log.error('Unable to get effective username, falling back to ' '\'root\'.') os.environ['USERNAME'] = '******' repo.index.add([x for x in os.listdir(self.tmp_repo_dir) if x != '.git']) repo.index.commit('Test') with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'], 'gitfs_remotes': ['file://' + self.tmp_repo_dir], 'sock_dir': self.master_opts['sock_dir'], '__role': self.master_opts['__role']}): gitfs.update()
def test_envs(self): gitfs.update() ret = gitfs.envs(ignore_cache=True) self.assertIn("base", ret) self.assertIn(UNICODE_ENVNAME, ret) self.assertIn(TAG_NAME, ret)
with the per-saltenv mapping being defined in the global gitfs_saltenv option. """ opts = salt.utils.yaml.safe_load( textwrap.dedent( """\ gitfs_remotes: - {0}: - disable_saltenv_mapping: True gitfs_saltenv: - hello: - ref: somebranch '''.format(self.tmp_repo_dir))) with patch.dict(gitfs.__opts__, opts): gitfs.update() ret = gitfs.envs(ignore_cache=True) # Since we are restricting to tags only, the tag should appear in # the envs list, but the branches should not. self.assertEqual(ret, ["base", "hello"]) @skipIf(True, "SLOWTEST skip") def test_disable_saltenv_mapping_per_remote_with_mapping_defined_per_remote(self): """ Test the per-remote disable_saltenv_mapping config option, combined with the per-saltenv mapping being defined in the remote itself via the "saltenv" per-remote option. """ opts = salt.utils.yaml.safe_load( textwrap.dedent( """\
def test_dir_list_fallback(self): with patch.dict(gitfs.__opts__, {"gitfs_fallback": "master"}): gitfs.update() ret = gitfs.dir_list({"saltenv": "notexisting"}) self.assertIn("grail", ret) self.assertIn(UNICODE_DIRNAME, ret)
def test_dir_list(self): gitfs.update() ret = gitfs.dir_list(LOAD) self.assertIn("grail", ret) self.assertIn(UNICODE_DIRNAME, ret)