test_mongodb.py 19 KB

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