test_mongodb.py 11 KB

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