test_pecl.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Libs
  8. import salt.states.pecl as pecl
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class PeclTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.pecl
  16. """
  17. def setup_loader_modules(self):
  18. return {pecl: {}}
  19. # 'installed' function tests: 1
  20. def test_installed(self):
  21. """
  22. Test to make sure that a pecl extension is installed.
  23. """
  24. name = "mongo"
  25. ver = "1.0.1"
  26. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  27. mock_lst = MagicMock(return_value={name: "stable"})
  28. mock_t = MagicMock(return_value=True)
  29. with patch.dict(pecl.__salt__, {"pecl.list": mock_lst, "pecl.install": mock_t}):
  30. comt = "Pecl extension {0} is already installed.".format(name)
  31. ret.update({"comment": comt, "result": True})
  32. self.assertDictEqual(pecl.installed(name), ret)
  33. with patch.dict(pecl.__opts__, {"test": True}):
  34. comt = "Pecl extension mongo-1.0.1 would have been installed"
  35. ret.update({"comment": comt, "result": None})
  36. self.assertDictEqual(pecl.installed(name, version=ver), ret)
  37. with patch.dict(pecl.__opts__, {"test": False}):
  38. comt = "Pecl extension mongo-1.0.1 was successfully installed"
  39. ret.update(
  40. {
  41. "comment": comt,
  42. "result": True,
  43. "changes": {"mongo-1.0.1": "Installed"},
  44. }
  45. )
  46. self.assertDictEqual(pecl.installed(name, version=ver), ret)
  47. # 'removed' function tests: 1
  48. def test_removed(self):
  49. """
  50. Test to make sure that a pecl extension is not installed.
  51. """
  52. name = "mongo"
  53. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  54. mock_lst = MagicMock(side_effect=[{}, {name: "stable"}, {name: "stable"}])
  55. mock_t = MagicMock(return_value=True)
  56. with patch.dict(
  57. pecl.__salt__, {"pecl.list": mock_lst, "pecl.uninstall": mock_t}
  58. ):
  59. comt = "Pecl extension {0} is not installed.".format(name)
  60. ret.update({"comment": comt, "result": True})
  61. self.assertDictEqual(pecl.removed(name), ret)
  62. with patch.dict(pecl.__opts__, {"test": True}):
  63. comt = "Pecl extension mongo would have been removed"
  64. ret.update({"comment": comt, "result": None})
  65. self.assertDictEqual(pecl.removed(name), ret)
  66. with patch.dict(pecl.__opts__, {"test": False}):
  67. comt = "Pecl extension mongo was successfully removed."
  68. ret.update(
  69. {"comment": comt, "result": True, "changes": {name: "Removed"}}
  70. )
  71. self.assertDictEqual(pecl.removed(name), ret)