def setUp(self): super(GitPillarTestCase, self).setUp() self.tmpdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) cachedir = os.path.join(self.tmpdir, 'cachedir') os.makedirs(os.path.join(cachedir, 'pillar_gitfs')) self.repo_path = self._create_repo() git_pillar.__opts__ = { 'cachedir': cachedir, 'pillar_roots': {}, 'file_roots': {}, 'state_top': 'top.sls', 'extension_modules': '', 'renderer': 'yaml_jinja', 'pillar_opts': False } git_pillar.__grains__ = {} git_pillar._update('master', 'file://{0}'.format(self.repo_path))
def test_no_loop(self): '''Check that the reinstantiation of a pillar object does recurse. This test goes in great details of patching that the dedicated utilities might do in a simpler way. Namely, we replace the main ``ext_pillar`` entry function by one that keeps count of its calls. Otherwise, the fact that the :class:`MaximumRecursion` error is caught can go in the way on the testing. On the current code base, this test fails if the two first lines of :func:``git_pillar.ext_pillar`:: if pillar_dirs is None: return are replaced by:: if pillar_dirs is None: pillar_dirs = {} .. note:: the explicit anti-recursion protection does not prevent looping between two different Git pillars. This test will help subsequent refactors, and also as a base for other external pillars of the same kind. ''' repo2 = os.path.join(self.tmpdir, 'repo_pillar2') conf_line2 = 'master file://{0}'.format(repo2) subprocess.check_call(['git', 'clone', self.repo_path, repo2]) git_pillar.__opts__['ext_pillar'] = [ dict(git=self.conf_line), dict(git=conf_line2), ] git_pillar._update(*conf_line2.split(None, 1)) pil = Pillar(git_pillar.__opts__, git_pillar.__grains__, 'myminion', 'base') orig_ext_pillar = pil.ext_pillars['git'] orig_ext_pillar.count = 0 def ext_pillar_count_calls(minion_id, repo_string, pillar_dirs): orig_ext_pillar.count += 1 if orig_ext_pillar.count > 6: # going all the way to an infinite loop is harsh on the # test machine raise RuntimeError("Infinite loop detected") return orig_ext_pillar(minion_id, repo_string, pillar_dirs) from salt.loader import LazyLoader orig_getitem = LazyLoader.__getitem__ def __getitem__(self, key): if key == 'git.ext_pillar': return ext_pillar_count_calls return orig_getitem(self, key) try: LazyLoader.__getitem__ = __getitem__ self.assertEqual(PILLAR_CONTENT, pil.compile_pillar(pillar_dirs={})) self.assertTrue(orig_ext_pillar.count < 7) finally: LazyLoader.__getitem__ = orig_getitem
def setUp(self): super(GitPillarTestCase, self).setUp() git_pillar._update('master', 'file://{0}'.format(self.repo_path))