test_match.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Oleg Lipovchenko <oleg.lipovchenko@gmail.com>
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.modules.match as match
  16. import salt.matchers.compound_match as compound_match
  17. import salt.matchers.glob_match as glob_match
  18. import salt.matchers.list_match as list_match
  19. MATCHERS_DICT = {
  20. 'compound_match.match': compound_match.match,
  21. 'glob_match.match': glob_match.match,
  22. 'list_match.match': list_match.match
  23. }
  24. # the name of the minion to be used for tests
  25. MINION_ID = 'bar03'
  26. @patch('salt.loader.matchers', MagicMock(return_value=MATCHERS_DICT))
  27. class MatchTestCase(TestCase, LoaderModuleMockMixin):
  28. '''
  29. This class contains a set of functions that test salt.modules.match.
  30. '''
  31. def setup_loader_modules(self):
  32. return {
  33. match: {
  34. '__opts__': {
  35. 'extension_modules': '',
  36. 'id': MINION_ID
  37. }
  38. },
  39. compound_match: {
  40. '__opts__': {
  41. 'id': MINION_ID
  42. }
  43. },
  44. glob_match: {
  45. '__opts__': {'id': MINION_ID}
  46. },
  47. list_match: {
  48. '__opts__': {'id': MINION_ID}
  49. }
  50. }
  51. def test_filter_by(self):
  52. '''
  53. Tests if filter_by returns the correct dictionary.
  54. '''
  55. lookup = {
  56. 'foo*': {
  57. 'key1': 'fooval1', 'key2': 'fooval2'
  58. },
  59. 'bar*': {
  60. 'key1': 'barval1', 'key2': 'barval2'
  61. }
  62. }
  63. result = {'key1': 'barval1', 'key2': 'barval2'}
  64. self.assertDictEqual(match.filter_by(lookup), result)
  65. def test_watch_for_opts_mismatch_glob_match(self):
  66. '''
  67. Tests for situations where the glob matcher might reference __opts__ directly
  68. instead of the local opts variable.
  69. When metaproxies/proxy minions are in use, matchers get called with a different `opts`
  70. dictionary. Inside the matchers we check to see if `opts` was passed
  71. and use it instead of `__opts__`. If sometime in the future we update the matchers
  72. and use `__opts__` directly this breaks proxy matching.
  73. '''
  74. self.assertTrue(glob_match.match('bar03'))
  75. self.assertTrue(glob_match.match('rest03', {'id': 'rest03'}))
  76. self.assertFalse(glob_match.match('rest03'))
  77. def test_watch_for_opts_mismatch_list_match(self):
  78. '''
  79. Tests for situations where the list matcher might reference __opts__ directly
  80. instead of the local opts variable
  81. When metaproxies/proxy minions are in use, matchers get called with a different `opts`
  82. dictionary. Inside the matchers we check to see if `opts` was passed
  83. and use it instead of `__opts__`. If sometime in the future we update the matchers
  84. and use `__opts__` directly this breaks proxy matching.
  85. '''
  86. self.assertTrue(list_match.match('bar03'))
  87. self.assertTrue(list_match.match('rest03', {'id': 'rest03'}))
  88. self.assertFalse(list_match.match('rest03'))
  89. def test_watch_for_opts_mismatch_compound_match(self):
  90. '''
  91. Tests for situations where the compound matcher might reference __opts__ directly
  92. instead of the local opts variable
  93. When metaproxies/proxy minions are in use, matchers get called with a different `opts`
  94. dictionary. Inside the matchers we check to see if `opts` was passed
  95. and use it instead of `__opts__`. If sometime in the future we update the matchers
  96. and use `__opts__` directly this breaks proxy matching.
  97. '''
  98. self.assertTrue(compound_match.match('L@bar03'))
  99. self.assertTrue(compound_match.match('L@rest03', {'id': 'rest03'}))
  100. self.assertFalse(compound_match.match('L@rest03'))