1
0

test_artifactory.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.artifactory as artifactory
  15. class ArtifactoryTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.states.artifactory
  18. '''
  19. def setup_loader_modules(self):
  20. return {artifactory: {}}
  21. # 'downloaded' function tests: 1
  22. def test_downloaded(self):
  23. '''
  24. Test to ensures that the artifact from artifactory exists at
  25. given location.
  26. '''
  27. name = 'jboss'
  28. arti_url = 'http://artifactory.intranet.example.com/artifactory'
  29. artifact = {'artifactory_url': arti_url, 'artifact_id': 'module',
  30. 'repository': 'libs-release-local', 'packaging': 'jar',
  31. 'group_id': 'com.company.module', 'classifier': 'sources',
  32. 'version': '1.0'}
  33. ret = {'name': name,
  34. 'result': False,
  35. 'changes': {},
  36. 'comment': ''}
  37. mck = MagicMock(return_value={'status': False, 'changes': {},
  38. 'comment': ''})
  39. with patch.dict(artifactory.__salt__, {'artifactory.get_release': mck}):
  40. self.assertDictEqual(artifactory.downloaded(name, artifact), ret)
  41. with patch.object(artifactory, '__fetch_from_artifactory',
  42. MagicMock(side_effect=Exception('error'))):
  43. ret = artifactory.downloaded(name, artifact)
  44. self.assertEqual(ret['result'], False)
  45. self.assertEqual(ret['comment'], 'error')
  46. # 'downloaded test=True' function tests: 1
  47. def test_downloaded_test_true(self):
  48. '''
  49. Test to ensures that the artifact from artifactory exists at
  50. given location.
  51. '''
  52. name = 'jboss'
  53. arti_url = 'http://artifactory.intranet.example.com/artifactory'
  54. artifact = {'artifactory_url': arti_url, 'artifact_id': 'module',
  55. 'repository': 'libs-release-local', 'packaging': 'jar',
  56. 'group_id': 'com.company.module', 'classifier': 'sources',
  57. 'version': '1.0'}
  58. ret = {'name': name,
  59. 'result': True,
  60. 'changes': {},
  61. 'comment': 'Artifact would be downloaded from URL: http://artifactory.intranet.example.com/artifactory'}
  62. mck = MagicMock(return_value={'status': False, 'changes': {},
  63. 'comment': ''})
  64. with patch.dict(artifactory.__salt__, {'artifactory.get_release': mck}):
  65. with patch.dict(artifactory.__opts__, {'test': True}):
  66. self.assertDictEqual(artifactory.downloaded(name, artifact), ret)
  67. with patch.object(artifactory, '__fetch_from_artifactory',
  68. MagicMock(side_effect=Exception('error'))):
  69. ret = artifactory.downloaded(name, artifact)
  70. self.assertEqual(ret['result'], False)
  71. self.assertEqual(ret['comment'], 'error')