test_sqlite.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. databases_config = [{'path': '/path/to/database', 'name': 'database'}, {'name': 'other'}]
  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. databases_config,
  86. {},
  87. 'test.yaml',
  88. database_name='database',
  89. dry_run=False,
  90. extract_process=extract_process,
  91. connection_params={'restore_path': None},
  92. )
  93. def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore():
  94. databases_config = [
  95. {'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'}
  96. ]
  97. extract_process = flexmock(stdout=flexmock())
  98. flexmock(module).should_receive('execute_command_with_processes').with_args(
  99. (
  100. 'sqlite3',
  101. 'cli/path/to/database',
  102. ),
  103. processes=[extract_process],
  104. output_log_level=logging.DEBUG,
  105. input_file=extract_process.stdout,
  106. ).once()
  107. flexmock(module.os).should_receive('remove').once()
  108. module.restore_database_dump(
  109. databases_config,
  110. {},
  111. 'test.yaml',
  112. database_name='database',
  113. dry_run=False,
  114. extract_process=extract_process,
  115. connection_params={'restore_path': 'cli/path/to/database'},
  116. )
  117. def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  118. databases_config = [
  119. {'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'}
  120. ]
  121. extract_process = flexmock(stdout=flexmock())
  122. flexmock(module).should_receive('execute_command_with_processes').with_args(
  123. (
  124. 'sqlite3',
  125. 'config/path/to/database',
  126. ),
  127. processes=[extract_process],
  128. output_log_level=logging.DEBUG,
  129. input_file=extract_process.stdout,
  130. ).once()
  131. flexmock(module.os).should_receive('remove').once()
  132. module.restore_database_dump(
  133. databases_config,
  134. {},
  135. 'test.yaml',
  136. database_name='database',
  137. dry_run=False,
  138. extract_process=extract_process,
  139. connection_params={'restore_path': None},
  140. )
  141. def test_restore_database_dump_does_not_restore_database_if_dry_run():
  142. databases_config = [{'path': '/path/to/database', 'name': 'database'}]
  143. extract_process = flexmock(stdout=flexmock())
  144. flexmock(module).should_receive('execute_command_with_processes').never()
  145. flexmock(module.os).should_receive('remove').never()
  146. module.restore_database_dump(
  147. databases_config,
  148. {},
  149. 'test.yaml',
  150. database_name='database',
  151. dry_run=True,
  152. extract_process=extract_process,
  153. connection_params={'restore_path': None},
  154. )
  155. def test_restore_database_dump_raises_error_if_database_config_is_empty():
  156. databases_config = []
  157. extract_process = flexmock(stdout=flexmock())
  158. with pytest.raises(ValueError):
  159. module.restore_database_dump(
  160. databases_config,
  161. {},
  162. 'test.yaml',
  163. database_name='database',
  164. dry_run=False,
  165. extract_process=extract_process,
  166. connection_params={'restore_path': None},
  167. )