test_mysql.py 25 KB

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