test_mysql.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. 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.borgmatic.hooks.credential.parse).should_receive(
  12. 'resolve_credential'
  13. ).replace_with(lambda value, config: value)
  14. flexmock(module).should_receive('execute_command_and_capture_output').never()
  15. names = module.database_names_to_dump({'name': 'all'}, {}, environment, dry_run=True)
  16. assert names == ()
  17. def test_database_names_to_dump_queries_mysql_for_database_names():
  18. environment = flexmock()
  19. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  20. 'resolve_credential'
  21. ).replace_with(lambda value, config: value)
  22. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  23. ('mysql', '--skip-column-names', '--batch', '--execute', 'show schemas'),
  24. environment=environment,
  25. ).and_return('foo\nbar\nmysql\n').once()
  26. names = module.database_names_to_dump({'name': 'all'}, {}, 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.os).should_receive('environ').and_return({'USER': 'root'})
  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).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. environment={'USER': 'root'},
  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.os).should_receive('environ').and_return({'USER': 'root'})
  120. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  121. for name, process in zip(('foo', 'bar'), processes):
  122. flexmock(module).should_receive('execute_dump_command').with_args(
  123. database={'name': name, 'format': 'sql'},
  124. config={},
  125. dump_path=object,
  126. database_names=(name,),
  127. environment={'USER': 'root'},
  128. dry_run=object,
  129. dry_run_label=object,
  130. ).and_return(process).once()
  131. assert (
  132. module.dump_data_sources(
  133. databases,
  134. {},
  135. config_paths=('test.yaml',),
  136. borgmatic_runtime_directory='/run/borgmatic',
  137. patterns=[],
  138. dry_run=False,
  139. )
  140. == processes
  141. )
  142. def test_database_names_to_dump_runs_mysql_with_list_options():
  143. database = {'name': 'all', 'list_options': '--defaults-extra-file=my.cnf'}
  144. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  145. (
  146. 'mysql',
  147. '--defaults-extra-file=my.cnf',
  148. '--skip-column-names',
  149. '--batch',
  150. '--execute',
  151. 'show schemas',
  152. ),
  153. environment=None,
  154. ).and_return(('foo\nbar')).once()
  155. assert module.database_names_to_dump(database, {}, None, '') == ('foo', 'bar')
  156. def test_database_names_to_dump_runs_non_default_mysql_with_list_options():
  157. database = {
  158. 'name': 'all',
  159. 'list_options': '--defaults-extra-file=my.cnf',
  160. 'mysql_command': 'custom_mysql',
  161. }
  162. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  163. environment=None,
  164. full_command=(
  165. 'custom_mysql', # Custom MySQL command
  166. '--defaults-extra-file=my.cnf',
  167. '--skip-column-names',
  168. '--batch',
  169. '--execute',
  170. 'show schemas',
  171. ),
  172. ).and_return(('foo\nbar')).once()
  173. assert module.database_names_to_dump(database, {}, None, '') == ('foo', 'bar')
  174. def test_execute_dump_command_runs_mysqldump():
  175. process = flexmock()
  176. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  177. flexmock(module.os.path).should_receive('exists').and_return(False)
  178. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  179. 'resolve_credential'
  180. ).replace_with(lambda value, config: value)
  181. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  182. flexmock(module).should_receive('execute_command').with_args(
  183. (
  184. 'mysqldump',
  185. '--add-drop-database',
  186. '--databases',
  187. 'foo',
  188. '--result-file',
  189. 'dump',
  190. ),
  191. environment=None,
  192. run_to_completion=False,
  193. ).and_return(process).once()
  194. assert (
  195. module.execute_dump_command(
  196. database={'name': 'foo'},
  197. config={},
  198. dump_path=flexmock(),
  199. database_names=('foo',),
  200. environment=None,
  201. dry_run=False,
  202. dry_run_label='',
  203. )
  204. == process
  205. )
  206. def test_execute_dump_command_runs_mysqldump_without_add_drop_database():
  207. process = flexmock()
  208. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  209. flexmock(module.os.path).should_receive('exists').and_return(False)
  210. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  211. 'resolve_credential'
  212. ).replace_with(lambda value, config: value)
  213. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  214. flexmock(module).should_receive('execute_command').with_args(
  215. (
  216. 'mysqldump',
  217. '--databases',
  218. 'foo',
  219. '--result-file',
  220. 'dump',
  221. ),
  222. environment=None,
  223. run_to_completion=False,
  224. ).and_return(process).once()
  225. assert (
  226. module.execute_dump_command(
  227. database={'name': 'foo', 'add_drop_database': False},
  228. config={},
  229. dump_path=flexmock(),
  230. database_names=('foo',),
  231. environment=None,
  232. dry_run=False,
  233. dry_run_label='',
  234. )
  235. == process
  236. )
  237. def test_execute_dump_command_runs_mysqldump_with_hostname_and_port():
  238. process = flexmock()
  239. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  240. flexmock(module.os.path).should_receive('exists').and_return(False)
  241. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  242. 'resolve_credential'
  243. ).replace_with(lambda value, config: value)
  244. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  245. flexmock(module).should_receive('execute_command').with_args(
  246. (
  247. 'mysqldump',
  248. '--add-drop-database',
  249. '--host',
  250. 'database.example.org',
  251. '--port',
  252. '5433',
  253. '--protocol',
  254. 'tcp',
  255. '--databases',
  256. 'foo',
  257. '--result-file',
  258. 'dump',
  259. ),
  260. environment=None,
  261. run_to_completion=False,
  262. ).and_return(process).once()
  263. assert (
  264. module.execute_dump_command(
  265. database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},
  266. config={},
  267. dump_path=flexmock(),
  268. database_names=('foo',),
  269. environment=None,
  270. dry_run=False,
  271. dry_run_label='',
  272. )
  273. == process
  274. )
  275. def test_execute_dump_command_runs_mysqldump_with_username_and_password():
  276. process = flexmock()
  277. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  278. flexmock(module.os.path).should_receive('exists').and_return(False)
  279. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  280. 'resolve_credential'
  281. ).replace_with(lambda value, config: value)
  282. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  283. flexmock(module).should_receive('execute_command').with_args(
  284. (
  285. 'mysqldump',
  286. '--add-drop-database',
  287. '--user',
  288. 'root',
  289. '--databases',
  290. 'foo',
  291. '--result-file',
  292. 'dump',
  293. ),
  294. environment={'MYSQL_PWD': 'trustsome1'},
  295. run_to_completion=False,
  296. ).and_return(process).once()
  297. assert (
  298. module.execute_dump_command(
  299. database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},
  300. config={},
  301. dump_path=flexmock(),
  302. database_names=('foo',),
  303. environment={'MYSQL_PWD': 'trustsome1'},
  304. dry_run=False,
  305. dry_run_label='',
  306. )
  307. == process
  308. )
  309. def test_execute_dump_command_runs_mysqldump_with_options():
  310. process = flexmock()
  311. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  312. flexmock(module.os.path).should_receive('exists').and_return(False)
  313. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  314. 'resolve_credential'
  315. ).replace_with(lambda value, config: value)
  316. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  317. flexmock(module).should_receive('execute_command').with_args(
  318. (
  319. 'mysqldump',
  320. '--stuff=such',
  321. '--add-drop-database',
  322. '--databases',
  323. 'foo',
  324. '--result-file',
  325. 'dump',
  326. ),
  327. environment=None,
  328. run_to_completion=False,
  329. ).and_return(process).once()
  330. assert (
  331. module.execute_dump_command(
  332. database={'name': 'foo', 'options': '--stuff=such'},
  333. config={},
  334. dump_path=flexmock(),
  335. database_names=('foo',),
  336. environment=None,
  337. dry_run=False,
  338. dry_run_label='',
  339. )
  340. == process
  341. )
  342. def test_execute_dump_command_runs_non_default_mysqldump():
  343. process = flexmock()
  344. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  345. flexmock(module.os.path).should_receive('exists').and_return(False)
  346. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  347. 'resolve_credential'
  348. ).replace_with(lambda value, config: value)
  349. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  350. flexmock(module).should_receive('execute_command').with_args(
  351. (
  352. 'custom_mysqldump', # Custom MySQL dump command
  353. '--add-drop-database',
  354. '--databases',
  355. 'foo',
  356. '--result-file',
  357. 'dump',
  358. ),
  359. environment=None,
  360. run_to_completion=False,
  361. ).and_return(process).once()
  362. assert (
  363. module.execute_dump_command(
  364. database={
  365. 'name': 'foo',
  366. 'mysql_dump_command': 'custom_mysqldump',
  367. }, # Custom MySQL dump command specified
  368. config={},
  369. dump_path=flexmock(),
  370. database_names=('foo',),
  371. environment=None,
  372. dry_run=False,
  373. dry_run_label='',
  374. )
  375. == process
  376. )
  377. def test_execute_dump_command_with_duplicate_dump_skips_mysqldump():
  378. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  379. flexmock(module.os.path).should_receive('exists').and_return(True)
  380. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  381. flexmock(module).should_receive('execute_command').never()
  382. assert (
  383. module.execute_dump_command(
  384. database={'name': 'foo'},
  385. config={},
  386. dump_path=flexmock(),
  387. database_names=('foo',),
  388. environment=None,
  389. dry_run=True,
  390. dry_run_label='SO DRY',
  391. )
  392. is None
  393. )
  394. def test_execute_dump_command_with_dry_run_skips_mysqldump():
  395. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  396. flexmock(module.os.path).should_receive('exists').and_return(False)
  397. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  398. 'resolve_credential'
  399. ).replace_with(lambda value, config: value)
  400. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  401. flexmock(module).should_receive('execute_command').never()
  402. assert (
  403. module.execute_dump_command(
  404. database={'name': 'foo'},
  405. config={},
  406. dump_path=flexmock(),
  407. database_names=('foo',),
  408. environment=None,
  409. dry_run=True,
  410. dry_run_label='SO DRY',
  411. )
  412. is None
  413. )
  414. def test_dump_data_sources_errors_for_missing_all_databases():
  415. databases = [{'name': 'all'}]
  416. flexmock(module).should_receive('make_dump_path').and_return('')
  417. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  418. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  419. 'resolve_credential'
  420. ).replace_with(lambda value, config: value)
  421. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  422. 'databases/localhost/all'
  423. )
  424. flexmock(module).should_receive('database_names_to_dump').and_return(())
  425. with pytest.raises(ValueError):
  426. assert module.dump_data_sources(
  427. databases,
  428. {},
  429. config_paths=('test.yaml',),
  430. borgmatic_runtime_directory='/run/borgmatic',
  431. patterns=[],
  432. dry_run=False,
  433. )
  434. def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
  435. databases = [{'name': 'all'}]
  436. flexmock(module).should_receive('make_dump_path').and_return('')
  437. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  438. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  439. 'resolve_credential'
  440. ).replace_with(lambda value, config: value)
  441. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  442. 'databases/localhost/all'
  443. )
  444. flexmock(module).should_receive('database_names_to_dump').and_return(())
  445. assert (
  446. module.dump_data_sources(
  447. databases,
  448. {},
  449. config_paths=('test.yaml',),
  450. borgmatic_runtime_directory='/run/borgmatic',
  451. patterns=[],
  452. dry_run=True,
  453. )
  454. == []
  455. )
  456. def test_restore_data_source_dump_runs_mysql_to_restore():
  457. hook_config = [{'name': 'foo'}, {'name': 'bar'}]
  458. extract_process = flexmock(stdout=flexmock())
  459. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  460. 'resolve_credential'
  461. ).replace_with(lambda value, config: value)
  462. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  463. flexmock(module).should_receive('execute_command_with_processes').with_args(
  464. ('mysql', '--batch'),
  465. processes=[extract_process],
  466. output_log_level=logging.DEBUG,
  467. input_file=extract_process.stdout,
  468. environment={'USER': 'root'},
  469. ).once()
  470. module.restore_data_source_dump(
  471. hook_config,
  472. {},
  473. data_source={'name': 'foo'},
  474. dry_run=False,
  475. extract_process=extract_process,
  476. connection_params={
  477. 'hostname': None,
  478. 'port': None,
  479. 'username': None,
  480. 'password': None,
  481. },
  482. borgmatic_runtime_directory='/run/borgmatic',
  483. )
  484. def test_restore_data_source_dump_runs_mysql_with_options():
  485. hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
  486. extract_process = flexmock(stdout=flexmock())
  487. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  488. 'resolve_credential'
  489. ).replace_with(lambda value, config: value)
  490. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  491. flexmock(module).should_receive('execute_command_with_processes').with_args(
  492. ('mysql', '--batch', '--harder'),
  493. processes=[extract_process],
  494. output_log_level=logging.DEBUG,
  495. input_file=extract_process.stdout,
  496. environment={'USER': 'root'},
  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_mysql_with_options():
  513. hook_config = [{'name': 'foo', 'mysql_command': 'custom_mysql', 'restore_options': '--harder'}]
  514. extract_process = flexmock(stdout=flexmock())
  515. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  516. 'resolve_credential'
  517. ).replace_with(lambda value, config: value)
  518. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  519. flexmock(module).should_receive('execute_command_with_processes').with_args(
  520. ('custom_mysql', '--batch', '--harder'),
  521. processes=[extract_process],
  522. output_log_level=logging.DEBUG,
  523. input_file=extract_process.stdout,
  524. environment={'USER': 'root'},
  525. ).once()
  526. module.restore_data_source_dump(
  527. hook_config,
  528. {},
  529. data_source=hook_config[0],
  530. dry_run=False,
  531. extract_process=extract_process,
  532. connection_params={
  533. 'hostname': None,
  534. 'port': None,
  535. 'username': None,
  536. 'password': None,
  537. },
  538. borgmatic_runtime_directory='/run/borgmatic',
  539. )
  540. def test_restore_data_source_dump_runs_mysql_with_hostname_and_port():
  541. hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  542. extract_process = flexmock(stdout=flexmock())
  543. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  544. 'resolve_credential'
  545. ).replace_with(lambda value, config: value)
  546. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  547. flexmock(module).should_receive('execute_command_with_processes').with_args(
  548. (
  549. 'mysql',
  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. environment={'USER': 'root'},
  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_mysql_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.os).should_receive('environ').and_return({'USER': 'root'})
  584. flexmock(module).should_receive('execute_command_with_processes').with_args(
  585. ('mysql', '--batch', '--user', 'root'),
  586. processes=[extract_process],
  587. output_log_level=logging.DEBUG,
  588. input_file=extract_process.stdout,
  589. environment={'USER': 'root', 'MYSQL_PWD': 'trustsome1'},
  590. ).once()
  591. module.restore_data_source_dump(
  592. hook_config,
  593. {},
  594. data_source=hook_config[0],
  595. dry_run=False,
  596. extract_process=extract_process,
  597. connection_params={
  598. 'hostname': None,
  599. 'port': None,
  600. 'username': None,
  601. 'password': None,
  602. },
  603. borgmatic_runtime_directory='/run/borgmatic',
  604. )
  605. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  606. hook_config = [
  607. {
  608. 'name': 'foo',
  609. 'username': 'root',
  610. 'password': 'trustsome1',
  611. 'restore_hostname': 'restorehost',
  612. 'restore_port': 'restoreport',
  613. 'restore_username': 'restoreusername',
  614. 'restore_password': 'restorepassword',
  615. }
  616. ]
  617. extract_process = flexmock(stdout=flexmock())
  618. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  619. 'resolve_credential'
  620. ).replace_with(lambda value, config: value)
  621. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  622. flexmock(module).should_receive('execute_command_with_processes').with_args(
  623. (
  624. 'mysql',
  625. '--batch',
  626. '--host',
  627. 'clihost',
  628. '--port',
  629. 'cliport',
  630. '--protocol',
  631. 'tcp',
  632. '--user',
  633. 'cliusername',
  634. ),
  635. processes=[extract_process],
  636. output_log_level=logging.DEBUG,
  637. input_file=extract_process.stdout,
  638. environment={'USER': 'root', 'MYSQL_PWD': 'clipassword'},
  639. ).once()
  640. module.restore_data_source_dump(
  641. hook_config,
  642. {},
  643. data_source={'name': 'foo'},
  644. dry_run=False,
  645. extract_process=extract_process,
  646. connection_params={
  647. 'hostname': 'clihost',
  648. 'port': 'cliport',
  649. 'username': 'cliusername',
  650. 'password': 'clipassword',
  651. },
  652. borgmatic_runtime_directory='/run/borgmatic',
  653. )
  654. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  655. hook_config = [
  656. {
  657. 'name': 'foo',
  658. 'username': 'root',
  659. 'password': 'trustsome1',
  660. 'hostname': 'dbhost',
  661. 'port': 'dbport',
  662. 'restore_username': 'restoreuser',
  663. 'restore_password': 'restorepass',
  664. 'restore_hostname': 'restorehost',
  665. 'restore_port': 'restoreport',
  666. }
  667. ]
  668. extract_process = flexmock(stdout=flexmock())
  669. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  670. 'resolve_credential'
  671. ).replace_with(lambda value, config: value)
  672. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  673. flexmock(module).should_receive('execute_command_with_processes').with_args(
  674. (
  675. 'mysql',
  676. '--batch',
  677. '--host',
  678. 'restorehost',
  679. '--port',
  680. 'restoreport',
  681. '--protocol',
  682. 'tcp',
  683. '--user',
  684. 'restoreuser',
  685. ),
  686. processes=[extract_process],
  687. output_log_level=logging.DEBUG,
  688. input_file=extract_process.stdout,
  689. environment={'USER': 'root', 'MYSQL_PWD': 'restorepass'},
  690. ).once()
  691. module.restore_data_source_dump(
  692. hook_config,
  693. {},
  694. data_source=hook_config[0],
  695. dry_run=False,
  696. extract_process=extract_process,
  697. connection_params={
  698. 'hostname': None,
  699. 'port': None,
  700. 'username': None,
  701. 'password': None,
  702. },
  703. borgmatic_runtime_directory='/run/borgmatic',
  704. )
  705. def test_restore_data_source_dump_with_dry_run_skips_restore():
  706. hook_config = [{'name': 'foo'}]
  707. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  708. 'resolve_credential'
  709. ).replace_with(lambda value, config: value)
  710. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  711. flexmock(module).should_receive('execute_command_with_processes').never()
  712. module.restore_data_source_dump(
  713. hook_config,
  714. {},
  715. data_source={'name': 'foo'},
  716. dry_run=True,
  717. extract_process=flexmock(),
  718. connection_params={
  719. 'hostname': None,
  720. 'port': None,
  721. 'username': None,
  722. 'password': None,
  723. },
  724. borgmatic_runtime_directory='/run/borgmatic',
  725. )