test_mariadb.py 26 KB

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