test_consul_pillar.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import TestCase, skipIf
  7. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
  8. # Import Salt Libs
  9. import salt.pillar.consul_pillar as consul_pillar
  10. # Import 3rd-party libs
  11. from salt.ext import six
  12. OPTS = {'consul_config': {'consul.port': 8500, 'consul.host': '172.17.0.15'}}
  13. PILLAR_DATA = [
  14. {'Value': '/path/to/certs/testsite1.crt', 'Key': 'test-shared/sites/testsite1/ssl/certs/SSLCertificateFile'},
  15. {'Value': '/path/to/certs/testsite1.key', 'Key': 'test-shared/sites/testsite1/ssl/certs/SSLCertificateKeyFile'},
  16. {'Value': None, 'Key': 'test-shared/sites/testsite1/ssl/certs/'},
  17. {'Value': 'True', 'Key': 'test-shared/sites/testsite1/ssl/force'},
  18. {'Value': None, 'Key': 'test-shared/sites/testsite1/ssl/'},
  19. {'Value': 'salt://sites/testsite1.tmpl', 'Key': 'test-shared/sites/testsite1/template'},
  20. {'Value': 'test.example.com', 'Key': 'test-shared/sites/testsite1/uri'},
  21. {'Value': None, 'Key': 'test-shared/sites/testsite1/'},
  22. {'Value': None, 'Key': 'test-shared/sites/'},
  23. {'Value': 'Test User', 'Key': 'test-shared/user/full_name'},
  24. {'Value': 'adm\nwww-data\nmlocate', 'Key': 'test-shared/user/groups'},
  25. {'Value': '"adm\nwww-data\nmlocate"', 'Key': 'test-shared/user/dontsplit'},
  26. {'Value': 'yaml:\n key: value\n', 'Key': 'test-shared/user/dontexpand'},
  27. {'Value': None, 'Key': 'test-shared/user/blankvalue'},
  28. {'Value': 'test', 'Key': 'test-shared/user/login'},
  29. {'Value': None, 'Key': 'test-shared/user/'}
  30. ]
  31. SIMPLE_DICT = {'key1': {'key2': 'val1'}}
  32. @skipIf(NO_MOCK, NO_MOCK_REASON)
  33. @skipIf(not consul_pillar.consul, 'python-consul module not installed')
  34. class ConsulPillarTestCase(TestCase, LoaderModuleMockMixin):
  35. '''
  36. Test cases for salt.pillar.consul_pillar
  37. '''
  38. def setup_loader_modules(self):
  39. return {
  40. consul_pillar: {
  41. '__opts__': OPTS,
  42. 'get_conn': MagicMock(return_value='consul_connection')
  43. }
  44. }
  45. def test_connection(self):
  46. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  47. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  48. consul_pillar.ext_pillar('testminion', {}, 'consul_config root=test-shared/')
  49. consul_pillar.get_conn.assert_called_once_with(OPTS, 'consul_config')
  50. def test_pillar_data(self):
  51. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  52. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  53. pillar_data = consul_pillar.ext_pillar('testminion', {}, 'consul_config root=test-shared/')
  54. consul_pillar.consul_fetch.assert_called_once_with('consul_connection', 'test-shared')
  55. assert sorted(pillar_data) == ['sites', 'user']
  56. self.assertNotIn('blankvalue', pillar_data['user'])
  57. def test_blank_root(self):
  58. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  59. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  60. pillar_data = consul_pillar.ext_pillar('testminion', {}, 'consul_config')
  61. consul_pillar.consul_fetch.assert_called_once_with('consul_connection', '')
  62. assert sorted(pillar_data) == ['test-shared']
  63. def test_pillar_nest(self):
  64. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  65. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  66. pillar_data = consul_pillar.ext_pillar(
  67. 'testminion', {}, 'consul_config pillar_root=nested-key/ root=test-shared/ '
  68. )
  69. assert sorted(pillar_data['nested-key']) == ['sites', 'user']
  70. self.assertNotIn('blankvalue', pillar_data['nested-key']['user'])
  71. def test_value_parsing(self):
  72. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  73. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  74. pillar_data = consul_pillar.ext_pillar('testminion', {}, 'consul_config root=test-shared/')
  75. assert isinstance(pillar_data['user']['dontsplit'], six.string_types)
  76. def test_non_expansion(self):
  77. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  78. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  79. pillar_data = consul_pillar.ext_pillar(
  80. 'testminion', {}, 'consul_config root=test-shared/ expand_keys=false'
  81. )
  82. assert isinstance(pillar_data['user']['dontexpand'], six.string_types)
  83. def test_dict_merge(self):
  84. test_dict = {}
  85. with patch.dict(test_dict, SIMPLE_DICT):
  86. self.assertDictEqual(consul_pillar.dict_merge(test_dict, SIMPLE_DICT), SIMPLE_DICT)
  87. with patch.dict(test_dict, {'key1': {'key3': {'key4': 'value'}}}):
  88. self.assertDictEqual(consul_pillar.dict_merge(test_dict, SIMPLE_DICT),
  89. {'key1': {'key2': 'val1', 'key3': {'key4': 'value'}}})