test_roster_matcher.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Libs
  9. import salt.config
  10. import salt.loader
  11. import salt.utils.roster_matcher
  12. # Import Salt Testing Libs
  13. from tests.support import mixins
  14. from tests.support.runtests import RUNTIME_VARS
  15. from tests.support.unit import TestCase, skipIf
  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": {"host": "host3", "passwd": "test123", "minion_opts": {}},
  38. "host4": "host4.example.com", # For testing get_data -> string_types branch
  39. "host5": None, # For testing get_data -> False
  40. }
  41. class RosterMatcherTestCase(TestCase, mixins.LoaderModuleMockMixin):
  42. """
  43. Test the RosterMatcher Utility
  44. """
  45. def setup_loader_modules(self):
  46. opts = salt.config.master_config(
  47. os.path.join(RUNTIME_VARS.TMP_CONF_DIR, "master")
  48. )
  49. utils = salt.loader.utils(opts, whitelist=["json", "stringutils"])
  50. runner = salt.loader.runner(opts, utils=utils, whitelist=["salt"])
  51. return {
  52. salt.utils.roster_matcher: {
  53. "__utils__": utils,
  54. "__opts__": {
  55. "ssh_list_nodegroups": {
  56. "list_nodegroup": ["host5", "host1", "host2"],
  57. "string_nodegroup": "host3,host5,host1,host4",
  58. }
  59. },
  60. "__runner__": runner,
  61. }
  62. }
  63. def test_get_data(self):
  64. """
  65. Test that the get_data method returns the expected dictionaries.
  66. """
  67. # We don't care about tgt and tgt_type here.
  68. roster_matcher = salt.utils.roster_matcher.RosterMatcher(
  69. EXPECTED, "tgt", "tgt_type"
  70. )
  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(
  100. EXPECTED, ["host1", "host2", "host5"], "list"
  101. )
  102. self.assertIn("host1", result)
  103. self.assertIn("host2", result)
  104. self.assertNotIn("host3", result)
  105. self.assertNotIn("host4", result)
  106. self.assertNotIn("host5", result)
  107. def test_ret_comma_delimited_string_minions(self):
  108. """
  109. Test that we return minions that are in a comma-delimited
  110. string of literal minion names.
  111. """
  112. result = salt.utils.roster_matcher.targets(
  113. EXPECTED, "host5,host3,host2", "list"
  114. )
  115. self.assertNotIn("host1", result)
  116. self.assertIn("host2", result)
  117. self.assertIn("host3", result)
  118. self.assertNotIn("host4", result)
  119. self.assertNotIn("host5", result)
  120. def test_ret_oops_minions(self):
  121. """
  122. Test that we return no minions when we try to use a matching
  123. method that is not defined.
  124. """
  125. result = salt.utils.roster_matcher.targets(EXPECTED, None, "xyzzy")
  126. self.assertEqual({}, result)
  127. def test_ret_literal_list_nodegroup_minions(self):
  128. """
  129. Test that we return minions that are in a nodegroup
  130. where the nodegroup expresses a literal list of minion names.
  131. """
  132. result = salt.utils.roster_matcher.targets(
  133. EXPECTED, "list_nodegroup", "nodegroup"
  134. )
  135. self.assertIn("host1", result)
  136. self.assertIn("host2", result)
  137. self.assertNotIn("host3", result)
  138. self.assertNotIn("host4", result)
  139. self.assertNotIn("host5", result)
  140. def test_ret_comma_delimited_string_nodegroup_minions(self):
  141. """
  142. Test that we return minions that are in a nodegroup
  143. where the nodegroup expresses a comma delimited string
  144. of minion names.
  145. """
  146. result = salt.utils.roster_matcher.targets(
  147. EXPECTED, "string_nodegroup", "nodegroup"
  148. )
  149. self.assertIn("host1", result)
  150. self.assertNotIn("host2", result)
  151. self.assertIn("host3", result)
  152. self.assertIn("host4", result)
  153. self.assertNotIn("host5", result)
  154. def test_ret_no_range_installed_minions(self):
  155. """
  156. Test that range matcher raises a Runtime Error if seco.range is not installed.
  157. """
  158. salt.utils.roster_matcher.HAS_RANGE = False
  159. with self.assertRaises(RuntimeError):
  160. salt.utils.roster_matcher.targets(EXPECTED, None, "range")
  161. @skipIf(not salt.utils.roster_matcher.HAS_RANGE, "seco.range is not installed")
  162. def test_ret_range_minions(self):
  163. """
  164. Test that range matcher raises a Runtime Error if seco.range is not installed.
  165. """
  166. self.fail("Not implemented")