test_attic.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. from collections import OrderedDict
  2. from flexmock import flexmock
  3. from atticmatic import attic as module
  4. def insert_subprocess_mock(check_call_command, **kwargs):
  5. subprocess = flexmock()
  6. subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
  7. flexmock(module).subprocess = subprocess
  8. def insert_subprocess_never():
  9. subprocess = flexmock()
  10. subprocess.should_receive('check_call').never()
  11. flexmock(module).subprocess = subprocess
  12. def insert_platform_mock():
  13. flexmock(module).platform = flexmock().should_receive('node').and_return('host').mock
  14. def insert_datetime_mock():
  15. flexmock(module).datetime = flexmock().should_receive('now').and_return(
  16. flexmock().should_receive('isoformat').and_return('now').mock
  17. ).mock
  18. def test_create_archive_should_call_attic_with_parameters():
  19. insert_subprocess_mock(
  20. ('attic', 'create', '--exclude-from', 'excludes', 'repo::host-now', 'foo', 'bar'),
  21. )
  22. insert_platform_mock()
  23. insert_datetime_mock()
  24. module.create_archive(
  25. excludes_filename='excludes',
  26. verbose=False,
  27. source_directories='foo bar',
  28. repository='repo',
  29. )
  30. def test_create_archive_with_verbose_should_call_attic_with_verbose_parameters():
  31. insert_subprocess_mock(
  32. (
  33. 'attic', 'create', '--exclude-from', 'excludes', 'repo::host-now', 'foo', 'bar',
  34. '--verbose', '--stats',
  35. ),
  36. )
  37. insert_platform_mock()
  38. insert_datetime_mock()
  39. module.create_archive(
  40. excludes_filename='excludes',
  41. verbose=True,
  42. source_directories='foo bar',
  43. repository='repo',
  44. )
  45. BASE_PRUNE_FLAGS = (
  46. ('--keep-daily', '1'),
  47. ('--keep-weekly', '2'),
  48. ('--keep-monthly', '3'),
  49. )
  50. def test_make_prune_flags_should_return_flags_from_config():
  51. retention_config = OrderedDict(
  52. (
  53. ('keep_daily', 1),
  54. ('keep_weekly', 2),
  55. ('keep_monthly', 3),
  56. )
  57. )
  58. result = module._make_prune_flags(retention_config)
  59. assert tuple(result) == BASE_PRUNE_FLAGS
  60. def test_prune_archives_should_call_attic_with_parameters():
  61. retention_config = flexmock()
  62. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  63. BASE_PRUNE_FLAGS,
  64. )
  65. insert_subprocess_mock(
  66. (
  67. 'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly',
  68. '3',
  69. ),
  70. )
  71. module.prune_archives(
  72. verbose=False,
  73. repository='repo',
  74. retention_config=retention_config,
  75. )
  76. def test_prune_archives_with_verbose_should_call_attic_with_verbose_parameters():
  77. retention_config = flexmock()
  78. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  79. BASE_PRUNE_FLAGS,
  80. )
  81. insert_subprocess_mock(
  82. (
  83. 'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly',
  84. '3', '--verbose',
  85. ),
  86. )
  87. module.prune_archives(
  88. repository='repo',
  89. verbose=True,
  90. retention_config=retention_config,
  91. )
  92. def test_parse_checks_returns_them_as_tuple():
  93. checks = module._parse_checks({'checks': 'foo disabled bar'})
  94. assert checks == ('foo', 'bar')
  95. def test_parse_checks_with_missing_value_returns_defaults():
  96. checks = module._parse_checks({})
  97. assert checks == module.DEFAULT_CHECKS
  98. def test_parse_checks_with_blank_value_returns_defaults():
  99. checks = module._parse_checks({'checks': ''})
  100. assert checks == module.DEFAULT_CHECKS
  101. def test_parse_checks_with_disabled_returns_no_checks():
  102. checks = module._parse_checks({'checks': 'disabled'})
  103. assert checks == ()
  104. def test_make_check_flags_with_checks_returns_flags():
  105. flags = module._make_check_flags(('foo', 'bar'))
  106. assert flags == ('--foo-only', '--bar-only')
  107. def test_make_check_flags_with_default_checks_returns_no_flags():
  108. flags = module._make_check_flags(module.DEFAULT_CHECKS)
  109. assert flags == ()
  110. def test_check_archives_should_call_attic_with_parameters():
  111. consistency_config = flexmock()
  112. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  113. flexmock(module).should_receive('_make_check_flags').and_return(())
  114. stdout = flexmock()
  115. insert_subprocess_mock(
  116. ('attic', 'check', 'repo'),
  117. stdout=stdout,
  118. )
  119. insert_platform_mock()
  120. insert_datetime_mock()
  121. flexmock(module).open = lambda filename, mode: stdout
  122. flexmock(module).os = flexmock().should_receive('devnull').mock
  123. module.check_archives(
  124. verbose=False,
  125. repository='repo',
  126. consistency_config=consistency_config,
  127. )
  128. def test_check_archives_with_verbose_should_call_attic_with_verbose_parameters():
  129. consistency_config = flexmock()
  130. flexmock(module).should_receive('_parse_checks').and_return(flexmock())
  131. flexmock(module).should_receive('_make_check_flags').and_return(())
  132. insert_subprocess_mock(
  133. ('attic', 'check', 'repo', '--verbose'),
  134. stdout=None,
  135. )
  136. insert_platform_mock()
  137. insert_datetime_mock()
  138. module.check_archives(
  139. verbose=True,
  140. repository='repo',
  141. consistency_config=consistency_config,
  142. )
  143. def test_check_archives_without_any_checks_should_bail():
  144. consistency_config = flexmock()
  145. flexmock(module).should_receive('_parse_checks').and_return(())
  146. insert_subprocess_never()
  147. module.check_archives(
  148. verbose=False,
  149. repository='repo',
  150. consistency_config=consistency_config,
  151. )