test_mongodb.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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', 'auth_db': "admin"}]
  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. '--authenticationDatabase',
  71. 'admin',
  72. '--db',
  73. 'foo',
  74. '>',
  75. 'databases/localhost/foo',
  76. ],
  77. shell=True,
  78. run_to_completion=False,
  79. ).and_return(process).once()
  80. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  81. def test_dump_databases_runs_mongodump_with_directory_format():
  82. databases = [{'name': 'foo', 'format': 'directory'}]
  83. process = flexmock()
  84. flexmock(module).should_receive('make_dump_path').and_return('')
  85. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  86. 'databases/localhost/foo'
  87. )
  88. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  89. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  90. flexmock(module).should_receive('execute_command').with_args(
  91. ['mongodump', '--archive', 'databases/localhost/foo', '--db', 'foo'],
  92. shell=True,
  93. run_to_completion=False,
  94. ).and_return(process).once()
  95. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  96. def test_dump_databases_runs_mongodump_with_options():
  97. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  98. process = flexmock()
  99. flexmock(module).should_receive('make_dump_path').and_return('')
  100. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  101. 'databases/localhost/foo'
  102. )
  103. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  104. flexmock(module).should_receive('execute_command').with_args(
  105. ['mongodump', '--archive', '--db', 'foo', '--stuff=such', '>', 'databases/localhost/foo'],
  106. shell=True,
  107. run_to_completion=False,
  108. ).and_return(process).once()
  109. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  110. def test_dump_databases_runs_mongodumpall_for_all_databases():
  111. databases = [{'name': 'all'}]
  112. process = flexmock()
  113. flexmock(module).should_receive('make_dump_path').and_return('')
  114. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  115. 'databases/localhost/all'
  116. )
  117. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  118. flexmock(module).should_receive('execute_command').with_args(
  119. ['mongodump', '--archive', '>', 'databases/localhost/all'],
  120. shell=True,
  121. run_to_completion=False,
  122. ).and_return(process).once()
  123. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  124. def test_restore_database_dump_runs_pg_restore():
  125. database_config = [{'name': 'foo'}]
  126. extract_process = flexmock(stdout=flexmock())
  127. flexmock(module).should_receive('make_dump_path')
  128. flexmock(module.dump).should_receive('make_database_dump_filename')
  129. flexmock(module).should_receive('execute_command_with_processes').with_args(
  130. ['mongorestore', '--archive', '--drop', '--db', 'foo'],
  131. processes=[extract_process],
  132. output_log_level=logging.DEBUG,
  133. input_file=extract_process.stdout,
  134. borg_local_path='borg',
  135. ).once()
  136. module.restore_database_dump(
  137. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  138. )
  139. def test_restore_database_dump_errors_on_multiple_database_config():
  140. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  141. flexmock(module).should_receive('make_dump_path')
  142. flexmock(module.dump).should_receive('make_database_dump_filename')
  143. flexmock(module).should_receive('execute_command_with_processes').never()
  144. flexmock(module).should_receive('execute_command').never()
  145. with pytest.raises(ValueError):
  146. module.restore_database_dump(
  147. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  148. )
  149. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  150. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  151. extract_process = flexmock(stdout=flexmock())
  152. flexmock(module).should_receive('make_dump_path')
  153. flexmock(module.dump).should_receive('make_database_dump_filename')
  154. flexmock(module).should_receive('execute_command_with_processes').with_args(
  155. [
  156. 'mongorestore',
  157. '--archive',
  158. '--drop',
  159. '--db',
  160. 'foo',
  161. '--host',
  162. 'database.example.org',
  163. '--port',
  164. '5433',
  165. ],
  166. processes=[extract_process],
  167. output_log_level=logging.DEBUG,
  168. input_file=extract_process.stdout,
  169. borg_local_path='borg',
  170. ).once()
  171. module.restore_database_dump(
  172. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  173. )
  174. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  175. database_config = [
  176. {'name': 'foo', 'username': 'mongo', 'password': 'trustsome1', 'auth_db': 'admin'}
  177. ]
  178. extract_process = flexmock(stdout=flexmock())
  179. flexmock(module).should_receive('make_dump_path')
  180. flexmock(module.dump).should_receive('make_database_dump_filename')
  181. flexmock(module).should_receive('execute_command_with_processes').with_args(
  182. [
  183. 'mongorestore',
  184. '--archive',
  185. '--drop',
  186. '--db',
  187. 'foo',
  188. '--username',
  189. 'mongo',
  190. '--password',
  191. 'trustsome1',
  192. '--authenticationDatabase',
  193. 'admin',
  194. ],
  195. processes=[extract_process],
  196. output_log_level=logging.DEBUG,
  197. input_file=extract_process.stdout,
  198. borg_local_path='borg',
  199. ).once()
  200. module.restore_database_dump(
  201. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  202. )
  203. def test_restore_database_dump_runs_psql_for_all_database_dump():
  204. database_config = [{'name': 'all'}]
  205. extract_process = flexmock(stdout=flexmock())
  206. flexmock(module).should_receive('make_dump_path')
  207. flexmock(module.dump).should_receive('make_database_dump_filename')
  208. flexmock(module).should_receive('execute_command_with_processes').with_args(
  209. ['mongorestore', '--archive'],
  210. processes=[extract_process],
  211. output_log_level=logging.DEBUG,
  212. input_file=extract_process.stdout,
  213. borg_local_path='borg',
  214. ).once()
  215. module.restore_database_dump(
  216. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  217. )
  218. def test_restore_database_dump_with_dry_run_skips_restore():
  219. database_config = [{'name': 'foo'}]
  220. flexmock(module).should_receive('make_dump_path')
  221. flexmock(module.dump).should_receive('make_database_dump_filename')
  222. flexmock(module).should_receive('execute_command_with_processes').never()
  223. module.restore_database_dump(
  224. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  225. )
  226. def test_restore_database_dump_without_extract_process_restores_from_disk():
  227. database_config = [{'name': 'foo'}]
  228. flexmock(module).should_receive('make_dump_path')
  229. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  230. flexmock(module).should_receive('execute_command_with_processes').with_args(
  231. ['mongorestore', '--archive', '/dump/path', '--drop', '--db', 'foo'],
  232. processes=[],
  233. output_log_level=logging.DEBUG,
  234. input_file=None,
  235. borg_local_path='borg',
  236. ).once()
  237. module.restore_database_dump(
  238. database_config, 'test.yaml', {}, dry_run=False, extract_process=None
  239. )