test_shared.py 9.3 KB

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