test_sqlite.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.hooks import sqlite as module
  4. def test_use_streaming_true_for_any_databases():
  5. assert module.use_streaming(
  6. databases=[flexmock(), flexmock()], config=flexmock(), log_prefix=flexmock()
  7. )
  8. def test_use_streaming_false_for_no_databases():
  9. assert not module.use_streaming(databases=[], config=flexmock(), log_prefix=flexmock())
  10. def test_dump_data_sources_logs_and_skips_if_dump_already_exists():
  11. databases = [{'path': '/path/to/database', 'name': 'database'}]
  12. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  13. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  14. '/path/to/dump/database'
  15. )
  16. flexmock(module.os.path).should_receive('exists').and_return(True)
  17. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  18. flexmock(module).should_receive('execute_command').never()
  19. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == []
  20. def test_dump_data_sources_dumps_each_database():
  21. databases = [
  22. {'path': '/path/to/database1', 'name': 'database1'},
  23. {'path': '/path/to/database2', 'name': 'database2'},
  24. ]
  25. processes = [flexmock(), flexmock()]
  26. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  27. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  28. '/path/to/dump/database'
  29. )
  30. flexmock(module.os.path).should_receive('exists').and_return(False)
  31. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  32. flexmock(module).should_receive('execute_command').and_return(processes[0]).and_return(
  33. processes[1]
  34. )
  35. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
  36. def test_dump_data_sources_with_path_injection_attack_gets_escaped():
  37. databases = [
  38. {'path': '/path/to/database1; naughty-command', 'name': 'database1'},
  39. ]
  40. processes = [flexmock()]
  41. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  42. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  43. '/path/to/dump/database'
  44. )
  45. flexmock(module.os.path).should_receive('exists').and_return(False)
  46. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  47. flexmock(module).should_receive('execute_command').with_args(
  48. (
  49. 'sqlite3',
  50. "'/path/to/database1; naughty-command'",
  51. '.dump',
  52. '>',
  53. '/path/to/dump/database',
  54. ),
  55. shell=True,
  56. run_to_completion=False,
  57. ).and_return(processes[0])
  58. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
  59. def test_dump_data_sources_with_non_existent_path_warns_and_dumps_database():
  60. databases = [
  61. {'path': '/path/to/database1', 'name': 'database1'},
  62. ]
  63. processes = [flexmock()]
  64. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  65. flexmock(module.logger).should_receive('warning').once()
  66. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  67. '/path/to/dump/database'
  68. )
  69. flexmock(module.os.path).should_receive('exists').and_return(False)
  70. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  71. flexmock(module).should_receive('execute_command').and_return(processes[0])
  72. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
  73. def test_dump_data_sources_with_name_all_warns_and_dumps_all_databases():
  74. databases = [
  75. {'path': '/path/to/database1', 'name': 'all'},
  76. ]
  77. processes = [flexmock()]
  78. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  79. flexmock(module.logger).should_receive(
  80. 'warning'
  81. ).twice() # once for the name=all, once for the non-existent path
  82. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  83. '/path/to/dump/database'
  84. )
  85. flexmock(module.os.path).should_receive('exists').and_return(False)
  86. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  87. flexmock(module).should_receive('execute_command').and_return(processes[0])
  88. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
  89. def test_dump_data_sources_does_not_dump_if_dry_run():
  90. databases = [{'path': '/path/to/database', 'name': 'database'}]
  91. flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
  92. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  93. '/path/to/dump/database'
  94. )
  95. flexmock(module.os.path).should_receive('exists').and_return(False)
  96. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  97. flexmock(module).should_receive('execute_command').never()
  98. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=True) == []
  99. def test_restore_data_source_dump_restores_database():
  100. hook_config = [{'path': '/path/to/database', 'name': 'database'}, {'name': 'other'}]
  101. extract_process = flexmock(stdout=flexmock())
  102. flexmock(module).should_receive('execute_command_with_processes').with_args(
  103. (
  104. 'sqlite3',
  105. '/path/to/database',
  106. ),
  107. processes=[extract_process],
  108. output_log_level=logging.DEBUG,
  109. input_file=extract_process.stdout,
  110. ).once()
  111. flexmock(module.os).should_receive('remove').once()
  112. module.restore_data_source_dump(
  113. hook_config,
  114. {},
  115. 'test.yaml',
  116. data_source=hook_config[0],
  117. dry_run=False,
  118. extract_process=extract_process,
  119. connection_params={'restore_path': None},
  120. )
  121. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  122. hook_config = [
  123. {'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'}
  124. ]
  125. extract_process = flexmock(stdout=flexmock())
  126. flexmock(module).should_receive('execute_command_with_processes').with_args(
  127. (
  128. 'sqlite3',
  129. 'cli/path/to/database',
  130. ),
  131. processes=[extract_process],
  132. output_log_level=logging.DEBUG,
  133. input_file=extract_process.stdout,
  134. ).once()
  135. flexmock(module.os).should_receive('remove').once()
  136. module.restore_data_source_dump(
  137. hook_config,
  138. {},
  139. 'test.yaml',
  140. data_source={'name': 'database'},
  141. dry_run=False,
  142. extract_process=extract_process,
  143. connection_params={'restore_path': 'cli/path/to/database'},
  144. )
  145. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  146. hook_config = [
  147. {'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'}
  148. ]
  149. extract_process = flexmock(stdout=flexmock())
  150. flexmock(module).should_receive('execute_command_with_processes').with_args(
  151. (
  152. 'sqlite3',
  153. 'config/path/to/database',
  154. ),
  155. processes=[extract_process],
  156. output_log_level=logging.DEBUG,
  157. input_file=extract_process.stdout,
  158. ).once()
  159. flexmock(module.os).should_receive('remove').once()
  160. module.restore_data_source_dump(
  161. hook_config,
  162. {},
  163. 'test.yaml',
  164. data_source=hook_config[0],
  165. dry_run=False,
  166. extract_process=extract_process,
  167. connection_params={'restore_path': None},
  168. )
  169. def test_restore_data_source_dump_does_not_restore_database_if_dry_run():
  170. hook_config = [{'path': '/path/to/database', 'name': 'database'}]
  171. extract_process = flexmock(stdout=flexmock())
  172. flexmock(module).should_receive('execute_command_with_processes').never()
  173. flexmock(module.os).should_receive('remove').never()
  174. module.restore_data_source_dump(
  175. hook_config,
  176. {},
  177. 'test.yaml',
  178. data_source={'name': 'database'},
  179. dry_run=True,
  180. extract_process=extract_process,
  181. connection_params={'restore_path': None},
  182. )