2
0

test_mysql.py 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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'},
  9. {},
  10. 'root',
  11. 'trustsome1',
  12. environment,
  13. dry_run=False,
  14. )
  15. assert names == ('foo',)
  16. def test_database_names_to_dump_bails_for_dry_run():
  17. environment = flexmock()
  18. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  19. 'resolve_credential',
  20. ).replace_with(lambda value, config: value)
  21. flexmock(module).should_receive('execute_command_and_capture_output').never()
  22. names = module.database_names_to_dump(
  23. {'name': 'all'},
  24. {},
  25. 'root',
  26. 'trustsome1',
  27. environment,
  28. dry_run=True,
  29. )
  30. assert names == ()
  31. def test_database_names_to_dump_queries_mysql_for_database_names():
  32. environment = flexmock()
  33. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  34. 'resolve_credential',
  35. ).replace_with(lambda value, config: value)
  36. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  37. 'parse_extra_options',
  38. ).and_return((), None)
  39. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  40. 'make_defaults_file_options',
  41. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  42. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  43. (
  44. 'mysql',
  45. '--defaults-extra-file=/dev/fd/99',
  46. '--skip-column-names',
  47. '--batch',
  48. '--execute',
  49. 'show schemas',
  50. ),
  51. environment=environment,
  52. ).and_return('foo\nbar\nmysql\n').once()
  53. names = module.database_names_to_dump(
  54. {'name': 'all'},
  55. {},
  56. 'root',
  57. 'trustsome1',
  58. environment,
  59. dry_run=False,
  60. )
  61. assert names == ('foo', 'bar')
  62. def test_database_names_to_dump_with_environment_password_transport_skips_defaults_file_and_passes_user_flag():
  63. environment = flexmock()
  64. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  65. 'resolve_credential',
  66. ).replace_with(lambda value, config: value)
  67. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  68. 'parse_extra_options',
  69. ).and_return((), None)
  70. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  71. 'make_defaults_file_options',
  72. ).never()
  73. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  74. (
  75. 'mysql',
  76. '--user',
  77. 'root',
  78. '--skip-column-names',
  79. '--batch',
  80. '--execute',
  81. 'show schemas',
  82. ),
  83. environment=environment,
  84. ).and_return('foo\nbar\nmysql\n').once()
  85. names = module.database_names_to_dump(
  86. {'name': 'all', 'password_transport': 'environment'},
  87. {},
  88. 'root',
  89. 'trustsome1',
  90. environment,
  91. dry_run=False,
  92. )
  93. assert names == ('foo', 'bar')
  94. def test_database_names_to_dump_runs_mysql_with_tls():
  95. environment = flexmock()
  96. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  97. 'resolve_credential',
  98. ).replace_with(lambda value, config: value)
  99. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  100. 'parse_extra_options',
  101. ).and_return((), None)
  102. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  103. 'make_defaults_file_options',
  104. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  105. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  106. (
  107. 'mysql',
  108. '--defaults-extra-file=/dev/fd/99',
  109. '--ssl',
  110. '--skip-column-names',
  111. '--batch',
  112. '--execute',
  113. 'show schemas',
  114. ),
  115. environment=environment,
  116. ).and_return('foo\nbar\nmysql\n').once()
  117. names = module.database_names_to_dump(
  118. {'name': 'all', 'tls': True},
  119. {},
  120. 'root',
  121. 'trustsome1',
  122. environment,
  123. dry_run=False,
  124. )
  125. assert names == ('foo', 'bar')
  126. def test_database_names_to_dump_runs_mysql_without_tls():
  127. environment = flexmock()
  128. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  129. 'resolve_credential',
  130. ).replace_with(lambda value, config: value)
  131. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  132. 'parse_extra_options',
  133. ).and_return((), None)
  134. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  135. 'make_defaults_file_options',
  136. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  137. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  138. (
  139. 'mysql',
  140. '--defaults-extra-file=/dev/fd/99',
  141. '--skip-ssl',
  142. '--skip-column-names',
  143. '--batch',
  144. '--execute',
  145. 'show schemas',
  146. ),
  147. environment=environment,
  148. ).and_return('foo\nbar\nmysql\n').once()
  149. names = module.database_names_to_dump(
  150. {'name': 'all', 'tls': False},
  151. {},
  152. 'root',
  153. 'trustsome1',
  154. environment,
  155. dry_run=False,
  156. )
  157. assert names == ('foo', 'bar')
  158. def test_use_streaming_true_for_any_databases():
  159. assert module.use_streaming(
  160. databases=[flexmock(), flexmock()],
  161. config=flexmock(),
  162. )
  163. def test_use_streaming_false_for_no_databases():
  164. assert not module.use_streaming(databases=[], config=flexmock())
  165. def test_dump_data_sources_dumps_each_database():
  166. databases = [{'name': 'foo'}, {'name': 'bar'}]
  167. processes = [flexmock(), flexmock()]
  168. flexmock(module).should_receive('make_dump_path').and_return('')
  169. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  170. 'resolve_credential',
  171. ).and_return(None)
  172. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  173. flexmock(module).should_receive('database_names_to_dump').with_args(
  174. database=databases[0],
  175. config={},
  176. username=None,
  177. password=None,
  178. environment={'USER': 'root'},
  179. dry_run=False,
  180. ).and_return(('foo',))
  181. flexmock(module).should_receive('database_names_to_dump').with_args(
  182. database=databases[1],
  183. config={},
  184. username=None,
  185. password=None,
  186. environment={'USER': 'root'},
  187. dry_run=False,
  188. ).and_return(('bar',))
  189. for name, process in zip(('foo', 'bar'), processes):
  190. flexmock(module).should_receive('execute_dump_command').with_args(
  191. database={'name': name},
  192. config={},
  193. username=None,
  194. password=None,
  195. dump_path=object,
  196. database_names=(name,),
  197. environment={'USER': 'root'},
  198. dry_run=object,
  199. dry_run_label=object,
  200. ).and_return(process).once()
  201. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  202. '/run/borgmatic',
  203. 'mysql_databases',
  204. [
  205. module.borgmatic.actions.restore.Dump('mysql_databases', 'foo'),
  206. module.borgmatic.actions.restore.Dump('mysql_databases', 'bar'),
  207. ],
  208. ).once()
  209. assert (
  210. module.dump_data_sources(
  211. databases,
  212. {},
  213. config_paths=('test.yaml',),
  214. borgmatic_runtime_directory='/run/borgmatic',
  215. patterns=[],
  216. dry_run=False,
  217. )
  218. == processes
  219. )
  220. def test_dump_data_sources_dumps_with_password():
  221. database = {'name': 'foo', 'username': 'root', 'password': 'trustsome1'}
  222. process = flexmock()
  223. flexmock(module).should_receive('make_dump_path').and_return('')
  224. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  225. 'resolve_credential',
  226. ).replace_with(lambda value, config: value)
  227. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  228. flexmock(module).should_receive('database_names_to_dump').with_args(
  229. database=database,
  230. config={},
  231. username='root',
  232. password='trustsome1',
  233. environment={'USER': 'root'},
  234. dry_run=False,
  235. ).and_return(('foo',))
  236. flexmock(module).should_receive('execute_dump_command').with_args(
  237. database=database,
  238. config={},
  239. username='root',
  240. password='trustsome1',
  241. dump_path=object,
  242. database_names=('foo',),
  243. environment={'USER': 'root'},
  244. dry_run=object,
  245. dry_run_label=object,
  246. ).and_return(process).once()
  247. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  248. '/run/borgmatic',
  249. 'mysql_databases',
  250. [
  251. module.borgmatic.actions.restore.Dump('mysql_databases', 'foo'),
  252. ],
  253. ).once()
  254. assert module.dump_data_sources(
  255. [database],
  256. {},
  257. config_paths=('test.yaml',),
  258. borgmatic_runtime_directory='/run/borgmatic',
  259. patterns=[],
  260. dry_run=False,
  261. ) == [process]
  262. def test_dump_data_sources_dumps_with_environment_password_transport_passes_password_environment_variable():
  263. database = {
  264. 'name': 'foo',
  265. 'username': 'root',
  266. 'password': 'trustsome1',
  267. 'password_transport': 'environment',
  268. }
  269. process = flexmock()
  270. flexmock(module).should_receive('make_dump_path').and_return('')
  271. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  272. 'resolve_credential',
  273. ).replace_with(lambda value, config: value)
  274. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  275. flexmock(module).should_receive('database_names_to_dump').with_args(
  276. database=database,
  277. config={},
  278. username='root',
  279. password='trustsome1',
  280. environment={'USER': 'root', 'MYSQL_PWD': 'trustsome1'},
  281. dry_run=False,
  282. ).and_return(('foo',))
  283. flexmock(module).should_receive('execute_dump_command').with_args(
  284. database=database,
  285. config={},
  286. username='root',
  287. password='trustsome1',
  288. dump_path=object,
  289. database_names=('foo',),
  290. environment={'USER': 'root', 'MYSQL_PWD': 'trustsome1'},
  291. dry_run=object,
  292. dry_run_label=object,
  293. ).and_return(process).once()
  294. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  295. '/run/borgmatic',
  296. 'mysql_databases',
  297. [
  298. module.borgmatic.actions.restore.Dump('mysql_databases', 'foo'),
  299. ],
  300. ).once()
  301. assert module.dump_data_sources(
  302. [database],
  303. {},
  304. config_paths=('test.yaml',),
  305. borgmatic_runtime_directory='/run/borgmatic',
  306. patterns=[],
  307. dry_run=False,
  308. ) == [process]
  309. def test_dump_data_sources_dumps_all_databases_at_once():
  310. databases = [{'name': 'all'}]
  311. process = flexmock()
  312. flexmock(module).should_receive('make_dump_path').and_return('')
  313. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  314. 'resolve_credential',
  315. ).and_return(None)
  316. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  317. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  318. flexmock(module).should_receive('execute_dump_command').with_args(
  319. database={'name': 'all'},
  320. config={},
  321. username=None,
  322. password=None,
  323. dump_path=object,
  324. database_names=('foo', 'bar'),
  325. environment={'USER': 'root'},
  326. dry_run=object,
  327. dry_run_label=object,
  328. ).and_return(process).once()
  329. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  330. '/run/borgmatic',
  331. 'mysql_databases',
  332. [
  333. module.borgmatic.actions.restore.Dump('mysql_databases', 'all'),
  334. ],
  335. ).once()
  336. assert module.dump_data_sources(
  337. databases,
  338. {},
  339. config_paths=('test.yaml',),
  340. borgmatic_runtime_directory='/run/borgmatic',
  341. patterns=[],
  342. dry_run=False,
  343. ) == [process]
  344. def test_dump_data_sources_dumps_all_databases_separately_when_format_configured():
  345. databases = [{'name': 'all', 'format': 'sql'}]
  346. processes = [flexmock(), flexmock()]
  347. flexmock(module).should_receive('make_dump_path').and_return('')
  348. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  349. 'resolve_credential',
  350. ).and_return(None)
  351. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  352. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  353. for name, process in zip(('foo', 'bar'), processes):
  354. flexmock(module).should_receive('execute_dump_command').with_args(
  355. database={'name': name, 'format': 'sql'},
  356. config={},
  357. username=None,
  358. password=None,
  359. dump_path=object,
  360. database_names=(name,),
  361. environment={'USER': 'root'},
  362. dry_run=object,
  363. dry_run_label=object,
  364. ).and_return(process).once()
  365. flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args(
  366. '/run/borgmatic',
  367. 'mysql_databases',
  368. [
  369. module.borgmatic.actions.restore.Dump('mysql_databases', 'foo'),
  370. module.borgmatic.actions.restore.Dump('mysql_databases', 'bar'),
  371. ],
  372. ).once()
  373. assert (
  374. module.dump_data_sources(
  375. databases,
  376. {},
  377. config_paths=('test.yaml',),
  378. borgmatic_runtime_directory='/run/borgmatic',
  379. patterns=[],
  380. dry_run=False,
  381. )
  382. == processes
  383. )
  384. def test_dump_data_sources_errors_for_missing_all_databases():
  385. databases = [{'name': 'all'}]
  386. flexmock(module).should_receive('make_dump_path').and_return('')
  387. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  388. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  389. 'resolve_credential',
  390. ).replace_with(lambda value, config: value)
  391. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  392. 'databases/localhost/all',
  393. )
  394. flexmock(module).should_receive('database_names_to_dump').and_return(())
  395. with pytest.raises(ValueError):
  396. assert module.dump_data_sources(
  397. databases,
  398. {},
  399. config_paths=('test.yaml',),
  400. borgmatic_runtime_directory='/run/borgmatic',
  401. patterns=[],
  402. dry_run=False,
  403. )
  404. def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
  405. databases = [{'name': 'all'}]
  406. flexmock(module).should_receive('make_dump_path').and_return('')
  407. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  408. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  409. 'resolve_credential',
  410. ).replace_with(lambda value, config: value)
  411. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  412. 'databases/localhost/all',
  413. )
  414. flexmock(module).should_receive('database_names_to_dump').and_return(())
  415. assert (
  416. module.dump_data_sources(
  417. databases,
  418. {},
  419. config_paths=('test.yaml',),
  420. borgmatic_runtime_directory='/run/borgmatic',
  421. patterns=[],
  422. dry_run=True,
  423. )
  424. == []
  425. )
  426. def test_database_names_to_dump_runs_mysql_with_list_options():
  427. database = {'name': 'all', 'list_options': '--defaults-extra-file=my.cnf --skip-ssl'}
  428. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  429. 'parse_extra_options',
  430. ).and_return(('--skip-ssl',), 'my.cnf')
  431. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  432. 'make_defaults_file_options',
  433. ).with_args('root', 'trustsome1', 'my.cnf').and_return(('--defaults-extra-file=/dev/fd/99',))
  434. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  435. (
  436. 'mysql',
  437. '--defaults-extra-file=/dev/fd/99',
  438. '--skip-ssl',
  439. '--skip-column-names',
  440. '--batch',
  441. '--execute',
  442. 'show schemas',
  443. ),
  444. environment=None,
  445. ).and_return('foo\nbar').once()
  446. assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == (
  447. 'foo',
  448. 'bar',
  449. )
  450. def test_database_names_to_dump_runs_non_default_mysql_with_list_options():
  451. database = {
  452. 'name': 'all',
  453. 'list_options': '--defaults-extra-file=my.cnf --skip-ssl',
  454. 'mysql_command': 'custom_mysql',
  455. }
  456. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  457. 'parse_extra_options',
  458. ).and_return(('--skip-ssl',), 'my.cnf')
  459. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  460. 'make_defaults_file_options',
  461. ).with_args('root', 'trustsome1', 'my.cnf').and_return(('--defaults-extra-file=/dev/fd/99',))
  462. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  463. environment=None,
  464. full_command=(
  465. 'custom_mysql', # Custom MySQL command
  466. '--defaults-extra-file=/dev/fd/99',
  467. '--skip-ssl',
  468. '--skip-column-names',
  469. '--batch',
  470. '--execute',
  471. 'show schemas',
  472. ),
  473. ).and_return('foo\nbar').once()
  474. assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == (
  475. 'foo',
  476. 'bar',
  477. )
  478. def test_execute_dump_command_runs_mysqldump():
  479. process = flexmock()
  480. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  481. flexmock(module.os.path).should_receive('exists').and_return(False)
  482. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  483. 'resolve_credential',
  484. ).replace_with(lambda value, config: value)
  485. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  486. 'parse_extra_options',
  487. ).and_return((), None)
  488. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  489. 'make_defaults_file_options',
  490. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  491. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  492. flexmock(module).should_receive('execute_command').with_args(
  493. (
  494. 'mysqldump',
  495. '--defaults-extra-file=/dev/fd/99',
  496. '--add-drop-database',
  497. '--databases',
  498. 'foo',
  499. '--result-file',
  500. 'dump',
  501. ),
  502. environment=None,
  503. run_to_completion=False,
  504. ).and_return(process).once()
  505. assert (
  506. module.execute_dump_command(
  507. database={'name': 'foo'},
  508. config={},
  509. username='root',
  510. password='trustsome1',
  511. dump_path=flexmock(),
  512. database_names=('foo',),
  513. environment=None,
  514. dry_run=False,
  515. dry_run_label='',
  516. )
  517. == process
  518. )
  519. def test_execute_dump_command_with_environment_password_transport_skips_defaults_file_and_passes_user_flag():
  520. process = flexmock()
  521. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  522. flexmock(module.os.path).should_receive('exists').and_return(False)
  523. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  524. 'resolve_credential',
  525. ).replace_with(lambda value, config: value)
  526. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  527. 'parse_extra_options',
  528. ).and_return((), None)
  529. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  530. 'make_defaults_file_options',
  531. ).never()
  532. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  533. flexmock(module).should_receive('execute_command').with_args(
  534. (
  535. 'mysqldump',
  536. '--add-drop-database',
  537. '--user',
  538. 'root',
  539. '--databases',
  540. 'foo',
  541. '--result-file',
  542. 'dump',
  543. ),
  544. environment=None,
  545. run_to_completion=False,
  546. ).and_return(process).once()
  547. assert (
  548. module.execute_dump_command(
  549. database={'name': 'foo', 'password_transport': 'environment'},
  550. config={},
  551. username='root',
  552. password='trustsome1',
  553. dump_path=flexmock(),
  554. database_names=('foo',),
  555. environment=None,
  556. dry_run=False,
  557. dry_run_label='',
  558. )
  559. == process
  560. )
  561. def test_execute_dump_command_runs_mysqldump_without_add_drop_database():
  562. process = flexmock()
  563. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  564. flexmock(module.os.path).should_receive('exists').and_return(False)
  565. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  566. 'resolve_credential',
  567. ).replace_with(lambda value, config: value)
  568. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  569. 'parse_extra_options',
  570. ).and_return((), None)
  571. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  572. 'make_defaults_file_options',
  573. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  574. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  575. flexmock(module).should_receive('execute_command').with_args(
  576. (
  577. 'mysqldump',
  578. '--defaults-extra-file=/dev/fd/99',
  579. '--databases',
  580. 'foo',
  581. '--result-file',
  582. 'dump',
  583. ),
  584. environment=None,
  585. run_to_completion=False,
  586. ).and_return(process).once()
  587. assert (
  588. module.execute_dump_command(
  589. database={'name': 'foo', 'add_drop_database': False},
  590. config={},
  591. username='root',
  592. password='trustsome1',
  593. dump_path=flexmock(),
  594. database_names=('foo',),
  595. environment=None,
  596. dry_run=False,
  597. dry_run_label='',
  598. )
  599. == process
  600. )
  601. def test_execute_dump_command_runs_mysqldump_with_hostname_and_port():
  602. process = flexmock()
  603. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  604. flexmock(module.os.path).should_receive('exists').and_return(False)
  605. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  606. 'resolve_credential',
  607. ).replace_with(lambda value, config: value)
  608. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  609. 'parse_extra_options',
  610. ).and_return((), None)
  611. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  612. 'make_defaults_file_options',
  613. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  614. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  615. flexmock(module).should_receive('execute_command').with_args(
  616. (
  617. 'mysqldump',
  618. '--defaults-extra-file=/dev/fd/99',
  619. '--add-drop-database',
  620. '--host',
  621. 'database.example.org',
  622. '--port',
  623. '5433',
  624. '--protocol',
  625. 'tcp',
  626. '--databases',
  627. 'foo',
  628. '--result-file',
  629. 'dump',
  630. ),
  631. environment=None,
  632. run_to_completion=False,
  633. ).and_return(process).once()
  634. assert (
  635. module.execute_dump_command(
  636. database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},
  637. config={},
  638. username='root',
  639. password='trustsome1',
  640. dump_path=flexmock(),
  641. database_names=('foo',),
  642. environment=None,
  643. dry_run=False,
  644. dry_run_label='',
  645. )
  646. == process
  647. )
  648. def test_execute_dump_command_runs_mysqldump_with_tls():
  649. process = flexmock()
  650. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  651. flexmock(module.os.path).should_receive('exists').and_return(False)
  652. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  653. 'resolve_credential',
  654. ).replace_with(lambda value, config: value)
  655. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  656. 'parse_extra_options',
  657. ).and_return((), None)
  658. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  659. 'make_defaults_file_options',
  660. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  661. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  662. flexmock(module).should_receive('execute_command').with_args(
  663. (
  664. 'mysqldump',
  665. '--defaults-extra-file=/dev/fd/99',
  666. '--add-drop-database',
  667. '--ssl',
  668. '--databases',
  669. 'foo',
  670. '--result-file',
  671. 'dump',
  672. ),
  673. environment=None,
  674. run_to_completion=False,
  675. ).and_return(process).once()
  676. assert (
  677. module.execute_dump_command(
  678. database={'name': 'foo', 'tls': True},
  679. config={},
  680. username='root',
  681. password='trustsome1',
  682. dump_path=flexmock(),
  683. database_names=('foo',),
  684. environment=None,
  685. dry_run=False,
  686. dry_run_label='',
  687. )
  688. == process
  689. )
  690. def test_execute_dump_command_runs_mysqldump_without_tls():
  691. process = flexmock()
  692. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  693. flexmock(module.os.path).should_receive('exists').and_return(False)
  694. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  695. 'resolve_credential',
  696. ).replace_with(lambda value, config: value)
  697. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  698. 'parse_extra_options',
  699. ).and_return((), None)
  700. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  701. 'make_defaults_file_options',
  702. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  703. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  704. flexmock(module).should_receive('execute_command').with_args(
  705. (
  706. 'mysqldump',
  707. '--defaults-extra-file=/dev/fd/99',
  708. '--add-drop-database',
  709. '--skip-ssl',
  710. '--databases',
  711. 'foo',
  712. '--result-file',
  713. 'dump',
  714. ),
  715. environment=None,
  716. run_to_completion=False,
  717. ).and_return(process).once()
  718. assert (
  719. module.execute_dump_command(
  720. database={'name': 'foo', 'tls': False},
  721. config={},
  722. username='root',
  723. password='trustsome1',
  724. dump_path=flexmock(),
  725. database_names=('foo',),
  726. environment=None,
  727. dry_run=False,
  728. dry_run_label='',
  729. )
  730. == process
  731. )
  732. def test_execute_dump_command_runs_mysqldump_with_username_and_password():
  733. process = flexmock()
  734. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  735. flexmock(module.os.path).should_receive('exists').and_return(False)
  736. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  737. 'resolve_credential',
  738. ).replace_with(lambda value, config: value)
  739. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  740. 'parse_extra_options',
  741. ).and_return((), None)
  742. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  743. 'make_defaults_file_options',
  744. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  745. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  746. flexmock(module).should_receive('execute_command').with_args(
  747. (
  748. 'mysqldump',
  749. '--defaults-extra-file=/dev/fd/99',
  750. '--add-drop-database',
  751. '--databases',
  752. 'foo',
  753. '--result-file',
  754. 'dump',
  755. ),
  756. environment={},
  757. run_to_completion=False,
  758. ).and_return(process).once()
  759. assert (
  760. module.execute_dump_command(
  761. database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},
  762. config={},
  763. username='root',
  764. password='trustsome1',
  765. dump_path=flexmock(),
  766. database_names=('foo',),
  767. environment={},
  768. dry_run=False,
  769. dry_run_label='',
  770. )
  771. == process
  772. )
  773. def test_execute_dump_command_runs_mysqldump_with_options():
  774. process = flexmock()
  775. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  776. flexmock(module.os.path).should_receive('exists').and_return(False)
  777. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  778. 'resolve_credential',
  779. ).replace_with(lambda value, config: value)
  780. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  781. 'parse_extra_options',
  782. ).and_return(('--stuff=such',), None)
  783. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  784. 'make_defaults_file_options',
  785. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  786. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  787. flexmock(module).should_receive('execute_command').with_args(
  788. (
  789. 'mysqldump',
  790. '--defaults-extra-file=/dev/fd/99',
  791. '--stuff=such',
  792. '--add-drop-database',
  793. '--databases',
  794. 'foo',
  795. '--result-file',
  796. 'dump',
  797. ),
  798. environment=None,
  799. run_to_completion=False,
  800. ).and_return(process).once()
  801. assert (
  802. module.execute_dump_command(
  803. database={'name': 'foo', 'options': '--stuff=such'},
  804. config={},
  805. username='root',
  806. password='trustsome1',
  807. dump_path=flexmock(),
  808. database_names=('foo',),
  809. environment=None,
  810. dry_run=False,
  811. dry_run_label='',
  812. )
  813. == process
  814. )
  815. def test_execute_dump_command_runs_non_default_mysqldump():
  816. process = flexmock()
  817. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  818. flexmock(module.os.path).should_receive('exists').and_return(False)
  819. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  820. 'resolve_credential',
  821. ).replace_with(lambda value, config: value)
  822. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  823. 'parse_extra_options',
  824. ).and_return((), None)
  825. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  826. 'make_defaults_file_options',
  827. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  828. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  829. flexmock(module).should_receive('execute_command').with_args(
  830. (
  831. 'custom_mysqldump', # Custom MySQL dump command
  832. '--defaults-extra-file=/dev/fd/99',
  833. '--add-drop-database',
  834. '--databases',
  835. 'foo',
  836. '--result-file',
  837. 'dump',
  838. ),
  839. environment=None,
  840. run_to_completion=False,
  841. ).and_return(process).once()
  842. assert (
  843. module.execute_dump_command(
  844. database={
  845. 'name': 'foo',
  846. 'mysql_dump_command': 'custom_mysqldump',
  847. }, # Custom MySQL dump command specified
  848. config={},
  849. username='root',
  850. password='trustsome1',
  851. dump_path=flexmock(),
  852. database_names=('foo',),
  853. environment=None,
  854. dry_run=False,
  855. dry_run_label='',
  856. )
  857. == process
  858. )
  859. def test_execute_dump_command_with_duplicate_dump_skips_mysqldump():
  860. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  861. flexmock(module.os.path).should_receive('exists').and_return(True)
  862. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  863. 'parse_extra_options',
  864. ).and_return((), None)
  865. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  866. 'make_defaults_file_options',
  867. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  868. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  869. flexmock(module).should_receive('execute_command').never()
  870. assert (
  871. module.execute_dump_command(
  872. database={'name': 'foo'},
  873. config={},
  874. username='root',
  875. password='trustsome1',
  876. dump_path=flexmock(),
  877. database_names=('foo',),
  878. environment=None,
  879. dry_run=True,
  880. dry_run_label='SO DRY',
  881. )
  882. is None
  883. )
  884. def test_execute_dump_command_with_dry_run_skips_mysqldump():
  885. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  886. flexmock(module.os.path).should_receive('exists').and_return(False)
  887. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  888. 'resolve_credential',
  889. ).replace_with(lambda value, config: value)
  890. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  891. 'parse_extra_options',
  892. ).and_return((), None)
  893. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  894. 'make_defaults_file_options',
  895. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  896. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  897. flexmock(module).should_receive('execute_command').never()
  898. assert (
  899. module.execute_dump_command(
  900. database={'name': 'foo'},
  901. config={},
  902. username='root',
  903. password='trustsome1',
  904. dump_path=flexmock(),
  905. database_names=('foo',),
  906. environment=None,
  907. dry_run=True,
  908. dry_run_label='SO DRY',
  909. )
  910. is None
  911. )
  912. def test_restore_data_source_dump_runs_mysql_to_restore():
  913. hook_config = [{'name': 'foo'}, {'name': 'bar'}]
  914. extract_process = flexmock(stdout=flexmock())
  915. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  916. 'resolve_credential',
  917. ).replace_with(lambda value, config: value)
  918. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  919. 'parse_extra_options',
  920. ).and_return((), None)
  921. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  922. 'make_defaults_file_options',
  923. ).with_args(None, None, None).and_return(())
  924. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  925. flexmock(module).should_receive('execute_command_with_processes').with_args(
  926. ('mysql', '--batch'),
  927. processes=[extract_process],
  928. output_log_level=logging.DEBUG,
  929. input_file=extract_process.stdout,
  930. environment={'USER': 'root'},
  931. ).once()
  932. module.restore_data_source_dump(
  933. hook_config,
  934. {},
  935. data_source={'name': 'foo'},
  936. dry_run=False,
  937. extract_process=extract_process,
  938. connection_params={
  939. 'hostname': None,
  940. 'port': None,
  941. 'username': None,
  942. 'password': None,
  943. },
  944. borgmatic_runtime_directory='/run/borgmatic',
  945. )
  946. def test_restore_data_source_dump_runs_mysql_with_options():
  947. hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
  948. extract_process = flexmock(stdout=flexmock())
  949. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  950. 'resolve_credential',
  951. ).replace_with(lambda value, config: value)
  952. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  953. 'parse_extra_options',
  954. ).and_return(('--harder',), None)
  955. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  956. 'make_defaults_file_options',
  957. ).with_args(None, None, None).and_return(())
  958. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  959. flexmock(module).should_receive('execute_command_with_processes').with_args(
  960. ('mysql', '--harder', '--batch'),
  961. processes=[extract_process],
  962. output_log_level=logging.DEBUG,
  963. input_file=extract_process.stdout,
  964. environment={'USER': 'root'},
  965. ).once()
  966. module.restore_data_source_dump(
  967. hook_config,
  968. {},
  969. data_source=hook_config[0],
  970. dry_run=False,
  971. extract_process=extract_process,
  972. connection_params={
  973. 'hostname': None,
  974. 'port': None,
  975. 'username': None,
  976. 'password': None,
  977. },
  978. borgmatic_runtime_directory='/run/borgmatic',
  979. )
  980. def test_restore_data_source_dump_runs_non_default_mysql_with_options():
  981. hook_config = [{'name': 'foo', 'mysql_command': 'custom_mysql', 'restore_options': '--harder'}]
  982. extract_process = flexmock(stdout=flexmock())
  983. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  984. 'resolve_credential',
  985. ).replace_with(lambda value, config: value)
  986. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  987. 'parse_extra_options',
  988. ).and_return(('--harder',), None)
  989. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  990. 'make_defaults_file_options',
  991. ).with_args(None, None, None).and_return(())
  992. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  993. flexmock(module).should_receive('execute_command_with_processes').with_args(
  994. ('custom_mysql', '--harder', '--batch'),
  995. processes=[extract_process],
  996. output_log_level=logging.DEBUG,
  997. input_file=extract_process.stdout,
  998. environment={'USER': 'root'},
  999. ).once()
  1000. module.restore_data_source_dump(
  1001. hook_config,
  1002. {},
  1003. data_source=hook_config[0],
  1004. dry_run=False,
  1005. extract_process=extract_process,
  1006. connection_params={
  1007. 'hostname': None,
  1008. 'port': None,
  1009. 'username': None,
  1010. 'password': None,
  1011. },
  1012. borgmatic_runtime_directory='/run/borgmatic',
  1013. )
  1014. def test_restore_data_source_dump_runs_mysql_with_hostname_and_port():
  1015. hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  1016. extract_process = flexmock(stdout=flexmock())
  1017. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1018. 'resolve_credential',
  1019. ).replace_with(lambda value, config: value)
  1020. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1021. 'parse_extra_options',
  1022. ).and_return((), None)
  1023. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1024. 'make_defaults_file_options',
  1025. ).with_args(None, None, None).and_return(())
  1026. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1027. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1028. (
  1029. 'mysql',
  1030. '--batch',
  1031. '--host',
  1032. 'database.example.org',
  1033. '--port',
  1034. '5433',
  1035. '--protocol',
  1036. 'tcp',
  1037. ),
  1038. processes=[extract_process],
  1039. output_log_level=logging.DEBUG,
  1040. input_file=extract_process.stdout,
  1041. environment={'USER': 'root'},
  1042. ).once()
  1043. module.restore_data_source_dump(
  1044. hook_config,
  1045. {},
  1046. data_source=hook_config[0],
  1047. dry_run=False,
  1048. extract_process=extract_process,
  1049. connection_params={
  1050. 'hostname': None,
  1051. 'port': None,
  1052. 'username': None,
  1053. 'password': None,
  1054. },
  1055. borgmatic_runtime_directory='/run/borgmatic',
  1056. )
  1057. def test_restore_data_source_dump_runs_mysql_with_tls():
  1058. hook_config = [{'name': 'foo', 'tls': True}]
  1059. extract_process = flexmock(stdout=flexmock())
  1060. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1061. 'resolve_credential',
  1062. ).replace_with(lambda value, config: value)
  1063. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1064. 'parse_extra_options',
  1065. ).and_return((), None)
  1066. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1067. 'make_defaults_file_options',
  1068. ).with_args(None, None, None).and_return(())
  1069. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1070. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1071. (
  1072. 'mysql',
  1073. '--batch',
  1074. '--ssl',
  1075. ),
  1076. processes=[extract_process],
  1077. output_log_level=logging.DEBUG,
  1078. input_file=extract_process.stdout,
  1079. environment={'USER': 'root'},
  1080. ).once()
  1081. module.restore_data_source_dump(
  1082. hook_config,
  1083. {},
  1084. data_source=hook_config[0],
  1085. dry_run=False,
  1086. extract_process=extract_process,
  1087. connection_params={
  1088. 'hostname': None,
  1089. 'port': None,
  1090. 'username': None,
  1091. 'password': None,
  1092. },
  1093. borgmatic_runtime_directory='/run/borgmatic',
  1094. )
  1095. def test_restore_data_source_dump_runs_mysql_without_tls():
  1096. hook_config = [{'name': 'foo', 'tls': False}]
  1097. extract_process = flexmock(stdout=flexmock())
  1098. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1099. 'resolve_credential',
  1100. ).replace_with(lambda value, config: value)
  1101. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1102. 'parse_extra_options',
  1103. ).and_return((), None)
  1104. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1105. 'make_defaults_file_options',
  1106. ).with_args(None, None, None).and_return(())
  1107. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1108. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1109. (
  1110. 'mysql',
  1111. '--batch',
  1112. '--skip-ssl',
  1113. ),
  1114. processes=[extract_process],
  1115. output_log_level=logging.DEBUG,
  1116. input_file=extract_process.stdout,
  1117. environment={'USER': 'root'},
  1118. ).once()
  1119. module.restore_data_source_dump(
  1120. hook_config,
  1121. {},
  1122. data_source=hook_config[0],
  1123. dry_run=False,
  1124. extract_process=extract_process,
  1125. connection_params={
  1126. 'hostname': None,
  1127. 'port': None,
  1128. 'username': None,
  1129. 'password': None,
  1130. },
  1131. borgmatic_runtime_directory='/run/borgmatic',
  1132. )
  1133. def test_restore_data_source_dump_runs_mysql_with_username_and_password():
  1134. hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  1135. extract_process = flexmock(stdout=flexmock())
  1136. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1137. 'resolve_credential',
  1138. ).replace_with(lambda value, config: value)
  1139. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1140. 'parse_extra_options',
  1141. ).and_return((), None)
  1142. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1143. 'make_defaults_file_options',
  1144. ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',))
  1145. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1146. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1147. ('mysql', '--defaults-extra-file=/dev/fd/99', '--batch'),
  1148. processes=[extract_process],
  1149. output_log_level=logging.DEBUG,
  1150. input_file=extract_process.stdout,
  1151. environment={'USER': 'root'},
  1152. ).once()
  1153. module.restore_data_source_dump(
  1154. hook_config,
  1155. {},
  1156. data_source=hook_config[0],
  1157. dry_run=False,
  1158. extract_process=extract_process,
  1159. connection_params={
  1160. 'hostname': None,
  1161. 'port': None,
  1162. 'username': None,
  1163. 'password': None,
  1164. },
  1165. borgmatic_runtime_directory='/run/borgmatic',
  1166. )
  1167. def test_restore_data_source_with_environment_password_transport_skips_defaults_file_and_passes_user_flag():
  1168. hook_config = [
  1169. {
  1170. 'name': 'foo',
  1171. 'username': 'root',
  1172. 'password': 'trustsome1',
  1173. 'password_transport': 'environment',
  1174. },
  1175. ]
  1176. extract_process = flexmock(stdout=flexmock())
  1177. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1178. 'resolve_credential',
  1179. ).replace_with(lambda value, config: value)
  1180. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1181. 'parse_extra_options',
  1182. ).and_return((), None)
  1183. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1184. 'make_defaults_file_options',
  1185. ).never()
  1186. flexmock(module.os).should_receive('environ').and_return(
  1187. {'USER': 'root', 'MYSQL_PWD': 'trustsome1'},
  1188. )
  1189. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1190. ('mysql', '--batch', '--user', 'root'),
  1191. processes=[extract_process],
  1192. output_log_level=logging.DEBUG,
  1193. input_file=extract_process.stdout,
  1194. environment={'USER': 'root', 'MYSQL_PWD': 'trustsome1'},
  1195. ).once()
  1196. module.restore_data_source_dump(
  1197. hook_config,
  1198. {},
  1199. data_source=hook_config[0],
  1200. dry_run=False,
  1201. extract_process=extract_process,
  1202. connection_params={
  1203. 'hostname': None,
  1204. 'port': None,
  1205. 'username': None,
  1206. 'password': None,
  1207. },
  1208. borgmatic_runtime_directory='/run/borgmatic',
  1209. )
  1210. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  1211. hook_config = [
  1212. {
  1213. 'name': 'foo',
  1214. 'username': 'root',
  1215. 'password': 'trustsome1',
  1216. 'restore_hostname': 'restorehost',
  1217. 'restore_port': 'restoreport',
  1218. 'restore_username': 'restoreusername',
  1219. 'restore_password': 'restorepassword',
  1220. },
  1221. ]
  1222. extract_process = flexmock(stdout=flexmock())
  1223. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1224. 'resolve_credential',
  1225. ).replace_with(lambda value, config: value)
  1226. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1227. 'parse_extra_options',
  1228. ).and_return((), None)
  1229. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1230. 'make_defaults_file_options',
  1231. ).with_args('cliusername', 'clipassword', None).and_return(
  1232. ('--defaults-extra-file=/dev/fd/99',),
  1233. )
  1234. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1235. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1236. (
  1237. 'mysql',
  1238. '--defaults-extra-file=/dev/fd/99',
  1239. '--batch',
  1240. '--host',
  1241. 'clihost',
  1242. '--port',
  1243. 'cliport',
  1244. '--protocol',
  1245. 'tcp',
  1246. ),
  1247. processes=[extract_process],
  1248. output_log_level=logging.DEBUG,
  1249. input_file=extract_process.stdout,
  1250. environment={'USER': 'root'},
  1251. ).once()
  1252. module.restore_data_source_dump(
  1253. hook_config,
  1254. {},
  1255. data_source={'name': 'foo'},
  1256. dry_run=False,
  1257. extract_process=extract_process,
  1258. connection_params={
  1259. 'hostname': 'clihost',
  1260. 'port': 'cliport',
  1261. 'username': 'cliusername',
  1262. 'password': 'clipassword',
  1263. },
  1264. borgmatic_runtime_directory='/run/borgmatic',
  1265. )
  1266. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  1267. hook_config = [
  1268. {
  1269. 'name': 'foo',
  1270. 'username': 'root',
  1271. 'password': 'trustsome1',
  1272. 'hostname': 'dbhost',
  1273. 'port': 'dbport',
  1274. 'tls': True,
  1275. 'restore_username': 'restoreuser',
  1276. 'restore_password': 'restorepass',
  1277. 'restore_hostname': 'restorehost',
  1278. 'restore_port': 'restoreport',
  1279. 'restore_tls': False,
  1280. },
  1281. ]
  1282. extract_process = flexmock(stdout=flexmock())
  1283. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1284. 'resolve_credential',
  1285. ).replace_with(lambda value, config: value)
  1286. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1287. 'parse_extra_options',
  1288. ).and_return((), None)
  1289. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1290. 'make_defaults_file_options',
  1291. ).with_args('restoreuser', 'restorepass', None).and_return(
  1292. ('--defaults-extra-file=/dev/fd/99',),
  1293. )
  1294. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1295. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1296. (
  1297. 'mysql',
  1298. '--defaults-extra-file=/dev/fd/99',
  1299. '--batch',
  1300. '--host',
  1301. 'restorehost',
  1302. '--port',
  1303. 'restoreport',
  1304. '--protocol',
  1305. 'tcp',
  1306. '--skip-ssl',
  1307. ),
  1308. processes=[extract_process],
  1309. output_log_level=logging.DEBUG,
  1310. input_file=extract_process.stdout,
  1311. environment={'USER': 'root'},
  1312. ).once()
  1313. module.restore_data_source_dump(
  1314. hook_config,
  1315. {},
  1316. data_source=hook_config[0],
  1317. dry_run=False,
  1318. extract_process=extract_process,
  1319. connection_params={
  1320. 'hostname': None,
  1321. 'port': None,
  1322. 'username': None,
  1323. 'password': None,
  1324. },
  1325. borgmatic_runtime_directory='/run/borgmatic',
  1326. )
  1327. def test_restore_data_source_dump_with_dry_run_skips_restore():
  1328. hook_config = [{'name': 'foo'}]
  1329. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1330. 'resolve_credential',
  1331. ).replace_with(lambda value, config: value)
  1332. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1333. 'parse_extra_options',
  1334. ).and_return((), None)
  1335. flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive(
  1336. 'make_defaults_file_options',
  1337. ).with_args(None, None, None).and_return(())
  1338. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1339. flexmock(module).should_receive('execute_command_with_processes').never()
  1340. module.restore_data_source_dump(
  1341. hook_config,
  1342. {},
  1343. data_source={'name': 'foo'},
  1344. dry_run=True,
  1345. extract_process=flexmock(),
  1346. connection_params={
  1347. 'hostname': None,
  1348. 'port': None,
  1349. 'username': None,
  1350. 'password': None,
  1351. },
  1352. borgmatic_runtime_directory='/run/borgmatic',
  1353. )