test_mongodb_user.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch)
  13. # Import Salt Libs
  14. import salt.states.mongodb_user as mongodb_user
  15. class MongodbUserTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.states.mongodb_user
  18. '''
  19. def setup_loader_modules(self):
  20. return {mongodb_user: {'__opts__': {'test': True}}}
  21. # 'present' function tests: 1
  22. def test_present(self):
  23. '''
  24. Test to ensure that the user is present with the specified properties.
  25. '''
  26. name = 'myapp'
  27. passwd = 'password-of-myapp'
  28. ret = {'name': name,
  29. 'result': False,
  30. 'comment': '',
  31. 'changes': {}}
  32. comt = ('Port ({}) is not an integer.')
  33. ret.update({'comment': comt})
  34. self.assertDictEqual(mongodb_user.present(name, passwd, port={}), ret)
  35. mock_t = MagicMock(return_value=True)
  36. mock_f = MagicMock(return_value=[])
  37. with patch.dict(mongodb_user.__salt__,
  38. {
  39. 'mongodb.user_create': mock_t,
  40. 'mongodb.user_find': mock_f
  41. }):
  42. comt = ('User {0} is not present and needs to be created'
  43. ).format(name)
  44. ret.update({'comment': comt, 'result': None})
  45. self.assertDictEqual(mongodb_user.present(name, passwd), ret)
  46. with patch.dict(mongodb_user.__opts__, {'test': True}):
  47. comt = ('User {0} is not present and needs to be created'
  48. .format(name))
  49. ret.update({'comment': comt, 'result': None})
  50. self.assertDictEqual(mongodb_user.present(name, passwd), ret)
  51. with patch.dict(mongodb_user.__opts__, {'test': False}):
  52. comt = ('User {0} has been created'.format(name))
  53. ret.update({'comment': comt, 'result': True,
  54. 'changes': {name: 'Present'}})
  55. self.assertDictEqual(mongodb_user.present(name, passwd), ret)
  56. # 'absent' function tests: 1
  57. def test_absent(self):
  58. '''
  59. Test to ensure that the named user is absent.
  60. '''
  61. name = 'myapp'
  62. ret = {'name': name,
  63. 'result': False,
  64. 'comment': '',
  65. 'changes': {}}
  66. mock = MagicMock(side_effect=[True, True, False])
  67. mock_t = MagicMock(return_value=True)
  68. with patch.dict(mongodb_user.__salt__,
  69. {'mongodb.user_exists': mock,
  70. 'mongodb.user_remove': mock_t}):
  71. with patch.dict(mongodb_user.__opts__, {'test': True}):
  72. comt = ('User {0} is present and needs to be removed'
  73. .format(name))
  74. ret.update({'comment': comt, 'result': None})
  75. self.assertDictEqual(mongodb_user.absent(name), ret)
  76. with patch.dict(mongodb_user.__opts__, {'test': False}):
  77. comt = ('User {0} has been removed'.format(name))
  78. ret.update({'comment': comt, 'result': True,
  79. 'changes': {name: 'Absent'}})
  80. self.assertDictEqual(mongodb_user.absent(name), ret)
  81. comt = 'User {0} is not present'.format(name)
  82. ret.update({'comment': comt, 'result': True, 'changes': {}})
  83. self.assertDictEqual(mongodb_user.absent(name), ret)