test_mongodb_database.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_database as mongodb_database
  15. class MongodbDatabaseTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.states.mongodb_database
  18. '''
  19. def setup_loader_modules(self):
  20. return {mongodb_database: {}}
  21. # 'absent' function tests: 1
  22. def test_absent(self):
  23. '''
  24. Test to ensure that the named database is absent.
  25. '''
  26. name = "mydb"
  27. ret = {'name': name,
  28. 'result': None,
  29. 'comment': '',
  30. 'changes': {}}
  31. mock = MagicMock(side_effect=[True, True, False])
  32. mock_t = MagicMock(return_value=True)
  33. with patch.dict(mongodb_database.__salt__,
  34. {'mongodb.db_exists': mock,
  35. 'mongodb.db_remove': mock_t}):
  36. with patch.dict(mongodb_database.__opts__, {'test': True}):
  37. comt = ('Database {0} is present and needs to be removed'
  38. .format(name))
  39. ret.update({'comment': comt})
  40. self.assertDictEqual(mongodb_database.absent(name), ret)
  41. with patch.dict(mongodb_database.__opts__, {'test': False}):
  42. comt = ('Database {0} has been removed'.format(name))
  43. ret.update({'comment': comt, 'result': True,
  44. 'changes': {'mydb': 'Absent'}})
  45. self.assertDictEqual(mongodb_database.absent(name), ret)
  46. comt = 'Database {0} is not present'.format(name)
  47. ret.update({'comment': comt, 'changes': {}})
  48. self.assertDictEqual(mongodb_database.absent(name), ret)