test_dump.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.path).should_receive('isdir').and_return(False)
  44. flexmock(module.os).should_receive('remove').with_args('databases/localhost/foo').once()
  45. flexmock(module.os).should_receive('remove').with_args('databases/localhost/bar').once()
  46. flexmock(module.os).should_receive('listdir').with_args('databases/localhost').and_return(
  47. ['bar']
  48. ).and_return([])
  49. flexmock(module.os).should_receive('rmdir').with_args('databases/localhost').once()
  50. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
  51. def test_remove_database_dumps_removes_dump_in_directory_format():
  52. databases = [{'name': 'foo'}]
  53. flexmock(module).should_receive('make_database_dump_filename').with_args(
  54. 'databases', 'foo', None
  55. ).and_return('databases/localhost/foo')
  56. flexmock(module.os.path).should_receive('isdir').and_return(True)
  57. flexmock(module.os).should_receive('remove').never()
  58. flexmock(module.shutil).should_receive('rmtree').with_args('databases/localhost/foo').once()
  59. flexmock(module.os).should_receive('listdir').with_args('databases/localhost').and_return([])
  60. flexmock(module.os).should_receive('rmdir').with_args('databases/localhost').once()
  61. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
  62. def test_remove_database_dumps_with_dry_run_skips_removal():
  63. databases = [{'name': 'foo'}, {'name': 'bar'}]
  64. flexmock(module.os).should_receive('rmdir').never()
  65. flexmock(module.os).should_receive('remove').never()
  66. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=True)
  67. def test_remove_database_dumps_without_databases_does_not_raise():
  68. module.remove_database_dumps('databases', [], 'SuperDB', 'test.yaml', dry_run=False)
  69. def test_convert_glob_patterns_to_borg_patterns_removes_leading_slash():
  70. assert module.convert_glob_patterns_to_borg_patterns(('/etc/foo/bar',)) == ['sh:etc/foo/bar']
  71. def test_get_database_names_from_dumps_gets_names_from_filenames_matching_globs():
  72. flexmock(module.glob).should_receive('glob').and_return(
  73. ('databases/localhost/foo',)
  74. ).and_return(('databases/localhost/bar',)).and_return(())
  75. assert module.get_database_names_from_dumps(
  76. ('databases/*/foo', 'databases/*/bar', 'databases/*/baz')
  77. ) == ['foo', 'bar']
  78. def test_get_database_configurations_only_produces_named_databases():
  79. databases = [
  80. {'name': 'foo', 'hostname': 'example.org'},
  81. {'name': 'bar', 'hostname': 'example.com'},
  82. {'name': 'baz', 'hostname': 'example.org'},
  83. ]
  84. assert list(module.get_database_configurations(databases, ('foo', 'baz'))) == [
  85. {'name': 'foo', 'hostname': 'example.org'},
  86. {'name': 'baz', 'hostname': 'example.org'},
  87. ]
  88. def test_get_database_configurations_matches_all_database():
  89. databases = [
  90. {'name': 'foo', 'hostname': 'example.org'},
  91. {'name': 'all', 'hostname': 'example.com'},
  92. ]
  93. assert list(module.get_database_configurations(databases, ('foo', 'bar', 'baz'))) == [
  94. {'name': 'foo', 'hostname': 'example.org'},
  95. {'name': 'bar', 'hostname': 'example.com'},
  96. {'name': 'baz', 'hostname': 'example.com'},
  97. ]
  98. def test_get_per_hook_database_configurations_partitions_by_hook():
  99. hooks = {'postgresql_databases': [flexmock()]}
  100. names = ('foo', 'bar')
  101. dump_patterns = flexmock()
  102. expected_config = {'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}]}
  103. flexmock(module).should_receive('get_database_configurations').with_args(
  104. hooks['postgresql_databases'], 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_defaults_to_detected_database_names():
  109. hooks = {'postgresql_databases': [flexmock()]}
  110. names = ()
  111. detected_names = flexmock()
  112. dump_patterns = {'postgresql_databases': [flexmock()]}
  113. expected_config = {'postgresql_databases': [flexmock()]}
  114. flexmock(module).should_receive('get_database_names_from_dumps').and_return(detected_names)
  115. flexmock(module).should_receive('get_database_configurations').with_args(
  116. hooks['postgresql_databases'], detected_names
  117. ).and_return(expected_config['postgresql_databases'])
  118. config = module.get_per_hook_database_configurations(hooks, names, dump_patterns)
  119. assert config == expected_config
  120. def test_get_per_hook_database_configurations_with_unknown_database_name_raises():
  121. hooks = {'postgresql_databases': [flexmock()]}
  122. names = ('foo', 'bar')
  123. dump_patterns = flexmock()
  124. flexmock(module).should_receive('get_database_configurations').with_args(
  125. hooks['postgresql_databases'], names
  126. ).and_return([])
  127. with pytest.raises(ValueError):
  128. module.get_per_hook_database_configurations(hooks, names, dump_patterns)
  129. def test_get_per_hook_database_configurations_with_all_and_no_archive_dumps_raises():
  130. hooks = {'postgresql_databases': [flexmock()]}
  131. names = ('foo', 'all')
  132. dump_patterns = flexmock()
  133. flexmock(module).should_receive('get_database_configurations').with_args(
  134. hooks['postgresql_databases'], names
  135. ).and_return([])
  136. with pytest.raises(ValueError):
  137. module.get_per_hook_database_configurations(hooks, names, dump_patterns)