test_dump.py 6.3 KB

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