1
0

test_consul_pillar.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.pillar.consul_pillar as consul_pillar
  6. # Import 3rd-party libs
  7. from salt.ext import six
  8. # Import Salt Testing libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.mock import MagicMock, patch
  11. from tests.support.unit import TestCase, skipIf
  12. OPTS = {"consul_config": {"consul.port": 8500, "consul.host": "172.17.0.15"}}
  13. PILLAR_DATA = [
  14. {
  15. "Value": "/path/to/certs/testsite1.crt",
  16. "Key": "test-shared/sites/testsite1/ssl/certs/SSLCertificateFile",
  17. },
  18. {
  19. "Value": "/path/to/certs/testsite1.key",
  20. "Key": "test-shared/sites/testsite1/ssl/certs/SSLCertificateKeyFile",
  21. },
  22. {"Value": None, "Key": "test-shared/sites/testsite1/ssl/certs/"},
  23. {"Value": "True", "Key": "test-shared/sites/testsite1/ssl/force"},
  24. {"Value": None, "Key": "test-shared/sites/testsite1/ssl/"},
  25. {
  26. "Value": "salt://sites/testsite1.tmpl",
  27. "Key": "test-shared/sites/testsite1/template",
  28. },
  29. {"Value": "test.example.com", "Key": "test-shared/sites/testsite1/uri"},
  30. {"Value": None, "Key": "test-shared/sites/testsite1/"},
  31. {"Value": None, "Key": "test-shared/sites/"},
  32. {"Value": "Test User", "Key": "test-shared/user/full_name"},
  33. {"Value": "adm\nwww-data\nmlocate", "Key": "test-shared/user/groups"},
  34. {"Value": '"adm\nwww-data\nmlocate"', "Key": "test-shared/user/dontsplit"},
  35. {"Value": "yaml:\n key: value\n", "Key": "test-shared/user/dontexpand"},
  36. {"Value": None, "Key": "test-shared/user/blankvalue"},
  37. {"Value": "test", "Key": "test-shared/user/login"},
  38. {"Value": None, "Key": "test-shared/user/"},
  39. ]
  40. SIMPLE_DICT = {"key1": {"key2": "val1"}}
  41. @skipIf(not consul_pillar.consul, "python-consul module not installed")
  42. class ConsulPillarTestCase(TestCase, LoaderModuleMockMixin):
  43. """
  44. Test cases for salt.pillar.consul_pillar
  45. """
  46. def setup_loader_modules(self):
  47. return {
  48. consul_pillar: {
  49. "__opts__": OPTS,
  50. "get_conn": MagicMock(return_value="consul_connection"),
  51. }
  52. }
  53. def test_connection(self):
  54. with patch.dict(
  55. consul_pillar.__salt__, {"grains.get": MagicMock(return_value=({}))}
  56. ):
  57. with patch.object(
  58. consul_pillar,
  59. "consul_fetch",
  60. MagicMock(return_value=("2232", PILLAR_DATA)),
  61. ):
  62. consul_pillar.ext_pillar(
  63. "testminion", {}, "consul_config root=test-shared/"
  64. )
  65. consul_pillar.get_conn.assert_called_once_with(OPTS, "consul_config")
  66. def test_pillar_data(self):
  67. with patch.dict(
  68. consul_pillar.__salt__, {"grains.get": MagicMock(return_value=({}))}
  69. ):
  70. with patch.object(
  71. consul_pillar,
  72. "consul_fetch",
  73. MagicMock(return_value=("2232", PILLAR_DATA)),
  74. ):
  75. pillar_data = consul_pillar.ext_pillar(
  76. "testminion", {}, "consul_config root=test-shared/"
  77. )
  78. consul_pillar.consul_fetch.assert_called_once_with(
  79. "consul_connection", "test-shared"
  80. )
  81. assert sorted(pillar_data) == ["sites", "user"]
  82. self.assertNotIn("blankvalue", pillar_data["user"])
  83. def test_blank_root(self):
  84. with patch.dict(
  85. consul_pillar.__salt__, {"grains.get": MagicMock(return_value=({}))}
  86. ):
  87. with patch.object(
  88. consul_pillar,
  89. "consul_fetch",
  90. MagicMock(return_value=("2232", PILLAR_DATA)),
  91. ):
  92. pillar_data = consul_pillar.ext_pillar(
  93. "testminion", {}, "consul_config"
  94. )
  95. consul_pillar.consul_fetch.assert_called_once_with(
  96. "consul_connection", ""
  97. )
  98. assert sorted(pillar_data) == ["test-shared"]
  99. def test_pillar_nest(self):
  100. with patch.dict(
  101. consul_pillar.__salt__, {"grains.get": MagicMock(return_value=({}))}
  102. ):
  103. with patch.object(
  104. consul_pillar,
  105. "consul_fetch",
  106. MagicMock(return_value=("2232", PILLAR_DATA)),
  107. ):
  108. pillar_data = consul_pillar.ext_pillar(
  109. "testminion",
  110. {},
  111. "consul_config pillar_root=nested-key/ root=test-shared/ ",
  112. )
  113. assert sorted(pillar_data["nested-key"]) == ["sites", "user"]
  114. self.assertNotIn("blankvalue", pillar_data["nested-key"]["user"])
  115. def test_value_parsing(self):
  116. with patch.dict(
  117. consul_pillar.__salt__, {"grains.get": MagicMock(return_value=({}))}
  118. ):
  119. with patch.object(
  120. consul_pillar,
  121. "consul_fetch",
  122. MagicMock(return_value=("2232", PILLAR_DATA)),
  123. ):
  124. pillar_data = consul_pillar.ext_pillar(
  125. "testminion", {}, "consul_config root=test-shared/"
  126. )
  127. assert isinstance(pillar_data["user"]["dontsplit"], six.string_types)
  128. def test_non_expansion(self):
  129. with patch.dict(
  130. consul_pillar.__salt__, {"grains.get": MagicMock(return_value=({}))}
  131. ):
  132. with patch.object(
  133. consul_pillar,
  134. "consul_fetch",
  135. MagicMock(return_value=("2232", PILLAR_DATA)),
  136. ):
  137. pillar_data = consul_pillar.ext_pillar(
  138. "testminion",
  139. {},
  140. "consul_config root=test-shared/ expand_keys=false",
  141. )
  142. assert isinstance(pillar_data["user"]["dontexpand"], six.string_types)
  143. def test_dict_merge(self):
  144. test_dict = {}
  145. with patch.dict(test_dict, SIMPLE_DICT):
  146. self.assertDictEqual(
  147. consul_pillar.dict_merge(test_dict, SIMPLE_DICT), SIMPLE_DICT
  148. )
  149. with patch.dict(test_dict, {"key1": {"key3": {"key4": "value"}}}):
  150. self.assertDictEqual(
  151. consul_pillar.dict_merge(test_dict, SIMPLE_DICT),
  152. {"key1": {"key2": "val1", "key3": {"key4": "value"}}},
  153. )