test_roster_matcher.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Test generic roster matching utility.
  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 import mixins
  10. from tests.support.unit import skipIf, TestCase
  11. from tests.support.runtests import RUNTIME_VARS
  12. # Import Salt Libs
  13. import salt.config
  14. import salt.loader
  15. import salt.utils.roster_matcher
  16. EXPECTED = {
  17. 'host1': {
  18. 'host': 'host1',
  19. 'passwd': 'test123',
  20. 'minion_opts': {
  21. 'escape_pods': 2,
  22. 'halon_system_timeout': 30,
  23. 'self_destruct_countdown': 60,
  24. 'some_server': 'foo.southeast.example.com'
  25. }
  26. },
  27. 'host2': {
  28. 'host': 'host2',
  29. 'passwd': 'test123',
  30. 'minion_opts': {
  31. 'escape_pods': 2,
  32. 'halon_system_timeout': 30,
  33. 'self_destruct_countdown': 60,
  34. 'some_server': 'foo.southeast.example.com'
  35. }
  36. },
  37. 'host3': {
  38. 'host': 'host3',
  39. 'passwd': 'test123',
  40. 'minion_opts': {}
  41. },
  42. 'host4': 'host4.example.com', # For testing get_data -> string_types branch
  43. 'host5': None, # For testing get_data -> False
  44. }
  45. class RosterMatcherTestCase(TestCase, mixins.LoaderModuleMockMixin):
  46. """
  47. Test the RosterMatcher Utility
  48. """
  49. def setup_loader_modules(self):
  50. opts = salt.config.master_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'master'))
  51. utils = salt.loader.utils(opts, whitelist=['json', 'stringutils'])
  52. runner = salt.loader.runner(opts, utils=utils, whitelist=['salt'])
  53. return {
  54. salt.utils.roster_matcher: {
  55. '__utils__': utils,
  56. '__opts__': {
  57. 'ssh_list_nodegroups': {
  58. 'list_nodegroup': ['host5', 'host1', 'host2'],
  59. 'string_nodegroup': 'host3,host5,host1,host4',
  60. }
  61. },
  62. '__runner__': runner
  63. }
  64. }
  65. def test_get_data(self):
  66. """
  67. Test that the get_data method returns the expected dictionaries.
  68. """
  69. # We don't care about tgt and tgt_type here.
  70. roster_matcher = salt.utils.roster_matcher.RosterMatcher(EXPECTED, 'tgt', 'tgt_type')
  71. self.assertEqual(EXPECTED['host1'], roster_matcher.get_data('host1'))
  72. self.assertEqual(EXPECTED['host2'], roster_matcher.get_data('host2'))
  73. self.assertEqual(EXPECTED['host3'], roster_matcher.get_data('host3'))
  74. self.assertEqual({'host': EXPECTED['host4']}, roster_matcher.get_data('host4'))
  75. def test_ret_glob_minions(self):
  76. """
  77. Test that we return minions matching a glob.
  78. """
  79. result = salt.utils.roster_matcher.targets(EXPECTED, '*[245]', 'glob')
  80. self.assertNotIn('host1', result)
  81. self.assertIn('host2', result)
  82. self.assertNotIn('host3', result)
  83. self.assertIn('host4', result)
  84. self.assertNotIn('host5', result)
  85. def test_ret_pcre_minions(self):
  86. """
  87. Test that we return minions matching a regular expression.
  88. """
  89. result = salt.utils.roster_matcher.targets(EXPECTED, '.*[^23]$', 'pcre')
  90. self.assertIn('host1', result)
  91. self.assertNotIn('host2', result)
  92. self.assertNotIn('host3', result)
  93. self.assertIn('host4', result)
  94. self.assertNotIn('host5', result)
  95. def test_ret_literal_list_minions(self):
  96. """
  97. Test that we return minions that are in a literal list.
  98. """
  99. result = salt.utils.roster_matcher.targets(EXPECTED, ['host1', 'host2', 'host5'], 'list')
  100. self.assertIn('host1', result)
  101. self.assertIn('host2', result)
  102. self.assertNotIn('host3', result)
  103. self.assertNotIn('host4', result)
  104. self.assertNotIn('host5', result)
  105. def test_ret_comma_delimited_string_minions(self):
  106. """
  107. Test that we return minions that are in a comma-delimited
  108. string of literal minion names.
  109. """
  110. result = salt.utils.roster_matcher.targets(EXPECTED, 'host5,host3,host2', 'list')
  111. self.assertNotIn('host1', result)
  112. self.assertIn('host2', result)
  113. self.assertIn('host3', result)
  114. self.assertNotIn('host4', result)
  115. self.assertNotIn('host5', result)
  116. def test_ret_oops_minions(self):
  117. """
  118. Test that we return no minions when we try to use a matching
  119. method that is not defined.
  120. """
  121. result = salt.utils.roster_matcher.targets(EXPECTED, None, 'xyzzy')
  122. self.assertEqual({}, result)
  123. def test_ret_literal_list_nodegroup_minions(self):
  124. """
  125. Test that we return minions that are in a nodegroup
  126. where the nodegroup expresses a literal list of minion names.
  127. """
  128. result = salt.utils.roster_matcher.targets(EXPECTED, 'list_nodegroup', 'nodegroup')
  129. self.assertIn('host1', result)
  130. self.assertIn('host2', result)
  131. self.assertNotIn('host3', result)
  132. self.assertNotIn('host4', result)
  133. self.assertNotIn('host5', result)
  134. def test_ret_comma_delimited_string_nodegroup_minions(self):
  135. """
  136. Test that we return minions that are in a nodegroup
  137. where the nodegroup expresses a comma delimited string
  138. of minion names.
  139. """
  140. result = salt.utils.roster_matcher.targets(EXPECTED, 'string_nodegroup', 'nodegroup')
  141. self.assertIn('host1', result)
  142. self.assertNotIn('host2', result)
  143. self.assertIn('host3', result)
  144. self.assertIn('host4', result)
  145. self.assertNotIn('host5', result)
  146. def test_ret_no_range_installed_minions(self):
  147. """
  148. Test that range matcher raises a Runtime Error if seco.range is not installed.
  149. """
  150. salt.utils.roster_matcher.HAS_RANGE = False
  151. with self.assertRaises(RuntimeError):
  152. salt.utils.roster_matcher.targets(EXPECTED, None, 'range')
  153. @skipIf(not salt.utils.roster_matcher.HAS_RANGE, 'seco.range is not installed')
  154. def test_ret_range_minions(self):
  155. """
  156. Test that range matcher raises a Runtime Error if seco.range is not installed.
  157. """
  158. self.fail('Not implemented')