test_hg_pillar.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 shutil
  7. import subprocess
  8. import tempfile
  9. import salt.pillar.hg_pillar as hg_pillar
  10. # Import Salt Libs
  11. import salt.utils.files
  12. import salt.utils.yaml
  13. # Import Salt Testing libs
  14. from tests.support.mixins import (
  15. AdaptedConfigurationTestCaseMixin,
  16. LoaderModuleMockMixin,
  17. )
  18. from tests.support.runtests import RUNTIME_VARS
  19. from tests.support.unit import TestCase, skipIf
  20. COMMIT_USER_NAME = "test_user"
  21. # file contents
  22. PILLAR_CONTENT = {"gna": "hello"}
  23. FILE_DATA = {"top.sls": {"base": {"*": ["user"]}}, "user.sls": PILLAR_CONTENT}
  24. HGLIB = hg_pillar.hglib
  25. @skipIf(HGLIB is None, "python-hglib library not installed")
  26. class HgPillarTestCase(
  27. TestCase, AdaptedConfigurationTestCaseMixin, LoaderModuleMockMixin
  28. ):
  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(
  61. os.path.join(hg_repo, filename), "w"
  62. ) as data_file:
  63. salt.utils.yaml.safe_dump(FILE_DATA[filename], data_file)
  64. subprocess.check_call(
  65. [
  66. "hg",
  67. "ci",
  68. "-A",
  69. "-R",
  70. hg_repo,
  71. "-m",
  72. "first commit",
  73. "-u",
  74. COMMIT_USER_NAME,
  75. ]
  76. )
  77. return hg_repo
  78. def test_base(self):
  79. "check hg repo is imported correctly"
  80. mypillar = hg_pillar.ext_pillar(
  81. "*", None, "file://{0}".format(self.hg_repo_path)
  82. )
  83. self.assertEqual(PILLAR_CONTENT, mypillar)