test_mongodb.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.hooks.data_source import mongodb as module
  4. def test_use_streaming_true_for_any_non_directory_format_databases():
  5. assert module.use_streaming(
  6. databases=[{'format': 'stuff'}, {'format': 'directory'}, {}],
  7. config=flexmock(),
  8. )
  9. def test_use_streaming_false_for_all_directory_format_databases():
  10. assert not module.use_streaming(
  11. databases=[{'format': 'directory'}, {'format': 'directory'}],
  12. config=flexmock(),
  13. )
  14. def test_use_streaming_false_for_no_databases():
  15. assert not module.use_streaming(databases=[], config=flexmock())
  16. def test_dump_data_sources_runs_mongodump_for_each_database():
  17. databases = [{'name': 'foo'}, {'name': 'bar'}]
  18. processes = [flexmock(), flexmock()]
  19. flexmock(module).should_receive('make_dump_path').and_return('')
  20. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  21. 'databases/localhost/foo'
  22. ).and_return('databases/localhost/bar')
  23. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  24. for name, process in zip(('foo', 'bar'), processes):
  25. flexmock(module).should_receive('execute_command').with_args(
  26. ('mongodump', '--db', name, '--archive', '>', f'databases/localhost/{name}'),
  27. shell=True,
  28. run_to_completion=False,
  29. ).and_return(process).once()
  30. assert (
  31. module.dump_data_sources(
  32. databases,
  33. {},
  34. config_paths=('test.yaml',),
  35. borgmatic_runtime_directory='/run/borgmatic',
  36. patterns=[],
  37. dry_run=False,
  38. )
  39. == processes
  40. )
  41. def test_dump_data_sources_with_dry_run_skips_mongodump():
  42. databases = [{'name': 'foo'}, {'name': 'bar'}]
  43. flexmock(module).should_receive('make_dump_path').and_return('')
  44. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  45. 'databases/localhost/foo'
  46. ).and_return('databases/localhost/bar')
  47. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  48. flexmock(module).should_receive('execute_command').never()
  49. assert (
  50. module.dump_data_sources(
  51. databases,
  52. {},
  53. config_paths=('test.yaml',),
  54. borgmatic_runtime_directory='/run/borgmatic',
  55. patterns=[],
  56. dry_run=True,
  57. )
  58. == []
  59. )
  60. def test_dump_data_sources_runs_mongodump_with_hostname_and_port():
  61. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  62. process = flexmock()
  63. flexmock(module).should_receive('make_dump_path').and_return('')
  64. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  65. 'databases/database.example.org/foo'
  66. )
  67. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  68. flexmock(module).should_receive('execute_command').with_args(
  69. (
  70. 'mongodump',
  71. '--host',
  72. 'database.example.org',
  73. '--port',
  74. '5433',
  75. '--db',
  76. 'foo',
  77. '--archive',
  78. '>',
  79. 'databases/database.example.org/foo',
  80. ),
  81. shell=True,
  82. run_to_completion=False,
  83. ).and_return(process).once()
  84. assert module.dump_data_sources(
  85. databases,
  86. {},
  87. config_paths=('test.yaml',),
  88. borgmatic_runtime_directory='/run/borgmatic',
  89. patterns=[],
  90. dry_run=False,
  91. ) == [process]
  92. def test_dump_data_sources_runs_mongodump_with_username_and_password():
  93. databases = [
  94. {
  95. 'name': 'foo',
  96. 'username': 'mongo',
  97. 'password': 'trustsome1',
  98. 'authentication_database': 'admin',
  99. }
  100. ]
  101. process = flexmock()
  102. flexmock(module).should_receive('make_dump_path').and_return('')
  103. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  104. 'databases/localhost/foo'
  105. )
  106. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  107. 'resolve_credential'
  108. ).replace_with(lambda value: value)
  109. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  110. flexmock(module).should_receive('execute_command').with_args(
  111. (
  112. 'mongodump',
  113. '--username',
  114. 'mongo',
  115. '--password',
  116. 'trustsome1',
  117. '--authenticationDatabase',
  118. 'admin',
  119. '--db',
  120. 'foo',
  121. '--archive',
  122. '>',
  123. 'databases/localhost/foo',
  124. ),
  125. shell=True,
  126. run_to_completion=False,
  127. ).and_return(process).once()
  128. assert module.dump_data_sources(
  129. databases,
  130. {},
  131. config_paths=('test.yaml',),
  132. borgmatic_runtime_directory='/run/borgmatic',
  133. patterns=[],
  134. dry_run=False,
  135. ) == [process]
  136. def test_dump_data_sources_runs_mongodump_with_directory_format():
  137. databases = [{'name': 'foo', 'format': 'directory'}]
  138. flexmock(module).should_receive('make_dump_path').and_return('')
  139. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  140. 'databases/localhost/foo'
  141. )
  142. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  143. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  144. flexmock(module).should_receive('execute_command').with_args(
  145. ('mongodump', '--out', 'databases/localhost/foo', '--db', 'foo'),
  146. shell=True,
  147. ).and_return(flexmock()).once()
  148. assert (
  149. module.dump_data_sources(
  150. databases,
  151. {},
  152. config_paths=('test.yaml',),
  153. borgmatic_runtime_directory='/run/borgmatic',
  154. patterns=[],
  155. dry_run=False,
  156. )
  157. == []
  158. )
  159. def test_dump_data_sources_runs_mongodump_with_options():
  160. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  161. process = flexmock()
  162. flexmock(module).should_receive('make_dump_path').and_return('')
  163. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  164. 'databases/localhost/foo'
  165. )
  166. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  167. flexmock(module).should_receive('execute_command').with_args(
  168. (
  169. 'mongodump',
  170. '--db',
  171. 'foo',
  172. '--stuff=such',
  173. '--archive',
  174. '>',
  175. 'databases/localhost/foo',
  176. ),
  177. shell=True,
  178. run_to_completion=False,
  179. ).and_return(process).once()
  180. assert module.dump_data_sources(
  181. databases,
  182. {},
  183. config_paths=('test.yaml',),
  184. borgmatic_runtime_directory='/run/borgmatic',
  185. patterns=[],
  186. dry_run=False,
  187. ) == [process]
  188. def test_dump_data_sources_runs_mongodumpall_for_all_databases():
  189. databases = [{'name': 'all'}]
  190. process = flexmock()
  191. flexmock(module).should_receive('make_dump_path').and_return('')
  192. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  193. 'databases/localhost/all'
  194. )
  195. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  196. flexmock(module).should_receive('execute_command').with_args(
  197. ('mongodump', '--archive', '>', 'databases/localhost/all'),
  198. shell=True,
  199. run_to_completion=False,
  200. ).and_return(process).once()
  201. assert module.dump_data_sources(
  202. databases,
  203. {},
  204. config_paths=('test.yaml',),
  205. borgmatic_runtime_directory='/run/borgmatic',
  206. patterns=[],
  207. dry_run=False,
  208. ) == [process]
  209. def test_build_dump_command_with_username_injection_attack_gets_escaped():
  210. database = {'name': 'test', 'username': 'bob; naughty-command'}
  211. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  212. 'resolve_credential'
  213. ).replace_with(lambda value: value)
  214. command = module.build_dump_command(database, dump_filename='test', dump_format='archive')
  215. assert "'bob; naughty-command'" in command
  216. def test_restore_data_source_dump_runs_mongorestore():
  217. hook_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
  218. extract_process = flexmock(stdout=flexmock())
  219. flexmock(module).should_receive('make_dump_path')
  220. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  221. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  222. 'resolve_credential'
  223. ).replace_with(lambda value: value)
  224. flexmock(module).should_receive('execute_command_with_processes').with_args(
  225. ['mongorestore', '--archive', '--drop'],
  226. processes=[extract_process],
  227. output_log_level=logging.DEBUG,
  228. input_file=extract_process.stdout,
  229. ).once()
  230. module.restore_data_source_dump(
  231. hook_config,
  232. {},
  233. data_source={'name': 'foo'},
  234. dry_run=False,
  235. extract_process=extract_process,
  236. connection_params={
  237. 'hostname': None,
  238. 'port': None,
  239. 'username': None,
  240. 'password': None,
  241. },
  242. borgmatic_runtime_directory='/run/borgmatic',
  243. )
  244. def test_restore_data_source_dump_runs_mongorestore_with_hostname_and_port():
  245. hook_config = [
  246. {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
  247. ]
  248. extract_process = flexmock(stdout=flexmock())
  249. flexmock(module).should_receive('make_dump_path')
  250. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  251. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  252. 'resolve_credential'
  253. ).replace_with(lambda value: value)
  254. flexmock(module).should_receive('execute_command_with_processes').with_args(
  255. [
  256. 'mongorestore',
  257. '--archive',
  258. '--drop',
  259. '--host',
  260. 'database.example.org',
  261. '--port',
  262. '5433',
  263. ],
  264. processes=[extract_process],
  265. output_log_level=logging.DEBUG,
  266. input_file=extract_process.stdout,
  267. ).once()
  268. module.restore_data_source_dump(
  269. hook_config,
  270. {},
  271. data_source=hook_config[0],
  272. dry_run=False,
  273. extract_process=extract_process,
  274. connection_params={
  275. 'hostname': None,
  276. 'port': None,
  277. 'username': None,
  278. 'password': None,
  279. },
  280. borgmatic_runtime_directory='/run/borgmatic',
  281. )
  282. def test_restore_data_source_dump_runs_mongorestore_with_username_and_password():
  283. hook_config = [
  284. {
  285. 'name': 'foo',
  286. 'username': 'mongo',
  287. 'password': 'trustsome1',
  288. 'authentication_database': 'admin',
  289. 'schemas': None,
  290. }
  291. ]
  292. extract_process = flexmock(stdout=flexmock())
  293. flexmock(module).should_receive('make_dump_path')
  294. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  295. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  296. 'resolve_credential'
  297. ).replace_with(lambda value: value)
  298. flexmock(module).should_receive('execute_command_with_processes').with_args(
  299. [
  300. 'mongorestore',
  301. '--archive',
  302. '--drop',
  303. '--username',
  304. 'mongo',
  305. '--password',
  306. 'trustsome1',
  307. '--authenticationDatabase',
  308. 'admin',
  309. ],
  310. processes=[extract_process],
  311. output_log_level=logging.DEBUG,
  312. input_file=extract_process.stdout,
  313. ).once()
  314. module.restore_data_source_dump(
  315. hook_config,
  316. {},
  317. data_source=hook_config[0],
  318. dry_run=False,
  319. extract_process=extract_process,
  320. connection_params={
  321. 'hostname': None,
  322. 'port': None,
  323. 'username': None,
  324. 'password': None,
  325. },
  326. borgmatic_runtime_directory='/run/borgmatic',
  327. )
  328. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  329. hook_config = [
  330. {
  331. 'name': 'foo',
  332. 'username': 'mongo',
  333. 'password': 'trustsome1',
  334. 'authentication_database': 'admin',
  335. 'restore_hostname': 'restorehost',
  336. 'restore_port': 'restoreport',
  337. 'restore_username': 'restoreusername',
  338. 'restore_password': 'restorepassword',
  339. 'schemas': None,
  340. }
  341. ]
  342. extract_process = flexmock(stdout=flexmock())
  343. flexmock(module).should_receive('make_dump_path')
  344. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  345. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  346. 'resolve_credential'
  347. ).replace_with(lambda value: value)
  348. flexmock(module).should_receive('execute_command_with_processes').with_args(
  349. [
  350. 'mongorestore',
  351. '--archive',
  352. '--drop',
  353. '--host',
  354. 'clihost',
  355. '--port',
  356. 'cliport',
  357. '--username',
  358. 'cliusername',
  359. '--password',
  360. 'clipassword',
  361. '--authenticationDatabase',
  362. 'admin',
  363. ],
  364. processes=[extract_process],
  365. output_log_level=logging.DEBUG,
  366. input_file=extract_process.stdout,
  367. ).once()
  368. module.restore_data_source_dump(
  369. hook_config,
  370. {},
  371. data_source=hook_config[0],
  372. dry_run=False,
  373. extract_process=extract_process,
  374. connection_params={
  375. 'hostname': 'clihost',
  376. 'port': 'cliport',
  377. 'username': 'cliusername',
  378. 'password': 'clipassword',
  379. },
  380. borgmatic_runtime_directory='/run/borgmatic',
  381. )
  382. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  383. hook_config = [
  384. {
  385. 'name': 'foo',
  386. 'username': 'mongo',
  387. 'password': 'trustsome1',
  388. 'authentication_database': 'admin',
  389. 'schemas': None,
  390. 'restore_hostname': 'restorehost',
  391. 'restore_port': 'restoreport',
  392. 'restore_username': 'restoreuser',
  393. 'restore_password': 'restorepass',
  394. }
  395. ]
  396. extract_process = flexmock(stdout=flexmock())
  397. flexmock(module).should_receive('make_dump_path')
  398. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  399. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  400. 'resolve_credential'
  401. ).replace_with(lambda value: value)
  402. flexmock(module).should_receive('execute_command_with_processes').with_args(
  403. [
  404. 'mongorestore',
  405. '--archive',
  406. '--drop',
  407. '--host',
  408. 'restorehost',
  409. '--port',
  410. 'restoreport',
  411. '--username',
  412. 'restoreuser',
  413. '--password',
  414. 'restorepass',
  415. '--authenticationDatabase',
  416. 'admin',
  417. ],
  418. processes=[extract_process],
  419. output_log_level=logging.DEBUG,
  420. input_file=extract_process.stdout,
  421. ).once()
  422. module.restore_data_source_dump(
  423. hook_config,
  424. {},
  425. data_source=hook_config[0],
  426. dry_run=False,
  427. extract_process=extract_process,
  428. connection_params={
  429. 'hostname': None,
  430. 'port': None,
  431. 'username': None,
  432. 'password': None,
  433. },
  434. borgmatic_runtime_directory='/run/borgmatic',
  435. )
  436. def test_restore_data_source_dump_runs_mongorestore_with_options():
  437. hook_config = [{'name': 'foo', 'restore_options': '--harder', 'schemas': None}]
  438. extract_process = flexmock(stdout=flexmock())
  439. flexmock(module).should_receive('make_dump_path')
  440. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  441. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  442. 'resolve_credential'
  443. ).replace_with(lambda value: value)
  444. flexmock(module).should_receive('execute_command_with_processes').with_args(
  445. ['mongorestore', '--archive', '--drop', '--harder'],
  446. processes=[extract_process],
  447. output_log_level=logging.DEBUG,
  448. input_file=extract_process.stdout,
  449. ).once()
  450. module.restore_data_source_dump(
  451. hook_config,
  452. {},
  453. data_source=hook_config[0],
  454. dry_run=False,
  455. extract_process=extract_process,
  456. connection_params={
  457. 'hostname': None,
  458. 'port': None,
  459. 'username': None,
  460. 'password': None,
  461. },
  462. borgmatic_runtime_directory='/run/borgmatic',
  463. )
  464. def test_restore_databases_dump_runs_mongorestore_with_schemas():
  465. hook_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
  466. extract_process = flexmock(stdout=flexmock())
  467. flexmock(module).should_receive('make_dump_path')
  468. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  469. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  470. 'resolve_credential'
  471. ).replace_with(lambda value: value)
  472. flexmock(module).should_receive('execute_command_with_processes').with_args(
  473. [
  474. 'mongorestore',
  475. '--archive',
  476. '--drop',
  477. '--nsInclude',
  478. 'bar',
  479. '--nsInclude',
  480. 'baz',
  481. ],
  482. processes=[extract_process],
  483. output_log_level=logging.DEBUG,
  484. input_file=extract_process.stdout,
  485. ).once()
  486. module.restore_data_source_dump(
  487. hook_config,
  488. {},
  489. data_source=hook_config[0],
  490. dry_run=False,
  491. extract_process=extract_process,
  492. connection_params={
  493. 'hostname': None,
  494. 'port': None,
  495. 'username': None,
  496. 'password': None,
  497. },
  498. borgmatic_runtime_directory='/run/borgmatic',
  499. )
  500. def test_restore_data_source_dump_runs_psql_for_all_database_dump():
  501. hook_config = [{'name': 'all', 'schemas': None}]
  502. extract_process = flexmock(stdout=flexmock())
  503. flexmock(module).should_receive('make_dump_path')
  504. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  505. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  506. 'resolve_credential'
  507. ).replace_with(lambda value: value)
  508. flexmock(module).should_receive('execute_command_with_processes').with_args(
  509. ['mongorestore', '--archive'],
  510. processes=[extract_process],
  511. output_log_level=logging.DEBUG,
  512. input_file=extract_process.stdout,
  513. ).once()
  514. module.restore_data_source_dump(
  515. hook_config,
  516. {},
  517. data_source=hook_config[0],
  518. dry_run=False,
  519. extract_process=extract_process,
  520. connection_params={
  521. 'hostname': None,
  522. 'port': None,
  523. 'username': None,
  524. 'password': None,
  525. },
  526. borgmatic_runtime_directory='/run/borgmatic',
  527. )
  528. def test_restore_data_source_dump_with_dry_run_skips_restore():
  529. hook_config = [{'name': 'foo', 'schemas': None}]
  530. flexmock(module).should_receive('make_dump_path')
  531. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  532. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  533. 'resolve_credential'
  534. ).replace_with(lambda value: value)
  535. flexmock(module).should_receive('execute_command_with_processes').never()
  536. module.restore_data_source_dump(
  537. hook_config,
  538. {},
  539. data_source={'name': 'foo'},
  540. dry_run=True,
  541. extract_process=flexmock(),
  542. connection_params={
  543. 'hostname': None,
  544. 'port': None,
  545. 'username': None,
  546. 'password': None,
  547. },
  548. borgmatic_runtime_directory='/run/borgmatic',
  549. )
  550. def test_restore_data_source_dump_without_extract_process_restores_from_disk():
  551. hook_config = [{'name': 'foo', 'format': 'directory', 'schemas': None}]
  552. flexmock(module).should_receive('make_dump_path')
  553. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  554. flexmock(module.borgmatic.hooks.credential.tag).should_receive(
  555. 'resolve_credential'
  556. ).replace_with(lambda value: value)
  557. flexmock(module).should_receive('execute_command_with_processes').with_args(
  558. ['mongorestore', '--dir', '/dump/path', '--drop'],
  559. processes=[],
  560. output_log_level=logging.DEBUG,
  561. input_file=None,
  562. ).once()
  563. module.restore_data_source_dump(
  564. hook_config,
  565. {},
  566. data_source={'name': 'foo'},
  567. dry_run=False,
  568. extract_process=None,
  569. connection_params={
  570. 'hostname': None,
  571. 'port': None,
  572. 'username': None,
  573. 'password': None,
  574. },
  575. borgmatic_runtime_directory='/run/borgmatic',
  576. )