test_mariadb.py 24 KB

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