test_shared.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. def test_create_archive_with_umask_should_call_attic_with_umask_parameters():
  125. insert_subprocess_mock(CREATE_COMMAND + ('--umask', '740'))
  126. insert_platform_mock()
  127. insert_datetime_mock()
  128. module.create_archive(
  129. excludes_filename='excludes',
  130. verbosity=None,
  131. storage_config={'umask': 740},
  132. source_directories='foo bar',
  133. repository='repo',
  134. command='attic',
  135. )
  136. BASE_PRUNE_FLAGS = (
  137. ('--keep-daily', '1'),
  138. ('--keep-weekly', '2'),
  139. ('--keep-monthly', '3'),
  140. )
  141. def test_make_prune_flags_should_return_flags_from_config():
  142. retention_config = OrderedDict(
  143. (
  144. ('keep_daily', 1),
  145. ('keep_weekly', 2),
  146. ('keep_monthly', 3),
  147. )
  148. )
  149. result = module._make_prune_flags(retention_config)
  150. assert tuple(result) == BASE_PRUNE_FLAGS
  151. PRUNE_COMMAND = (
  152. 'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3',
  153. )
  154. def test_prune_archives_should_call_attic_with_parameters():
  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)
  160. module.prune_archives(
  161. verbosity=None,
  162. repository='repo',
  163. retention_config=retention_config,
  164. command='attic',
  165. )
  166. def test_prune_archives_with_verbosity_some_should_call_attic_with_stats_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 + ('--stats',))
  172. module.prune_archives(
  173. repository='repo',
  174. verbosity=VERBOSITY_SOME,
  175. retention_config=retention_config,
  176. command='attic',
  177. )
  178. def test_prune_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
  179. retention_config = flexmock()
  180. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  181. BASE_PRUNE_FLAGS,
  182. )
  183. insert_subprocess_mock(PRUNE_COMMAND + ('--verbose', '--stats',))
  184. module.prune_archives(
  185. repository='repo',
  186. verbosity=VERBOSITY_LOTS,
  187. retention_config=retention_config,
  188. command='attic',
  189. )
  190. def test_parse_checks_returns_them_as_tuple():
  191. checks = module._parse_checks({'checks': 'foo disabled bar'})
  192. assert checks == ('foo', 'bar')
  193. def test_parse_checks_with_missing_value_returns_defaults():
  194. checks = module._parse_checks({})
  195. assert checks == module.DEFAULT_CHECKS
  196. def test_parse_checks_with_blank_value_returns_defaults():
  197. checks = module._parse_checks({'checks': ''})
  198. assert checks == module.DEFAULT_CHECKS
  199. def test_parse_checks_with_disabled_returns_no_checks():
  200. checks = module._parse_checks({'checks': 'disabled'})
  201. assert checks == ()
  202. def test_make_check_flags_with_checks_returns_flags():
  203. flags = module._make_check_flags(('foo', 'bar'))
  204. assert flags == ('--foo-only', '--bar-only')
  205. def test_make_check_flags_with_default_checks_returns_no_flags():
  206. flags = module._make_check_flags(module.DEFAULT_CHECKS)
  207. assert flags == ()
  208. def test_make_check_flags_with_checks_and_last_returns_flags_including_last():
  209. flags = module._make_check_flags(('foo', 'bar'), check_last=3)
  210. assert flags == ('--foo-only', '--bar-only', '--last', 3)
  211. def test_make_check_flags_with_last_returns_last_flag():
  212. flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3)
  213. assert flags == ('--last', 3)
  214. def test_check_archives_should_call_attic_with_parameters():
  215. checks = flexmock()
  216. check_last = flexmock()
  217. consistency_config = flexmock().should_receive('get').and_return(check_last).mock
  218. flexmock(module).should_receive('_parse_checks').and_return(checks)
  219. flexmock(module).should_receive('_make_check_flags').with_args(checks, check_last).and_return(())
  220. stdout = flexmock()
  221. insert_subprocess_mock(
  222. ('attic', 'check', 'repo'),
  223. stdout=stdout,
  224. )
  225. insert_platform_mock()
  226. insert_datetime_mock()
  227. builtins_mock().should_receive('open').and_return(stdout)
  228. flexmock(module.os).should_receive('devnull')
  229. module.check_archives(
  230. verbosity=None,
  231. repository='repo',
  232. consistency_config=consistency_config,
  233. command='attic',
  234. )
  235. def test_check_archives_with_verbosity_some_should_call_attic_with_verbose_parameter():
  236. consistency_config = flexmock().should_receive('get').and_return(None).mock
  237. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  238. flexmock(module).should_receive('_make_check_flags').and_return(())
  239. insert_subprocess_mock(
  240. ('attic', 'check', 'repo', '--verbose'),
  241. stdout=None,
  242. )
  243. insert_platform_mock()
  244. insert_datetime_mock()
  245. module.check_archives(
  246. verbosity=VERBOSITY_SOME,
  247. repository='repo',
  248. consistency_config=consistency_config,
  249. command='attic',
  250. )
  251. def test_check_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
  252. consistency_config = flexmock().should_receive('get').and_return(None).mock
  253. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  254. flexmock(module).should_receive('_make_check_flags').and_return(())
  255. insert_subprocess_mock(
  256. ('attic', 'check', 'repo', '--verbose'),
  257. stdout=None,
  258. )
  259. insert_platform_mock()
  260. insert_datetime_mock()
  261. module.check_archives(
  262. verbosity=VERBOSITY_LOTS,
  263. repository='repo',
  264. consistency_config=consistency_config,
  265. command='attic',
  266. )
  267. def test_check_archives_without_any_checks_should_bail():
  268. consistency_config = flexmock().should_receive('get').and_return(None).mock
  269. flexmock(module).should_receive('_parse_checks').and_return(())
  270. insert_subprocess_never()
  271. module.check_archives(
  272. verbosity=None,
  273. repository='repo',
  274. consistency_config=consistency_config,
  275. command='attic',
  276. )