test_shared.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_two_spaces_in_source_directories():
  52. insert_subprocess_mock(CREATE_COMMAND)
  53. insert_platform_mock()
  54. insert_datetime_mock()
  55. module.create_archive(
  56. excludes_filename='excludes',
  57. verbosity=None,
  58. storage_config={},
  59. source_directories='foo bar',
  60. repository='repo',
  61. command='attic',
  62. )
  63. def test_create_archive_with_none_excludes_filename_should_call_attic_without_excludes():
  64. insert_subprocess_mock(CREATE_COMMAND_WITHOUT_EXCLUDES)
  65. insert_platform_mock()
  66. insert_datetime_mock()
  67. module.create_archive(
  68. excludes_filename=None,
  69. verbosity=None,
  70. storage_config={},
  71. source_directories='foo bar',
  72. repository='repo',
  73. command='attic',
  74. )
  75. def test_create_archive_with_verbosity_some_should_call_attic_with_stats_parameter():
  76. insert_subprocess_mock(CREATE_COMMAND + ('--stats',))
  77. insert_platform_mock()
  78. insert_datetime_mock()
  79. module.create_archive(
  80. excludes_filename='excludes',
  81. verbosity=VERBOSITY_SOME,
  82. storage_config={},
  83. source_directories='foo bar',
  84. repository='repo',
  85. command='attic',
  86. )
  87. def test_create_archive_with_verbosity_lots_should_call_attic_with_verbose_parameter():
  88. insert_subprocess_mock(CREATE_COMMAND + ('--verbose', '--stats'))
  89. insert_platform_mock()
  90. insert_datetime_mock()
  91. module.create_archive(
  92. excludes_filename='excludes',
  93. verbosity=VERBOSITY_LOTS,
  94. storage_config={},
  95. source_directories='foo bar',
  96. repository='repo',
  97. command='attic',
  98. )
  99. def test_create_archive_with_compression_should_call_attic_with_compression_parameters():
  100. insert_subprocess_mock(CREATE_COMMAND + ('--compression', 'rle'))
  101. insert_platform_mock()
  102. insert_datetime_mock()
  103. module.create_archive(
  104. excludes_filename='excludes',
  105. verbosity=None,
  106. storage_config={'compression': 'rle'},
  107. source_directories='foo bar',
  108. repository='repo',
  109. command='attic',
  110. )
  111. def test_create_archive_with_one_file_system_should_call_attic_with_one_file_system_parameters():
  112. insert_subprocess_mock(CREATE_COMMAND + ('--one-file-system',))
  113. insert_platform_mock()
  114. insert_datetime_mock()
  115. module.create_archive(
  116. excludes_filename='excludes',
  117. verbosity=None,
  118. storage_config={},
  119. source_directories='foo bar',
  120. repository='repo',
  121. command='attic',
  122. one_file_system=True,
  123. )
  124. BASE_PRUNE_FLAGS = (
  125. ('--keep-daily', '1'),
  126. ('--keep-weekly', '2'),
  127. ('--keep-monthly', '3'),
  128. )
  129. def test_make_prune_flags_should_return_flags_from_config():
  130. retention_config = OrderedDict(
  131. (
  132. ('keep_daily', 1),
  133. ('keep_weekly', 2),
  134. ('keep_monthly', 3),
  135. )
  136. )
  137. result = module._make_prune_flags(retention_config)
  138. assert tuple(result) == BASE_PRUNE_FLAGS
  139. PRUNE_COMMAND = (
  140. 'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3',
  141. )
  142. def test_prune_archives_should_call_attic_with_parameters():
  143. retention_config = flexmock()
  144. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  145. BASE_PRUNE_FLAGS,
  146. )
  147. insert_subprocess_mock(PRUNE_COMMAND)
  148. module.prune_archives(
  149. verbosity=None,
  150. repository='repo',
  151. retention_config=retention_config,
  152. command='attic',
  153. )
  154. def test_prune_archives_with_verbosity_some_should_call_attic_with_stats_parameter():
  155. retention_config = flexmock()
  156. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  157. BASE_PRUNE_FLAGS,
  158. )
  159. insert_subprocess_mock(PRUNE_COMMAND + ('--stats',))
  160. module.prune_archives(
  161. repository='repo',
  162. verbosity=VERBOSITY_SOME,
  163. retention_config=retention_config,
  164. command='attic',
  165. )
  166. def test_prune_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
  167. retention_config = flexmock()
  168. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  169. BASE_PRUNE_FLAGS,
  170. )
  171. insert_subprocess_mock(PRUNE_COMMAND + ('--verbose', '--stats',))
  172. module.prune_archives(
  173. repository='repo',
  174. verbosity=VERBOSITY_LOTS,
  175. retention_config=retention_config,
  176. command='attic',
  177. )
  178. def test_parse_checks_returns_them_as_tuple():
  179. checks = module._parse_checks({'checks': 'foo disabled bar'})
  180. assert checks == ('foo', 'bar')
  181. def test_parse_checks_with_missing_value_returns_defaults():
  182. checks = module._parse_checks({})
  183. assert checks == module.DEFAULT_CHECKS
  184. def test_parse_checks_with_blank_value_returns_defaults():
  185. checks = module._parse_checks({'checks': ''})
  186. assert checks == module.DEFAULT_CHECKS
  187. def test_parse_checks_with_disabled_returns_no_checks():
  188. checks = module._parse_checks({'checks': 'disabled'})
  189. assert checks == ()
  190. def test_make_check_flags_with_checks_returns_flags():
  191. flags = module._make_check_flags(('foo', 'bar'))
  192. assert flags == ('--foo-only', '--bar-only')
  193. def test_make_check_flags_with_default_checks_returns_no_flags():
  194. flags = module._make_check_flags(module.DEFAULT_CHECKS)
  195. assert flags == ()
  196. def test_make_check_flags_with_checks_and_last_returns_flags_including_last():
  197. flags = module._make_check_flags(('foo', 'bar'), check_last=3)
  198. assert flags == ('--foo-only', '--bar-only', '--last', 3)
  199. def test_make_check_flags_with_last_returns_last_flag():
  200. flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3)
  201. assert flags == ('--last', 3)
  202. def test_check_archives_should_call_attic_with_parameters():
  203. checks = flexmock()
  204. check_last = flexmock()
  205. consistency_config = flexmock().should_receive('get').and_return(check_last).mock
  206. flexmock(module).should_receive('_parse_checks').and_return(checks)
  207. flexmock(module).should_receive('_make_check_flags').with_args(checks, check_last).and_return(())
  208. stdout = flexmock()
  209. insert_subprocess_mock(
  210. ('attic', 'check', 'repo'),
  211. stdout=stdout,
  212. )
  213. insert_platform_mock()
  214. insert_datetime_mock()
  215. builtins_mock().should_receive('open').and_return(stdout)
  216. flexmock(module.os).should_receive('devnull')
  217. module.check_archives(
  218. verbosity=None,
  219. repository='repo',
  220. consistency_config=consistency_config,
  221. command='attic',
  222. )
  223. def test_check_archives_with_verbosity_some_should_call_attic_with_verbose_parameter():
  224. consistency_config = flexmock().should_receive('get').and_return(None).mock
  225. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  226. flexmock(module).should_receive('_make_check_flags').and_return(())
  227. insert_subprocess_mock(
  228. ('attic', 'check', 'repo', '--verbose'),
  229. stdout=None,
  230. )
  231. insert_platform_mock()
  232. insert_datetime_mock()
  233. module.check_archives(
  234. verbosity=VERBOSITY_SOME,
  235. repository='repo',
  236. consistency_config=consistency_config,
  237. command='attic',
  238. )
  239. def test_check_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
  240. consistency_config = flexmock().should_receive('get').and_return(None).mock
  241. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  242. flexmock(module).should_receive('_make_check_flags').and_return(())
  243. insert_subprocess_mock(
  244. ('attic', 'check', 'repo', '--verbose'),
  245. stdout=None,
  246. )
  247. insert_platform_mock()
  248. insert_datetime_mock()
  249. module.check_archives(
  250. verbosity=VERBOSITY_LOTS,
  251. repository='repo',
  252. consistency_config=consistency_config,
  253. command='attic',
  254. )
  255. def test_check_archives_without_any_checks_should_bail():
  256. consistency_config = flexmock().should_receive('get').and_return(None).mock
  257. flexmock(module).should_receive('_parse_checks').and_return(())
  258. insert_subprocess_never()
  259. module.check_archives(
  260. verbosity=None,
  261. repository='repo',
  262. consistency_config=consistency_config,
  263. command='attic',
  264. )