test_jinja.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 Testing libs
  9. from tests.support.runtests import RUNTIME_VARS
  10. from tests.support.case import ModuleCase
  11. from tests.support.helpers import requires_system_grains
  12. # Import Salt libs
  13. import salt.utils.json
  14. import salt.utils.files
  15. import salt.utils.yaml
  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(self._path('osfamilymap.yaml', absolute=True)) 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(self._path('osfingermap.yaml', absolute=True)) as fh_:
  48. osfingermap = salt.utils.yaml.safe_load(fh_)
  49. self.assertEqual(ret.get('arch'), osarchmap.get(grains['osarch'], {}).get('arch'))
  50. self.assertEqual(
  51. ret.get('config'),
  52. osfingermap.get(
  53. grains['osfinger'], {}
  54. ).get('config', osmap.get(
  55. grains['os'], {}
  56. ).get('config', osfamilymap.get(
  57. grains['os_family'], {}
  58. ).get('config', defaults.get(
  59. 'template'
  60. ).get('config'))))
  61. )