test_mysql.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks.data_source import mysql 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_mysql_for_database_names():
  21. extra_environment = flexmock()
  22. log_prefix = ''
  23. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  24. ('mysql', '--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. patterns=[],
  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. patterns=[],
  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. patterns=[],
  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. patterns=[],
  137. dry_run=False,
  138. )
  139. == processes
  140. )
  141. def test_database_names_to_dump_runs_mysql_with_list_options():
  142. database = {'name': 'all', 'list_options': '--defaults-extra-file=my.cnf'}
  143. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  144. (
  145. 'mysql',
  146. '--defaults-extra-file=my.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_mysql_with_list_options():
  156. database = {
  157. 'name': 'all',
  158. 'list_options': '--defaults-extra-file=my.cnf',
  159. 'mysql_command': 'custom_mysql',
  160. }
  161. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  162. extra_environment=None,
  163. full_command=(
  164. 'custom_mysql', # Custom MySQL command
  165. '--defaults-extra-file=my.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_mysqldump():
  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. 'mysqldump',
  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_mysqldump_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. 'mysqldump',
  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_mysqldump_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. 'mysqldump',
  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_mysqldump_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. 'mysqldump',
  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_mysqldump_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. 'mysqldump',
  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_mysqldump():
  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_mysqldump', # Custom MySQL dump command
  334. '--add-drop-database',
  335. '--databases',
  336. 'foo',
  337. '--result-file',
  338. 'dump',
  339. ),
  340. extra_environment=None,
  341. run_to_completion=False,
  342. ).and_return(process).once()
  343. assert (
  344. module.execute_dump_command(
  345. database={
  346. 'name': 'foo',
  347. 'mysql_dump_command': 'custom_mysqldump',
  348. }, # Custom MySQL dump command specified
  349. log_prefix='log',
  350. dump_path=flexmock(),
  351. database_names=('foo',),
  352. extra_environment=None,
  353. dry_run=False,
  354. dry_run_label='',
  355. )
  356. == process
  357. )
  358. def test_execute_dump_command_with_duplicate_dump_skips_mysqldump():
  359. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  360. flexmock(module.os.path).should_receive('exists').and_return(True)
  361. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  362. flexmock(module).should_receive('execute_command').never()
  363. assert (
  364. module.execute_dump_command(
  365. database={'name': 'foo'},
  366. log_prefix='log',
  367. dump_path=flexmock(),
  368. database_names=('foo',),
  369. extra_environment=None,
  370. dry_run=True,
  371. dry_run_label='SO DRY',
  372. )
  373. is None
  374. )
  375. def test_execute_dump_command_with_dry_run_skips_mysqldump():
  376. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  377. flexmock(module.os.path).should_receive('exists').and_return(False)
  378. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  379. flexmock(module).should_receive('execute_command').never()
  380. assert (
  381. module.execute_dump_command(
  382. database={'name': 'foo'},
  383. log_prefix='log',
  384. dump_path=flexmock(),
  385. database_names=('foo',),
  386. extra_environment=None,
  387. dry_run=True,
  388. dry_run_label='SO DRY',
  389. )
  390. is None
  391. )
  392. def test_dump_data_sources_errors_for_missing_all_databases():
  393. databases = [{'name': 'all'}]
  394. flexmock(module).should_receive('make_dump_path').and_return('')
  395. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  396. 'databases/localhost/all'
  397. )
  398. flexmock(module).should_receive('database_names_to_dump').and_return(())
  399. with pytest.raises(ValueError):
  400. assert module.dump_data_sources(
  401. databases,
  402. {},
  403. 'test.yaml',
  404. config_paths=('test.yaml',),
  405. borgmatic_runtime_directory='/run/borgmatic',
  406. patterns=[],
  407. dry_run=False,
  408. )
  409. def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
  410. databases = [{'name': 'all'}]
  411. flexmock(module).should_receive('make_dump_path').and_return('')
  412. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  413. 'databases/localhost/all'
  414. )
  415. flexmock(module).should_receive('database_names_to_dump').and_return(())
  416. assert (
  417. module.dump_data_sources(
  418. databases,
  419. {},
  420. 'test.yaml',
  421. config_paths=('test.yaml',),
  422. borgmatic_runtime_directory='/run/borgmatic',
  423. patterns=[],
  424. dry_run=True,
  425. )
  426. == []
  427. )
  428. def test_restore_data_source_dump_runs_mysql_to_restore():
  429. hook_config = [{'name': 'foo'}, {'name': 'bar'}]
  430. extract_process = flexmock(stdout=flexmock())
  431. flexmock(module).should_receive('execute_command_with_processes').with_args(
  432. ('mysql', '--batch'),
  433. processes=[extract_process],
  434. output_log_level=logging.DEBUG,
  435. input_file=extract_process.stdout,
  436. extra_environment=None,
  437. ).once()
  438. module.restore_data_source_dump(
  439. hook_config,
  440. {},
  441. 'test.yaml',
  442. data_source={'name': 'foo'},
  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_data_source_dump_runs_mysql_with_options():
  454. hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
  455. extract_process = flexmock(stdout=flexmock())
  456. flexmock(module).should_receive('execute_command_with_processes').with_args(
  457. ('mysql', '--batch', '--harder'),
  458. processes=[extract_process],
  459. output_log_level=logging.DEBUG,
  460. input_file=extract_process.stdout,
  461. extra_environment=None,
  462. ).once()
  463. module.restore_data_source_dump(
  464. hook_config,
  465. {},
  466. 'test.yaml',
  467. data_source=hook_config[0],
  468. dry_run=False,
  469. extract_process=extract_process,
  470. connection_params={
  471. 'hostname': None,
  472. 'port': None,
  473. 'username': None,
  474. 'password': None,
  475. },
  476. borgmatic_runtime_directory='/run/borgmatic',
  477. )
  478. def test_restore_data_source_dump_runs_non_default_mysql_with_options():
  479. hook_config = [{'name': 'foo', 'mysql_command': 'custom_mysql', 'restore_options': '--harder'}]
  480. extract_process = flexmock(stdout=flexmock())
  481. flexmock(module).should_receive('execute_command_with_processes').with_args(
  482. ('custom_mysql', '--batch', '--harder'),
  483. processes=[extract_process],
  484. output_log_level=logging.DEBUG,
  485. input_file=extract_process.stdout,
  486. extra_environment=None,
  487. ).once()
  488. module.restore_data_source_dump(
  489. hook_config,
  490. {},
  491. 'test.yaml',
  492. data_source=hook_config[0],
  493. dry_run=False,
  494. extract_process=extract_process,
  495. connection_params={
  496. 'hostname': None,
  497. 'port': None,
  498. 'username': None,
  499. 'password': None,
  500. },
  501. borgmatic_runtime_directory='/run/borgmatic',
  502. )
  503. def test_restore_data_source_dump_runs_mysql_with_hostname_and_port():
  504. hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  505. extract_process = flexmock(stdout=flexmock())
  506. flexmock(module).should_receive('execute_command_with_processes').with_args(
  507. (
  508. 'mysql',
  509. '--batch',
  510. '--host',
  511. 'database.example.org',
  512. '--port',
  513. '5433',
  514. '--protocol',
  515. 'tcp',
  516. ),
  517. processes=[extract_process],
  518. output_log_level=logging.DEBUG,
  519. input_file=extract_process.stdout,
  520. extra_environment=None,
  521. ).once()
  522. module.restore_data_source_dump(
  523. hook_config,
  524. {},
  525. 'test.yaml',
  526. data_source=hook_config[0],
  527. dry_run=False,
  528. extract_process=extract_process,
  529. connection_params={
  530. 'hostname': None,
  531. 'port': None,
  532. 'username': None,
  533. 'password': None,
  534. },
  535. borgmatic_runtime_directory='/run/borgmatic',
  536. )
  537. def test_restore_data_source_dump_runs_mysql_with_username_and_password():
  538. hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  539. extract_process = flexmock(stdout=flexmock())
  540. flexmock(module).should_receive('execute_command_with_processes').with_args(
  541. ('mysql', '--batch', '--user', 'root'),
  542. processes=[extract_process],
  543. output_log_level=logging.DEBUG,
  544. input_file=extract_process.stdout,
  545. extra_environment={'MYSQL_PWD': 'trustsome1'},
  546. ).once()
  547. module.restore_data_source_dump(
  548. hook_config,
  549. {},
  550. 'test.yaml',
  551. data_source=hook_config[0],
  552. dry_run=False,
  553. extract_process=extract_process,
  554. connection_params={
  555. 'hostname': None,
  556. 'port': None,
  557. 'username': None,
  558. 'password': None,
  559. },
  560. borgmatic_runtime_directory='/run/borgmatic',
  561. )
  562. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  563. hook_config = [
  564. {
  565. 'name': 'foo',
  566. 'username': 'root',
  567. 'password': 'trustsome1',
  568. 'restore_hostname': 'restorehost',
  569. 'restore_port': 'restoreport',
  570. 'restore_username': 'restoreusername',
  571. 'restore_password': 'restorepassword',
  572. }
  573. ]
  574. extract_process = flexmock(stdout=flexmock())
  575. flexmock(module).should_receive('execute_command_with_processes').with_args(
  576. (
  577. 'mysql',
  578. '--batch',
  579. '--host',
  580. 'clihost',
  581. '--port',
  582. 'cliport',
  583. '--protocol',
  584. 'tcp',
  585. '--user',
  586. 'cliusername',
  587. ),
  588. processes=[extract_process],
  589. output_log_level=logging.DEBUG,
  590. input_file=extract_process.stdout,
  591. extra_environment={'MYSQL_PWD': 'clipassword'},
  592. ).once()
  593. module.restore_data_source_dump(
  594. hook_config,
  595. {},
  596. 'test.yaml',
  597. data_source={'name': 'foo'},
  598. dry_run=False,
  599. extract_process=extract_process,
  600. connection_params={
  601. 'hostname': 'clihost',
  602. 'port': 'cliport',
  603. 'username': 'cliusername',
  604. 'password': 'clipassword',
  605. },
  606. borgmatic_runtime_directory='/run/borgmatic',
  607. )
  608. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  609. hook_config = [
  610. {
  611. 'name': 'foo',
  612. 'username': 'root',
  613. 'password': 'trustsome1',
  614. 'hostname': 'dbhost',
  615. 'port': 'dbport',
  616. 'restore_username': 'restoreuser',
  617. 'restore_password': 'restorepass',
  618. 'restore_hostname': 'restorehost',
  619. 'restore_port': 'restoreport',
  620. }
  621. ]
  622. extract_process = flexmock(stdout=flexmock())
  623. flexmock(module).should_receive('execute_command_with_processes').with_args(
  624. (
  625. 'mysql',
  626. '--batch',
  627. '--host',
  628. 'restorehost',
  629. '--port',
  630. 'restoreport',
  631. '--protocol',
  632. 'tcp',
  633. '--user',
  634. 'restoreuser',
  635. ),
  636. processes=[extract_process],
  637. output_log_level=logging.DEBUG,
  638. input_file=extract_process.stdout,
  639. extra_environment={'MYSQL_PWD': 'restorepass'},
  640. ).once()
  641. module.restore_data_source_dump(
  642. hook_config,
  643. {},
  644. 'test.yaml',
  645. data_source=hook_config[0],
  646. dry_run=False,
  647. extract_process=extract_process,
  648. connection_params={
  649. 'hostname': None,
  650. 'port': None,
  651. 'username': None,
  652. 'password': None,
  653. },
  654. borgmatic_runtime_directory='/run/borgmatic',
  655. )
  656. def test_restore_data_source_dump_with_dry_run_skips_restore():
  657. hook_config = [{'name': 'foo'}]
  658. flexmock(module).should_receive('execute_command_with_processes').never()
  659. module.restore_data_source_dump(
  660. hook_config,
  661. {},
  662. 'test.yaml',
  663. data_source={'name': 'foo'},
  664. dry_run=True,
  665. extract_process=flexmock(),
  666. connection_params={
  667. 'hostname': None,
  668. 'port': None,
  669. 'username': None,
  670. 'password': None,
  671. },
  672. borgmatic_runtime_directory='/run/borgmatic',
  673. )