test_mysql.py 31 KB

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