test_attic.py 6.8 KB

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