test_mariadb.py 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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_escapes_password_containing_backslash():
  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\n' + br'password="trust\\nsome1"'
  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=r'trust\nsome1') == (
  42. '--defaults-extra-file=/dev/fd/99',
  43. )
  44. def test_make_defaults_file_pipe_with_only_username_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]\nuser=root'
  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='root', password=None) == (
  54. '--defaults-extra-file=/dev/fd/99',
  55. )
  56. def test_make_defaults_file_pipe_with_only_password_writes_it_to_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'[client]\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(username=None, password='trustsome1') == (
  66. '--defaults-extra-file=/dev/fd/99',
  67. )
  68. def test_make_defaults_file_option_with_defaults_extra_filename_includes_it_in_file_descriptor():
  69. read_descriptor = 99
  70. write_descriptor = flexmock()
  71. flexmock(module.os).should_receive('pipe').and_return(read_descriptor, write_descriptor)
  72. flexmock(module.os).should_receive('write').with_args(
  73. write_descriptor, b'!include extra.cnf\n[client]\nuser=root\npassword="trustsome1"'
  74. ).once()
  75. flexmock(module.os).should_receive('close')
  76. flexmock(module.os).should_receive('set_inheritable')
  77. assert module.make_defaults_file_options(
  78. username='root', password='trustsome1', defaults_extra_filename='extra.cnf'
  79. ) == ('--defaults-extra-file=/dev/fd/99',)
  80. def test_make_defaults_file_option_with_only_defaults_extra_filename_uses_it_instead_of_file_descriptor():
  81. flexmock(module.os).should_receive('pipe').never()
  82. assert module.make_defaults_file_options(
  83. username=None, password=None, defaults_extra_filename='extra.cnf'
  84. ) == ('--defaults-extra-file=extra.cnf',)
  85. def test_database_names_to_dump_passes_through_name():
  86. environment = flexmock()
  87. names = module.database_names_to_dump(
  88. {'name': 'foo'}, {}, 'root', 'trustsome1', environment, dry_run=False
  89. )
  90. assert names == ('foo',)
  91. def test_database_names_to_dump_bails_for_dry_run():
  92. environment = flexmock()
  93. flexmock(module).should_receive('execute_command_and_capture_output').never()
  94. names = module.database_names_to_dump(
  95. {'name': 'all'}, {}, 'root', 'trustsome1', environment, dry_run=True
  96. )
  97. assert names == ()
  98. def test_database_names_to_dump_queries_mariadb_for_database_names():
  99. environment = flexmock()
  100. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  101. 'resolve_credential'
  102. ).replace_with(lambda value, config: value)
  103. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  104. flexmock(module).should_receive('make_defaults_file_options').with_args(
  105. 'root', 'trustsome1', None
  106. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  107. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  108. (
  109. 'mariadb',
  110. '--defaults-extra-file=/dev/fd/99',
  111. '--skip-column-names',
  112. '--batch',
  113. '--execute',
  114. 'show schemas',
  115. ),
  116. environment=environment,
  117. ).and_return('foo\nbar\nmysql\n').once()
  118. names = module.database_names_to_dump(
  119. {'name': 'all'}, {}, 'root', 'trustsome1', environment, dry_run=False
  120. )
  121. assert names == ('foo', 'bar')
  122. def test_database_names_to_dump_runs_mariadb_with_tls():
  123. environment = flexmock()
  124. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  125. 'resolve_credential'
  126. ).replace_with(lambda value, config: value)
  127. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  128. flexmock(module).should_receive('make_defaults_file_options').with_args(
  129. 'root', 'trustsome1', None
  130. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  131. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  132. (
  133. 'mariadb',
  134. '--defaults-extra-file=/dev/fd/99',
  135. '--ssl',
  136. '--skip-column-names',
  137. '--batch',
  138. '--execute',
  139. 'show schemas',
  140. ),
  141. environment=environment,
  142. ).and_return('foo\nbar\nmysql\n').once()
  143. names = module.database_names_to_dump(
  144. {'name': 'all', 'tls': True}, {}, 'root', 'trustsome1', environment, dry_run=False
  145. )
  146. assert names == ('foo', 'bar')
  147. def test_database_names_to_dump_runs_mariadb_without_tls():
  148. environment = flexmock()
  149. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  150. 'resolve_credential'
  151. ).replace_with(lambda value, config: value)
  152. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  153. flexmock(module).should_receive('make_defaults_file_options').with_args(
  154. 'root', 'trustsome1', None
  155. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  156. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  157. (
  158. 'mariadb',
  159. '--defaults-extra-file=/dev/fd/99',
  160. '--skip-ssl',
  161. '--skip-column-names',
  162. '--batch',
  163. '--execute',
  164. 'show schemas',
  165. ),
  166. environment=environment,
  167. ).and_return('foo\nbar\nmysql\n').once()
  168. names = module.database_names_to_dump(
  169. {'name': 'all', 'tls': False}, {}, 'root', 'trustsome1', environment, dry_run=False
  170. )
  171. assert names == ('foo', 'bar')
  172. def test_use_streaming_true_for_any_databases():
  173. assert module.use_streaming(
  174. databases=[flexmock(), flexmock()],
  175. config=flexmock(),
  176. )
  177. def test_use_streaming_false_for_no_databases():
  178. assert not module.use_streaming(databases=[], config=flexmock())
  179. def test_dump_data_sources_dumps_each_database():
  180. databases = [{'name': 'foo'}, {'name': 'bar'}]
  181. processes = [flexmock(), flexmock()]
  182. flexmock(module).should_receive('make_dump_path').and_return('')
  183. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  184. 'resolve_credential'
  185. ).and_return(None)
  186. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  187. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  188. 'resolve_credential'
  189. ).replace_with(lambda value, config: value)
  190. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  191. ('bar',)
  192. )
  193. for name, process in zip(('foo', 'bar'), processes):
  194. flexmock(module).should_receive('execute_dump_command').with_args(
  195. database={'name': name},
  196. config={},
  197. username=None,
  198. password=None,
  199. dump_path=object,
  200. database_names=(name,),
  201. environment={'USER': 'root'},
  202. dry_run=object,
  203. dry_run_label=object,
  204. ).and_return(process).once()
  205. assert (
  206. module.dump_data_sources(
  207. databases,
  208. {},
  209. config_paths=('test.yaml',),
  210. borgmatic_runtime_directory='/run/borgmatic',
  211. patterns=[],
  212. dry_run=False,
  213. )
  214. == processes
  215. )
  216. def test_dump_data_sources_dumps_with_password():
  217. database = {'name': 'foo', 'username': 'root', 'password': 'trustsome1'}
  218. process = flexmock()
  219. flexmock(module).should_receive('make_dump_path').and_return('')
  220. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  221. 'resolve_credential'
  222. ).replace_with(lambda value, config: value)
  223. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  224. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  225. ('bar',)
  226. )
  227. flexmock(module).should_receive('execute_dump_command').with_args(
  228. database=database,
  229. config={},
  230. username='root',
  231. password='trustsome1',
  232. dump_path=object,
  233. database_names=('foo',),
  234. environment={'USER': 'root'},
  235. dry_run=object,
  236. dry_run_label=object,
  237. ).and_return(process).once()
  238. assert module.dump_data_sources(
  239. [database],
  240. {},
  241. config_paths=('test.yaml',),
  242. borgmatic_runtime_directory='/run/borgmatic',
  243. patterns=[],
  244. dry_run=False,
  245. ) == [process]
  246. def test_dump_data_sources_dumps_all_databases_at_once():
  247. databases = [{'name': 'all'}]
  248. process = flexmock()
  249. flexmock(module).should_receive('make_dump_path').and_return('')
  250. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  251. 'resolve_credential'
  252. ).replace_with(lambda value, config: value)
  253. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  254. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  255. flexmock(module).should_receive('execute_dump_command').with_args(
  256. database={'name': 'all'},
  257. config={},
  258. username=None,
  259. password=None,
  260. dump_path=object,
  261. database_names=('foo', 'bar'),
  262. environment={'USER': 'root'},
  263. dry_run=object,
  264. dry_run_label=object,
  265. ).and_return(process).once()
  266. assert module.dump_data_sources(
  267. databases,
  268. {},
  269. config_paths=('test.yaml',),
  270. borgmatic_runtime_directory='/run/borgmatic',
  271. patterns=[],
  272. dry_run=False,
  273. ) == [process]
  274. def test_dump_data_sources_dumps_all_databases_separately_when_format_configured():
  275. databases = [{'name': 'all', 'format': 'sql'}]
  276. processes = [flexmock(), flexmock()]
  277. flexmock(module).should_receive('make_dump_path').and_return('')
  278. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  279. 'resolve_credential'
  280. ).and_return(None)
  281. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  282. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  283. for name, process in zip(('foo', 'bar'), processes):
  284. flexmock(module).should_receive('execute_dump_command').with_args(
  285. database={'name': name, 'format': 'sql'},
  286. config={},
  287. username=None,
  288. password=None,
  289. dump_path=object,
  290. database_names=(name,),
  291. environment={'USER': 'root'},
  292. dry_run=object,
  293. dry_run_label=object,
  294. ).and_return(process).once()
  295. assert (
  296. module.dump_data_sources(
  297. databases,
  298. {},
  299. config_paths=('test.yaml',),
  300. borgmatic_runtime_directory='/run/borgmatic',
  301. patterns=[],
  302. dry_run=False,
  303. )
  304. == processes
  305. )
  306. def test_database_names_to_dump_runs_mariadb_with_list_options():
  307. database = {'name': 'all', 'list_options': '--defaults-extra-file=mariadb.cnf --skip-ssl'}
  308. flexmock(module).should_receive('parse_extra_options').and_return(
  309. ('--skip-ssl',), 'mariadb.cnf'
  310. )
  311. flexmock(module).should_receive('make_defaults_file_options').with_args(
  312. 'root', 'trustsome1', 'mariadb.cnf'
  313. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  314. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  315. (
  316. 'mariadb',
  317. '--defaults-extra-file=/dev/fd/99',
  318. '--skip-ssl',
  319. '--skip-column-names',
  320. '--batch',
  321. '--execute',
  322. 'show schemas',
  323. ),
  324. environment=None,
  325. ).and_return(('foo\nbar')).once()
  326. assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == (
  327. 'foo',
  328. 'bar',
  329. )
  330. def test_database_names_to_dump_runs_non_default_mariadb_with_list_options():
  331. database = {
  332. 'name': 'all',
  333. 'list_options': '--defaults-extra-file=mariadb.cnf --skip-ssl',
  334. 'mariadb_command': 'custom_mariadb',
  335. }
  336. flexmock(module).should_receive('parse_extra_options').and_return(
  337. ('--skip-ssl',), 'mariadb.cnf'
  338. )
  339. flexmock(module).should_receive('make_defaults_file_options').with_args(
  340. 'root', 'trustsome1', 'mariadb.cnf'
  341. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  342. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  343. environment=None,
  344. full_command=(
  345. 'custom_mariadb', # Custom MariaDB command
  346. '--defaults-extra-file=/dev/fd/99',
  347. '--skip-ssl',
  348. '--skip-column-names',
  349. '--batch',
  350. '--execute',
  351. 'show schemas',
  352. ),
  353. ).and_return(('foo\nbar')).once()
  354. assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == (
  355. 'foo',
  356. 'bar',
  357. )
  358. def test_execute_dump_command_runs_mariadb_dump():
  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).should_receive('parse_extra_options').and_return((), None)
  366. flexmock(module).should_receive('make_defaults_file_options').with_args(
  367. 'root', 'trustsome1', None
  368. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  369. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  370. flexmock(module).should_receive('execute_command').with_args(
  371. (
  372. 'mariadb-dump',
  373. '--defaults-extra-file=/dev/fd/99',
  374. '--add-drop-database',
  375. '--databases',
  376. 'foo',
  377. '--result-file',
  378. 'dump',
  379. ),
  380. environment=None,
  381. run_to_completion=False,
  382. ).and_return(process).once()
  383. assert (
  384. module.execute_dump_command(
  385. database={'name': 'foo'},
  386. config={},
  387. username='root',
  388. password='trustsome1',
  389. dump_path=flexmock(),
  390. database_names=('foo',),
  391. environment=None,
  392. dry_run=False,
  393. dry_run_label='',
  394. )
  395. == process
  396. )
  397. def test_execute_dump_command_runs_mariadb_dump_without_add_drop_database():
  398. process = flexmock()
  399. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  400. flexmock(module.os.path).should_receive('exists').and_return(False)
  401. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  402. 'resolve_credential'
  403. ).replace_with(lambda value, config: value)
  404. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  405. flexmock(module).should_receive('make_defaults_file_options').with_args(
  406. 'root', 'trustsome1', None
  407. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  408. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  409. flexmock(module).should_receive('execute_command').with_args(
  410. (
  411. 'mariadb-dump',
  412. '--defaults-extra-file=/dev/fd/99',
  413. '--databases',
  414. 'foo',
  415. '--result-file',
  416. 'dump',
  417. ),
  418. environment=None,
  419. run_to_completion=False,
  420. ).and_return(process).once()
  421. assert (
  422. module.execute_dump_command(
  423. database={'name': 'foo', 'add_drop_database': False},
  424. config={},
  425. username='root',
  426. password='trustsome1',
  427. dump_path=flexmock(),
  428. database_names=('foo',),
  429. environment=None,
  430. dry_run=False,
  431. dry_run_label='',
  432. )
  433. == process
  434. )
  435. def test_execute_dump_command_runs_mariadb_dump_with_hostname_and_port():
  436. process = flexmock()
  437. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  438. flexmock(module.os.path).should_receive('exists').and_return(False)
  439. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  440. 'resolve_credential'
  441. ).replace_with(lambda value, config: value)
  442. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  443. flexmock(module).should_receive('make_defaults_file_options').with_args(
  444. 'root', 'trustsome1', None
  445. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  446. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  447. flexmock(module).should_receive('execute_command').with_args(
  448. (
  449. 'mariadb-dump',
  450. '--defaults-extra-file=/dev/fd/99',
  451. '--add-drop-database',
  452. '--host',
  453. 'database.example.org',
  454. '--port',
  455. '5433',
  456. '--protocol',
  457. 'tcp',
  458. '--databases',
  459. 'foo',
  460. '--result-file',
  461. 'dump',
  462. ),
  463. environment=None,
  464. run_to_completion=False,
  465. ).and_return(process).once()
  466. assert (
  467. module.execute_dump_command(
  468. database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},
  469. config={},
  470. username='root',
  471. password='trustsome1',
  472. dump_path=flexmock(),
  473. database_names=('foo',),
  474. environment=None,
  475. dry_run=False,
  476. dry_run_label='',
  477. )
  478. == process
  479. )
  480. def test_execute_dump_command_runs_mariadb_dump_with_tls():
  481. process = flexmock()
  482. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  483. flexmock(module.os.path).should_receive('exists').and_return(False)
  484. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  485. 'resolve_credential'
  486. ).replace_with(lambda value, config: value)
  487. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  488. flexmock(module).should_receive('make_defaults_file_options').with_args(
  489. 'root', 'trustsome1', None
  490. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  491. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  492. flexmock(module).should_receive('execute_command').with_args(
  493. (
  494. 'mariadb-dump',
  495. '--defaults-extra-file=/dev/fd/99',
  496. '--add-drop-database',
  497. '--ssl',
  498. '--databases',
  499. 'foo',
  500. '--result-file',
  501. 'dump',
  502. ),
  503. environment=None,
  504. run_to_completion=False,
  505. ).and_return(process).once()
  506. assert (
  507. module.execute_dump_command(
  508. database={'name': 'foo', 'tls': True},
  509. config={},
  510. username='root',
  511. password='trustsome1',
  512. dump_path=flexmock(),
  513. database_names=('foo',),
  514. environment=None,
  515. dry_run=False,
  516. dry_run_label='',
  517. )
  518. == process
  519. )
  520. def test_execute_dump_command_runs_mariadb_dump_without_tls():
  521. process = flexmock()
  522. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  523. flexmock(module.os.path).should_receive('exists').and_return(False)
  524. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  525. 'resolve_credential'
  526. ).replace_with(lambda value, config: value)
  527. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  528. flexmock(module).should_receive('make_defaults_file_options').with_args(
  529. 'root', 'trustsome1', None
  530. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  531. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  532. flexmock(module).should_receive('execute_command').with_args(
  533. (
  534. 'mariadb-dump',
  535. '--defaults-extra-file=/dev/fd/99',
  536. '--add-drop-database',
  537. '--skip-ssl',
  538. '--databases',
  539. 'foo',
  540. '--result-file',
  541. 'dump',
  542. ),
  543. environment=None,
  544. run_to_completion=False,
  545. ).and_return(process).once()
  546. assert (
  547. module.execute_dump_command(
  548. database={'name': 'foo', 'tls': False},
  549. config={},
  550. username='root',
  551. password='trustsome1',
  552. dump_path=flexmock(),
  553. database_names=('foo',),
  554. environment=None,
  555. dry_run=False,
  556. dry_run_label='',
  557. )
  558. == process
  559. )
  560. def test_execute_dump_command_runs_mariadb_dump_with_username_and_password():
  561. process = flexmock()
  562. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  563. flexmock(module.os.path).should_receive('exists').and_return(False)
  564. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  565. 'resolve_credential'
  566. ).replace_with(lambda value, config: value)
  567. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  568. flexmock(module).should_receive('make_defaults_file_options').with_args(
  569. 'root', 'trustsome1', None
  570. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  571. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  572. flexmock(module).should_receive('execute_command').with_args(
  573. (
  574. 'mariadb-dump',
  575. '--defaults-extra-file=/dev/fd/99',
  576. '--add-drop-database',
  577. '--databases',
  578. 'foo',
  579. '--result-file',
  580. 'dump',
  581. ),
  582. environment={},
  583. run_to_completion=False,
  584. ).and_return(process).once()
  585. assert (
  586. module.execute_dump_command(
  587. database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},
  588. config={},
  589. username='root',
  590. password='trustsome1',
  591. dump_path=flexmock(),
  592. database_names=('foo',),
  593. environment={},
  594. dry_run=False,
  595. dry_run_label='',
  596. )
  597. == process
  598. )
  599. def test_execute_dump_command_runs_mariadb_dump_with_options():
  600. process = flexmock()
  601. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  602. flexmock(module.os.path).should_receive('exists').and_return(False)
  603. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  604. 'resolve_credential'
  605. ).replace_with(lambda value, config: value)
  606. flexmock(module).should_receive('parse_extra_options').and_return(('--stuff=such',), None)
  607. flexmock(module).should_receive('make_defaults_file_options').with_args(
  608. 'root', 'trustsome1', None
  609. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  610. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  611. flexmock(module).should_receive('execute_command').with_args(
  612. (
  613. 'mariadb-dump',
  614. '--defaults-extra-file=/dev/fd/99',
  615. '--stuff=such',
  616. '--add-drop-database',
  617. '--databases',
  618. 'foo',
  619. '--result-file',
  620. 'dump',
  621. ),
  622. environment=None,
  623. run_to_completion=False,
  624. ).and_return(process).once()
  625. assert (
  626. module.execute_dump_command(
  627. database={'name': 'foo', 'options': '--stuff=such'},
  628. config={},
  629. username='root',
  630. password='trustsome1',
  631. dump_path=flexmock(),
  632. database_names=('foo',),
  633. environment=None,
  634. dry_run=False,
  635. dry_run_label='',
  636. )
  637. == process
  638. )
  639. def test_execute_dump_command_runs_non_default_mariadb_dump_with_options():
  640. process = flexmock()
  641. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  642. flexmock(module.os.path).should_receive('exists').and_return(False)
  643. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  644. 'resolve_credential'
  645. ).replace_with(lambda value, config: value)
  646. flexmock(module).should_receive('parse_extra_options').and_return(('--stuff=such',), None)
  647. flexmock(module).should_receive('make_defaults_file_options').with_args(
  648. 'root', 'trustsome1', None
  649. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  650. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  651. flexmock(module).should_receive('execute_command').with_args(
  652. (
  653. 'custom_mariadb_dump', # Custom MariaDB dump command
  654. '--defaults-extra-file=/dev/fd/99',
  655. '--stuff=such',
  656. '--add-drop-database',
  657. '--databases',
  658. 'foo',
  659. '--result-file',
  660. 'dump',
  661. ),
  662. environment=None,
  663. run_to_completion=False,
  664. ).and_return(process).once()
  665. assert (
  666. module.execute_dump_command(
  667. database={
  668. 'name': 'foo',
  669. 'mariadb_dump_command': 'custom_mariadb_dump',
  670. 'options': '--stuff=such',
  671. }, # Custom MariaDB dump command specified
  672. config={},
  673. username='root',
  674. password='trustsome1',
  675. dump_path=flexmock(),
  676. database_names=('foo',),
  677. environment=None,
  678. dry_run=False,
  679. dry_run_label='',
  680. )
  681. == process
  682. )
  683. def test_execute_dump_command_with_duplicate_dump_skips_mariadb_dump():
  684. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  685. flexmock(module.os.path).should_receive('exists').and_return(True)
  686. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  687. flexmock(module).should_receive('make_defaults_file_options').with_args(
  688. 'root', 'trustsome1', None
  689. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  690. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  691. flexmock(module).should_receive('execute_command').never()
  692. assert (
  693. module.execute_dump_command(
  694. database={'name': 'foo'},
  695. config={},
  696. username='root',
  697. password='trustsome1',
  698. dump_path=flexmock(),
  699. database_names=('foo',),
  700. environment=None,
  701. dry_run=True,
  702. dry_run_label='SO DRY',
  703. )
  704. is None
  705. )
  706. def test_execute_dump_command_with_dry_run_skips_mariadb_dump():
  707. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  708. flexmock(module.os.path).should_receive('exists').and_return(False)
  709. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  710. 'resolve_credential'
  711. ).replace_with(lambda value, config: value)
  712. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  713. flexmock(module).should_receive('make_defaults_file_options').with_args(
  714. 'root', 'trustsome1', None
  715. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  716. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  717. flexmock(module).should_receive('execute_command').never()
  718. assert (
  719. module.execute_dump_command(
  720. database={'name': 'foo'},
  721. config={},
  722. username='root',
  723. password='trustsome1',
  724. dump_path=flexmock(),
  725. database_names=('foo',),
  726. environment=None,
  727. dry_run=True,
  728. dry_run_label='SO DRY',
  729. )
  730. is None
  731. )
  732. def test_dump_data_sources_errors_for_missing_all_databases():
  733. databases = [{'name': 'all'}]
  734. flexmock(module).should_receive('make_dump_path').and_return('')
  735. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  736. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  737. 'resolve_credential'
  738. ).replace_with(lambda value, config: value)
  739. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  740. 'databases/localhost/all'
  741. )
  742. flexmock(module).should_receive('database_names_to_dump').and_return(())
  743. with pytest.raises(ValueError):
  744. assert module.dump_data_sources(
  745. databases,
  746. {},
  747. config_paths=('test.yaml',),
  748. borgmatic_runtime_directory='/run/borgmatic',
  749. patterns=[],
  750. dry_run=False,
  751. )
  752. def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
  753. databases = [{'name': 'all'}]
  754. flexmock(module).should_receive('make_dump_path').and_return('')
  755. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  756. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  757. 'resolve_credential'
  758. ).replace_with(lambda value, config: value)
  759. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  760. 'databases/localhost/all'
  761. )
  762. flexmock(module).should_receive('database_names_to_dump').and_return(())
  763. assert (
  764. module.dump_data_sources(
  765. databases,
  766. {},
  767. config_paths=('test.yaml',),
  768. borgmatic_runtime_directory='/run/borgmatic',
  769. patterns=[],
  770. dry_run=True,
  771. )
  772. == []
  773. )
  774. def test_restore_data_source_dump_runs_mariadb_to_restore():
  775. hook_config = [{'name': 'foo'}, {'name': 'bar'}]
  776. extract_process = flexmock(stdout=flexmock())
  777. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  778. 'resolve_credential'
  779. ).replace_with(lambda value, config: value)
  780. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  781. flexmock(module).should_receive('make_defaults_file_options').with_args(
  782. None, None, None
  783. ).and_return(())
  784. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  785. flexmock(module).should_receive('execute_command_with_processes').with_args(
  786. ('mariadb', '--batch'),
  787. processes=[extract_process],
  788. output_log_level=logging.DEBUG,
  789. input_file=extract_process.stdout,
  790. environment={'USER': 'root'},
  791. ).once()
  792. module.restore_data_source_dump(
  793. hook_config,
  794. {},
  795. data_source={'name': 'foo'},
  796. dry_run=False,
  797. extract_process=extract_process,
  798. connection_params={
  799. 'hostname': None,
  800. 'port': None,
  801. 'username': None,
  802. 'password': None,
  803. },
  804. borgmatic_runtime_directory='/run/borgmatic',
  805. )
  806. def test_restore_data_source_dump_runs_mariadb_with_options():
  807. hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
  808. extract_process = flexmock(stdout=flexmock())
  809. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  810. 'resolve_credential'
  811. ).replace_with(lambda value, config: value)
  812. flexmock(module).should_receive('parse_extra_options').and_return(('--harder',), None)
  813. flexmock(module).should_receive('make_defaults_file_options').with_args(
  814. None, None, None
  815. ).and_return(())
  816. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  817. flexmock(module).should_receive('execute_command_with_processes').with_args(
  818. ('mariadb', '--harder', '--batch'),
  819. processes=[extract_process],
  820. output_log_level=logging.DEBUG,
  821. input_file=extract_process.stdout,
  822. environment={'USER': 'root'},
  823. ).once()
  824. module.restore_data_source_dump(
  825. hook_config,
  826. {},
  827. data_source=hook_config[0],
  828. dry_run=False,
  829. extract_process=extract_process,
  830. connection_params={
  831. 'hostname': None,
  832. 'port': None,
  833. 'username': None,
  834. 'password': None,
  835. },
  836. borgmatic_runtime_directory='/run/borgmatic',
  837. )
  838. def test_restore_data_source_dump_runs_non_default_mariadb_with_options():
  839. hook_config = [
  840. {'name': 'foo', 'restore_options': '--harder', 'mariadb_command': 'custom_mariadb'}
  841. ]
  842. extract_process = flexmock(stdout=flexmock())
  843. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  844. 'resolve_credential'
  845. ).replace_with(lambda value, config: value)
  846. flexmock(module).should_receive('parse_extra_options').and_return(('--harder',), None)
  847. flexmock(module).should_receive('make_defaults_file_options').with_args(
  848. None, None, None
  849. ).and_return(())
  850. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  851. flexmock(module).should_receive('execute_command_with_processes').with_args(
  852. ('custom_mariadb', '--harder', '--batch'),
  853. processes=[extract_process],
  854. output_log_level=logging.DEBUG,
  855. input_file=extract_process.stdout,
  856. environment={'USER': 'root'},
  857. ).once()
  858. module.restore_data_source_dump(
  859. hook_config,
  860. {},
  861. data_source=hook_config[0],
  862. dry_run=False,
  863. extract_process=extract_process,
  864. connection_params={
  865. 'hostname': None,
  866. 'port': None,
  867. 'username': None,
  868. 'password': None,
  869. },
  870. borgmatic_runtime_directory='/run/borgmatic',
  871. )
  872. def test_restore_data_source_dump_runs_mariadb_with_hostname_and_port():
  873. hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  874. extract_process = flexmock(stdout=flexmock())
  875. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  876. 'resolve_credential'
  877. ).replace_with(lambda value, config: value)
  878. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  879. flexmock(module).should_receive('make_defaults_file_options').with_args(
  880. None, None, None
  881. ).and_return(())
  882. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  883. flexmock(module).should_receive('execute_command_with_processes').with_args(
  884. (
  885. 'mariadb',
  886. '--batch',
  887. '--host',
  888. 'database.example.org',
  889. '--port',
  890. '5433',
  891. '--protocol',
  892. 'tcp',
  893. ),
  894. processes=[extract_process],
  895. output_log_level=logging.DEBUG,
  896. input_file=extract_process.stdout,
  897. environment={'USER': 'root'},
  898. ).once()
  899. module.restore_data_source_dump(
  900. hook_config,
  901. {},
  902. data_source=hook_config[0],
  903. dry_run=False,
  904. extract_process=extract_process,
  905. connection_params={
  906. 'hostname': None,
  907. 'port': None,
  908. 'username': None,
  909. 'password': None,
  910. },
  911. borgmatic_runtime_directory='/run/borgmatic',
  912. )
  913. def test_restore_data_source_dump_runs_mariadb_with_tls():
  914. hook_config = [{'name': 'foo', 'tls': True}]
  915. extract_process = flexmock(stdout=flexmock())
  916. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  917. 'resolve_credential'
  918. ).replace_with(lambda value, config: value)
  919. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  920. flexmock(module).should_receive('make_defaults_file_options').with_args(
  921. None, None, None
  922. ).and_return(())
  923. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  924. flexmock(module).should_receive('execute_command_with_processes').with_args(
  925. (
  926. 'mariadb',
  927. '--batch',
  928. '--ssl',
  929. ),
  930. processes=[extract_process],
  931. output_log_level=logging.DEBUG,
  932. input_file=extract_process.stdout,
  933. environment={'USER': 'root'},
  934. ).once()
  935. module.restore_data_source_dump(
  936. hook_config,
  937. {},
  938. data_source=hook_config[0],
  939. dry_run=False,
  940. extract_process=extract_process,
  941. connection_params={
  942. 'hostname': None,
  943. 'port': None,
  944. 'username': None,
  945. 'password': None,
  946. },
  947. borgmatic_runtime_directory='/run/borgmatic',
  948. )
  949. def test_restore_data_source_dump_runs_mariadb_without_tls():
  950. hook_config = [{'name': 'foo', 'tls': False}]
  951. extract_process = flexmock(stdout=flexmock())
  952. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  953. 'resolve_credential'
  954. ).replace_with(lambda value, config: value)
  955. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  956. flexmock(module).should_receive('make_defaults_file_options').with_args(
  957. None, None, None
  958. ).and_return(())
  959. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  960. flexmock(module).should_receive('execute_command_with_processes').with_args(
  961. (
  962. 'mariadb',
  963. '--batch',
  964. '--skip-ssl',
  965. ),
  966. processes=[extract_process],
  967. output_log_level=logging.DEBUG,
  968. input_file=extract_process.stdout,
  969. environment={'USER': 'root'},
  970. ).once()
  971. module.restore_data_source_dump(
  972. hook_config,
  973. {},
  974. data_source=hook_config[0],
  975. dry_run=False,
  976. extract_process=extract_process,
  977. connection_params={
  978. 'hostname': None,
  979. 'port': None,
  980. 'username': None,
  981. 'password': None,
  982. },
  983. borgmatic_runtime_directory='/run/borgmatic',
  984. )
  985. def test_restore_data_source_dump_runs_mariadb_with_username_and_password():
  986. hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  987. extract_process = flexmock(stdout=flexmock())
  988. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  989. 'resolve_credential'
  990. ).replace_with(lambda value, config: value)
  991. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  992. flexmock(module).should_receive('make_defaults_file_options').with_args(
  993. 'root', 'trustsome1', None
  994. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  995. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  996. flexmock(module).should_receive('execute_command_with_processes').with_args(
  997. ('mariadb', '--defaults-extra-file=/dev/fd/99', '--batch'),
  998. processes=[extract_process],
  999. output_log_level=logging.DEBUG,
  1000. input_file=extract_process.stdout,
  1001. environment={'USER': 'root'},
  1002. ).once()
  1003. module.restore_data_source_dump(
  1004. hook_config,
  1005. {},
  1006. data_source=hook_config[0],
  1007. dry_run=False,
  1008. extract_process=extract_process,
  1009. connection_params={
  1010. 'hostname': None,
  1011. 'port': None,
  1012. 'username': None,
  1013. 'password': None,
  1014. },
  1015. borgmatic_runtime_directory='/run/borgmatic',
  1016. )
  1017. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  1018. hook_config = [
  1019. {
  1020. 'name': 'foo',
  1021. 'username': 'root',
  1022. 'password': 'trustsome1',
  1023. 'restore_hostname': 'restorehost',
  1024. 'restore_port': 'restoreport',
  1025. 'restore_username': 'restoreusername',
  1026. 'restore_password': 'restorepassword',
  1027. }
  1028. ]
  1029. extract_process = flexmock(stdout=flexmock())
  1030. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1031. 'resolve_credential'
  1032. ).replace_with(lambda value, config: value)
  1033. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1034. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1035. 'cliusername', 'clipassword', None
  1036. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  1037. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1038. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1039. (
  1040. 'mariadb',
  1041. '--defaults-extra-file=/dev/fd/99',
  1042. '--batch',
  1043. '--host',
  1044. 'clihost',
  1045. '--port',
  1046. 'cliport',
  1047. '--protocol',
  1048. 'tcp',
  1049. ),
  1050. processes=[extract_process],
  1051. output_log_level=logging.DEBUG,
  1052. input_file=extract_process.stdout,
  1053. environment={'USER': 'root'},
  1054. ).once()
  1055. module.restore_data_source_dump(
  1056. hook_config,
  1057. {},
  1058. data_source=hook_config[0],
  1059. dry_run=False,
  1060. extract_process=extract_process,
  1061. connection_params={
  1062. 'hostname': 'clihost',
  1063. 'port': 'cliport',
  1064. 'username': 'cliusername',
  1065. 'password': 'clipassword',
  1066. },
  1067. borgmatic_runtime_directory='/run/borgmatic',
  1068. )
  1069. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  1070. hook_config = [
  1071. {
  1072. 'name': 'foo',
  1073. 'username': 'root',
  1074. 'password': 'trustsome1',
  1075. 'hostname': 'dbhost',
  1076. 'port': 'dbport',
  1077. 'tls': True,
  1078. 'restore_username': 'restoreuser',
  1079. 'restore_password': 'restorepass',
  1080. 'restore_hostname': 'restorehost',
  1081. 'restore_port': 'restoreport',
  1082. 'restore_tls': False,
  1083. }
  1084. ]
  1085. extract_process = flexmock(stdout=flexmock())
  1086. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1087. 'resolve_credential'
  1088. ).replace_with(lambda value, config: value)
  1089. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1090. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1091. 'restoreuser', 'restorepass', None
  1092. ).and_return(('--defaults-extra-file=/dev/fd/99',))
  1093. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1094. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1095. (
  1096. 'mariadb',
  1097. '--defaults-extra-file=/dev/fd/99',
  1098. '--batch',
  1099. '--host',
  1100. 'restorehost',
  1101. '--port',
  1102. 'restoreport',
  1103. '--protocol',
  1104. 'tcp',
  1105. '--skip-ssl',
  1106. ),
  1107. processes=[extract_process],
  1108. output_log_level=logging.DEBUG,
  1109. input_file=extract_process.stdout,
  1110. environment={'USER': 'root'},
  1111. ).once()
  1112. module.restore_data_source_dump(
  1113. hook_config,
  1114. {},
  1115. data_source=hook_config[0],
  1116. dry_run=False,
  1117. extract_process=extract_process,
  1118. connection_params={
  1119. 'hostname': None,
  1120. 'port': None,
  1121. 'username': None,
  1122. 'password': None,
  1123. },
  1124. borgmatic_runtime_directory='/run/borgmatic',
  1125. )
  1126. def test_restore_data_source_dump_with_dry_run_skips_restore():
  1127. hook_config = [{'name': 'foo'}]
  1128. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1129. 'resolve_credential'
  1130. ).replace_with(lambda value, config: value)
  1131. flexmock(module).should_receive('parse_extra_options').and_return((), None)
  1132. flexmock(module).should_receive('make_defaults_file_options').with_args(
  1133. None, None, None
  1134. ).and_return(())
  1135. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  1136. flexmock(module).should_receive('execute_command_with_processes').never()
  1137. module.restore_data_source_dump(
  1138. hook_config,
  1139. {},
  1140. data_source={'name': 'foo'},
  1141. dry_run=True,
  1142. extract_process=flexmock(),
  1143. connection_params={
  1144. 'hostname': None,
  1145. 'port': None,
  1146. 'username': None,
  1147. 'password': None,
  1148. },
  1149. borgmatic_runtime_directory='/run/borgmatic',
  1150. )