test_shared.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 = ('attic', 'create', '--exclude-from', 'excludes', 'repo::host-now', 'foo', 'bar')
  21. def test_create_archive_should_call_attic_with_parameters():
  22. insert_subprocess_mock(CREATE_COMMAND)
  23. insert_platform_mock()
  24. insert_datetime_mock()
  25. module.create_archive(
  26. excludes_filename='excludes',
  27. verbosity=None,
  28. source_directories='foo bar',
  29. repository='repo',
  30. command='attic',
  31. )
  32. def test_create_archive_with_verbosity_some_should_call_attic_with_stats_parameter():
  33. insert_subprocess_mock(CREATE_COMMAND + ('--stats',))
  34. insert_platform_mock()
  35. insert_datetime_mock()
  36. module.create_archive(
  37. excludes_filename='excludes',
  38. verbosity=VERBOSITY_SOME,
  39. source_directories='foo bar',
  40. repository='repo',
  41. command='attic',
  42. )
  43. def test_create_archive_with_verbosity_lots_should_call_attic_with_verbose_parameter():
  44. insert_subprocess_mock(CREATE_COMMAND + ('--verbose', '--stats'))
  45. insert_platform_mock()
  46. insert_datetime_mock()
  47. module.create_archive(
  48. excludes_filename='excludes',
  49. verbosity=VERBOSITY_LOTS,
  50. source_directories='foo bar',
  51. repository='repo',
  52. command='attic',
  53. )
  54. BASE_PRUNE_FLAGS = (
  55. ('--keep-daily', '1'),
  56. ('--keep-weekly', '2'),
  57. ('--keep-monthly', '3'),
  58. )
  59. def test_make_prune_flags_should_return_flags_from_config():
  60. retention_config = OrderedDict(
  61. (
  62. ('keep_daily', 1),
  63. ('keep_weekly', 2),
  64. ('keep_monthly', 3),
  65. )
  66. )
  67. result = module._make_prune_flags(retention_config)
  68. assert tuple(result) == BASE_PRUNE_FLAGS
  69. PRUNE_COMMAND = (
  70. 'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3',
  71. )
  72. def test_prune_archives_should_call_attic_with_parameters():
  73. retention_config = flexmock()
  74. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  75. BASE_PRUNE_FLAGS,
  76. )
  77. insert_subprocess_mock(PRUNE_COMMAND)
  78. module.prune_archives(
  79. verbosity=None,
  80. repository='repo',
  81. retention_config=retention_config,
  82. command='attic',
  83. )
  84. def test_prune_archives_with_verbosity_some_should_call_attic_with_stats_parameter():
  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 + ('--stats',))
  90. module.prune_archives(
  91. repository='repo',
  92. verbosity=VERBOSITY_SOME,
  93. retention_config=retention_config,
  94. command='attic',
  95. )
  96. def test_prune_archives_with_verbosity_lots_should_call_attic_with_verbose_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 + ('--verbose', '--stats',))
  102. module.prune_archives(
  103. repository='repo',
  104. verbosity=VERBOSITY_LOTS,
  105. retention_config=retention_config,
  106. command='attic',
  107. )
  108. def test_parse_checks_returns_them_as_tuple():
  109. checks = module._parse_checks({'checks': 'foo disabled bar'})
  110. assert checks == ('foo', 'bar')
  111. def test_parse_checks_with_missing_value_returns_defaults():
  112. checks = module._parse_checks({})
  113. assert checks == module.DEFAULT_CHECKS
  114. def test_parse_checks_with_blank_value_returns_defaults():
  115. checks = module._parse_checks({'checks': ''})
  116. assert checks == module.DEFAULT_CHECKS
  117. def test_parse_checks_with_disabled_returns_no_checks():
  118. checks = module._parse_checks({'checks': 'disabled'})
  119. assert checks == ()
  120. def test_make_check_flags_with_checks_returns_flags():
  121. flags = module._make_check_flags(('foo', 'bar'))
  122. assert flags == ('--foo-only', '--bar-only')
  123. def test_make_check_flags_with_default_checks_returns_no_flags():
  124. flags = module._make_check_flags(module.DEFAULT_CHECKS)
  125. assert flags == ()
  126. def test_check_archives_should_call_attic_with_parameters():
  127. consistency_config = flexmock()
  128. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  129. flexmock(module).should_receive('_make_check_flags').and_return(())
  130. stdout = flexmock()
  131. insert_subprocess_mock(
  132. ('attic', 'check', 'repo'),
  133. stdout=stdout,
  134. )
  135. insert_platform_mock()
  136. insert_datetime_mock()
  137. builtins_mock().should_receive('open').and_return(stdout)
  138. flexmock(module.os).should_receive('devnull')
  139. module.check_archives(
  140. verbosity=None,
  141. repository='repo',
  142. consistency_config=consistency_config,
  143. command='attic',
  144. )
  145. def test_check_archives_with_verbosity_some_should_call_attic_with_verbose_parameter():
  146. consistency_config = flexmock()
  147. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  148. flexmock(module).should_receive('_make_check_flags').and_return(())
  149. insert_subprocess_mock(
  150. ('attic', 'check', 'repo', '--verbose'),
  151. stdout=None,
  152. )
  153. insert_platform_mock()
  154. insert_datetime_mock()
  155. module.check_archives(
  156. verbosity=VERBOSITY_SOME,
  157. repository='repo',
  158. consistency_config=consistency_config,
  159. command='attic',
  160. )
  161. def test_check_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
  162. consistency_config = flexmock()
  163. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  164. flexmock(module).should_receive('_make_check_flags').and_return(())
  165. insert_subprocess_mock(
  166. ('attic', 'check', 'repo', '--verbose'),
  167. stdout=None,
  168. )
  169. insert_platform_mock()
  170. insert_datetime_mock()
  171. module.check_archives(
  172. verbosity=VERBOSITY_LOTS,
  173. repository='repo',
  174. consistency_config=consistency_config,
  175. command='attic',
  176. )
  177. def test_check_archives_without_any_checks_should_bail():
  178. consistency_config = flexmock()
  179. flexmock(module).should_receive('_parse_checks').and_return(())
  180. insert_subprocess_never()
  181. module.check_archives(
  182. verbosity=None,
  183. repository='repo',
  184. consistency_config=consistency_config,
  185. command='attic',
  186. )