test_composer.py 4.2 KB

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