test_hg_pillar.py 2.7 KB

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