test_attic.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_platform_mock():
  9. flexmock(module).platform = flexmock().should_receive('node').and_return('host').mock
  10. def insert_datetime_mock():
  11. flexmock(module).datetime = flexmock().should_receive('now').and_return(
  12. flexmock().should_receive('isoformat').and_return('now').mock
  13. ).mock
  14. def test_create_archive_should_call_attic_with_parameters():
  15. insert_subprocess_mock(
  16. ('attic', 'create', '--exclude-from', 'excludes', 'repo::host-now', 'foo', 'bar'),
  17. )
  18. insert_platform_mock()
  19. insert_datetime_mock()
  20. module.create_archive(
  21. excludes_filename='excludes',
  22. verbose=False,
  23. source_directories='foo bar',
  24. repository='repo',
  25. )
  26. def test_create_archive_with_verbose_should_call_attic_with_verbose_parameters():
  27. insert_subprocess_mock(
  28. (
  29. 'attic', 'create', '--exclude-from', 'excludes', 'repo::host-now', 'foo', 'bar',
  30. '--verbose', '--stats',
  31. ),
  32. )
  33. insert_platform_mock()
  34. insert_datetime_mock()
  35. module.create_archive(
  36. excludes_filename='excludes',
  37. verbose=True,
  38. source_directories='foo bar',
  39. repository='repo',
  40. )
  41. BASE_PRUNE_FLAGS = (
  42. ('--keep-daily', '1'),
  43. ('--keep-weekly', '2'),
  44. ('--keep-monthly', '3'),
  45. )
  46. def test_make_prune_flags_should_return_flags_from_config():
  47. retention_config = OrderedDict(
  48. (
  49. ('keep_daily', 1),
  50. ('keep_weekly', 2),
  51. ('keep_monthly', 3),
  52. )
  53. )
  54. result = module.make_prune_flags(retention_config)
  55. assert tuple(result) == BASE_PRUNE_FLAGS
  56. def test_prune_archives_should_call_attic_with_parameters():
  57. retention_config = flexmock()
  58. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  59. BASE_PRUNE_FLAGS,
  60. )
  61. insert_subprocess_mock(
  62. (
  63. 'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly',
  64. '3',
  65. ),
  66. )
  67. module.prune_archives(
  68. verbose=False,
  69. repository='repo',
  70. retention_config=retention_config,
  71. )
  72. def test_prune_archives_with_verbose_should_call_attic_with_verbose_parameters():
  73. retention_config = flexmock()
  74. flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return(
  75. BASE_PRUNE_FLAGS,
  76. )
  77. insert_subprocess_mock(
  78. (
  79. 'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly',
  80. '3', '--verbose',
  81. ),
  82. )
  83. module.prune_archives(
  84. repository='repo',
  85. verbose=True,
  86. retention_config=retention_config,
  87. )
  88. def test_check_archives_should_call_attic_with_parameters():
  89. stdout = flexmock()
  90. insert_subprocess_mock(
  91. ('attic', 'check', 'repo'),
  92. stdout=stdout,
  93. )
  94. insert_platform_mock()
  95. insert_datetime_mock()
  96. flexmock(module).open = lambda filename, mode: stdout
  97. flexmock(module).os = flexmock().should_receive('devnull').mock
  98. module.check_archives(
  99. verbose=False,
  100. repository='repo',
  101. )
  102. def test_check_archives_with_verbose_should_call_attic_with_verbose_parameters():
  103. insert_subprocess_mock(
  104. ('attic', 'check', 'repo', '--verbose'),
  105. stdout=None,
  106. )
  107. insert_platform_mock()
  108. insert_datetime_mock()
  109. module.check_archives(
  110. verbose=True,
  111. repository='repo',
  112. )