test_mariadb.py 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  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. assert (
  287. module.dump_data_sources(
  288. databases,
  289. {},
  290. config_paths=('test.yaml',),
  291. borgmatic_runtime_directory='/run/borgmatic',
  292. patterns=[],
  293. dry_run=False,
  294. )
  295. == processes
  296. )
  297. def test_dump_data_sources_dumps_with_password():
  298. database = {'name': 'foo', 'username': 'root', 'password': 'trustsome1'}
  299. process = flexmock()
  300. flexmock(module).should_receive('make_dump_path').and_return('')
  301. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  302. 'resolve_credential',
  303. ).replace_with(lambda value, config: value)
  304. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  305. flexmock(module).should_receive('database_names_to_dump').with_args(
  306. database=database,
  307. config={},
  308. username='root',
  309. password='trustsome1',
  310. environment={'USER': 'root'},
  311. dry_run=False,
  312. ).and_return(('foo',))
  313. flexmock(module).should_receive('execute_dump_command').with_args(
  314. database=database,
  315. config={},
  316. username='root',
  317. password='trustsome1',
  318. dump_path=object,
  319. database_names=('foo',),
  320. environment={'USER': 'root'},
  321. dry_run=object,
  322. dry_run_label=object,
  323. ).and_return(process).once()
  324. assert module.dump_data_sources(
  325. [database],
  326. {},
  327. config_paths=('test.yaml',),
  328. borgmatic_runtime_directory='/run/borgmatic',
  329. patterns=[],
  330. dry_run=False,
  331. ) == [process]
  332. def test_dump_data_sources_dumps_with_environment_password_transport_passes_password_environment_variable():
  333. database = {
  334. 'name': 'foo',
  335. 'username': 'root',
  336. 'password': 'trustsome1',
  337. 'password_transport': 'environment',
  338. }
  339. process = flexmock()
  340. flexmock(module).should_receive('make_dump_path').and_return('')
  341. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  342. 'resolve_credential',
  343. ).replace_with(lambda value, config: value)
  344. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  345. flexmock(module).should_receive('database_names_to_dump').with_args(
  346. database=database,
  347. config={},
  348. username='root',
  349. password='trustsome1',
  350. environment={'USER': 'root', 'MYSQL_PWD': 'trustsome1'},
  351. dry_run=False,
  352. ).and_return(('foo',))
  353. flexmock(module).should_receive('execute_dump_command').with_args(
  354. database=database,
  355. config={},
  356. username='root',
  357. password='trustsome1',
  358. dump_path=object,
  359. database_names=('foo',),
  360. environment={'USER': 'root', 'MYSQL_PWD': 'trustsome1'},
  361. dry_run=object,
  362. dry_run_label=object,
  363. ).and_return(process).once()
  364. assert module.dump_data_sources(
  365. [database],
  366. {},
  367. config_paths=('test.yaml',),
  368. borgmatic_runtime_directory='/run/borgmatic',
  369. patterns=[],
  370. dry_run=False,
  371. ) == [process]
  372. def test_dump_data_sources_dumps_all_databases_at_once():
  373. databases = [{'name': 'all'}]
  374. process = flexmock()
  375. flexmock(module).should_receive('make_dump_path').and_return('')
  376. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  377. 'resolve_credential',
  378. ).replace_with(lambda value, config: value)
  379. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  380. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  381. flexmock(module).should_receive('execute_dump_command').with_args(
  382. database={'name': 'all'},
  383. config={},
  384. username=None,
  385. password=None,
  386. dump_path=object,
  387. database_names=('foo', 'bar'),
  388. environment={'USER': 'root'},
  389. dry_run=object,
  390. dry_run_label=object,
  391. ).and_return(process).once()
  392. assert module.dump_data_sources(
  393. databases,
  394. {},
  395. config_paths=('test.yaml',),
  396. borgmatic_runtime_directory='/run/borgmatic',
  397. patterns=[],
  398. dry_run=False,
  399. ) == [process]
  400. def test_dump_data_sources_dumps_all_databases_separately_when_format_configured():
  401. databases = [{'name': 'all', 'format': 'sql'}]
  402. processes = [flexmock(), flexmock()]
  403. flexmock(module).should_receive('make_dump_path').and_return('')
  404. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  405. 'resolve_credential',
  406. ).and_return(None)
  407. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  408. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  409. for name, process in zip(('foo', 'bar'), processes):
  410. flexmock(module).should_receive('execute_dump_command').with_args(
  411. database={'name': name, 'format': 'sql'},
  412. config={},
  413. username=None,
  414. password=None,
  415. dump_path=object,
  416. database_names=(name,),
  417. environment={'USER': 'root'},
  418. dry_run=object,
  419. dry_run_label=object,
  420. ).and_return(process).once()
  421. assert (
  422. module.dump_data_sources(
  423. databases,
  424. {},
  425. config_paths=('test.yaml',),
  426. borgmatic_runtime_directory='/run/borgmatic',
  427. patterns=[],
  428. dry_run=False,
  429. )
  430. == processes
  431. )
  432. def test_dump_data_sources_errors_for_missing_all_databases():
  433. databases = [{'name': 'all'}]
  434. flexmock(module).should_receive('make_dump_path').and_return('')
  435. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  436. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  437. 'resolve_credential',
  438. ).replace_with(lambda value, config: value)
  439. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  440. 'databases/localhost/all',
  441. )
  442. flexmock(module).should_receive('database_names_to_dump').and_return(())
  443. with pytest.raises(ValueError):
  444. assert module.dump_data_sources(
  445. databases,
  446. {},
  447. config_paths=('test.yaml',),
  448. borgmatic_runtime_directory='/run/borgmatic',
  449. patterns=[],
  450. dry_run=False,
  451. )
  452. def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
  453. databases = [{'name': 'all'}]
  454. flexmock(module).should_receive('make_dump_path').and_return('')
  455. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  456. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  457. 'resolve_credential',
  458. ).replace_with(lambda value, config: value)
  459. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  460. 'databases/localhost/all',
  461. )
  462. flexmock(module).should_receive('database_names_to_dump').and_return(())
  463. assert (
  464. module.dump_data_sources(
  465. databases,
  466. {},
  467. config_paths=('test.yaml',),
  468. borgmatic_runtime_directory='/run/borgmatic',
  469. patterns=[],
  470. dry_run=True,
  471. )
  472. == []
  473. )
  474. def test_database_names_to_dump_runs_mariadb_with_list_options():
  475. database = {'name': 'all', 'list_options': '--defaults-extra-file=mariadb.cnf --skip-ssl'}
  476. flexmock(module).should_receive('parse_extra_options').and_return(
  477. ('--skip-ssl',),
  478. 'mariadb.cnf',
  479. )
  480. flexmock(module).should_receive('make_defaults_file_options').with_args(
  481. 'root',
  482. 'trustsome1',
  483. 'mariadb.cnf',
  484. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  485. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  486. (
  487. 'mariadb',
  488. '--defaults-extra-file=/dev/fd/99',
  489. '--skip-ssl',
  490. '--skip-column-names',
  491. '--batch',
  492. '--execute',
  493. 'show schemas',
  494. ),
  495. environment=None,
  496. ).and_return('foo\nbar').once()
  497. assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == (
  498. 'foo',
  499. 'bar',
  500. )
  501. def test_database_names_to_dump_runs_non_default_mariadb_with_list_options():
  502. database = {
  503. 'name': 'all',
  504. 'list_options': '--defaults-extra-file=mariadb.cnf --skip-ssl',
  505. 'mariadb_command': 'custom_mariadb',
  506. }
  507. flexmock(module).should_receive('parse_extra_options').and_return(
  508. ('--skip-ssl',),
  509. 'mariadb.cnf',
  510. )
  511. flexmock(module).should_receive('make_defaults_file_options').with_args(
  512. 'root',
  513. 'trustsome1',
  514. 'mariadb.cnf',
  515. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  516. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  517. environment=None,
  518. full_command=(
  519. 'custom_mariadb', # Custom MariaDB command
  520. '--defaults-extra-file=/dev/fd/99',
  521. '--skip-ssl',
  522. '--skip-column-names',
  523. '--batch',
  524. '--execute',
  525. 'show schemas',
  526. ),
  527. ).and_return('foo\nbar').once()
  528. assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == (
  529. 'foo',
  530. 'bar',
  531. )
  532. def test_execute_dump_command_runs_mariadb_dump():
  533. process = flexmock()
  534. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  535. flexmock(module.os.path).should_receive('exists').and_return(False)
  536. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  537. 'resolve_credential',
  538. ).replace_with(lambda value, config: value)
  539. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  540. flexmock(module).should_receive('make_defaults_file_options').with_args(
  541. 'root',
  542. 'trustsome1',
  543. None,
  544. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  545. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  546. flexmock(module).should_receive('execute_command').with_args(
  547. (
  548. 'mariadb-dump',
  549. '--defaults-extra-file=/dev/fd/99',
  550. '--add-drop-database',
  551. '--databases',
  552. 'foo',
  553. '--result-file',
  554. 'dump',
  555. ),
  556. environment=None,
  557. run_to_completion=False,
  558. ).and_return(process).once()
  559. assert (
  560. module.execute_dump_command(
  561. database={'name': 'foo'},
  562. config={},
  563. username='root',
  564. password='trustsome1',
  565. dump_path=flexmock(),
  566. database_names=('foo',),
  567. environment=None,
  568. dry_run=False,
  569. dry_run_label='',
  570. )
  571. == process
  572. )
  573. def test_execute_dump_command_with_environment_password_transport_skips_defaults_file_and_passes_user_flag():
  574. process = flexmock()
  575. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  576. flexmock(module.os.path).should_receive('exists').and_return(False)
  577. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  578. 'resolve_credential',
  579. ).replace_with(lambda value, config: value)
  580. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  581. flexmock(module).should_receive('make_defaults_file_options').never()
  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. '--add-drop-database',
  587. '--user',
  588. 'root',
  589. '--databases',
  590. 'foo',
  591. '--result-file',
  592. 'dump',
  593. ),
  594. environment=None,
  595. run_to_completion=False,
  596. ).and_return(process).once()
  597. assert (
  598. module.execute_dump_command(
  599. database={'name': 'foo', 'password_transport': 'environment'},
  600. config={},
  601. username='root',
  602. password='trustsome1',
  603. dump_path=flexmock(),
  604. database_names=('foo',),
  605. environment=None,
  606. dry_run=False,
  607. dry_run_label='',
  608. )
  609. == process
  610. )
  611. def test_execute_dump_command_runs_mariadb_dump_without_add_drop_database():
  612. process = flexmock()
  613. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  614. flexmock(module.os.path).should_receive('exists').and_return(False)
  615. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  616. 'resolve_credential',
  617. ).replace_with(lambda value, config: value)
  618. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  619. flexmock(module).should_receive('make_defaults_file_options').with_args(
  620. 'root',
  621. 'trustsome1',
  622. None,
  623. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  624. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  625. flexmock(module).should_receive('execute_command').with_args(
  626. (
  627. 'mariadb-dump',
  628. '--defaults-extra-file=/dev/fd/99',
  629. '--databases',
  630. 'foo',
  631. '--result-file',
  632. 'dump',
  633. ),
  634. environment=None,
  635. run_to_completion=False,
  636. ).and_return(process).once()
  637. assert (
  638. module.execute_dump_command(
  639. database={'name': 'foo', 'add_drop_database': False},
  640. config={},
  641. username='root',
  642. password='trustsome1',
  643. dump_path=flexmock(),
  644. database_names=('foo',),
  645. environment=None,
  646. dry_run=False,
  647. dry_run_label='',
  648. )
  649. == process
  650. )
  651. def test_execute_dump_command_runs_mariadb_dump_with_hostname_and_port():
  652. process = flexmock()
  653. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  654. flexmock(module.os.path).should_receive('exists').and_return(False)
  655. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  656. 'resolve_credential',
  657. ).replace_with(lambda value, config: value)
  658. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  659. flexmock(module).should_receive('make_defaults_file_options').with_args(
  660. 'root',
  661. 'trustsome1',
  662. None,
  663. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  664. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  665. flexmock(module).should_receive('execute_command').with_args(
  666. (
  667. 'mariadb-dump',
  668. '--defaults-extra-file=/dev/fd/99',
  669. '--add-drop-database',
  670. '--host',
  671. 'database.example.org',
  672. '--port',
  673. '5433',
  674. '--protocol',
  675. 'tcp',
  676. '--databases',
  677. 'foo',
  678. '--result-file',
  679. 'dump',
  680. ),
  681. environment=None,
  682. run_to_completion=False,
  683. ).and_return(process).once()
  684. assert (
  685. module.execute_dump_command(
  686. database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},
  687. config={},
  688. username='root',
  689. password='trustsome1',
  690. dump_path=flexmock(),
  691. database_names=('foo',),
  692. environment=None,
  693. dry_run=False,
  694. dry_run_label='',
  695. )
  696. == process
  697. )
  698. def test_execute_dump_command_runs_mariadb_dump_with_tls():
  699. process = flexmock()
  700. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  701. flexmock(module.os.path).should_receive('exists').and_return(False)
  702. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  703. 'resolve_credential',
  704. ).replace_with(lambda value, config: value)
  705. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  706. flexmock(module).should_receive('make_defaults_file_options').with_args(
  707. 'root',
  708. 'trustsome1',
  709. None,
  710. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  711. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  712. flexmock(module).should_receive('execute_command').with_args(
  713. (
  714. 'mariadb-dump',
  715. '--defaults-extra-file=/dev/fd/99',
  716. '--add-drop-database',
  717. '--ssl',
  718. '--databases',
  719. 'foo',
  720. '--result-file',
  721. 'dump',
  722. ),
  723. environment=None,
  724. run_to_completion=False,
  725. ).and_return(process).once()
  726. assert (
  727. module.execute_dump_command(
  728. database={'name': 'foo', 'tls': True},
  729. config={},
  730. username='root',
  731. password='trustsome1',
  732. dump_path=flexmock(),
  733. database_names=('foo',),
  734. environment=None,
  735. dry_run=False,
  736. dry_run_label='',
  737. )
  738. == process
  739. )
  740. def test_execute_dump_command_runs_mariadb_dump_without_tls():
  741. process = flexmock()
  742. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  743. flexmock(module.os.path).should_receive('exists').and_return(False)
  744. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  745. 'resolve_credential',
  746. ).replace_with(lambda value, config: value)
  747. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  748. flexmock(module).should_receive('make_defaults_file_options').with_args(
  749. 'root',
  750. 'trustsome1',
  751. None,
  752. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  753. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  754. flexmock(module).should_receive('execute_command').with_args(
  755. (
  756. 'mariadb-dump',
  757. '--defaults-extra-file=/dev/fd/99',
  758. '--add-drop-database',
  759. '--skip-ssl',
  760. '--databases',
  761. 'foo',
  762. '--result-file',
  763. 'dump',
  764. ),
  765. environment=None,
  766. run_to_completion=False,
  767. ).and_return(process).once()
  768. assert (
  769. module.execute_dump_command(
  770. database={'name': 'foo', 'tls': False},
  771. config={},
  772. username='root',
  773. password='trustsome1',
  774. dump_path=flexmock(),
  775. database_names=('foo',),
  776. environment=None,
  777. dry_run=False,
  778. dry_run_label='',
  779. )
  780. == process
  781. )
  782. def test_execute_dump_command_runs_mariadb_dump_with_username_and_password():
  783. process = flexmock()
  784. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  785. flexmock(module.os.path).should_receive('exists').and_return(False)
  786. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  787. 'resolve_credential',
  788. ).replace_with(lambda value, config: value)
  789. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  790. flexmock(module).should_receive('make_defaults_file_options').with_args(
  791. 'root',
  792. 'trustsome1',
  793. None,
  794. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  795. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  796. flexmock(module).should_receive('execute_command').with_args(
  797. (
  798. 'mariadb-dump',
  799. '--defaults-extra-file=/dev/fd/99',
  800. '--add-drop-database',
  801. '--databases',
  802. 'foo',
  803. '--result-file',
  804. 'dump',
  805. ),
  806. environment={},
  807. run_to_completion=False,
  808. ).and_return(process).once()
  809. assert (
  810. module.execute_dump_command(
  811. database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},
  812. config={},
  813. username='root',
  814. password='trustsome1',
  815. dump_path=flexmock(),
  816. database_names=('foo',),
  817. environment={},
  818. dry_run=False,
  819. dry_run_label='',
  820. )
  821. == process
  822. )
  823. def test_execute_dump_command_runs_mariadb_dump_with_options():
  824. process = flexmock()
  825. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  826. flexmock(module.os.path).should_receive('exists').and_return(False)
  827. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  828. 'resolve_credential',
  829. ).replace_with(lambda value, config: value)
  830. flexmock(module).should_receive('parse_extra_options').and_return(('--stuff=such',), None)
  831. flexmock(module).should_receive('make_defaults_file_options').with_args(
  832. 'root',
  833. 'trustsome1',
  834. None,
  835. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  836. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  837. flexmock(module).should_receive('execute_command').with_args(
  838. (
  839. 'mariadb-dump',
  840. '--defaults-extra-file=/dev/fd/99',
  841. '--stuff=such',
  842. '--add-drop-database',
  843. '--databases',
  844. 'foo',
  845. '--result-file',
  846. 'dump',
  847. ),
  848. environment=None,
  849. run_to_completion=False,
  850. ).and_return(process).once()
  851. assert (
  852. module.execute_dump_command(
  853. database={'name': 'foo', 'options': '--stuff=such'},
  854. config={},
  855. username='root',
  856. password='trustsome1',
  857. dump_path=flexmock(),
  858. database_names=('foo',),
  859. environment=None,
  860. dry_run=False,
  861. dry_run_label='',
  862. )
  863. == process
  864. )
  865. def test_execute_dump_command_runs_non_default_mariadb_dump_with_options():
  866. process = flexmock()
  867. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  868. flexmock(module.os.path).should_receive('exists').and_return(False)
  869. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  870. 'resolve_credential',
  871. ).replace_with(lambda value, config: value)
  872. flexmock(module).should_receive('parse_extra_options').and_return(('--stuff=such',), None)
  873. flexmock(module).should_receive('make_defaults_file_options').with_args(
  874. 'root',
  875. 'trustsome1',
  876. None,
  877. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  878. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  879. flexmock(module).should_receive('execute_command').with_args(
  880. (
  881. 'custom_mariadb_dump', # Custom MariaDB dump command
  882. '--defaults-extra-file=/dev/fd/99',
  883. '--stuff=such',
  884. '--add-drop-database',
  885. '--databases',
  886. 'foo',
  887. '--result-file',
  888. 'dump',
  889. ),
  890. environment=None,
  891. run_to_completion=False,
  892. ).and_return(process).once()
  893. assert (
  894. module.execute_dump_command(
  895. database={
  896. 'name': 'foo',
  897. 'mariadb_dump_command': 'custom_mariadb_dump',
  898. 'options': '--stuff=such',
  899. }, # Custom MariaDB dump command specified
  900. config={},
  901. username='root',
  902. password='trustsome1',
  903. dump_path=flexmock(),
  904. database_names=('foo',),
  905. environment=None,
  906. dry_run=False,
  907. dry_run_label='',
  908. )
  909. == process
  910. )
  911. def test_execute_dump_command_with_duplicate_dump_skips_mariadb_dump():
  912. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  913. flexmock(module.os.path).should_receive('exists').and_return(True)
  914. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  915. flexmock(module).should_receive('make_defaults_file_options').with_args(
  916. 'root',
  917. 'trustsome1',
  918. None,
  919. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  920. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  921. flexmock(module).should_receive('execute_command').never()
  922. assert (
  923. module.execute_dump_command(
  924. database={'name': 'foo'},
  925. config={},
  926. username='root',
  927. password='trustsome1',
  928. dump_path=flexmock(),
  929. database_names=('foo',),
  930. environment=None,
  931. dry_run=True,
  932. dry_run_label='SO DRY',
  933. )
  934. is None
  935. )
  936. def test_execute_dump_command_with_dry_run_skips_mariadb_dump():
  937. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  938. flexmock(module.os.path).should_receive('exists').and_return(False)
  939. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  940. 'resolve_credential',
  941. ).replace_with(lambda value, config: value)
  942. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  943. flexmock(module).should_receive('make_defaults_file_options').with_args(
  944. 'root',
  945. 'trustsome1',
  946. None,
  947. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  948. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  949. flexmock(module).should_receive('execute_command').never()
  950. assert (
  951. module.execute_dump_command(
  952. database={'name': 'foo'},
  953. config={},
  954. username='root',
  955. password='trustsome1',
  956. dump_path=flexmock(),
  957. database_names=('foo',),
  958. environment=None,
  959. dry_run=True,
  960. dry_run_label='SO DRY',
  961. )
  962. is None
  963. )
  964. def test_restore_data_source_dump_runs_mariadb_to_restore():
  965. hook_config = [{'name': 'foo'}, {'name': 'bar'}]
  966. extract_process = flexmock(stdout=flexmock())
  967. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  968. 'resolve_credential',
  969. ).replace_with(lambda value, config: value)
  970. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  971. flexmock(module).should_receive('make_defaults_file_options').with_args(
  972. None,
  973. None,
  974. None,
  975. ).and_return(())
  976. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  977. flexmock(module).should_receive('execute_command_with_processes').with_args(
  978. ('mariadb', '--batch'),
  979. processes=[extract_process],
  980. output_log_level=logging.DEBUG,
  981. input_file=extract_process.stdout,
  982. environment={'USER': 'root'},
  983. ).once()
  984. module.restore_data_source_dump(
  985. hook_config,
  986. {},
  987. data_source={'name': 'foo'},
  988. dry_run=False,
  989. extract_process=extract_process,
  990. connection_params={
  991. 'hostname': None,
  992. 'port': None,
  993. 'username': None,
  994. 'password': None,
  995. },
  996. borgmatic_runtime_directory='/run/borgmatic',
  997. )
  998. def test_restore_data_source_dump_runs_mariadb_with_options():
  999. hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
  1000. extract_process = flexmock(stdout=flexmock())
  1001. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1002. 'resolve_credential',
  1003. ).replace_with(lambda value, config: value)
  1004. flexmock(module).should_receive('parse_extra_options').and_return(('--harder',), None)
  1005. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1006. None,
  1007. None,
  1008. None,
  1009. ).and_return(())
  1010. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1011. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1012. ('mariadb', '--harder', '--batch'),
  1013. processes=[extract_process],
  1014. output_log_level=logging.DEBUG,
  1015. input_file=extract_process.stdout,
  1016. environment={'USER': 'root'},
  1017. ).once()
  1018. module.restore_data_source_dump(
  1019. hook_config,
  1020. {},
  1021. data_source=hook_config[0],
  1022. dry_run=False,
  1023. extract_process=extract_process,
  1024. connection_params={
  1025. 'hostname': None,
  1026. 'port': None,
  1027. 'username': None,
  1028. 'password': None,
  1029. },
  1030. borgmatic_runtime_directory='/run/borgmatic',
  1031. )
  1032. def test_restore_data_source_dump_runs_non_default_mariadb_with_options():
  1033. hook_config = [
  1034. {'name': 'foo', 'restore_options': '--harder', 'mariadb_command': 'custom_mariadb'},
  1035. ]
  1036. extract_process = flexmock(stdout=flexmock())
  1037. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1038. 'resolve_credential',
  1039. ).replace_with(lambda value, config: value)
  1040. flexmock(module).should_receive('parse_extra_options').and_return(('--harder',), None)
  1041. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1042. None,
  1043. None,
  1044. None,
  1045. ).and_return(())
  1046. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1047. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1048. ('custom_mariadb', '--harder', '--batch'),
  1049. processes=[extract_process],
  1050. output_log_level=logging.DEBUG,
  1051. input_file=extract_process.stdout,
  1052. environment={'USER': 'root'},
  1053. ).once()
  1054. module.restore_data_source_dump(
  1055. hook_config,
  1056. {},
  1057. data_source=hook_config[0],
  1058. dry_run=False,
  1059. extract_process=extract_process,
  1060. connection_params={
  1061. 'hostname': None,
  1062. 'port': None,
  1063. 'username': None,
  1064. 'password': None,
  1065. },
  1066. borgmatic_runtime_directory='/run/borgmatic',
  1067. )
  1068. def test_restore_data_source_dump_runs_mariadb_with_hostname_and_port():
  1069. hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  1070. extract_process = flexmock(stdout=flexmock())
  1071. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1072. 'resolve_credential',
  1073. ).replace_with(lambda value, config: value)
  1074. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1075. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1076. None,
  1077. None,
  1078. None,
  1079. ).and_return(())
  1080. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1081. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1082. (
  1083. 'mariadb',
  1084. '--batch',
  1085. '--host',
  1086. 'database.example.org',
  1087. '--port',
  1088. '5433',
  1089. '--protocol',
  1090. 'tcp',
  1091. ),
  1092. processes=[extract_process],
  1093. output_log_level=logging.DEBUG,
  1094. input_file=extract_process.stdout,
  1095. environment={'USER': 'root'},
  1096. ).once()
  1097. module.restore_data_source_dump(
  1098. hook_config,
  1099. {},
  1100. data_source=hook_config[0],
  1101. dry_run=False,
  1102. extract_process=extract_process,
  1103. connection_params={
  1104. 'hostname': None,
  1105. 'port': None,
  1106. 'username': None,
  1107. 'password': None,
  1108. },
  1109. borgmatic_runtime_directory='/run/borgmatic',
  1110. )
  1111. def test_restore_data_source_dump_runs_mariadb_with_tls():
  1112. hook_config = [{'name': 'foo', 'tls': True}]
  1113. extract_process = flexmock(stdout=flexmock())
  1114. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1115. 'resolve_credential',
  1116. ).replace_with(lambda value, config: value)
  1117. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1118. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1119. None,
  1120. None,
  1121. None,
  1122. ).and_return(())
  1123. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1124. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1125. (
  1126. 'mariadb',
  1127. '--batch',
  1128. '--ssl',
  1129. ),
  1130. processes=[extract_process],
  1131. output_log_level=logging.DEBUG,
  1132. input_file=extract_process.stdout,
  1133. environment={'USER': 'root'},
  1134. ).once()
  1135. module.restore_data_source_dump(
  1136. hook_config,
  1137. {},
  1138. data_source=hook_config[0],
  1139. dry_run=False,
  1140. extract_process=extract_process,
  1141. connection_params={
  1142. 'hostname': None,
  1143. 'port': None,
  1144. 'username': None,
  1145. 'password': None,
  1146. },
  1147. borgmatic_runtime_directory='/run/borgmatic',
  1148. )
  1149. def test_restore_data_source_dump_runs_mariadb_without_tls():
  1150. hook_config = [{'name': 'foo', 'tls': False}]
  1151. extract_process = flexmock(stdout=flexmock())
  1152. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1153. 'resolve_credential',
  1154. ).replace_with(lambda value, config: value)
  1155. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1156. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1157. None,
  1158. None,
  1159. None,
  1160. ).and_return(())
  1161. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1162. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1163. (
  1164. 'mariadb',
  1165. '--batch',
  1166. '--skip-ssl',
  1167. ),
  1168. processes=[extract_process],
  1169. output_log_level=logging.DEBUG,
  1170. input_file=extract_process.stdout,
  1171. environment={'USER': 'root'},
  1172. ).once()
  1173. module.restore_data_source_dump(
  1174. hook_config,
  1175. {},
  1176. data_source=hook_config[0],
  1177. dry_run=False,
  1178. extract_process=extract_process,
  1179. connection_params={
  1180. 'hostname': None,
  1181. 'port': None,
  1182. 'username': None,
  1183. 'password': None,
  1184. },
  1185. borgmatic_runtime_directory='/run/borgmatic',
  1186. )
  1187. def test_restore_data_source_dump_runs_mariadb_with_username_and_password():
  1188. hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  1189. extract_process = flexmock(stdout=flexmock())
  1190. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1191. 'resolve_credential',
  1192. ).replace_with(lambda value, config: value)
  1193. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1194. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1195. 'root',
  1196. 'trustsome1',
  1197. None,
  1198. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  1199. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1200. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1201. ('mariadb', '--defaults-extra-file=/dev/fd/99', '--batch'),
  1202. processes=[extract_process],
  1203. output_log_level=logging.DEBUG,
  1204. input_file=extract_process.stdout,
  1205. environment={'USER': 'root'},
  1206. ).once()
  1207. module.restore_data_source_dump(
  1208. hook_config,
  1209. {},
  1210. data_source=hook_config[0],
  1211. dry_run=False,
  1212. extract_process=extract_process,
  1213. connection_params={
  1214. 'hostname': None,
  1215. 'port': None,
  1216. 'username': None,
  1217. 'password': None,
  1218. },
  1219. borgmatic_runtime_directory='/run/borgmatic',
  1220. )
  1221. def test_restore_data_source_with_environment_password_transport_skips_defaults_file_and_passes_user_flag():
  1222. hook_config = [
  1223. {
  1224. 'name': 'foo',
  1225. 'username': 'root',
  1226. 'password': 'trustsome1',
  1227. 'password_transport': 'environment',
  1228. },
  1229. ]
  1230. extract_process = flexmock(stdout=flexmock())
  1231. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1232. 'resolve_credential',
  1233. ).replace_with(lambda value, config: value)
  1234. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1235. flexmock(module).should_receive('make_defaults_file_options').never()
  1236. flexmock(module.os).should_receive('environ').and_return(
  1237. {'USER': 'root', 'MYSQL_PWD': 'trustsome1'},
  1238. )
  1239. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1240. ('mariadb', '--batch', '--user', 'root'),
  1241. processes=[extract_process],
  1242. output_log_level=logging.DEBUG,
  1243. input_file=extract_process.stdout,
  1244. environment={'USER': 'root', 'MYSQL_PWD': 'trustsome1'},
  1245. ).once()
  1246. module.restore_data_source_dump(
  1247. hook_config,
  1248. {},
  1249. data_source=hook_config[0],
  1250. dry_run=False,
  1251. extract_process=extract_process,
  1252. connection_params={
  1253. 'hostname': None,
  1254. 'port': None,
  1255. 'username': None,
  1256. 'password': None,
  1257. },
  1258. borgmatic_runtime_directory='/run/borgmatic',
  1259. )
  1260. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  1261. hook_config = [
  1262. {
  1263. 'name': 'foo',
  1264. 'username': 'root',
  1265. 'password': 'trustsome1',
  1266. 'restore_hostname': 'restorehost',
  1267. 'restore_port': 'restoreport',
  1268. 'restore_username': 'restoreusername',
  1269. 'restore_password': 'restorepassword',
  1270. },
  1271. ]
  1272. extract_process = flexmock(stdout=flexmock())
  1273. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1274. 'resolve_credential',
  1275. ).replace_with(lambda value, config: value)
  1276. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1277. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1278. 'cliusername',
  1279. 'clipassword',
  1280. None,
  1281. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  1282. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1283. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1284. (
  1285. 'mariadb',
  1286. '--defaults-extra-file=/dev/fd/99',
  1287. '--batch',
  1288. '--host',
  1289. 'clihost',
  1290. '--port',
  1291. 'cliport',
  1292. '--protocol',
  1293. 'tcp',
  1294. ),
  1295. processes=[extract_process],
  1296. output_log_level=logging.DEBUG,
  1297. input_file=extract_process.stdout,
  1298. environment={'USER': 'root'},
  1299. ).once()
  1300. module.restore_data_source_dump(
  1301. hook_config,
  1302. {},
  1303. data_source=hook_config[0],
  1304. dry_run=False,
  1305. extract_process=extract_process,
  1306. connection_params={
  1307. 'hostname': 'clihost',
  1308. 'port': 'cliport',
  1309. 'username': 'cliusername',
  1310. 'password': 'clipassword',
  1311. },
  1312. borgmatic_runtime_directory='/run/borgmatic',
  1313. )
  1314. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  1315. hook_config = [
  1316. {
  1317. 'name': 'foo',
  1318. 'username': 'root',
  1319. 'password': 'trustsome1',
  1320. 'hostname': 'dbhost',
  1321. 'port': 'dbport',
  1322. 'tls': True,
  1323. 'restore_username': 'restoreuser',
  1324. 'restore_password': 'restorepass',
  1325. 'restore_hostname': 'restorehost',
  1326. 'restore_port': 'restoreport',
  1327. 'restore_tls': False,
  1328. },
  1329. ]
  1330. extract_process = flexmock(stdout=flexmock())
  1331. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1332. 'resolve_credential',
  1333. ).replace_with(lambda value, config: value)
  1334. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1335. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1336. 'restoreuser',
  1337. 'restorepass',
  1338. None,
  1339. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  1340. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1341. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1342. (
  1343. 'mariadb',
  1344. '--defaults-extra-file=/dev/fd/99',
  1345. '--batch',
  1346. '--host',
  1347. 'restorehost',
  1348. '--port',
  1349. 'restoreport',
  1350. '--protocol',
  1351. 'tcp',
  1352. '--skip-ssl',
  1353. ),
  1354. processes=[extract_process],
  1355. output_log_level=logging.DEBUG,
  1356. input_file=extract_process.stdout,
  1357. environment={'USER': 'root'},
  1358. ).once()
  1359. module.restore_data_source_dump(
  1360. hook_config,
  1361. {},
  1362. data_source=hook_config[0],
  1363. dry_run=False,
  1364. extract_process=extract_process,
  1365. connection_params={
  1366. 'hostname': None,
  1367. 'port': None,
  1368. 'username': None,
  1369. 'password': None,
  1370. },
  1371. borgmatic_runtime_directory='/run/borgmatic',
  1372. )
  1373. def test_restore_data_source_dump_with_dry_run_skips_restore():
  1374. hook_config = [{'name': 'foo'}]
  1375. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1376. 'resolve_credential',
  1377. ).replace_with(lambda value, config: value)
  1378. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1379. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1380. None,
  1381. None,
  1382. None,
  1383. ).and_return(())
  1384. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1385. flexmock(module).should_receive('execute_command_with_processes').never()
  1386. module.restore_data_source_dump(
  1387. hook_config,
  1388. {},
  1389. data_source={'name': 'foo'},
  1390. dry_run=True,
  1391. extract_process=flexmock(),
  1392. connection_params={
  1393. 'hostname': None,
  1394. 'port': None,
  1395. 'username': None,
  1396. 'password': None,
  1397. },
  1398. borgmatic_runtime_directory='/run/borgmatic',
  1399. )