test_mysql.py 23 KB

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