test_config.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import fnmatch
  5. # Import Salt libs
  6. import salt.modules.config as config
  7. # Import Salt Testing libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.mock import patch
  10. from tests.support.unit import TestCase
  11. DEFAULTS = {
  12. "test.option.foo": "value of test.option.foo in DEFAULTS",
  13. "test.option.bar": "value of test.option.bar in DEFAULTS",
  14. "test.option.baz": "value of test.option.baz in DEFAULTS",
  15. "test.option": "value of test.option in DEFAULTS",
  16. }
  17. class TestModulesConfig(TestCase, LoaderModuleMockMixin):
  18. no_match = "test.option.nope"
  19. opt_name = "test.option.foo"
  20. wildcard_opt_name = "test.option.b*"
  21. def setup_loader_modules(self):
  22. return {
  23. config: {
  24. "__opts__": {
  25. "test.option.foo": "value of test.option.foo in __opts__",
  26. "test.option.bar": "value of test.option.bar in __opts__",
  27. "test.option.baz": "value of test.option.baz in __opts__",
  28. },
  29. "__pillar__": {
  30. "test.option.foo": "value of test.option.foo in __pillar__",
  31. "test.option.bar": "value of test.option.bar in __pillar__",
  32. "test.option.baz": "value of test.option.baz in __pillar__",
  33. "master": {
  34. "test.option.foo": "value of test.option.foo in master",
  35. "test.option.bar": "value of test.option.bar in master",
  36. "test.option.baz": "value of test.option.baz in master",
  37. },
  38. },
  39. "__grains__": {
  40. "test.option.foo": "value of test.option.foo in __grains__",
  41. "test.option.bar": "value of test.option.bar in __grains__",
  42. "test.option.baz": "value of test.option.baz in __grains__",
  43. },
  44. }
  45. }
  46. def _wildcard_match(self, data):
  47. return {x: data[x] for x in fnmatch.filter(data, self.wildcard_opt_name)}
  48. def test_defaults_only_name(self):
  49. with patch.dict(config.DEFAULTS, DEFAULTS):
  50. opt_name = "test.option"
  51. opt = config.option(opt_name)
  52. self.assertEqual(opt, config.DEFAULTS[opt_name])
  53. def test_no_match(self):
  54. """
  55. Make sure that the defa
  56. """
  57. with patch.dict(config.DEFAULTS, DEFAULTS):
  58. ret = config.option(self.no_match)
  59. assert ret == "", ret
  60. default = "wat"
  61. ret = config.option(self.no_match, default=default)
  62. assert ret == default, ret
  63. ret = config.option(self.no_match, wildcard=True)
  64. assert ret == {}, ret
  65. default = {"foo": "bar"}
  66. ret = config.option(self.no_match, default=default, wildcard=True)
  67. assert ret == default, ret
  68. # Should be no match since wildcard=False
  69. ret = config.option(self.wildcard_opt_name)
  70. assert ret == "", ret
  71. def test_omits(self):
  72. with patch.dict(config.DEFAULTS, DEFAULTS):
  73. # ********** OMIT NOTHING **********
  74. # Match should be in __opts__ dict
  75. ret = config.option(self.opt_name)
  76. assert ret == config.__opts__[self.opt_name], ret
  77. # Wildcard match
  78. ret = config.option(self.wildcard_opt_name, wildcard=True)
  79. assert ret == self._wildcard_match(config.__opts__), ret
  80. # ********** OMIT __opts__ **********
  81. # Match should be in __grains__ dict
  82. ret = config.option(self.opt_name, omit_opts=True)
  83. assert ret == config.__grains__[self.opt_name], ret
  84. # Wildcard match
  85. ret = config.option(self.wildcard_opt_name, omit_opts=True, wildcard=True)
  86. assert ret == self._wildcard_match(config.__grains__), ret
  87. # ********** OMIT __opts__, __grains__ **********
  88. # Match should be in __pillar__ dict
  89. ret = config.option(self.opt_name, omit_opts=True, omit_grains=True)
  90. assert ret == config.__pillar__[self.opt_name], ret
  91. # Wildcard match
  92. ret = config.option(
  93. self.wildcard_opt_name, omit_opts=True, omit_grains=True, wildcard=True
  94. )
  95. assert ret == self._wildcard_match(config.__pillar__), ret
  96. # ********** OMIT __opts__, __grains__, __pillar__ **********
  97. # Match should be in master opts
  98. ret = config.option(
  99. self.opt_name, omit_opts=True, omit_grains=True, omit_pillar=True
  100. )
  101. assert ret == config.__pillar__["master"][self.opt_name], ret
  102. # Wildcard match
  103. ret = config.option(
  104. self.wildcard_opt_name,
  105. omit_opts=True,
  106. omit_grains=True,
  107. omit_pillar=True,
  108. wildcard=True,
  109. )
  110. assert ret == self._wildcard_match(config.__pillar__["master"]), ret
  111. # ********** OMIT ALL THE THINGS **********
  112. # Match should be in master opts
  113. ret = config.option(
  114. self.opt_name,
  115. omit_opts=True,
  116. omit_grains=True,
  117. omit_pillar=True,
  118. omit_master=True,
  119. )
  120. assert ret == config.DEFAULTS[self.opt_name], ret
  121. # Wildcard match
  122. ret = config.option(
  123. self.wildcard_opt_name,
  124. omit_opts=True,
  125. omit_grains=True,
  126. omit_pillar=True,
  127. omit_master=True,
  128. wildcard=True,
  129. )
  130. assert ret == self._wildcard_match(config.DEFAULTS), ret
  131. # Match should be in master opts
  132. ret = config.option(self.opt_name, omit_all=True)
  133. assert ret == config.DEFAULTS[self.opt_name], ret
  134. # Wildcard match
  135. ret = config.option(self.wildcard_opt_name, omit_all=True, wildcard=True)
  136. assert ret == self._wildcard_match(config.DEFAULTS), ret