test_mongodb.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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'],
  98. shell=True,
  99. ).and_return(flexmock()).once()
  100. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == []
  101. def test_dump_databases_runs_mongodump_with_options():
  102. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  103. process = flexmock()
  104. flexmock(module).should_receive('make_dump_path').and_return('')
  105. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  106. 'databases/localhost/foo'
  107. )
  108. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  109. flexmock(module).should_receive('execute_command').with_args(
  110. ['mongodump', '--db', 'foo', '--stuff=such', '--archive', '>', 'databases/localhost/foo'],
  111. shell=True,
  112. run_to_completion=False,
  113. ).and_return(process).once()
  114. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  115. def test_dump_databases_runs_mongodumpall_for_all_databases():
  116. databases = [{'name': 'all'}]
  117. process = flexmock()
  118. flexmock(module).should_receive('make_dump_path').and_return('')
  119. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  120. 'databases/localhost/all'
  121. )
  122. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  123. flexmock(module).should_receive('execute_command').with_args(
  124. ['mongodump', '--archive', '>', 'databases/localhost/all'],
  125. shell=True,
  126. run_to_completion=False,
  127. ).and_return(process).once()
  128. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  129. def test_restore_database_dump_runs_mongorestore():
  130. database_config = [{'name': 'foo', 'schemas': None}]
  131. extract_process = flexmock(stdout=flexmock())
  132. flexmock(module).should_receive('make_dump_path')
  133. flexmock(module.dump).should_receive('make_database_dump_filename')
  134. flexmock(module).should_receive('execute_command_with_processes').with_args(
  135. ['mongorestore', '--archive', '--drop', '--db', 'foo'],
  136. processes=[extract_process],
  137. output_log_level=logging.DEBUG,
  138. input_file=extract_process.stdout,
  139. ).once()
  140. module.restore_database_dump(
  141. database_config,
  142. 'test.yaml',
  143. {},
  144. dry_run=False,
  145. extract_process=extract_process,
  146. connection_params={
  147. 'hostname': None,
  148. 'port': None,
  149. 'username': None,
  150. 'password': None,
  151. },
  152. )
  153. def test_restore_database_dump_errors_on_multiple_database_config():
  154. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  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').never()
  158. flexmock(module).should_receive('execute_command').never()
  159. with pytest.raises(ValueError):
  160. module.restore_database_dump(
  161. database_config,
  162. 'test.yaml',
  163. {},
  164. dry_run=False,
  165. extract_process=flexmock(),
  166. connection_params={
  167. 'hostname': None,
  168. 'port': None,
  169. 'username': None,
  170. 'password': None,
  171. },
  172. )
  173. def test_restore_database_dump_runs_mongorestore_with_hostname_and_port():
  174. database_config = [
  175. {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
  176. ]
  177. extract_process = flexmock(stdout=flexmock())
  178. flexmock(module).should_receive('make_dump_path')
  179. flexmock(module.dump).should_receive('make_database_dump_filename')
  180. flexmock(module).should_receive('execute_command_with_processes').with_args(
  181. [
  182. 'mongorestore',
  183. '--archive',
  184. '--drop',
  185. '--db',
  186. 'foo',
  187. '--host',
  188. 'database.example.org',
  189. '--port',
  190. '5433',
  191. ],
  192. processes=[extract_process],
  193. output_log_level=logging.DEBUG,
  194. input_file=extract_process.stdout,
  195. ).once()
  196. module.restore_database_dump(
  197. database_config,
  198. 'test.yaml',
  199. {},
  200. dry_run=False,
  201. extract_process=extract_process,
  202. connection_params={
  203. 'hostname': None,
  204. 'port': None,
  205. 'username': None,
  206. 'password': None,
  207. },
  208. )
  209. def test_restore_database_dump_runs_mongorestore_with_username_and_password():
  210. database_config = [
  211. {
  212. 'name': 'foo',
  213. 'username': 'mongo',
  214. 'password': 'trustsome1',
  215. 'authentication_database': 'admin',
  216. 'schemas': None,
  217. }
  218. ]
  219. extract_process = flexmock(stdout=flexmock())
  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').with_args(
  223. [
  224. 'mongorestore',
  225. '--archive',
  226. '--drop',
  227. '--db',
  228. 'foo',
  229. '--username',
  230. 'mongo',
  231. '--password',
  232. 'trustsome1',
  233. '--authenticationDatabase',
  234. 'admin',
  235. ],
  236. processes=[extract_process],
  237. output_log_level=logging.DEBUG,
  238. input_file=extract_process.stdout,
  239. ).once()
  240. module.restore_database_dump(
  241. database_config,
  242. 'test.yaml',
  243. {},
  244. dry_run=False,
  245. extract_process=extract_process,
  246. connection_params={
  247. 'hostname': None,
  248. 'port': None,
  249. 'username': None,
  250. 'password': None,
  251. },
  252. )
  253. def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore():
  254. database_config = [
  255. {
  256. 'name': 'foo',
  257. 'username': 'mongo',
  258. 'password': 'trustsome1',
  259. 'authentication_database': 'admin',
  260. 'restore_hostname': 'restorehost',
  261. 'restore_port': 'restoreport',
  262. 'restore_username': 'restoreusername',
  263. 'restore_password': 'restorepassword',
  264. 'schemas': None,
  265. }
  266. ]
  267. extract_process = flexmock(stdout=flexmock())
  268. flexmock(module).should_receive('make_dump_path')
  269. flexmock(module.dump).should_receive('make_database_dump_filename')
  270. flexmock(module).should_receive('execute_command_with_processes').with_args(
  271. [
  272. 'mongorestore',
  273. '--archive',
  274. '--drop',
  275. '--db',
  276. 'foo',
  277. '--host',
  278. 'clihost',
  279. '--port',
  280. 'cliport',
  281. '--username',
  282. 'cliusername',
  283. '--password',
  284. 'clipassword',
  285. '--authenticationDatabase',
  286. 'admin',
  287. ],
  288. processes=[extract_process],
  289. output_log_level=logging.DEBUG,
  290. input_file=extract_process.stdout,
  291. ).once()
  292. module.restore_database_dump(
  293. database_config,
  294. 'test.yaml',
  295. {},
  296. dry_run=False,
  297. extract_process=extract_process,
  298. connection_params={
  299. 'hostname': 'clihost',
  300. 'port': 'cliport',
  301. 'username': 'cliusername',
  302. 'password': 'clipassword',
  303. },
  304. )
  305. def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  306. database_config = [
  307. {
  308. 'name': 'foo',
  309. 'username': 'mongo',
  310. 'password': 'trustsome1',
  311. 'authentication_database': 'admin',
  312. 'schemas': None,
  313. 'restore_hostname': 'restorehost',
  314. 'restore_port': 'restoreport',
  315. 'restore_username': 'restoreuser',
  316. 'restore_password': 'restorepass',
  317. }
  318. ]
  319. extract_process = flexmock(stdout=flexmock())
  320. flexmock(module).should_receive('make_dump_path')
  321. flexmock(module.dump).should_receive('make_database_dump_filename')
  322. flexmock(module).should_receive('execute_command_with_processes').with_args(
  323. [
  324. 'mongorestore',
  325. '--archive',
  326. '--drop',
  327. '--db',
  328. 'foo',
  329. '--host',
  330. 'restorehost',
  331. '--port',
  332. 'restoreport',
  333. '--username',
  334. 'restoreuser',
  335. '--password',
  336. 'restorepass',
  337. '--authenticationDatabase',
  338. 'admin',
  339. ],
  340. processes=[extract_process],
  341. output_log_level=logging.DEBUG,
  342. input_file=extract_process.stdout,
  343. ).once()
  344. module.restore_database_dump(
  345. database_config,
  346. 'test.yaml',
  347. {},
  348. dry_run=False,
  349. extract_process=extract_process,
  350. connection_params={
  351. 'hostname': None,
  352. 'port': None,
  353. 'username': None,
  354. 'password': None,
  355. },
  356. )
  357. def test_restore_database_dump_runs_mongorestore_with_options():
  358. database_config = [{'name': 'foo', 'restore_options': '--harder', 'schemas': None}]
  359. extract_process = flexmock(stdout=flexmock())
  360. flexmock(module).should_receive('make_dump_path')
  361. flexmock(module.dump).should_receive('make_database_dump_filename')
  362. flexmock(module).should_receive('execute_command_with_processes').with_args(
  363. ['mongorestore', '--archive', '--drop', '--db', 'foo', '--harder'],
  364. processes=[extract_process],
  365. output_log_level=logging.DEBUG,
  366. input_file=extract_process.stdout,
  367. ).once()
  368. module.restore_database_dump(
  369. database_config,
  370. 'test.yaml',
  371. {},
  372. dry_run=False,
  373. extract_process=extract_process,
  374. connection_params={
  375. 'hostname': None,
  376. 'port': None,
  377. 'username': None,
  378. 'password': None,
  379. },
  380. )
  381. def test_restore_databases_dump_runs_mongorestore_with_schemas():
  382. database_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
  383. extract_process = flexmock(stdout=flexmock())
  384. flexmock(module).should_receive('make_dump_path')
  385. flexmock(module.dump).should_receive('make_database_dump_filename')
  386. flexmock(module).should_receive('execute_command_with_processes').with_args(
  387. [
  388. 'mongorestore',
  389. '--archive',
  390. '--drop',
  391. '--db',
  392. 'foo',
  393. '--nsInclude',
  394. 'bar',
  395. '--nsInclude',
  396. 'baz',
  397. ],
  398. processes=[extract_process],
  399. output_log_level=logging.DEBUG,
  400. input_file=extract_process.stdout,
  401. ).once()
  402. module.restore_database_dump(
  403. database_config,
  404. 'test.yaml',
  405. {},
  406. dry_run=False,
  407. extract_process=extract_process,
  408. connection_params={
  409. 'hostname': None,
  410. 'port': None,
  411. 'username': None,
  412. 'password': None,
  413. },
  414. )
  415. def test_restore_database_dump_runs_psql_for_all_database_dump():
  416. database_config = [{'name': 'all', 'schemas': None}]
  417. extract_process = flexmock(stdout=flexmock())
  418. flexmock(module).should_receive('make_dump_path')
  419. flexmock(module.dump).should_receive('make_database_dump_filename')
  420. flexmock(module).should_receive('execute_command_with_processes').with_args(
  421. ['mongorestore', '--archive'],
  422. processes=[extract_process],
  423. output_log_level=logging.DEBUG,
  424. input_file=extract_process.stdout,
  425. ).once()
  426. module.restore_database_dump(
  427. database_config,
  428. 'test.yaml',
  429. {},
  430. dry_run=False,
  431. extract_process=extract_process,
  432. connection_params={
  433. 'hostname': None,
  434. 'port': None,
  435. 'username': None,
  436. 'password': None,
  437. },
  438. )
  439. def test_restore_database_dump_with_dry_run_skips_restore():
  440. database_config = [{'name': 'foo', 'schemas': None}]
  441. flexmock(module).should_receive('make_dump_path')
  442. flexmock(module.dump).should_receive('make_database_dump_filename')
  443. flexmock(module).should_receive('execute_command_with_processes').never()
  444. module.restore_database_dump(
  445. database_config,
  446. 'test.yaml',
  447. {},
  448. dry_run=True,
  449. extract_process=flexmock(),
  450. connection_params={
  451. 'hostname': None,
  452. 'port': None,
  453. 'username': None,
  454. 'password': None,
  455. },
  456. )
  457. def test_restore_database_dump_without_extract_process_restores_from_disk():
  458. database_config = [{'name': 'foo', 'format': 'directory', 'schemas': None}]
  459. flexmock(module).should_receive('make_dump_path')
  460. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  461. flexmock(module).should_receive('execute_command_with_processes').with_args(
  462. ['mongorestore', '--dir', '/dump/path', '--drop', '--db', 'foo'],
  463. processes=[],
  464. output_log_level=logging.DEBUG,
  465. input_file=None,
  466. ).once()
  467. module.restore_database_dump(
  468. database_config,
  469. 'test.yaml',
  470. {},
  471. dry_run=False,
  472. extract_process=None,
  473. connection_params={
  474. 'hostname': None,
  475. 'port': None,
  476. 'username': None,
  477. 'password': None,
  478. },
  479. )