test_mariadb.py 27 KB

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