test_hg_pillar.py 2.6 KB

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