test_dump.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.hooks import dump as module
  4. def test_make_database_dump_filename_uses_name_and_hostname():
  5. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  6. assert (
  7. module.make_database_dump_filename('databases', 'test', 'hostname')
  8. == 'databases/hostname/test'
  9. )
  10. def test_make_database_dump_filename_without_hostname_defaults_to_localhost():
  11. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  12. assert module.make_database_dump_filename('databases', 'test') == 'databases/localhost/test'
  13. def test_make_database_dump_filename_with_invalid_name_raises():
  14. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  15. with pytest.raises(ValueError):
  16. module.make_database_dump_filename('databases', 'invalid/name')
  17. def test_flatten_dump_patterns_produces_list_of_all_patterns():
  18. dump_patterns = {'postgresql_databases': ['*/glob', 'glob/*'], 'mysql_databases': ['*/*/*']}
  19. expected_patterns = sorted(
  20. dump_patterns['postgresql_databases'] + dump_patterns['mysql_databases']
  21. )
  22. assert sorted(module.flatten_dump_patterns(dump_patterns, ('bob',))) == expected_patterns
  23. def test_flatten_dump_patterns_with_no_patterns_errors():
  24. dump_patterns = {'postgresql_databases': [], 'mysql_databases': []}
  25. with pytest.raises(ValueError):
  26. assert module.flatten_dump_patterns(dump_patterns, ('bob',))
  27. def test_flatten_dump_patterns_with_no_hooks_errors():
  28. dump_patterns = {}
  29. with pytest.raises(ValueError):
  30. assert module.flatten_dump_patterns(dump_patterns, ('bob',))
  31. def test_remove_database_dumps_removes_dump_for_each_database():
  32. databases = [{'name': 'foo'}, {'name': 'bar'}]
  33. flexmock(module).should_receive('make_database_dump_filename').with_args(
  34. 'databases', 'foo', None
  35. ).and_return('databases/localhost/foo')
  36. flexmock(module).should_receive('make_database_dump_filename').with_args(
  37. 'databases', 'bar', None
  38. ).and_return('databases/localhost/bar')
  39. flexmock(module.os).should_receive('remove').with_args('databases/localhost/foo').once()
  40. flexmock(module.os).should_receive('remove').with_args('databases/localhost/bar').once()
  41. flexmock(module.os).should_receive('listdir').with_args('databases/localhost').and_return(
  42. ['bar']
  43. ).and_return([])
  44. flexmock(module.os).should_receive('rmdir').with_args('databases/localhost').once()
  45. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
  46. def test_remove_database_dumps_with_dry_run_skips_removal():
  47. databases = [{'name': 'foo'}, {'name': 'bar'}]
  48. flexmock(module.os).should_receive('rmdir').never()
  49. flexmock(module.os).should_receive('remove').never()
  50. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=True)
  51. def test_remove_database_dumps_without_databases_does_not_raise():
  52. module.remove_database_dumps('databases', [], 'SuperDB', 'test.yaml', dry_run=False)
  53. def test_convert_glob_patterns_to_borg_patterns_removes_leading_slash():
  54. assert module.convert_glob_patterns_to_borg_patterns(('/etc/foo/bar',)) == ['sh:etc/foo/bar']
  55. def test_get_database_names_from_dumps_gets_names_from_filenames_matching_globs():
  56. flexmock(module.glob).should_receive('glob').and_return(
  57. ('databases/localhost/foo',)
  58. ).and_return(('databases/localhost/bar',)).and_return(())
  59. assert module.get_database_names_from_dumps(
  60. ('databases/*/foo', 'databases/*/bar', 'databases/*/baz')
  61. ) == ['foo', 'bar']
  62. def test_get_database_configurations_only_produces_named_databases():
  63. databases = [
  64. {'name': 'foo', 'hostname': 'example.org'},
  65. {'name': 'bar', 'hostname': 'example.com'},
  66. {'name': 'baz', 'hostname': 'example.org'},
  67. ]
  68. assert list(module.get_database_configurations(databases, ('foo', 'baz'))) == [
  69. {'name': 'foo', 'hostname': 'example.org'},
  70. {'name': 'baz', 'hostname': 'example.org'},
  71. ]
  72. def test_get_database_configurations_matches_all_database():
  73. databases = [
  74. {'name': 'foo', 'hostname': 'example.org'},
  75. {'name': 'all', 'hostname': 'example.com'},
  76. ]
  77. assert list(module.get_database_configurations(databases, ('foo', 'bar', 'baz'))) == [
  78. {'name': 'foo', 'hostname': 'example.org'},
  79. {'name': 'bar', 'hostname': 'example.com'},
  80. {'name': 'baz', 'hostname': 'example.com'},
  81. ]
  82. def test_get_per_hook_database_configurations_partitions_by_hook():
  83. hooks = {'postgresql_databases': [flexmock()]}
  84. names = ('foo', 'bar')
  85. dump_patterns = flexmock()
  86. expected_config = {'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}]}
  87. flexmock(module).should_receive('get_database_configurations').with_args(
  88. hooks['postgresql_databases'], names
  89. ).and_return(expected_config['postgresql_databases'])
  90. config = module.get_per_hook_database_configurations(hooks, names, dump_patterns)
  91. assert config == expected_config
  92. def test_get_per_hook_database_configurations_defaults_to_detected_database_names():
  93. hooks = {'postgresql_databases': [flexmock()]}
  94. names = ()
  95. detected_names = flexmock()
  96. dump_patterns = {'postgresql_databases': [flexmock()]}
  97. expected_config = {'postgresql_databases': [flexmock()]}
  98. flexmock(module).should_receive('get_database_names_from_dumps').and_return(detected_names)
  99. flexmock(module).should_receive('get_database_configurations').with_args(
  100. hooks['postgresql_databases'], detected_names
  101. ).and_return(expected_config['postgresql_databases'])
  102. config = module.get_per_hook_database_configurations(hooks, names, dump_patterns)
  103. assert config == expected_config
  104. def test_get_per_hook_database_configurations_with_unknown_database_name_raises():
  105. hooks = {'postgresql_databases': [flexmock()]}
  106. names = ('foo', 'bar')
  107. dump_patterns = flexmock()
  108. flexmock(module).should_receive('get_database_configurations').with_args(
  109. hooks['postgresql_databases'], names
  110. ).and_return([])
  111. with pytest.raises(ValueError):
  112. module.get_per_hook_database_configurations(hooks, names, dump_patterns)
  113. def test_get_per_hook_database_configurations_with_all_and_no_archive_dumps_raises():
  114. hooks = {'postgresql_databases': [flexmock()]}
  115. names = ('foo', 'all')
  116. dump_patterns = flexmock()
  117. flexmock(module).should_receive('get_database_configurations').with_args(
  118. hooks['postgresql_databases'], names
  119. ).and_return([])
  120. with pytest.raises(ValueError):
  121. module.get_per_hook_database_configurations(hooks, names, dump_patterns)