test_rsync.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Gareth J. Greenaway <gareth@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.rsync as rsync
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase
  12. from tests.support.mock import (
  13. MagicMock,
  14. patch
  15. )
  16. class RsyncTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Validate the Rsync state
  19. '''
  20. def setup_loader_modules(self):
  21. return {rsync: {}}
  22. def test_syncronized_failed(self):
  23. '''
  24. Test to perform an rsync.synchorized call that fails
  25. '''
  26. ret = {'pid': 100, 'retcode': 23, 'stderr': '', 'stdout': ''}
  27. _expected = {'changes': {},
  28. 'comment': 'Destination directory name was not found.',
  29. 'name': u'name',
  30. 'result': False}
  31. with patch.dict(rsync.__opts__, {'test': True}):
  32. mock = MagicMock(return_value=ret)
  33. with patch.dict(rsync.__salt__, {'rsync.rsync': mock}):
  34. self.assertDictEqual(rsync.synchronized("name", "source"),
  35. _expected)
  36. # Run again mocking os.path.exists as True
  37. ret = {'pid': 100,
  38. 'retcode': 23,
  39. 'stderr': 'Something went wrong',
  40. 'stdout': ''}
  41. _expected = {'changes': {},
  42. 'comment': 'Something went wrong',
  43. 'name': u'name',
  44. 'result': False}
  45. with patch('os.path.exists', MagicMock(return_value=True)):
  46. with patch.dict(rsync.__opts__, {'test': False}):
  47. mock = MagicMock(return_value=ret)
  48. with patch.dict(rsync.__salt__, {'rsync.rsync': mock}):
  49. self.assertDictEqual(rsync.synchronized("name", "source"),
  50. _expected)
  51. def test_syncronized(self):
  52. '''
  53. Test to perform an rsync.synchorized call that succeeds
  54. '''
  55. ret = {'pid': 100,
  56. 'retcode': 0,
  57. 'stderr': '',
  58. 'stdout': ('sending incremental file list\nsnapshot.jar\n\n'
  59. 'sent 106 bytes received 35 bytes 282.00 bytes/sec\n'
  60. 'total size is 0 speedup is 0.00')}
  61. _expected = {'changes': {'copied': 'snapshot.jar', 'deleted': u'N/A'},
  62. 'comment': ('- sent 106 bytes\n- received 35 bytes\n- '
  63. '282.00 bytes/sec\n- total size is 0\n- speedup is 0.00'),
  64. 'name': 'name',
  65. 'result': True}
  66. with patch('os.path.exists', MagicMock(return_value=True)):
  67. with patch.dict(rsync.__opts__, {'test': False}):
  68. mock = MagicMock(return_value=ret)
  69. with patch.dict(rsync.__salt__, {'rsync.rsync': mock}):
  70. self.assertDictEqual(rsync.synchronized("name", "source"),
  71. _expected)
  72. # Second pass simulating the file being in place
  73. ret = {'pid': 100,
  74. 'retcode': 0,
  75. 'stderr': '',
  76. 'stdout': ('sending incremental file list\n\n'
  77. 'sent 106 bytes received 35 bytes '
  78. '282.00 bytes/sec\ntotal size is 0 '
  79. 'speedup is 0.00')}
  80. _expected = {'changes': {},
  81. 'comment': ('- sent 106 bytes\n- received '
  82. '35 bytes\n- 282.00 bytes/sec\n- total '
  83. 'size is 0\n- speedup is 0.00'),
  84. 'name': 'name',
  85. 'result': True}
  86. with patch('os.path.exists', MagicMock(return_value=True)):
  87. with patch.dict(rsync.__opts__, {'test': False}):
  88. mock = MagicMock(return_value=ret)
  89. with patch.dict(rsync.__salt__, {'rsync.rsync': mock}):
  90. self.assertDictEqual(rsync.synchronized("name", "source"),
  91. _expected)
  92. def test_syncronized_test_true(self):
  93. '''
  94. Test to perform an rsync.synchorized call that fails
  95. '''
  96. ret = {'pid': 100,
  97. 'retcode': 23,
  98. 'stderr': 'Something went wrong',
  99. 'stdout': ''}
  100. _expected = {'changes': {},
  101. 'comment': '- ',
  102. 'name': u'name',
  103. 'result': None}
  104. with patch('os.path.exists', MagicMock(return_value=True)):
  105. with patch.dict(rsync.__opts__, {'test': True}):
  106. mock = MagicMock(return_value=ret)
  107. with patch.dict(rsync.__salt__, {'rsync.rsync': mock}):
  108. self.assertDictEqual(rsync.synchronized("name", "source"),
  109. _expected)