test_jinja.py 2.7 KB

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