test_saltclass.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. # Import Salt Testing libs
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.unit import TestCase, skipIf
  8. from tests.support.mock import NO_MOCK, NO_MOCK_REASON
  9. # Import Salt Libs
  10. import salt.pillar.saltclass as saltclass
  11. base_path = os.path.dirname(os.path.realpath(__file__))
  12. fake_minion_id = 'fake_id'
  13. fake_pillar = {}
  14. fake_args = ({'path': os.path.abspath(
  15. os.path.join(base_path, '..', '..', 'integration',
  16. 'files', 'saltclass', 'examples'))})
  17. fake_opts = {}
  18. fake_salt = {}
  19. fake_grains = {}
  20. @skipIf(NO_MOCK, NO_MOCK_REASON)
  21. class SaltclassPillarTestCase(TestCase, LoaderModuleMockMixin):
  22. '''
  23. Tests for salt.pillar.saltclass
  24. '''
  25. def setup_loader_modules(self):
  26. return {saltclass: {'__opts__': fake_opts,
  27. '__salt__': fake_salt,
  28. '__grains__': fake_grains}}
  29. def _runner(self, expected_ret):
  30. try:
  31. full_ret = saltclass.ext_pillar(fake_minion_id, fake_pillar, fake_args)
  32. parsed_ret = full_ret['__saltclass__']['classes']
  33. # Fail the test if we hit our NoneType error
  34. except TypeError as err:
  35. self.fail(err)
  36. # Else give the parsed content result
  37. self.assertListEqual(parsed_ret, expected_ret)
  38. def test_succeeds(self):
  39. ret = ['default.users', 'default.motd', 'default.empty', 'default', 'roles.app', 'roles.nginx']
  40. self._runner(ret)
  41. @skipIf(NO_MOCK, NO_MOCK_REASON)
  42. class SaltclassPillarTestCaseListExpansion(TestCase, LoaderModuleMockMixin):
  43. '''
  44. Tests for salt.pillar.saltclass variable expansion in list
  45. '''
  46. def setup_loader_modules(self):
  47. return {saltclass: {'__opts__': fake_opts,
  48. '__salt__': fake_salt,
  49. '__grains__': fake_grains
  50. }}
  51. def _runner(self, expected_ret):
  52. try:
  53. full_ret = saltclass.ext_pillar(fake_minion_id, fake_pillar, fake_args)
  54. parsed_ret = full_ret['test_list']
  55. # Fail the test if we hit our NoneType error
  56. except TypeError as err:
  57. self.fail(err)
  58. # Else give the parsed content result
  59. self.assertListEqual(parsed_ret, expected_ret)
  60. def test_succeeds(self):
  61. ret = [{'a': '192.168.10.10'}, '192.168.10.20']
  62. self._runner(ret)