test_mariadb.py 26 KB

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