2
0

test_mongodb.py 19 KB

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