test_mariadb.py 37 KB

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