test_mariadb.py 33 KB

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