test_sqlite.py 7.0 KB

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