test_attic.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. from collections import OrderedDict
  2. import sys
  3. from flexmock import flexmock
  4. from nose.tools import assert_raises
  5. from atticmatic import attic as module
  6. class MockCalledProcessError(Exception):
  7. def __init__(self, output):
  8. self.output = output
  9. def insert_subprocess_check_output_mock(call_command, error_output=None, **kwargs):
  10. subprocess = flexmock(CalledProcessError=MockCalledProcessError, STDOUT=flexmock())
  11. expectation = subprocess.should_receive('check_output').with_args(
  12. call_command,
  13. stderr=subprocess.STDOUT,
  14. **kwargs
  15. ).once()
  16. if error_output:
  17. expectation.and_raise(MockCalledProcessError, output=error_output)
  18. flexmock(sys.modules['__builtin__']).should_receive('print')
  19. flexmock(module).subprocess = subprocess
  20. return subprocess
  21. def insert_subprocess_check_call_mock(call_command, **kwargs):
  22. subprocess = flexmock()
  23. subprocess.should_receive('check_call').with_args(
  24. call_command,
  25. **kwargs
  26. ).once()
  27. flexmock(module).subprocess = subprocess
  28. return subprocess
  29. def insert_platform_mock():
  30. flexmock(module).platform = flexmock().should_receive('node').and_return('host').mock
  31. def insert_datetime_mock():
  32. flexmock(module).datetime = flexmock().should_receive('now').and_return(
  33. flexmock().should_receive('isoformat').and_return('now').mock
  34. ).mock
  35. def test_create_archive_should_call_attic_with_parameters():
  36. insert_subprocess_check_output_mock(
  37. ('attic', 'create', '--exclude-from', 'excludes', 'repo::host-now', 'foo', 'bar'),
  38. )
  39. insert_platform_mock()
  40. insert_datetime_mock()
  41. module.create_archive(
  42. excludes_filename='excludes',
  43. verbose=False,
  44. source_directories='foo bar',
  45. repository='repo',
  46. )
  47. def test_create_archive_with_verbose_should_call_attic_with_verbose_parameters():
  48. insert_subprocess_check_output_mock(
  49. (
  50. 'attic', 'create', '--exclude-from', 'excludes', 'repo::host-now', 'foo', 'bar',
  51. '--verbose', '--stats',
  52. ),
  53. )
  54. insert_platform_mock()
  55. insert_datetime_mock()
  56. module.create_archive(
  57. excludes_filename='excludes',
  58. verbose=True,
  59. source_directories='foo bar',
  60. repository='repo',
  61. )
  62. def test_create_archive_with_missing_repository_should_raise():
  63. insert_subprocess_check_output_mock(
  64. ('attic', 'create', '--exclude-from', 'excludes', 'repo::host-now', 'foo', 'bar'),
  65. error_output='Error: Repository repo does not exist',
  66. )
  67. insert_platform_mock()
  68. insert_datetime_mock()
  69. with assert_raises(RuntimeError):
  70. module.create_archive(
  71. excludes_filename='excludes',
  72. verbose=False,
  73. source_directories='foo bar',
  74. repository='repo',
  75. )
  76. def test_create_archive_with_other_error_should_raise():
  77. subprocess = insert_subprocess_check_output_mock(
  78. ('attic', 'create', '--exclude-from', 'excludes', 'repo::host-now', 'foo', 'bar'),
  79. error_output='Something went wrong',
  80. )
  81. insert_platform_mock()
  82. insert_datetime_mock()
  83. with assert_raises(subprocess.CalledProcessError):
  84. module.create_archive(
  85. excludes_filename='excludes',
  86. verbose=False,
  87. source_directories='foo bar',
  88. repository='repo',
  89. )
  90. BASE_PRUNE_FLAGS = (
  91. ('--keep-daily', '1'),
  92. ('--keep-weekly', '2'),
  93. ('--keep-monthly', '3'),
  94. )
  95. def test_make_prune_flags_should_return_flags_from_config():
  96. retention_config = OrderedDict(
  97. (
  98. ('keep_daily', 1),
  99. ('keep_weekly', 2),
  100. ('keep_monthly', 3),
  101. )
  102. )
  103. result = module.make_prune_flags(retention_config)
  104. assert tuple(result) == BASE_PRUNE_FLAGS
  105. def test_prune_archives_should_call_attic_with_parameters():
  106. retention_config = flexmock()
  107. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  108. BASE_PRUNE_FLAGS,
  109. )
  110. insert_subprocess_check_call_mock(
  111. (
  112. 'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly',
  113. '3',
  114. ),
  115. )
  116. module.prune_archives(
  117. verbose=False,
  118. repository='repo',
  119. retention_config=retention_config,
  120. )
  121. def test_prune_archives_with_verbose_should_call_attic_with_verbose_parameters():
  122. retention_config = flexmock()
  123. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  124. BASE_PRUNE_FLAGS,
  125. )
  126. insert_subprocess_check_call_mock(
  127. (
  128. 'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly',
  129. '3', '--verbose',
  130. ),
  131. )
  132. module.prune_archives(
  133. repository='repo',
  134. verbose=True,
  135. retention_config=retention_config,
  136. )
  137. def test_check_archives_should_call_attic_with_parameters():
  138. stdout = flexmock()
  139. insert_subprocess_check_call_mock(
  140. ('attic', 'check', 'repo'),
  141. stdout=stdout,
  142. )
  143. insert_platform_mock()
  144. insert_datetime_mock()
  145. flexmock(module).open = lambda filename, mode: stdout
  146. flexmock(module).os = flexmock().should_receive('devnull').mock
  147. module.check_archives(
  148. verbose=False,
  149. repository='repo',
  150. )
  151. def test_check_archives_with_verbose_should_call_attic_with_verbose_parameters():
  152. insert_subprocess_check_call_mock(
  153. ('attic', 'check', 'repo', '--verbose'),
  154. stdout=None,
  155. )
  156. insert_platform_mock()
  157. insert_datetime_mock()
  158. module.check_archives(
  159. verbose=True,
  160. repository='repo',
  161. )