test_mysql.py 34 KB

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