test_svn.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import os
  8. # Import Salt Libs
  9. import salt.states.svn as svn
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.unit import TestCase
  13. from tests.support.mock import (
  14. MagicMock,
  15. patch
  16. )
  17. class SvnTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Validate the svn state
  20. '''
  21. def setup_loader_modules(self):
  22. return {svn: {}}
  23. def test_latest(self):
  24. '''
  25. Checkout or update the working directory to
  26. the latest revision from the remote repository.
  27. '''
  28. mock = MagicMock(return_value=True)
  29. with patch.object(svn, '_fail', mock):
  30. self.assertTrue(svn.latest("salt"))
  31. mock = MagicMock(side_effect=[True, False, False, False])
  32. with patch.object(os.path, 'exists', mock):
  33. mock = MagicMock(return_value=False)
  34. with patch.object(os.path, 'isdir', mock):
  35. with patch.object(svn, '_fail', mock):
  36. self.assertFalse(svn.latest("salt", "c://salt"))
  37. with patch.dict(svn.__opts__, {'test': True}):
  38. mock = MagicMock(return_value=["salt"])
  39. with patch.object(svn, '_neutral_test', mock):
  40. self.assertListEqual(svn.latest("salt", "c://salt"),
  41. ['salt'])
  42. mock = MagicMock(side_effect=[False, True])
  43. with patch.object(os.path, 'exists', mock):
  44. mock = MagicMock(return_value=True)
  45. info_mock = MagicMock(return_value=[{'Revision': 'mocked'}])
  46. with patch.dict(svn.__salt__, {'svn.diff': mock, 'svn.info': info_mock}):
  47. mock = MagicMock(return_value=["Dude"])
  48. with patch.object(svn, '_neutral_test', mock):
  49. self.assertListEqual(svn.latest("salt",
  50. "c://salt"),
  51. ['Dude'])
  52. with patch.dict(svn.__opts__, {'test': False}):
  53. mock = MagicMock(return_value=[{'Revision': 'a'}])
  54. with patch.dict(svn.__salt__, {'svn.info': mock}):
  55. mock = MagicMock(return_value=True)
  56. with patch.dict(svn.__salt__, {'svn.update': mock}):
  57. self.assertDictEqual(svn.latest("salt", "c://salt"),
  58. {'changes': {}, 'comment': True,
  59. 'name': 'salt', 'result': True})
  60. def test_export(self):
  61. '''
  62. Test to export a file or directory from an SVN repository
  63. '''
  64. mock = MagicMock(return_value=True)
  65. with patch.object(svn, '_fail', mock):
  66. self.assertTrue(svn.export("salt"))
  67. mock = MagicMock(side_effect=[True, False, False, False])
  68. with patch.object(os.path, 'exists', mock):
  69. mock = MagicMock(return_value=False)
  70. with patch.object(os.path, 'isdir', mock):
  71. with patch.object(svn, '_fail', mock):
  72. self.assertFalse(svn.export("salt", "c://salt"))
  73. with patch.dict(svn.__opts__, {'test': True}):
  74. mock = MagicMock(return_value=["salt"])
  75. with patch.object(svn, '_neutral_test', mock):
  76. self.assertListEqual(svn.export("salt", "c://salt"),
  77. ['salt'])
  78. mock = MagicMock(side_effect=[False, True])
  79. with patch.object(os.path, 'exists', mock):
  80. mock = MagicMock(return_value=True)
  81. with patch.dict(svn.__salt__, {'svn.list': mock}):
  82. mock = MagicMock(return_value=["Dude"])
  83. with patch.object(svn, '_neutral_test', mock):
  84. self.assertListEqual(svn.export("salt",
  85. "c://salt"),
  86. ['Dude'])
  87. with patch.dict(svn.__opts__, {'test': False}):
  88. mock = MagicMock(return_value=True)
  89. with patch.dict(svn.__salt__, {'svn.export': mock}):
  90. self.assertDictEqual(
  91. svn.export("salt", "c://salt"),
  92. {
  93. 'changes': {
  94. 'new': 'salt',
  95. 'comment': 'salt was Exported to c://salt',
  96. },
  97. 'comment': '',
  98. 'name': 'salt',
  99. 'result': True,
  100. }
  101. )
  102. def test_dirty(self):
  103. '''
  104. Test to determine if the working directory has been changed.
  105. '''
  106. mock = MagicMock(return_value=True)
  107. with patch.object(svn, '_fail', mock):
  108. self.assertTrue(svn.dirty("salt", "c://salt"))