test_shared.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. from collections import OrderedDict
  2. from flexmock import flexmock
  3. from atticmatic.backends import shared as module
  4. from atticmatic.tests.builtins import builtins_mock
  5. from atticmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
  6. def insert_subprocess_mock(check_call_command, **kwargs):
  7. subprocess = flexmock()
  8. subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
  9. flexmock(module).subprocess = subprocess
  10. def insert_subprocess_never():
  11. subprocess = flexmock()
  12. subprocess.should_receive('check_call').never()
  13. flexmock(module).subprocess = subprocess
  14. def insert_platform_mock():
  15. flexmock(module.platform).should_receive('node').and_return('host')
  16. def insert_datetime_mock():
  17. flexmock(module).datetime = flexmock().should_receive('now').and_return(
  18. flexmock().should_receive('isoformat').and_return('now').mock
  19. ).mock
  20. CREATE_COMMAND_WITHOUT_EXCLUDES = ('attic', 'create', 'repo::host-now', 'foo', 'bar')
  21. CREATE_COMMAND = CREATE_COMMAND_WITHOUT_EXCLUDES + ('--exclude-from', 'excludes')
  22. def test_create_archive_should_call_attic_with_parameters():
  23. insert_subprocess_mock(CREATE_COMMAND)
  24. insert_platform_mock()
  25. insert_datetime_mock()
  26. module.create_archive(
  27. excludes_filename='excludes',
  28. verbosity=None,
  29. source_directories='foo bar',
  30. repository='repo',
  31. command='attic',
  32. )
  33. def test_create_archive_with_none_excludes_filename_should_call_attic_without_excludes():
  34. insert_subprocess_mock(CREATE_COMMAND_WITHOUT_EXCLUDES)
  35. insert_platform_mock()
  36. insert_datetime_mock()
  37. module.create_archive(
  38. excludes_filename=None,
  39. verbosity=None,
  40. source_directories='foo bar',
  41. repository='repo',
  42. command='attic',
  43. )
  44. def test_create_archive_with_verbosity_some_should_call_attic_with_stats_parameter():
  45. insert_subprocess_mock(CREATE_COMMAND + ('--stats',))
  46. insert_platform_mock()
  47. insert_datetime_mock()
  48. module.create_archive(
  49. excludes_filename='excludes',
  50. verbosity=VERBOSITY_SOME,
  51. source_directories='foo bar',
  52. repository='repo',
  53. command='attic',
  54. )
  55. def test_create_archive_with_verbosity_lots_should_call_attic_with_verbose_parameter():
  56. insert_subprocess_mock(CREATE_COMMAND + ('--verbose', '--stats'))
  57. insert_platform_mock()
  58. insert_datetime_mock()
  59. module.create_archive(
  60. excludes_filename='excludes',
  61. verbosity=VERBOSITY_LOTS,
  62. source_directories='foo bar',
  63. repository='repo',
  64. command='attic',
  65. )
  66. BASE_PRUNE_FLAGS = (
  67. ('--keep-daily', '1'),
  68. ('--keep-weekly', '2'),
  69. ('--keep-monthly', '3'),
  70. )
  71. def test_make_prune_flags_should_return_flags_from_config():
  72. retention_config = OrderedDict(
  73. (
  74. ('keep_daily', 1),
  75. ('keep_weekly', 2),
  76. ('keep_monthly', 3),
  77. )
  78. )
  79. result = module._make_prune_flags(retention_config)
  80. assert tuple(result) == BASE_PRUNE_FLAGS
  81. PRUNE_COMMAND = (
  82. 'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3',
  83. )
  84. def test_prune_archives_should_call_attic_with_parameters():
  85. retention_config = flexmock()
  86. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  87. BASE_PRUNE_FLAGS,
  88. )
  89. insert_subprocess_mock(PRUNE_COMMAND)
  90. module.prune_archives(
  91. verbosity=None,
  92. repository='repo',
  93. retention_config=retention_config,
  94. command='attic',
  95. )
  96. def test_prune_archives_with_verbosity_some_should_call_attic_with_stats_parameter():
  97. retention_config = flexmock()
  98. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  99. BASE_PRUNE_FLAGS,
  100. )
  101. insert_subprocess_mock(PRUNE_COMMAND + ('--stats',))
  102. module.prune_archives(
  103. repository='repo',
  104. verbosity=VERBOSITY_SOME,
  105. retention_config=retention_config,
  106. command='attic',
  107. )
  108. def test_prune_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
  109. retention_config = flexmock()
  110. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  111. BASE_PRUNE_FLAGS,
  112. )
  113. insert_subprocess_mock(PRUNE_COMMAND + ('--verbose', '--stats',))
  114. module.prune_archives(
  115. repository='repo',
  116. verbosity=VERBOSITY_LOTS,
  117. retention_config=retention_config,
  118. command='attic',
  119. )
  120. def test_parse_checks_returns_them_as_tuple():
  121. checks = module._parse_checks({'checks': 'foo disabled bar'})
  122. assert checks == ('foo', 'bar')
  123. def test_parse_checks_with_missing_value_returns_defaults():
  124. checks = module._parse_checks({})
  125. assert checks == module.DEFAULT_CHECKS
  126. def test_parse_checks_with_blank_value_returns_defaults():
  127. checks = module._parse_checks({'checks': ''})
  128. assert checks == module.DEFAULT_CHECKS
  129. def test_parse_checks_with_disabled_returns_no_checks():
  130. checks = module._parse_checks({'checks': 'disabled'})
  131. assert checks == ()
  132. def test_make_check_flags_with_checks_returns_flags():
  133. flags = module._make_check_flags(('foo', 'bar'))
  134. assert flags == ('--foo-only', '--bar-only')
  135. def test_make_check_flags_with_default_checks_returns_no_flags():
  136. flags = module._make_check_flags(module.DEFAULT_CHECKS)
  137. assert flags == ()
  138. def test_make_check_flags_with_checks_and_last_returns_flags_including_last():
  139. flags = module._make_check_flags(('foo', 'bar'), check_last=3)
  140. assert flags == ('--foo-only', '--bar-only', '--last', 3)
  141. def test_make_check_flags_with_last_returns_last_flag():
  142. flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3)
  143. assert flags == ('--last', 3)
  144. def test_check_archives_should_call_attic_with_parameters():
  145. checks = flexmock()
  146. check_last = flexmock()
  147. consistency_config = flexmock().should_receive('get').and_return(check_last).mock
  148. flexmock(module).should_receive('_parse_checks').and_return(checks)
  149. flexmock(module).should_receive('_make_check_flags').with_args(checks, check_last).and_return(())
  150. stdout = flexmock()
  151. insert_subprocess_mock(
  152. ('attic', 'check', 'repo'),
  153. stdout=stdout,
  154. )
  155. insert_platform_mock()
  156. insert_datetime_mock()
  157. builtins_mock().should_receive('open').and_return(stdout)
  158. flexmock(module.os).should_receive('devnull')
  159. module.check_archives(
  160. verbosity=None,
  161. repository='repo',
  162. consistency_config=consistency_config,
  163. command='attic',
  164. )
  165. def test_check_archives_with_verbosity_some_should_call_attic_with_verbose_parameter():
  166. consistency_config = flexmock().should_receive('get').and_return(None).mock
  167. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  168. flexmock(module).should_receive('_make_check_flags').and_return(())
  169. insert_subprocess_mock(
  170. ('attic', 'check', 'repo', '--verbose'),
  171. stdout=None,
  172. )
  173. insert_platform_mock()
  174. insert_datetime_mock()
  175. module.check_archives(
  176. verbosity=VERBOSITY_SOME,
  177. repository='repo',
  178. consistency_config=consistency_config,
  179. command='attic',
  180. )
  181. def test_check_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
  182. consistency_config = flexmock().should_receive('get').and_return(None).mock
  183. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  184. flexmock(module).should_receive('_make_check_flags').and_return(())
  185. insert_subprocess_mock(
  186. ('attic', 'check', 'repo', '--verbose'),
  187. stdout=None,
  188. )
  189. insert_platform_mock()
  190. insert_datetime_mock()
  191. module.check_archives(
  192. verbosity=VERBOSITY_LOTS,
  193. repository='repo',
  194. consistency_config=consistency_config,
  195. command='attic',
  196. )
  197. def test_check_archives_without_any_checks_should_bail():
  198. consistency_config = flexmock().should_receive('get').and_return(None).mock
  199. flexmock(module).should_receive('_parse_checks').and_return(())
  200. insert_subprocess_never()
  201. module.check_archives(
  202. verbosity=None,
  203. repository='repo',
  204. consistency_config=consistency_config,
  205. command='attic',
  206. )