test_sqlite.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks import sqlite as module
  5. def test_dump_databases_logs_and_skips_if_dump_already_exists():
  6. databases = [{'path': '/path/to/database', 'name': 'database'}]
  7. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  8. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  9. '/path/to/dump/database'
  10. )
  11. flexmock(module.os.path).should_receive('exists').and_return(True)
  12. flexmock(module.dump).should_receive('create_parent_directory_for_dump').never()
  13. flexmock(module).should_receive('execute_command').never()
  14. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == []
  15. def test_dump_databases_dumps_each_database():
  16. databases = [
  17. {'path': '/path/to/database1', 'name': 'database1'},
  18. {'path': '/path/to/database2', 'name': 'database2'},
  19. ]
  20. processes = [flexmock(), flexmock()]
  21. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  22. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  23. '/path/to/dump/database'
  24. )
  25. flexmock(module.os.path).should_receive('exists').and_return(False)
  26. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  27. flexmock(module).should_receive('execute_command').and_return(processes[0]).and_return(
  28. processes[1]
  29. )
  30. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  31. def test_dumping_database_with_non_existent_path_warns_and_dumps_database():
  32. databases = [
  33. {'path': '/path/to/database1', 'name': 'database1'},
  34. ]
  35. processes = [flexmock()]
  36. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  37. flexmock(module.logger).should_receive('warning').once()
  38. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  39. '/path/to/dump/database'
  40. )
  41. flexmock(module.os.path).should_receive('exists').and_return(False)
  42. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  43. flexmock(module).should_receive('execute_command').and_return(processes[0])
  44. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  45. def test_dumping_database_with_name_all_warns_and_dumps_all_databases():
  46. databases = [
  47. {'path': '/path/to/database1', 'name': 'all'},
  48. ]
  49. processes = [flexmock()]
  50. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  51. flexmock(module.logger).should_receive(
  52. 'warning'
  53. ).twice() # once for the name=all, once for the non-existent path
  54. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  55. '/path/to/dump/database'
  56. )
  57. flexmock(module.os.path).should_receive('exists').and_return(False)
  58. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  59. flexmock(module).should_receive('execute_command').and_return(processes[0])
  60. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  61. def test_dump_databases_does_not_dump_if_dry_run():
  62. databases = [{'path': '/path/to/database', 'name': 'database'}]
  63. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  64. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  65. '/path/to/dump/database'
  66. )
  67. flexmock(module.os.path).should_receive('exists').and_return(False)
  68. flexmock(module.dump).should_receive('create_parent_directory_for_dump').never()
  69. flexmock(module).should_receive('execute_command').never()
  70. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
  71. def test_restore_database_dump_restores_database():
  72. database_config = [{'path': '/path/to/database', 'name': 'database'}]
  73. extract_process = flexmock(stdout=flexmock())
  74. flexmock(module).should_receive('execute_command_with_processes').with_args(
  75. (
  76. 'sqlite3',
  77. '/path/to/database',
  78. ),
  79. processes=[extract_process],
  80. output_log_level=logging.DEBUG,
  81. input_file=extract_process.stdout,
  82. ).once()
  83. flexmock(module.os).should_receive('remove').once()
  84. module.restore_database_dump(
  85. database_config,
  86. 'test.yaml',
  87. {},
  88. dry_run=False,
  89. extract_process=extract_process,
  90. connection_params={'restore_path': None},
  91. )
  92. def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore():
  93. database_config = [
  94. {'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'}
  95. ]
  96. extract_process = flexmock(stdout=flexmock())
  97. flexmock(module).should_receive('execute_command_with_processes').with_args(
  98. (
  99. 'sqlite3',
  100. 'cli/path/to/database',
  101. ),
  102. processes=[extract_process],
  103. output_log_level=logging.DEBUG,
  104. input_file=extract_process.stdout,
  105. ).once()
  106. flexmock(module.os).should_receive('remove').once()
  107. module.restore_database_dump(
  108. database_config,
  109. 'test.yaml',
  110. {},
  111. dry_run=False,
  112. extract_process=extract_process,
  113. connection_params={'restore_path': 'cli/path/to/database'},
  114. )
  115. def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  116. database_config = [
  117. {'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'}
  118. ]
  119. extract_process = flexmock(stdout=flexmock())
  120. flexmock(module).should_receive('execute_command_with_processes').with_args(
  121. (
  122. 'sqlite3',
  123. 'config/path/to/database',
  124. ),
  125. processes=[extract_process],
  126. output_log_level=logging.DEBUG,
  127. input_file=extract_process.stdout,
  128. ).once()
  129. flexmock(module.os).should_receive('remove').once()
  130. module.restore_database_dump(
  131. database_config,
  132. 'test.yaml',
  133. {},
  134. dry_run=False,
  135. extract_process=extract_process,
  136. connection_params={'restore_path': None},
  137. )
  138. def test_restore_database_dump_does_not_restore_database_if_dry_run():
  139. database_config = [{'path': '/path/to/database', 'name': 'database'}]
  140. extract_process = flexmock(stdout=flexmock())
  141. flexmock(module).should_receive('execute_command_with_processes').never()
  142. flexmock(module.os).should_receive('remove').never()
  143. module.restore_database_dump(
  144. database_config,
  145. 'test.yaml',
  146. {},
  147. dry_run=True,
  148. extract_process=extract_process,
  149. connection_params={'restore_path': None},
  150. )
  151. def test_restore_database_dump_raises_error_if_database_config_is_invalid():
  152. database_config = []
  153. extract_process = flexmock(stdout=flexmock())
  154. with pytest.raises(ValueError):
  155. module.restore_database_dump(
  156. database_config,
  157. 'test.yaml',
  158. {},
  159. dry_run=False,
  160. extract_process=extract_process,
  161. connection_params={'restore_path': None},
  162. )