test_dump.py 6.9 KB

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