test_mariadb.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks.data_source import mariadb as module
  5. def test_database_names_to_dump_passes_through_name():
  6. extra_environment = flexmock()
  7. log_prefix = ''
  8. names = module.database_names_to_dump(
  9. {'name': 'foo'}, extra_environment, log_prefix, dry_run=False
  10. )
  11. assert names == ('foo',)
  12. def test_database_names_to_dump_bails_for_dry_run():
  13. extra_environment = flexmock()
  14. log_prefix = ''
  15. flexmock(module).should_receive('execute_command_and_capture_output').never()
  16. names = module.database_names_to_dump(
  17. {'name': 'all'}, extra_environment, log_prefix, dry_run=True
  18. )
  19. assert names == ()
  20. def test_database_names_to_dump_queries_mariadb_for_database_names():
  21. extra_environment = flexmock()
  22. log_prefix = ''
  23. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  24. ('mariadb', '--skip-column-names', '--batch', '--execute', 'show schemas'),
  25. extra_environment=extra_environment,
  26. ).and_return('foo\nbar\nmysql\n').once()
  27. names = module.database_names_to_dump(
  28. {'name': 'all'}, extra_environment, log_prefix, dry_run=False
  29. )
  30. assert names == ('foo', 'bar')
  31. def test_use_streaming_true_for_any_databases():
  32. assert module.use_streaming(
  33. databases=[flexmock(), flexmock()], config=flexmock(), log_prefix=flexmock()
  34. )
  35. def test_use_streaming_false_for_no_databases():
  36. assert not module.use_streaming(databases=[], config=flexmock(), log_prefix=flexmock())
  37. def test_dump_data_sources_dumps_each_database():
  38. databases = [{'name': 'foo'}, {'name': 'bar'}]
  39. processes = [flexmock(), flexmock()]
  40. flexmock(module).should_receive('make_dump_path').and_return('')
  41. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  42. ('bar',)
  43. )
  44. for name, process in zip(('foo', 'bar'), processes):
  45. flexmock(module).should_receive('execute_dump_command').with_args(
  46. database={'name': name},
  47. log_prefix=object,
  48. dump_path=object,
  49. database_names=(name,),
  50. extra_environment=object,
  51. dry_run=object,
  52. dry_run_label=object,
  53. ).and_return(process).once()
  54. assert (
  55. module.dump_data_sources(
  56. databases,
  57. {},
  58. 'test.yaml',
  59. config_paths=('test.yaml',),
  60. borgmatic_runtime_directory='/run/borgmatic',
  61. source_directories=[],
  62. dry_run=False,
  63. )
  64. == processes
  65. )
  66. def test_dump_data_sources_dumps_with_password():
  67. database = {'name': 'foo', 'username': 'root', 'password': 'trustsome1'}
  68. process = flexmock()
  69. flexmock(module).should_receive('make_dump_path').and_return('')
  70. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  71. ('bar',)
  72. )
  73. flexmock(module).should_receive('execute_dump_command').with_args(
  74. database=database,
  75. log_prefix=object,
  76. dump_path=object,
  77. database_names=('foo',),
  78. extra_environment={'MYSQL_PWD': 'trustsome1'},
  79. dry_run=object,
  80. dry_run_label=object,
  81. ).and_return(process).once()
  82. assert module.dump_data_sources(
  83. [database],
  84. {},
  85. 'test.yaml',
  86. config_paths=('test.yaml',),
  87. borgmatic_runtime_directory='/run/borgmatic',
  88. source_directories=[],
  89. dry_run=False,
  90. ) == [process]
  91. def test_dump_data_sources_dumps_all_databases_at_once():
  92. databases = [{'name': 'all'}]
  93. process = flexmock()
  94. flexmock(module).should_receive('make_dump_path').and_return('')
  95. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  96. flexmock(module).should_receive('execute_dump_command').with_args(
  97. database={'name': 'all'},
  98. log_prefix=object,
  99. dump_path=object,
  100. database_names=('foo', 'bar'),
  101. extra_environment=object,
  102. dry_run=object,
  103. dry_run_label=object,
  104. ).and_return(process).once()
  105. assert module.dump_data_sources(
  106. databases,
  107. {},
  108. 'test.yaml',
  109. config_paths=('test.yaml',),
  110. borgmatic_runtime_directory='/run/borgmatic',
  111. source_directories=[],
  112. dry_run=False,
  113. ) == [process]
  114. def test_dump_data_sources_dumps_all_databases_separately_when_format_configured():
  115. databases = [{'name': 'all', 'format': 'sql'}]
  116. processes = [flexmock(), flexmock()]
  117. flexmock(module).should_receive('make_dump_path').and_return('')
  118. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  119. for name, process in zip(('foo', 'bar'), processes):
  120. flexmock(module).should_receive('execute_dump_command').with_args(
  121. database={'name': name, 'format': 'sql'},
  122. log_prefix=object,
  123. dump_path=object,
  124. database_names=(name,),
  125. extra_environment=object,
  126. dry_run=object,
  127. dry_run_label=object,
  128. ).and_return(process).once()
  129. assert (
  130. 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. )
  139. == processes
  140. )
  141. def test_database_names_to_dump_runs_mariadb_with_list_options():
  142. database = {'name': 'all', 'list_options': '--defaults-extra-file=mariadb.cnf'}
  143. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  144. (
  145. 'mariadb',
  146. '--defaults-extra-file=mariadb.cnf',
  147. '--skip-column-names',
  148. '--batch',
  149. '--execute',
  150. 'show schemas',
  151. ),
  152. extra_environment=None,
  153. ).and_return(('foo\nbar')).once()
  154. assert module.database_names_to_dump(database, None, 'test.yaml', '') == ('foo', 'bar')
  155. def test_database_names_to_dump_runs_non_default_mariadb_with_list_options():
  156. database = {
  157. 'name': 'all',
  158. 'list_options': '--defaults-extra-file=mariadb.cnf',
  159. 'mariadb_command': 'custom_mariadb',
  160. }
  161. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  162. extra_environment=None,
  163. full_command=(
  164. 'custom_mariadb', # Custom MariaDB command
  165. '--defaults-extra-file=mariadb.cnf',
  166. '--skip-column-names',
  167. '--batch',
  168. '--execute',
  169. 'show schemas',
  170. ),
  171. ).and_return(('foo\nbar')).once()
  172. assert module.database_names_to_dump(database, None, 'test.yaml', '') == ('foo', 'bar')
  173. def test_execute_dump_command_runs_mariadb_dump():
  174. process = flexmock()
  175. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  176. flexmock(module.os.path).should_receive('exists').and_return(False)
  177. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  178. flexmock(module).should_receive('execute_command').with_args(
  179. (
  180. 'mariadb-dump',
  181. '--add-drop-database',
  182. '--databases',
  183. 'foo',
  184. '--result-file',
  185. 'dump',
  186. ),
  187. extra_environment=None,
  188. run_to_completion=False,
  189. ).and_return(process).once()
  190. assert (
  191. module.execute_dump_command(
  192. database={'name': 'foo'},
  193. log_prefix='log',
  194. dump_path=flexmock(),
  195. database_names=('foo',),
  196. extra_environment=None,
  197. dry_run=False,
  198. dry_run_label='',
  199. )
  200. == process
  201. )
  202. def test_execute_dump_command_runs_mariadb_dump_without_add_drop_database():
  203. process = flexmock()
  204. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  205. flexmock(module.os.path).should_receive('exists').and_return(False)
  206. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  207. flexmock(module).should_receive('execute_command').with_args(
  208. (
  209. 'mariadb-dump',
  210. '--databases',
  211. 'foo',
  212. '--result-file',
  213. 'dump',
  214. ),
  215. extra_environment=None,
  216. run_to_completion=False,
  217. ).and_return(process).once()
  218. assert (
  219. module.execute_dump_command(
  220. database={'name': 'foo', 'add_drop_database': False},
  221. log_prefix='log',
  222. dump_path=flexmock(),
  223. database_names=('foo',),
  224. extra_environment=None,
  225. dry_run=False,
  226. dry_run_label='',
  227. )
  228. == process
  229. )
  230. def test_execute_dump_command_runs_mariadb_dump_with_hostname_and_port():
  231. process = flexmock()
  232. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  233. flexmock(module.os.path).should_receive('exists').and_return(False)
  234. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  235. flexmock(module).should_receive('execute_command').with_args(
  236. (
  237. 'mariadb-dump',
  238. '--add-drop-database',
  239. '--host',
  240. 'database.example.org',
  241. '--port',
  242. '5433',
  243. '--protocol',
  244. 'tcp',
  245. '--databases',
  246. 'foo',
  247. '--result-file',
  248. 'dump',
  249. ),
  250. extra_environment=None,
  251. run_to_completion=False,
  252. ).and_return(process).once()
  253. assert (
  254. module.execute_dump_command(
  255. database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},
  256. log_prefix='log',
  257. dump_path=flexmock(),
  258. database_names=('foo',),
  259. extra_environment=None,
  260. dry_run=False,
  261. dry_run_label='',
  262. )
  263. == process
  264. )
  265. def test_execute_dump_command_runs_mariadb_dump_with_username_and_password():
  266. process = flexmock()
  267. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  268. flexmock(module.os.path).should_receive('exists').and_return(False)
  269. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  270. flexmock(module).should_receive('execute_command').with_args(
  271. (
  272. 'mariadb-dump',
  273. '--add-drop-database',
  274. '--user',
  275. 'root',
  276. '--databases',
  277. 'foo',
  278. '--result-file',
  279. 'dump',
  280. ),
  281. extra_environment={'MYSQL_PWD': 'trustsome1'},
  282. run_to_completion=False,
  283. ).and_return(process).once()
  284. assert (
  285. module.execute_dump_command(
  286. database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},
  287. log_prefix='log',
  288. dump_path=flexmock(),
  289. database_names=('foo',),
  290. extra_environment={'MYSQL_PWD': 'trustsome1'},
  291. dry_run=False,
  292. dry_run_label='',
  293. )
  294. == process
  295. )
  296. def test_execute_dump_command_runs_mariadb_dump_with_options():
  297. process = flexmock()
  298. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  299. flexmock(module.os.path).should_receive('exists').and_return(False)
  300. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  301. flexmock(module).should_receive('execute_command').with_args(
  302. (
  303. 'mariadb-dump',
  304. '--stuff=such',
  305. '--add-drop-database',
  306. '--databases',
  307. 'foo',
  308. '--result-file',
  309. 'dump',
  310. ),
  311. extra_environment=None,
  312. run_to_completion=False,
  313. ).and_return(process).once()
  314. assert (
  315. module.execute_dump_command(
  316. database={'name': 'foo', 'options': '--stuff=such'},
  317. log_prefix='log',
  318. dump_path=flexmock(),
  319. database_names=('foo',),
  320. extra_environment=None,
  321. dry_run=False,
  322. dry_run_label='',
  323. )
  324. == process
  325. )
  326. def test_execute_dump_command_runs_non_default_mariadb_dump_with_options():
  327. process = flexmock()
  328. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  329. flexmock(module.os.path).should_receive('exists').and_return(False)
  330. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  331. flexmock(module).should_receive('execute_command').with_args(
  332. (
  333. 'custom_mariadb_dump', # Custom MariaDB dump command
  334. '--stuff=such',
  335. '--add-drop-database',
  336. '--databases',
  337. 'foo',
  338. '--result-file',
  339. 'dump',
  340. ),
  341. extra_environment=None,
  342. run_to_completion=False,
  343. ).and_return(process).once()
  344. assert (
  345. module.execute_dump_command(
  346. database={
  347. 'name': 'foo',
  348. 'mariadb_dump_command': 'custom_mariadb_dump',
  349. 'options': '--stuff=such',
  350. }, # Custom MariaDB dump command specified
  351. log_prefix='log',
  352. dump_path=flexmock(),
  353. database_names=('foo',),
  354. extra_environment=None,
  355. dry_run=False,
  356. dry_run_label='',
  357. )
  358. == process
  359. )
  360. def test_execute_dump_command_with_duplicate_dump_skips_mariadb_dump():
  361. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  362. flexmock(module.os.path).should_receive('exists').and_return(True)
  363. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  364. flexmock(module).should_receive('execute_command').never()
  365. assert (
  366. module.execute_dump_command(
  367. database={'name': 'foo'},
  368. log_prefix='log',
  369. dump_path=flexmock(),
  370. database_names=('foo',),
  371. extra_environment=None,
  372. dry_run=True,
  373. dry_run_label='SO DRY',
  374. )
  375. is None
  376. )
  377. def test_execute_dump_command_with_dry_run_skips_mariadb_dump():
  378. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  379. flexmock(module.os.path).should_receive('exists').and_return(False)
  380. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  381. flexmock(module).should_receive('execute_command').never()
  382. assert (
  383. module.execute_dump_command(
  384. database={'name': 'foo'},
  385. log_prefix='log',
  386. dump_path=flexmock(),
  387. database_names=('foo',),
  388. extra_environment=None,
  389. dry_run=True,
  390. dry_run_label='SO DRY',
  391. )
  392. is None
  393. )
  394. def test_dump_data_sources_errors_for_missing_all_databases():
  395. databases = [{'name': 'all'}]
  396. flexmock(module).should_receive('make_dump_path').and_return('')
  397. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  398. 'databases/localhost/all'
  399. )
  400. flexmock(module).should_receive('database_names_to_dump').and_return(())
  401. with pytest.raises(ValueError):
  402. assert module.dump_data_sources(
  403. databases,
  404. {},
  405. 'test.yaml',
  406. config_paths=('test.yaml',),
  407. borgmatic_runtime_directory='/run/borgmatic',
  408. source_directories=[],
  409. dry_run=False,
  410. )
  411. def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
  412. databases = [{'name': 'all'}]
  413. flexmock(module).should_receive('make_dump_path').and_return('')
  414. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  415. 'databases/localhost/all'
  416. )
  417. flexmock(module).should_receive('database_names_to_dump').and_return(())
  418. assert (
  419. module.dump_data_sources(
  420. databases,
  421. {},
  422. 'test.yaml',
  423. config_paths=('test.yaml',),
  424. borgmatic_runtime_directory='/run/borgmatic',
  425. source_directories=[],
  426. dry_run=True,
  427. )
  428. == []
  429. )
  430. def test_restore_data_source_dump_runs_mariadb_to_restore():
  431. hook_config = [{'name': 'foo'}, {'name': 'bar'}]
  432. extract_process = flexmock(stdout=flexmock())
  433. flexmock(module).should_receive('execute_command_with_processes').with_args(
  434. ('mariadb', '--batch'),
  435. processes=[extract_process],
  436. output_log_level=logging.DEBUG,
  437. input_file=extract_process.stdout,
  438. extra_environment=None,
  439. ).once()
  440. module.restore_data_source_dump(
  441. hook_config,
  442. {},
  443. 'test.yaml',
  444. data_source={'name': 'foo'},
  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_data_source_dump_runs_mariadb_with_options():
  456. hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
  457. extract_process = flexmock(stdout=flexmock())
  458. flexmock(module).should_receive('execute_command_with_processes').with_args(
  459. ('mariadb', '--batch', '--harder'),
  460. processes=[extract_process],
  461. output_log_level=logging.DEBUG,
  462. input_file=extract_process.stdout,
  463. extra_environment=None,
  464. ).once()
  465. module.restore_data_source_dump(
  466. hook_config,
  467. {},
  468. 'test.yaml',
  469. data_source=hook_config[0],
  470. dry_run=False,
  471. extract_process=extract_process,
  472. connection_params={
  473. 'hostname': None,
  474. 'port': None,
  475. 'username': None,
  476. 'password': None,
  477. },
  478. borgmatic_runtime_directory='/run/borgmatic',
  479. )
  480. def test_restore_data_source_dump_runs_non_default_mariadb_with_options():
  481. hook_config = [
  482. {'name': 'foo', 'restore_options': '--harder', 'mariadb_command': 'custom_mariadb'}
  483. ]
  484. extract_process = flexmock(stdout=flexmock())
  485. flexmock(module).should_receive('execute_command_with_processes').with_args(
  486. ('custom_mariadb', '--batch', '--harder'),
  487. processes=[extract_process],
  488. output_log_level=logging.DEBUG,
  489. input_file=extract_process.stdout,
  490. extra_environment=None,
  491. ).once()
  492. module.restore_data_source_dump(
  493. hook_config,
  494. {},
  495. 'test.yaml',
  496. data_source=hook_config[0],
  497. dry_run=False,
  498. extract_process=extract_process,
  499. connection_params={
  500. 'hostname': None,
  501. 'port': None,
  502. 'username': None,
  503. 'password': None,
  504. },
  505. borgmatic_runtime_directory='/run/borgmatic',
  506. )
  507. def test_restore_data_source_dump_runs_mariadb_with_hostname_and_port():
  508. hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  509. extract_process = flexmock(stdout=flexmock())
  510. flexmock(module).should_receive('execute_command_with_processes').with_args(
  511. (
  512. 'mariadb',
  513. '--batch',
  514. '--host',
  515. 'database.example.org',
  516. '--port',
  517. '5433',
  518. '--protocol',
  519. 'tcp',
  520. ),
  521. processes=[extract_process],
  522. output_log_level=logging.DEBUG,
  523. input_file=extract_process.stdout,
  524. extra_environment=None,
  525. ).once()
  526. module.restore_data_source_dump(
  527. hook_config,
  528. {},
  529. 'test.yaml',
  530. data_source=hook_config[0],
  531. dry_run=False,
  532. extract_process=extract_process,
  533. connection_params={
  534. 'hostname': None,
  535. 'port': None,
  536. 'username': None,
  537. 'password': None,
  538. },
  539. borgmatic_runtime_directory='/run/borgmatic',
  540. )
  541. def test_restore_data_source_dump_runs_mariadb_with_username_and_password():
  542. hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  543. extract_process = flexmock(stdout=flexmock())
  544. flexmock(module).should_receive('execute_command_with_processes').with_args(
  545. ('mariadb', '--batch', '--user', 'root'),
  546. processes=[extract_process],
  547. output_log_level=logging.DEBUG,
  548. input_file=extract_process.stdout,
  549. extra_environment={'MYSQL_PWD': 'trustsome1'},
  550. ).once()
  551. module.restore_data_source_dump(
  552. hook_config,
  553. {},
  554. 'test.yaml',
  555. data_source=hook_config[0],
  556. dry_run=False,
  557. extract_process=extract_process,
  558. connection_params={
  559. 'hostname': None,
  560. 'port': None,
  561. 'username': None,
  562. 'password': None,
  563. },
  564. borgmatic_runtime_directory='/run/borgmatic',
  565. )
  566. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  567. hook_config = [
  568. {
  569. 'name': 'foo',
  570. 'username': 'root',
  571. 'password': 'trustsome1',
  572. 'restore_hostname': 'restorehost',
  573. 'restore_port': 'restoreport',
  574. 'restore_username': 'restoreusername',
  575. 'restore_password': 'restorepassword',
  576. }
  577. ]
  578. extract_process = flexmock(stdout=flexmock())
  579. flexmock(module).should_receive('execute_command_with_processes').with_args(
  580. (
  581. 'mariadb',
  582. '--batch',
  583. '--host',
  584. 'clihost',
  585. '--port',
  586. 'cliport',
  587. '--protocol',
  588. 'tcp',
  589. '--user',
  590. 'cliusername',
  591. ),
  592. processes=[extract_process],
  593. output_log_level=logging.DEBUG,
  594. input_file=extract_process.stdout,
  595. extra_environment={'MYSQL_PWD': 'clipassword'},
  596. ).once()
  597. module.restore_data_source_dump(
  598. hook_config,
  599. {},
  600. 'test.yaml',
  601. data_source=hook_config[0],
  602. dry_run=False,
  603. extract_process=extract_process,
  604. connection_params={
  605. 'hostname': 'clihost',
  606. 'port': 'cliport',
  607. 'username': 'cliusername',
  608. 'password': 'clipassword',
  609. },
  610. borgmatic_runtime_directory='/run/borgmatic',
  611. )
  612. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  613. hook_config = [
  614. {
  615. 'name': 'foo',
  616. 'username': 'root',
  617. 'password': 'trustsome1',
  618. 'hostname': 'dbhost',
  619. 'port': 'dbport',
  620. 'restore_username': 'restoreuser',
  621. 'restore_password': 'restorepass',
  622. 'restore_hostname': 'restorehost',
  623. 'restore_port': 'restoreport',
  624. }
  625. ]
  626. extract_process = flexmock(stdout=flexmock())
  627. flexmock(module).should_receive('execute_command_with_processes').with_args(
  628. (
  629. 'mariadb',
  630. '--batch',
  631. '--host',
  632. 'restorehost',
  633. '--port',
  634. 'restoreport',
  635. '--protocol',
  636. 'tcp',
  637. '--user',
  638. 'restoreuser',
  639. ),
  640. processes=[extract_process],
  641. output_log_level=logging.DEBUG,
  642. input_file=extract_process.stdout,
  643. extra_environment={'MYSQL_PWD': 'restorepass'},
  644. ).once()
  645. module.restore_data_source_dump(
  646. hook_config,
  647. {},
  648. 'test.yaml',
  649. data_source=hook_config[0],
  650. dry_run=False,
  651. extract_process=extract_process,
  652. connection_params={
  653. 'hostname': None,
  654. 'port': None,
  655. 'username': None,
  656. 'password': None,
  657. },
  658. borgmatic_runtime_directory='/run/borgmatic',
  659. )
  660. def test_restore_data_source_dump_with_dry_run_skips_restore():
  661. hook_config = [{'name': 'foo'}]
  662. flexmock(module).should_receive('execute_command_with_processes').never()
  663. module.restore_data_source_dump(
  664. hook_config,
  665. {},
  666. 'test.yaml',
  667. data_source={'name': 'foo'},
  668. dry_run=True,
  669. extract_process=flexmock(),
  670. connection_params={
  671. 'hostname': None,
  672. 'port': None,
  673. 'username': None,
  674. 'password': None,
  675. },
  676. borgmatic_runtime_directory='/run/borgmatic',
  677. )