1
0

test_postgres_language.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Andrew Colin Kissa <andrew@topdog.za.net>
  4. '''
  5. from __future__ import absolute_import, unicode_literals, print_function
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.unit import TestCase
  8. from tests.support.mock import (
  9. MagicMock,
  10. patch
  11. )
  12. import salt.states.postgres_language as postgres_language
  13. class PostgresLanguageTestCase(TestCase, LoaderModuleMockMixin):
  14. '''
  15. Test cases for salt.states.postgres_language
  16. '''
  17. def setup_loader_modules(self):
  18. return {postgres_language: {}}
  19. def setUp(self):
  20. '''
  21. Setup data for the tests
  22. '''
  23. self.name = 'plpgsql'
  24. self.ret = {'name': self.name,
  25. 'changes': {},
  26. 'result': False,
  27. 'comment': ''}
  28. self.mock_true = MagicMock(return_value=True)
  29. self.mock_false = MagicMock(return_value=False)
  30. self.mock_empty_language_list = MagicMock(return_value={})
  31. self.mock_language_list = MagicMock(
  32. return_value={'plpgsql': self.name})
  33. def tearDown(self):
  34. del self.ret
  35. del self.mock_true
  36. del self.mock_false
  37. del self.mock_language_list
  38. del self.mock_empty_language_list
  39. def test_present_existing(self):
  40. '''
  41. Test present, language is already present in database
  42. '''
  43. with patch.dict(postgres_language.__salt__,
  44. {'postgres.language_list': self.mock_language_list}):
  45. comt = 'Language {0} is already installed'.format(self.name)
  46. self.ret.update({'comment': comt, 'result': True})
  47. self.assertDictEqual(
  48. postgres_language.present(self.name, 'testdb'), self.ret)
  49. def test_present_non_existing_pass(self):
  50. '''
  51. Test present, language not present in database - pass
  52. '''
  53. with patch.dict(postgres_language.__salt__,
  54. {'postgres.language_list': self.mock_empty_language_list,
  55. 'postgres.language_create': self.mock_true}):
  56. with patch.dict(postgres_language.__opts__, {'test': True}):
  57. comt = 'Language {0} is set to be installed'.format(self.name)
  58. self.ret.update({'comment': comt, 'result': None})
  59. self.assertDictEqual(
  60. postgres_language.present(self.name, 'testdb'), self.ret)
  61. with patch.dict(postgres_language.__opts__, {'test': False}):
  62. comt = 'Language {0} has been installed'.format(self.name)
  63. self.ret.update({'comment': comt,
  64. 'result': True,
  65. 'changes': {'plpgsql': 'Present'}})
  66. self.assertDictEqual(
  67. postgres_language.present(self.name, 'testdb'), self.ret)
  68. def test_present_non_existing_fail(self):
  69. '''
  70. Test present, language not present in database - fail
  71. '''
  72. with patch.dict(postgres_language.__salt__,
  73. {'postgres.language_list': self.mock_empty_language_list,
  74. 'postgres.language_create': self.mock_false}):
  75. with patch.dict(postgres_language.__opts__, {'test': True}):
  76. comt = 'Language {0} is set to be installed'.format(self.name)
  77. self.ret.update({'comment': comt, 'result': None})
  78. self.assertDictEqual(
  79. postgres_language.present(self.name, 'testdb'), self.ret)
  80. with patch.dict(postgres_language.__opts__, {'test': False}):
  81. comt = 'Failed to install language {0}'.format(self.name)
  82. self.ret.update({'comment': comt, 'result': False})
  83. self.assertDictEqual(
  84. postgres_language.present(self.name, 'testdb'), self.ret)
  85. def test_absent_existing(self):
  86. '''
  87. Test absent, language present in database
  88. '''
  89. with patch.dict(postgres_language.__salt__,
  90. {'postgres.language_exists': self.mock_true,
  91. 'postgres.language_remove': self.mock_true}):
  92. with patch.dict(postgres_language.__opts__, {'test': True}):
  93. comt = 'Language {0} is set to be removed'.format(self.name)
  94. self.ret.update({'comment': comt, 'result': None})
  95. self.assertDictEqual(
  96. postgres_language.absent(self.name, 'testdb'), self.ret)
  97. with patch.dict(postgres_language.__opts__, {'test': False}):
  98. comt = 'Language {0} has been removed'.format(self.name)
  99. self.ret.update({'comment': comt,
  100. 'result': True,
  101. 'changes': {'plpgsql': 'Absent'}})
  102. self.assertDictEqual(
  103. postgres_language.absent(self.name, 'testdb'), self.ret)
  104. def test_absent_non_existing(self):
  105. '''
  106. Test absent, language not present in database
  107. '''
  108. with patch.dict(postgres_language.__salt__,
  109. {'postgres.language_exists': self.mock_false}):
  110. with patch.dict(postgres_language.__opts__, {'test': True}):
  111. comt = 'Language {0} is not present so ' \
  112. 'it cannot be removed'.format(self.name)
  113. self.ret.update({'comment': comt, 'result': True})
  114. self.assertDictEqual(
  115. postgres_language.absent(self.name, 'testdb'), self.ret)