test_mongodb.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks import mongodb as module
  5. def test_dump_databases_runs_mongodump_for_each_database():
  6. databases = [{'name': 'foo'}, {'name': 'bar'}]
  7. processes = [flexmock(), flexmock()]
  8. flexmock(module).should_receive('make_dump_path').and_return('')
  9. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  10. 'databases/localhost/foo'
  11. ).and_return('databases/localhost/bar')
  12. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  13. for name, process in zip(('foo', 'bar'), processes):
  14. flexmock(module).should_receive('execute_command').with_args(
  15. ['mongodump', '--archive', '--db', name, '>', 'databases/localhost/{}'.format(name)],
  16. shell=True,
  17. run_to_completion=False,
  18. ).and_return(process).once()
  19. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  20. def test_dump_databases_with_dry_run_skips_mongodump():
  21. databases = [{'name': 'foo'}, {'name': 'bar'}]
  22. flexmock(module).should_receive('make_dump_path').and_return('')
  23. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  24. 'databases/localhost/foo'
  25. ).and_return('databases/localhost/bar')
  26. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  27. flexmock(module).should_receive('execute_command').never()
  28. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
  29. def test_dump_databases_runs_mongodump_with_hostname_and_port():
  30. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  31. process = flexmock()
  32. flexmock(module).should_receive('make_dump_path').and_return('')
  33. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  34. 'databases/database.example.org/foo'
  35. )
  36. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  37. flexmock(module).should_receive('execute_command').with_args(
  38. [
  39. 'mongodump',
  40. '--archive',
  41. '--host',
  42. 'database.example.org',
  43. '--port',
  44. '5433',
  45. '--db',
  46. 'foo',
  47. '>',
  48. 'databases/database.example.org/foo',
  49. ],
  50. shell=True,
  51. run_to_completion=False,
  52. ).and_return(process).once()
  53. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  54. def test_dump_databases_runs_mongodump_with_username_and_password():
  55. databases = [{'name': 'foo', 'username': 'mongo', 'password': 'trustsome1'}]
  56. process = flexmock()
  57. flexmock(module).should_receive('make_dump_path').and_return('')
  58. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  59. 'databases/localhost/foo'
  60. )
  61. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  62. flexmock(module).should_receive('execute_command').with_args(
  63. [
  64. 'mongodump',
  65. '--archive',
  66. '--username',
  67. 'mongo',
  68. '--password',
  69. 'trustsome1',
  70. '--db',
  71. 'foo',
  72. '>',
  73. 'databases/localhost/foo',
  74. ],
  75. shell=True,
  76. run_to_completion=False,
  77. ).and_return(process).once()
  78. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  79. def test_dump_databases_runs_mongodump_with_directory_format():
  80. databases = [{'name': 'foo', 'format': 'directory'}]
  81. process = flexmock()
  82. flexmock(module).should_receive('make_dump_path').and_return('')
  83. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  84. 'databases/localhost/foo'
  85. )
  86. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  87. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  88. flexmock(module).should_receive('execute_command').with_args(
  89. ['mongodump', '--archive', 'databases/localhost/foo', '--db', 'foo'],
  90. shell=True,
  91. run_to_completion=False,
  92. ).and_return(process).once()
  93. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  94. def test_dump_databases_runs_mongodump_with_options():
  95. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  96. process = flexmock()
  97. flexmock(module).should_receive('make_dump_path').and_return('')
  98. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  99. 'databases/localhost/foo'
  100. )
  101. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  102. flexmock(module).should_receive('execute_command').with_args(
  103. ['mongodump', '--archive', '--db', 'foo', '--stuff=such', '>', 'databases/localhost/foo'],
  104. shell=True,
  105. run_to_completion=False,
  106. ).and_return(process).once()
  107. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  108. def test_dump_databases_runs_mongodumpall_for_all_databases():
  109. databases = [{'name': 'all'}]
  110. process = flexmock()
  111. flexmock(module).should_receive('make_dump_path').and_return('')
  112. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  113. 'databases/localhost/all'
  114. )
  115. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  116. flexmock(module).should_receive('execute_command').with_args(
  117. ['mongodump', '--archive', '>', 'databases/localhost/all'],
  118. shell=True,
  119. run_to_completion=False,
  120. ).and_return(process).once()
  121. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  122. def test_restore_database_dump_runs_pg_restore():
  123. database_config = [{'name': 'foo'}]
  124. extract_process = flexmock(stdout=flexmock())
  125. flexmock(module).should_receive('make_dump_path')
  126. flexmock(module.dump).should_receive('make_database_dump_filename')
  127. flexmock(module).should_receive('execute_command_with_processes').with_args(
  128. ['mongorestore', '--archive', '--drop', '--db', 'foo'],
  129. processes=[extract_process],
  130. output_log_level=logging.DEBUG,
  131. input_file=extract_process.stdout,
  132. borg_local_path='borg',
  133. ).once()
  134. module.restore_database_dump(
  135. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  136. )
  137. def test_restore_database_dump_errors_on_multiple_database_config():
  138. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  139. flexmock(module).should_receive('make_dump_path')
  140. flexmock(module.dump).should_receive('make_database_dump_filename')
  141. flexmock(module).should_receive('execute_command_with_processes').never()
  142. flexmock(module).should_receive('execute_command').never()
  143. with pytest.raises(ValueError):
  144. module.restore_database_dump(
  145. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  146. )
  147. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  148. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  149. extract_process = flexmock(stdout=flexmock())
  150. flexmock(module).should_receive('make_dump_path')
  151. flexmock(module.dump).should_receive('make_database_dump_filename')
  152. flexmock(module).should_receive('execute_command_with_processes').with_args(
  153. [
  154. 'mongorestore',
  155. '--archive',
  156. '--drop',
  157. '--db',
  158. 'foo',
  159. '--host',
  160. 'database.example.org',
  161. '--port',
  162. '5433',
  163. ],
  164. processes=[extract_process],
  165. output_log_level=logging.DEBUG,
  166. input_file=extract_process.stdout,
  167. borg_local_path='borg',
  168. ).once()
  169. module.restore_database_dump(
  170. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  171. )
  172. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  173. database_config = [{'name': 'foo', 'username': 'mongo', 'password': 'trustsome1'}]
  174. extract_process = flexmock(stdout=flexmock())
  175. flexmock(module).should_receive('make_dump_path')
  176. flexmock(module.dump).should_receive('make_database_dump_filename')
  177. flexmock(module).should_receive('execute_command_with_processes').with_args(
  178. [
  179. 'mongorestore',
  180. '--archive',
  181. '--drop',
  182. '--db',
  183. 'foo',
  184. '--username',
  185. 'mongo',
  186. '--password',
  187. 'trustsome1',
  188. ],
  189. processes=[extract_process],
  190. output_log_level=logging.DEBUG,
  191. input_file=extract_process.stdout,
  192. borg_local_path='borg',
  193. ).once()
  194. module.restore_database_dump(
  195. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  196. )
  197. def test_restore_database_dump_runs_psql_for_all_database_dump():
  198. database_config = [{'name': 'all'}]
  199. extract_process = flexmock(stdout=flexmock())
  200. flexmock(module).should_receive('make_dump_path')
  201. flexmock(module.dump).should_receive('make_database_dump_filename')
  202. flexmock(module).should_receive('execute_command_with_processes').with_args(
  203. ['mongorestore', '--archive'],
  204. processes=[extract_process],
  205. output_log_level=logging.DEBUG,
  206. input_file=extract_process.stdout,
  207. borg_local_path='borg',
  208. ).once()
  209. module.restore_database_dump(
  210. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  211. )
  212. def test_restore_database_dump_with_dry_run_skips_restore():
  213. database_config = [{'name': 'foo'}]
  214. flexmock(module).should_receive('make_dump_path')
  215. flexmock(module.dump).should_receive('make_database_dump_filename')
  216. flexmock(module).should_receive('execute_command_with_processes').never()
  217. module.restore_database_dump(
  218. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  219. )
  220. def test_restore_database_dump_without_extract_process_restores_from_disk():
  221. database_config = [{'name': 'foo'}]
  222. flexmock(module).should_receive('make_dump_path')
  223. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  224. flexmock(module).should_receive('execute_command_with_processes').with_args(
  225. ['mongorestore', '--archive', '/dump/path', '--drop', '--db', 'foo'],
  226. processes=[],
  227. output_log_level=logging.DEBUG,
  228. input_file=None,
  229. borg_local_path='borg',
  230. ).once()
  231. module.restore_database_dump(
  232. database_config, 'test.yaml', {}, dry_run=False, extract_process=None
  233. )