test_jinja.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """
  2. Test the jinja module
  3. """
  4. import os
  5. import salt.utils.files
  6. import salt.utils.json
  7. import salt.utils.yaml
  8. from tests.support.case import ModuleCase
  9. from tests.support.helpers import requires_system_grains
  10. from tests.support.runtests import RUNTIME_VARS
  11. class TestModulesJinja(ModuleCase):
  12. """
  13. Test the jinja map module
  14. """
  15. def _path(self, name, absolute=False):
  16. path = os.path.join("modules", "jinja", name)
  17. if absolute:
  18. return os.path.join(RUNTIME_VARS.BASE_FILES, path)
  19. else:
  20. return path
  21. def test_import_json(self):
  22. json_file = "osarchmap.json"
  23. ret = self.run_function("jinja.import_json", [self._path(json_file)])
  24. with salt.utils.files.fopen(self._path(json_file, absolute=True)) as fh_:
  25. self.assertDictEqual(salt.utils.json.load(fh_), ret)
  26. def test_import_yaml(self):
  27. yaml_file = "defaults.yaml"
  28. ret = self.run_function("jinja.import_yaml", [self._path(yaml_file)])
  29. with salt.utils.files.fopen(self._path(yaml_file, absolute=True)) as fh_:
  30. self.assertDictEqual(salt.utils.yaml.safe_load(fh_), ret)
  31. @requires_system_grains
  32. def test_load_map(self, grains):
  33. ret = self.run_function("jinja.load_map", [self._path("map.jinja"), "template"])
  34. assert isinstance(
  35. ret, dict
  36. ), "failed to return dictionary from jinja.load_map: {}".format(ret)
  37. with salt.utils.files.fopen(self._path("defaults.yaml", absolute=True)) as fh_:
  38. defaults = salt.utils.yaml.safe_load(fh_)
  39. with salt.utils.files.fopen(self._path("osarchmap.json", absolute=True)) as fh_:
  40. osarchmap = salt.utils.json.load(fh_)
  41. with salt.utils.files.fopen(
  42. self._path("osfamilymap.yaml", absolute=True)
  43. ) as fh_:
  44. osfamilymap = salt.utils.yaml.safe_load(fh_)
  45. with salt.utils.files.fopen(self._path("osmap.yaml", absolute=True)) as fh_:
  46. osmap = salt.utils.yaml.safe_load(fh_)
  47. with salt.utils.files.fopen(
  48. self._path("osfingermap.yaml", absolute=True)
  49. ) as fh_:
  50. osfingermap = salt.utils.yaml.safe_load(fh_)
  51. self.assertEqual(
  52. ret.get("arch"), osarchmap.get(grains["osarch"], {}).get("arch")
  53. )
  54. self.assertEqual(
  55. ret.get("config"),
  56. osfingermap.get(grains["osfinger"], {}).get(
  57. "config",
  58. osmap.get(grains["os"], {}).get(
  59. "config",
  60. osfamilymap.get(grains["os_family"], {}).get(
  61. "config", defaults.get("template").get("config")
  62. ),
  63. ),
  64. ),
  65. )