test_dump.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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').and_return(
  34. 'databases/localhost/foo'
  35. ).and_return('databases/localhost/bar')
  36. flexmock(module.os).should_receive('listdir').and_return([])
  37. flexmock(module.os).should_receive('rmdir')
  38. for name in ('foo', 'bar'):
  39. flexmock(module.os).should_receive('remove').with_args(
  40. 'databases/localhost/{}'.format(name)
  41. ).once()
  42. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
  43. def test_remove_database_dumps_with_dry_run_skips_removal():
  44. databases = [{'name': 'foo'}, {'name': 'bar'}]
  45. flexmock(module.os).should_receive('rmdir').never()
  46. flexmock(module.os).should_receive('remove').never()
  47. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=True)
  48. def test_remove_database_dumps_without_databases_does_not_raise():
  49. module.remove_database_dumps('databases', [], 'SuperDB', 'test.yaml', dry_run=False)
  50. def test_convert_glob_patterns_to_borg_patterns_removes_leading_slash():
  51. assert module.convert_glob_patterns_to_borg_patterns(('/etc/foo/bar',)) == ['sh:etc/foo/bar']
  52. def test_get_database_names_from_dumps_gets_names_from_filenames_matching_globs():
  53. flexmock(module.glob).should_receive('glob').and_return(
  54. ('databases/localhost/foo',)
  55. ).and_return(('databases/localhost/bar',)).and_return(())
  56. assert module.get_database_names_from_dumps(
  57. ('databases/*/foo', 'databases/*/bar', 'databases/*/baz')
  58. ) == ['foo', 'bar']
  59. def test_get_database_configurations_only_produces_named_databases():
  60. databases = [
  61. {'name': 'foo', 'hostname': 'example.org'},
  62. {'name': 'bar', 'hostname': 'example.com'},
  63. {'name': 'baz', 'hostname': 'example.org'},
  64. ]
  65. assert list(module.get_database_configurations(databases, ('foo', 'baz'))) == [
  66. {'name': 'foo', 'hostname': 'example.org'},
  67. {'name': 'baz', 'hostname': 'example.org'},
  68. ]
  69. def test_get_database_configurations_matches_all_database():
  70. databases = [
  71. {'name': 'foo', 'hostname': 'example.org'},
  72. {'name': 'all', 'hostname': 'example.com'},
  73. ]
  74. assert list(module.get_database_configurations(databases, ('foo', 'bar', 'baz'))) == [
  75. {'name': 'foo', 'hostname': 'example.org'},
  76. {'name': 'bar', 'hostname': 'example.com'},
  77. {'name': 'baz', 'hostname': 'example.com'},
  78. ]
  79. def test_get_per_hook_database_configurations_partitions_by_hook():
  80. hooks = {'postgresql_databases': [flexmock()]}
  81. names = ('foo', 'bar')
  82. dump_patterns = flexmock()
  83. expected_config = {'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}]}
  84. flexmock(module).should_receive('get_database_configurations').with_args(
  85. hooks['postgresql_databases'], names
  86. ).and_return(expected_config['postgresql_databases'])
  87. config = module.get_per_hook_database_configurations(hooks, names, dump_patterns)
  88. assert config == expected_config
  89. def test_get_per_hook_database_configurations_defaults_to_detected_database_names():
  90. hooks = {'postgresql_databases': [flexmock()]}
  91. names = ()
  92. detected_names = flexmock()
  93. dump_patterns = {'postgresql_databases': [flexmock()]}
  94. expected_config = {'postgresql_databases': [flexmock()]}
  95. flexmock(module).should_receive('get_database_names_from_dumps').and_return(detected_names)
  96. flexmock(module).should_receive('get_database_configurations').with_args(
  97. hooks['postgresql_databases'], detected_names
  98. ).and_return(expected_config['postgresql_databases'])
  99. config = module.get_per_hook_database_configurations(hooks, names, dump_patterns)
  100. assert config == expected_config
  101. def test_get_per_hook_database_configurations_with_unknown_database_name_raises():
  102. hooks = {'postgresql_databases': [flexmock()]}
  103. names = ('foo', 'bar')
  104. dump_patterns = flexmock()
  105. flexmock(module).should_receive('get_database_configurations').with_args(
  106. hooks['postgresql_databases'], names
  107. ).and_return([])
  108. with pytest.raises(ValueError):
  109. module.get_per_hook_database_configurations(hooks, names, dump_patterns)
  110. def test_get_per_hook_database_configurations_with_all_and_no_archive_dumps_raises():
  111. hooks = {'postgresql_databases': [flexmock()]}
  112. names = ('foo', 'all')
  113. dump_patterns = flexmock()
  114. flexmock(module).should_receive('get_database_configurations').with_args(
  115. hooks['postgresql_databases'], names
  116. ).and_return([])
  117. with pytest.raises(ValueError):
  118. module.get_per_hook_database_configurations(hooks, names, dump_patterns)