test_consul_pillar.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_pillar_nest(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(
  61. 'testminion', {}, 'consul_config root=test-shared/ pillar_root=nested-key/'
  62. )
  63. consul_pillar.consul_fetch.assert_called_once_with('consul_connection', 'test-shared/')
  64. assert sorted(pillar_data['nested-key']) == ['sites', 'user']
  65. self.assertNotIn('blankvalue', pillar_data['nested-key']['user'])
  66. def test_value_parsing(self):
  67. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  68. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  69. pillar_data = consul_pillar.ext_pillar('testminion', {}, 'consul_config root=test-shared/')
  70. assert isinstance(pillar_data['user']['dontsplit'], six.string_types)
  71. def test_non_expansion(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(
  75. 'testminion', {}, 'consul_config root=test-shared/ expand_keys=false'
  76. )
  77. assert isinstance(pillar_data['user']['dontexpand'], six.string_types)
  78. def test_dict_merge(self):
  79. test_dict = {}
  80. with patch.dict(test_dict, SIMPLE_DICT):
  81. self.assertDictEqual(consul_pillar.dict_merge(test_dict, SIMPLE_DICT), SIMPLE_DICT)
  82. with patch.dict(test_dict, {'key1': {'key3': {'key4': 'value'}}}):
  83. self.assertDictEqual(consul_pillar.dict_merge(test_dict, SIMPLE_DICT),
  84. {'key1': {'key2': 'val1', 'key3': {'key4': 'value'}}})