test_consul_pillar.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 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(not consul_pillar.consul, 'python-consul module not installed')
  33. class ConsulPillarTestCase(TestCase, LoaderModuleMockMixin):
  34. '''
  35. Test cases for salt.pillar.consul_pillar
  36. '''
  37. def setup_loader_modules(self):
  38. return {
  39. consul_pillar: {
  40. '__opts__': OPTS,
  41. 'get_conn': MagicMock(return_value='consul_connection')
  42. }
  43. }
  44. def test_connection(self):
  45. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  46. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  47. consul_pillar.ext_pillar('testminion', {}, 'consul_config root=test-shared/')
  48. consul_pillar.get_conn.assert_called_once_with(OPTS, 'consul_config')
  49. def test_pillar_data(self):
  50. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  51. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  52. pillar_data = consul_pillar.ext_pillar('testminion', {}, 'consul_config root=test-shared/')
  53. consul_pillar.consul_fetch.assert_called_once_with('consul_connection', 'test-shared')
  54. assert sorted(pillar_data) == ['sites', 'user']
  55. self.assertNotIn('blankvalue', pillar_data['user'])
  56. def test_blank_root(self):
  57. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  58. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  59. pillar_data = consul_pillar.ext_pillar('testminion', {}, 'consul_config')
  60. consul_pillar.consul_fetch.assert_called_once_with('consul_connection', '')
  61. assert sorted(pillar_data) == ['test-shared']
  62. def test_pillar_nest(self):
  63. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  64. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  65. pillar_data = consul_pillar.ext_pillar(
  66. 'testminion', {}, 'consul_config pillar_root=nested-key/ root=test-shared/ '
  67. )
  68. assert sorted(pillar_data['nested-key']) == ['sites', 'user']
  69. self.assertNotIn('blankvalue', pillar_data['nested-key']['user'])
  70. def test_value_parsing(self):
  71. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  72. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  73. pillar_data = consul_pillar.ext_pillar('testminion', {}, 'consul_config root=test-shared/')
  74. assert isinstance(pillar_data['user']['dontsplit'], six.string_types)
  75. def test_non_expansion(self):
  76. with patch.dict(consul_pillar.__salt__, {'grains.get': MagicMock(return_value=({}))}):
  77. with patch.object(consul_pillar, 'consul_fetch', MagicMock(return_value=('2232', PILLAR_DATA))):
  78. pillar_data = consul_pillar.ext_pillar(
  79. 'testminion', {}, 'consul_config root=test-shared/ expand_keys=false'
  80. )
  81. assert isinstance(pillar_data['user']['dontexpand'], six.string_types)
  82. def test_dict_merge(self):
  83. test_dict = {}
  84. with patch.dict(test_dict, SIMPLE_DICT):
  85. self.assertDictEqual(consul_pillar.dict_merge(test_dict, SIMPLE_DICT), SIMPLE_DICT)
  86. with patch.dict(test_dict, {'key1': {'key3': {'key4': 'value'}}}):
  87. self.assertDictEqual(consul_pillar.dict_merge(test_dict, SIMPLE_DICT),
  88. {'key1': {'key2': 'val1', 'key3': {'key4': 'value'}}})