test_svn.py 5.1 KB

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