test_composer.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.composer as composer
  9. from salt.exceptions import SaltException
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. class ComposerTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.states.composer
  17. """
  18. def setup_loader_modules(self):
  19. return {composer: {}}
  20. # 'installed' function tests: 1
  21. def test_installed(self):
  22. """
  23. Test to verify that the correct versions of composer
  24. dependencies are present.
  25. """
  26. name = "CURL"
  27. ret = {"name": name, "result": True, "comment": "", "changes": {}}
  28. mock = MagicMock(return_value=True)
  29. with patch.dict(composer.__salt__, {"composer.did_composer_install": mock}):
  30. comt = "Composer already installed this directory"
  31. ret.update({"comment": comt})
  32. self.assertDictEqual(composer.installed(name, always_check=False), ret)
  33. with patch.dict(composer.__opts__, {"test": True}):
  34. comt = 'The state of "CURL" will be changed.'
  35. changes = {
  36. "new": "composer install will be run in CURL",
  37. "old": "composer install has been run in CURL",
  38. }
  39. ret.update({"comment": comt, "result": None, "changes": changes})
  40. self.assertDictEqual(composer.installed(name), ret)
  41. with patch.dict(composer.__opts__, {"test": False}):
  42. mock = MagicMock(side_effect=[SaltException, {}])
  43. with patch.dict(composer.__salt__, {"composer.install": mock}):
  44. comt = "Error executing composer in " "'CURL': "
  45. ret.update({"comment": comt, "result": False, "changes": {}})
  46. self.assertDictEqual(composer.installed(name), ret)
  47. comt = (
  48. "Composer install completed successfully,"
  49. " output silenced by quiet flag"
  50. )
  51. ret.update({"comment": comt, "result": True})
  52. self.assertDictEqual(composer.installed(name, quiet=True), ret)
  53. # 'update' function tests: 1
  54. def test_update(self):
  55. """
  56. Test to composer update the directory to ensure we have
  57. the latest versions of all project dependencies.
  58. """
  59. name = "CURL"
  60. ret = {"name": name, "result": True, "comment": "", "changes": {}}
  61. changes = {
  62. "new": "composer install/update will be run in CURL",
  63. "old": "composer install has not yet been run in CURL",
  64. }
  65. mock = MagicMock(return_value=True)
  66. with patch.dict(composer.__salt__, {"composer.did_composer_install": mock}):
  67. with patch.dict(composer.__opts__, {"test": True}):
  68. comt = 'The state of "CURL" will be changed.'
  69. ret.update({"comment": comt, "result": None, "changes": changes})
  70. self.assertDictEqual(composer.update(name), ret)
  71. with patch.dict(composer.__opts__, {"test": False}):
  72. mock = MagicMock(side_effect=[SaltException, {}])
  73. with patch.dict(composer.__salt__, {"composer.update": mock}):
  74. comt = "Error executing composer in " "'CURL': "
  75. ret.update({"comment": comt, "result": False, "changes": {}})
  76. self.assertDictEqual(composer.update(name), ret)
  77. comt = (
  78. "Composer update completed successfully,"
  79. " output silenced by quiet flag"
  80. )
  81. ret.update({"comment": comt, "result": True})
  82. self.assertDictEqual(composer.update(name, quiet=True), ret)