2
0

test_mongodb.py 11 KB

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