1
0

test_hg_pillar.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. '''test for pillar hg_pillar.py'''
  3. # Import python libs
  4. from __future__ import absolute_import, print_function, unicode_literals
  5. import os
  6. import tempfile
  7. import shutil
  8. import subprocess
  9. # Import Salt Testing libs
  10. from tests.support.mixins import AdaptedConfigurationTestCaseMixin, LoaderModuleMockMixin
  11. from tests.support.unit import TestCase, skipIf
  12. from tests.support.runtests import RUNTIME_VARS
  13. COMMIT_USER_NAME = 'test_user'
  14. # file contents
  15. PILLAR_CONTENT = {'gna': 'hello'}
  16. FILE_DATA = {
  17. 'top.sls': {'base': {'*': ['user']}},
  18. 'user.sls': PILLAR_CONTENT
  19. }
  20. # Import Salt Libs
  21. import salt.utils.files
  22. import salt.utils.yaml
  23. import salt.pillar.hg_pillar as hg_pillar
  24. HGLIB = hg_pillar.hglib
  25. @skipIf(HGLIB is None, 'python-hglib library not installed')
  26. class HgPillarTestCase(TestCase, AdaptedConfigurationTestCaseMixin, LoaderModuleMockMixin):
  27. 'test hg_pillar pillar'
  28. maxDiff = None
  29. def setup_loader_modules(self):
  30. self.tmpdir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
  31. self.addCleanup(shutil.rmtree, self.tmpdir)
  32. cachedir = os.path.join(self.tmpdir, 'cachedir')
  33. os.makedirs(os.path.join(cachedir, 'hg_pillar'))
  34. self.hg_repo_path = self._create_hg_repo()
  35. return {
  36. hg_pillar: {
  37. '__opts__': {
  38. 'cachedir': cachedir,
  39. 'pillar_roots': {},
  40. 'file_roots': {},
  41. 'state_top': 'top.sls',
  42. 'extension_modules': '',
  43. 'renderer': 'yaml_jinja',
  44. 'pillar_opts': False,
  45. 'renderer_blacklist': [],
  46. 'renderer_whitelist': [],
  47. 'file_ignore_glob': [],
  48. 'file_ignore_regex': [],
  49. }
  50. }
  51. }
  52. def _create_hg_repo(self):
  53. 'create repo in tempdir'
  54. hg_repo = os.path.join(self.tmpdir, 'repo_pillar')
  55. os.makedirs(hg_repo)
  56. subprocess.check_call(['hg', 'init', hg_repo])
  57. for filename in FILE_DATA:
  58. with salt.utils.files.fopen(os.path.join(hg_repo, filename), 'w') as data_file:
  59. salt.utils.yaml.safe_dump(FILE_DATA[filename], data_file)
  60. subprocess.check_call(['hg', 'ci', '-A', '-R', hg_repo, '-m', 'first commit', '-u', COMMIT_USER_NAME])
  61. return hg_repo
  62. def test_base(self):
  63. 'check hg repo is imported correctly'
  64. mypillar = hg_pillar.ext_pillar('*', None, 'file://{0}'.format(self.hg_repo_path))
  65. self.assertEqual(PILLAR_CONTENT, mypillar)