test_dump.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_remove_database_dumps_removes_dump_for_each_database():
  18. databases = [{'name': 'foo'}, {'name': 'bar'}]
  19. flexmock(module).should_receive('make_database_dump_filename').and_return(
  20. 'databases/localhost/foo'
  21. ).and_return('databases/localhost/bar')
  22. flexmock(module.os).should_receive('listdir').and_return([])
  23. flexmock(module.os).should_receive('rmdir')
  24. for name in ('foo', 'bar'):
  25. flexmock(module.os).should_receive('remove').with_args(
  26. 'databases/localhost/{}'.format(name)
  27. ).once()
  28. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
  29. def test_remove_database_dumps_with_dry_run_skips_removal():
  30. databases = [{'name': 'foo'}, {'name': 'bar'}]
  31. flexmock(module.os).should_receive('rmdir').never()
  32. flexmock(module.os).should_receive('remove').never()
  33. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=True)
  34. def test_remove_database_dumps_without_databases_does_not_raise():
  35. module.remove_database_dumps('databases', [], 'SuperDB', 'test.yaml', dry_run=False)
  36. def test_convert_glob_patterns_to_borg_patterns_removes_leading_slash():
  37. assert module.convert_glob_patterns_to_borg_patterns(('/etc/foo/bar',)) == ['sh:etc/foo/bar']
  38. def test_get_database_names_from_dumps_gets_names_from_filenames_matching_globs():
  39. flexmock(module.glob).should_receive('glob').and_return(
  40. ('databases/localhost/foo',)
  41. ).and_return(('databases/localhost/bar',)).and_return(())
  42. assert module.get_database_names_from_dumps(
  43. ('databases/*/foo', 'databases/*/bar', 'databases/*/baz')
  44. ) == ['foo', 'bar']
  45. def test_get_database_configurations_only_produces_named_databases():
  46. databases = [
  47. {'name': 'foo', 'hostname': 'example.org'},
  48. {'name': 'bar', 'hostname': 'example.com'},
  49. {'name': 'baz', 'hostname': 'example.org'},
  50. ]
  51. assert list(module.get_database_configurations(databases, ('foo', 'baz'))) == [
  52. {'name': 'foo', 'hostname': 'example.org'},
  53. {'name': 'baz', 'hostname': 'example.org'},
  54. ]
  55. def test_get_database_configurations_matches_all_database():
  56. databases = [
  57. {'name': 'foo', 'hostname': 'example.org'},
  58. {'name': 'all', 'hostname': 'example.com'},
  59. ]
  60. assert list(module.get_database_configurations(databases, ('foo', 'bar', 'baz'))) == [
  61. {'name': 'foo', 'hostname': 'example.org'},
  62. {'name': 'bar', 'hostname': 'example.com'},
  63. {'name': 'baz', 'hostname': 'example.com'},
  64. ]
  65. def test_get_database_configurations_with_unknown_database_name_raises():
  66. databases = [{'name': 'foo', 'hostname': 'example.org'}]
  67. with pytest.raises(ValueError):
  68. list(module.get_database_configurations(databases, ('foo', 'bar')))
  69. def test_get_per_hook_database_configurations_partitions_by_hook():
  70. hooks = {'postgresql_databases': [flexmock()]}
  71. names = ('foo', 'bar')
  72. dump_patterns = flexmock()
  73. expected_config = {'postgresql_databases': [flexmock()]}
  74. flexmock(module).should_receive('get_database_configurations').with_args(
  75. hooks['postgresql_databases'], names
  76. ).and_return(expected_config['postgresql_databases'])
  77. config = module.get_per_hook_database_configurations(hooks, names, dump_patterns)
  78. assert config == expected_config
  79. def test_get_per_hook_database_configurations_defaults_to_detected_database_names():
  80. hooks = {'postgresql_databases': [flexmock()]}
  81. names = ()
  82. detected_names = flexmock()
  83. dump_patterns = {'postgresql_databases': [flexmock()]}
  84. expected_config = {'postgresql_databases': [flexmock()]}
  85. flexmock(module).should_receive('get_database_names_from_dumps').and_return(detected_names)
  86. flexmock(module).should_receive('get_database_configurations').with_args(
  87. hooks['postgresql_databases'], detected_names
  88. ).and_return(expected_config['postgresql_databases'])
  89. config = module.get_per_hook_database_configurations(hooks, names, dump_patterns)
  90. assert config == expected_config