2
0

test_mariadb.py 53 KB

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