test_postgresql.py 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks.data_source import postgresql as module
  5. def test_make_environment_maps_options_to_environment():
  6. database = {
  7. 'name': 'foo',
  8. 'password': 'pass',
  9. 'ssl_mode': 'require',
  10. 'ssl_cert': 'cert.crt',
  11. 'ssl_key': 'key.key',
  12. 'ssl_root_cert': 'root.crt',
  13. 'ssl_crl': 'crl.crl',
  14. }
  15. expected = {
  16. 'USER': 'root',
  17. 'PGPASSWORD': 'pass',
  18. 'PGSSLMODE': 'require',
  19. 'PGSSLCERT': 'cert.crt',
  20. 'PGSSLKEY': 'key.key',
  21. 'PGSSLROOTCERT': 'root.crt',
  22. 'PGSSLCRL': 'crl.crl',
  23. }
  24. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  25. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  26. 'resolve_credential'
  27. ).replace_with(lambda value, config: value)
  28. assert module.make_environment(database, {}) == expected
  29. def test_make_environment_with_cli_password_sets_correct_password():
  30. database = {'name': 'foo', 'restore_password': 'trustsome1', 'password': 'anotherpassword'}
  31. flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
  32. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  33. 'resolve_credential'
  34. ).replace_with(lambda value, config: value)
  35. environment = module.make_environment(
  36. database, {}, restore_connection_params={'password': 'clipassword'}
  37. )
  38. assert environment['PGPASSWORD'] == 'clipassword'
  39. def test_make_environment_without_cli_password_or_configured_password_does_not_set_password():
  40. database = {'name': 'foo'}
  41. environment = module.make_environment(
  42. database, {}, restore_connection_params={'username': 'someone'}
  43. )
  44. assert 'PGPASSWORD' not in environment
  45. def test_make_environment_without_ssl_mode_does_not_set_ssl_mode():
  46. database = {'name': 'foo'}
  47. environment = module.make_environment(database, {})
  48. assert 'PGSSLMODE' not in environment
  49. def test_database_names_to_dump_passes_through_individual_database_name():
  50. database = {'name': 'foo'}
  51. assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == ('foo',)
  52. def test_database_names_to_dump_passes_through_individual_database_name_with_format():
  53. database = {'name': 'foo', 'format': 'custom'}
  54. assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == ('foo',)
  55. def test_database_names_to_dump_passes_through_all_without_format():
  56. database = {'name': 'all'}
  57. assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == ('all',)
  58. def test_database_names_to_dump_with_all_and_format_and_dry_run_bails():
  59. database = {'name': 'all', 'format': 'custom'}
  60. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  61. 'resolve_credential'
  62. ).replace_with(lambda value, config: value)
  63. flexmock(module).should_receive('execute_command_and_capture_output').never()
  64. assert module.database_names_to_dump(database, {}, flexmock(), dry_run=True) == ()
  65. def test_database_names_to_dump_with_all_and_format_lists_databases():
  66. database = {'name': 'all', 'format': 'custom'}
  67. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  68. 'resolve_credential'
  69. ).replace_with(lambda value, config: value)
  70. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  71. 'foo,test,\nbar,test,"stuff and such"'
  72. )
  73. assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == (
  74. 'foo',
  75. 'bar',
  76. )
  77. def test_database_names_to_dump_with_all_and_format_lists_databases_with_hostname_and_port():
  78. database = {'name': 'all', 'format': 'custom', 'hostname': 'localhost', 'port': 1234}
  79. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  80. 'resolve_credential'
  81. ).replace_with(lambda value, config: value)
  82. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  83. (
  84. 'psql',
  85. '--list',
  86. '--no-password',
  87. '--no-psqlrc',
  88. '--csv',
  89. '--tuples-only',
  90. '--host',
  91. 'localhost',
  92. '--port',
  93. '1234',
  94. ),
  95. environment=object,
  96. ).and_return('foo,test,\nbar,test,"stuff and such"')
  97. assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == (
  98. 'foo',
  99. 'bar',
  100. )
  101. def test_database_names_to_dump_with_all_and_format_lists_databases_with_username():
  102. database = {'name': 'all', 'format': 'custom', 'username': 'postgres'}
  103. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  104. 'resolve_credential'
  105. ).replace_with(lambda value, config: value)
  106. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  107. (
  108. 'psql',
  109. '--list',
  110. '--no-password',
  111. '--no-psqlrc',
  112. '--csv',
  113. '--tuples-only',
  114. '--username',
  115. 'postgres',
  116. ),
  117. environment=object,
  118. ).and_return('foo,test,\nbar,test,"stuff and such"')
  119. assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == (
  120. 'foo',
  121. 'bar',
  122. )
  123. def test_database_names_to_dump_with_all_and_format_lists_databases_with_options():
  124. database = {'name': 'all', 'format': 'custom', 'list_options': '--harder'}
  125. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  126. 'resolve_credential'
  127. ).replace_with(lambda value, config: value)
  128. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  129. ('psql', '--list', '--no-password', '--no-psqlrc', '--csv', '--tuples-only', '--harder'),
  130. environment=object,
  131. ).and_return('foo,test,\nbar,test,"stuff and such"')
  132. assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == (
  133. 'foo',
  134. 'bar',
  135. )
  136. def test_database_names_to_dump_with_all_and_format_excludes_particular_databases():
  137. database = {'name': 'all', 'format': 'custom'}
  138. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  139. 'resolve_credential'
  140. ).replace_with(lambda value, config: value)
  141. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  142. 'foo,test,\ntemplate0,test,blah'
  143. )
  144. assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == ('foo',)
  145. def test_database_names_to_dump_with_all_and_psql_command_uses_custom_command():
  146. database = {
  147. 'name': 'all',
  148. 'format': 'custom',
  149. 'psql_command': 'docker exec --workdir * mycontainer psql',
  150. }
  151. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  152. 'resolve_credential'
  153. ).replace_with(lambda value, config: value)
  154. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  155. (
  156. 'docker',
  157. 'exec',
  158. '--workdir',
  159. "'*'", # Should get shell escaped to prevent injection attacks.
  160. 'mycontainer',
  161. 'psql',
  162. '--list',
  163. '--no-password',
  164. '--no-psqlrc',
  165. '--csv',
  166. '--tuples-only',
  167. ),
  168. environment=object,
  169. ).and_return('foo,text').once()
  170. assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == ('foo',)
  171. def test_use_streaming_true_for_any_non_directory_format_databases():
  172. assert module.use_streaming(
  173. databases=[{'format': 'stuff'}, {'format': 'directory'}, {}],
  174. config=flexmock(),
  175. )
  176. def test_use_streaming_false_for_all_directory_format_databases():
  177. assert not module.use_streaming(
  178. databases=[{'format': 'directory'}, {'format': 'directory'}],
  179. config=flexmock(),
  180. )
  181. def test_use_streaming_false_for_no_databases():
  182. assert not module.use_streaming(databases=[], config=flexmock())
  183. def test_dump_data_sources_runs_pg_dump_for_each_database():
  184. databases = [{'name': 'foo'}, {'name': 'bar'}]
  185. processes = [flexmock(), flexmock()]
  186. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  187. flexmock(module).should_receive('make_dump_path').and_return('')
  188. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  189. ('bar',)
  190. )
  191. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  192. 'databases/localhost/foo'
  193. ).and_return('databases/localhost/bar')
  194. flexmock(module.os.path).should_receive('exists').and_return(False)
  195. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  196. 'resolve_credential'
  197. ).replace_with(lambda value, config: value)
  198. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  199. for name, process in zip(('foo', 'bar'), processes):
  200. flexmock(module).should_receive('execute_command').with_args(
  201. (
  202. 'pg_dump',
  203. '--no-password',
  204. '--clean',
  205. '--if-exists',
  206. '--format',
  207. 'custom',
  208. name,
  209. '>',
  210. f'databases/localhost/{name}',
  211. ),
  212. shell=True,
  213. environment={'PGSSLMODE': 'disable'},
  214. run_to_completion=False,
  215. ).and_return(process).once()
  216. assert (
  217. module.dump_data_sources(
  218. databases,
  219. {},
  220. config_paths=('test.yaml',),
  221. borgmatic_runtime_directory='/run/borgmatic',
  222. patterns=[],
  223. dry_run=False,
  224. )
  225. == processes
  226. )
  227. def test_dump_data_sources_raises_when_no_database_names_to_dump():
  228. databases = [{'name': 'foo'}, {'name': 'bar'}]
  229. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  230. flexmock(module).should_receive('make_dump_path').and_return('')
  231. flexmock(module).should_receive('database_names_to_dump').and_return(())
  232. with pytest.raises(ValueError):
  233. module.dump_data_sources(
  234. databases,
  235. {},
  236. config_paths=('test.yaml',),
  237. borgmatic_runtime_directory='/run/borgmatic',
  238. patterns=[],
  239. dry_run=False,
  240. )
  241. def test_dump_data_sources_does_not_raise_when_no_database_names_to_dump():
  242. databases = [{'name': 'foo'}, {'name': 'bar'}]
  243. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  244. flexmock(module).should_receive('make_dump_path').and_return('')
  245. flexmock(module).should_receive('database_names_to_dump').and_return(())
  246. module.dump_data_sources(
  247. databases,
  248. {},
  249. config_paths=('test.yaml',),
  250. borgmatic_runtime_directory='/run/borgmatic',
  251. patterns=[],
  252. dry_run=True,
  253. ) == []
  254. def test_dump_data_sources_with_duplicate_dump_skips_pg_dump():
  255. databases = [{'name': 'foo'}, {'name': 'bar'}]
  256. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  257. flexmock(module).should_receive('make_dump_path').and_return('')
  258. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  259. ('bar',)
  260. )
  261. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  262. 'databases/localhost/foo'
  263. ).and_return('databases/localhost/bar')
  264. flexmock(module.os.path).should_receive('exists').and_return(True)
  265. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  266. flexmock(module).should_receive('execute_command').never()
  267. assert (
  268. module.dump_data_sources(
  269. databases,
  270. {},
  271. config_paths=('test.yaml',),
  272. borgmatic_runtime_directory='/run/borgmatic',
  273. patterns=[],
  274. dry_run=False,
  275. )
  276. == []
  277. )
  278. def test_dump_data_sources_with_dry_run_skips_pg_dump():
  279. databases = [{'name': 'foo'}, {'name': 'bar'}]
  280. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  281. flexmock(module).should_receive('make_dump_path').and_return('')
  282. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  283. ('bar',)
  284. )
  285. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  286. 'databases/localhost/foo'
  287. ).and_return('databases/localhost/bar')
  288. flexmock(module.os.path).should_receive('exists').and_return(False)
  289. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  290. 'resolve_credential'
  291. ).replace_with(lambda value, config: value)
  292. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  293. flexmock(module).should_receive('execute_command').never()
  294. assert (
  295. module.dump_data_sources(
  296. databases,
  297. {},
  298. config_paths=('test.yaml',),
  299. borgmatic_runtime_directory='/run/borgmatic',
  300. patterns=[],
  301. dry_run=True,
  302. )
  303. == []
  304. )
  305. def test_dump_data_sources_runs_pg_dump_with_hostname_and_port():
  306. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  307. process = flexmock()
  308. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  309. flexmock(module).should_receive('make_dump_path').and_return('')
  310. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  311. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  312. 'databases/database.example.org/foo'
  313. )
  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.dump).should_receive('create_named_pipe_for_dump')
  319. flexmock(module).should_receive('execute_command').with_args(
  320. (
  321. 'pg_dump',
  322. '--no-password',
  323. '--clean',
  324. '--if-exists',
  325. '--host',
  326. 'database.example.org',
  327. '--port',
  328. '5433',
  329. '--format',
  330. 'custom',
  331. 'foo',
  332. '>',
  333. 'databases/database.example.org/foo',
  334. ),
  335. shell=True,
  336. environment={'PGSSLMODE': 'disable'},
  337. run_to_completion=False,
  338. ).and_return(process).once()
  339. assert module.dump_data_sources(
  340. databases,
  341. {},
  342. config_paths=('test.yaml',),
  343. borgmatic_runtime_directory='/run/borgmatic',
  344. patterns=[],
  345. dry_run=False,
  346. ) == [process]
  347. def test_dump_data_sources_runs_pg_dump_with_username_and_password():
  348. databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  349. process = flexmock()
  350. flexmock(module).should_receive('make_environment').and_return(
  351. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  352. )
  353. flexmock(module).should_receive('make_dump_path').and_return('')
  354. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  355. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  356. 'databases/localhost/foo'
  357. )
  358. flexmock(module.os.path).should_receive('exists').and_return(False)
  359. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  360. 'resolve_credential'
  361. ).replace_with(lambda value, config: value)
  362. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  363. flexmock(module).should_receive('execute_command').with_args(
  364. (
  365. 'pg_dump',
  366. '--no-password',
  367. '--clean',
  368. '--if-exists',
  369. '--username',
  370. 'postgres',
  371. '--format',
  372. 'custom',
  373. 'foo',
  374. '>',
  375. 'databases/localhost/foo',
  376. ),
  377. shell=True,
  378. environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  379. run_to_completion=False,
  380. ).and_return(process).once()
  381. assert module.dump_data_sources(
  382. databases,
  383. {},
  384. config_paths=('test.yaml',),
  385. borgmatic_runtime_directory='/run/borgmatic',
  386. patterns=[],
  387. dry_run=False,
  388. ) == [process]
  389. def test_dump_data_sources_with_username_injection_attack_gets_escaped():
  390. databases = [{'name': 'foo', 'username': 'postgres; naughty-command', 'password': 'trustsome1'}]
  391. process = flexmock()
  392. flexmock(module).should_receive('make_environment').and_return(
  393. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  394. )
  395. flexmock(module).should_receive('make_dump_path').and_return('')
  396. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  397. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  398. 'databases/localhost/foo'
  399. )
  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.dump).should_receive('create_named_pipe_for_dump')
  405. flexmock(module).should_receive('execute_command').with_args(
  406. (
  407. 'pg_dump',
  408. '--no-password',
  409. '--clean',
  410. '--if-exists',
  411. '--username',
  412. "'postgres; naughty-command'",
  413. '--format',
  414. 'custom',
  415. 'foo',
  416. '>',
  417. 'databases/localhost/foo',
  418. ),
  419. shell=True,
  420. environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  421. run_to_completion=False,
  422. ).and_return(process).once()
  423. assert module.dump_data_sources(
  424. databases,
  425. {},
  426. config_paths=('test.yaml',),
  427. borgmatic_runtime_directory='/run/borgmatic',
  428. patterns=[],
  429. dry_run=False,
  430. ) == [process]
  431. def test_dump_data_sources_runs_pg_dump_with_directory_format():
  432. databases = [{'name': 'foo', 'format': 'directory'}]
  433. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  434. flexmock(module).should_receive('make_dump_path').and_return('')
  435. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  436. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  437. 'databases/localhost/foo'
  438. )
  439. flexmock(module.os.path).should_receive('exists').and_return(False)
  440. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  441. 'resolve_credential'
  442. ).replace_with(lambda value, config: value)
  443. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  444. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  445. flexmock(module).should_receive('execute_command').with_args(
  446. (
  447. 'pg_dump',
  448. '--no-password',
  449. '--clean',
  450. '--if-exists',
  451. '--format',
  452. 'directory',
  453. '--file',
  454. 'databases/localhost/foo',
  455. 'foo',
  456. ),
  457. shell=True,
  458. environment={'PGSSLMODE': 'disable'},
  459. ).and_return(flexmock()).once()
  460. assert (
  461. module.dump_data_sources(
  462. databases,
  463. {},
  464. config_paths=('test.yaml',),
  465. borgmatic_runtime_directory='/run/borgmatic',
  466. patterns=[],
  467. dry_run=False,
  468. )
  469. == []
  470. )
  471. def test_dump_data_sources_runs_pg_dump_with_string_compression():
  472. databases = [{'name': 'foo', 'compression': 'winrar'}]
  473. processes = [flexmock()]
  474. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  475. flexmock(module).should_receive('make_dump_path').and_return('')
  476. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  477. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  478. 'databases/localhost/foo'
  479. )
  480. flexmock(module.os.path).should_receive('exists').and_return(False)
  481. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  482. 'resolve_credential'
  483. ).replace_with(lambda value, config: value)
  484. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  485. flexmock(module).should_receive('execute_command').with_args(
  486. (
  487. 'pg_dump',
  488. '--no-password',
  489. '--clean',
  490. '--if-exists',
  491. '--format',
  492. 'custom',
  493. '--compress',
  494. 'winrar',
  495. 'foo',
  496. '>',
  497. 'databases/localhost/foo',
  498. ),
  499. shell=True,
  500. environment={'PGSSLMODE': 'disable'},
  501. run_to_completion=False,
  502. ).and_return(processes[0]).once()
  503. assert (
  504. module.dump_data_sources(
  505. databases,
  506. {},
  507. config_paths=('test.yaml',),
  508. borgmatic_runtime_directory='/run/borgmatic',
  509. patterns=[],
  510. dry_run=False,
  511. )
  512. == processes
  513. )
  514. def test_dump_data_sources_runs_pg_dump_with_integer_compression():
  515. databases = [{'name': 'foo', 'compression': 0}]
  516. processes = [flexmock()]
  517. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  518. flexmock(module).should_receive('make_dump_path').and_return('')
  519. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  520. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  521. 'databases/localhost/foo'
  522. )
  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.dump).should_receive('create_named_pipe_for_dump')
  528. flexmock(module).should_receive('execute_command').with_args(
  529. (
  530. 'pg_dump',
  531. '--no-password',
  532. '--clean',
  533. '--if-exists',
  534. '--format',
  535. 'custom',
  536. '--compress',
  537. '0',
  538. 'foo',
  539. '>',
  540. 'databases/localhost/foo',
  541. ),
  542. shell=True,
  543. environment={'PGSSLMODE': 'disable'},
  544. run_to_completion=False,
  545. ).and_return(processes[0]).once()
  546. assert (
  547. module.dump_data_sources(
  548. databases,
  549. {},
  550. config_paths=('test.yaml',),
  551. borgmatic_runtime_directory='/run/borgmatic',
  552. patterns=[],
  553. dry_run=False,
  554. )
  555. == processes
  556. )
  557. def test_dump_data_sources_runs_pg_dump_with_options():
  558. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  559. process = flexmock()
  560. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  561. flexmock(module).should_receive('make_dump_path').and_return('')
  562. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  563. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  564. 'databases/localhost/foo'
  565. )
  566. flexmock(module.os.path).should_receive('exists').and_return(False)
  567. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  568. 'resolve_credential'
  569. ).replace_with(lambda value, config: value)
  570. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  571. flexmock(module).should_receive('execute_command').with_args(
  572. (
  573. 'pg_dump',
  574. '--no-password',
  575. '--clean',
  576. '--if-exists',
  577. '--format',
  578. 'custom',
  579. '--stuff=such',
  580. 'foo',
  581. '>',
  582. 'databases/localhost/foo',
  583. ),
  584. shell=True,
  585. environment={'PGSSLMODE': 'disable'},
  586. run_to_completion=False,
  587. ).and_return(process).once()
  588. assert module.dump_data_sources(
  589. databases,
  590. {},
  591. config_paths=('test.yaml',),
  592. borgmatic_runtime_directory='/run/borgmatic',
  593. patterns=[],
  594. dry_run=False,
  595. ) == [process]
  596. def test_dump_data_sources_runs_pg_dumpall_for_all_databases():
  597. databases = [{'name': 'all'}]
  598. process = flexmock()
  599. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  600. flexmock(module).should_receive('make_dump_path').and_return('')
  601. flexmock(module).should_receive('database_names_to_dump').and_return(('all',))
  602. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  603. 'databases/localhost/all'
  604. )
  605. flexmock(module.os.path).should_receive('exists').and_return(False)
  606. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  607. 'resolve_credential'
  608. ).replace_with(lambda value, config: value)
  609. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  610. flexmock(module).should_receive('execute_command').with_args(
  611. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  612. shell=True,
  613. environment={'PGSSLMODE': 'disable'},
  614. run_to_completion=False,
  615. ).and_return(process).once()
  616. assert module.dump_data_sources(
  617. databases,
  618. {},
  619. config_paths=('test.yaml',),
  620. borgmatic_runtime_directory='/run/borgmatic',
  621. patterns=[],
  622. dry_run=False,
  623. ) == [process]
  624. def test_dump_data_sources_runs_non_default_pg_dump():
  625. databases = [{'name': 'foo', 'pg_dump_command': 'special_pg_dump --compress *'}]
  626. process = flexmock()
  627. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  628. flexmock(module).should_receive('make_dump_path').and_return('')
  629. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  630. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  631. 'databases/localhost/foo'
  632. )
  633. flexmock(module.os.path).should_receive('exists').and_return(False)
  634. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  635. 'resolve_credential'
  636. ).replace_with(lambda value, config: value)
  637. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  638. flexmock(module).should_receive('execute_command').with_args(
  639. (
  640. 'special_pg_dump',
  641. '--compress',
  642. "'*'", # Should get shell escaped to prevent injection attacks.
  643. '--no-password',
  644. '--clean',
  645. '--if-exists',
  646. '--format',
  647. 'custom',
  648. 'foo',
  649. '>',
  650. 'databases/localhost/foo',
  651. ),
  652. shell=True,
  653. environment={'PGSSLMODE': 'disable'},
  654. run_to_completion=False,
  655. ).and_return(process).once()
  656. assert module.dump_data_sources(
  657. databases,
  658. {},
  659. config_paths=('test.yaml',),
  660. borgmatic_runtime_directory='/run/borgmatic',
  661. patterns=[],
  662. dry_run=False,
  663. ) == [process]
  664. def test_restore_data_source_dump_runs_pg_restore():
  665. hook_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
  666. extract_process = flexmock(stdout=flexmock())
  667. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  668. 'resolve_credential'
  669. ).replace_with(lambda value, config: value)
  670. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  671. flexmock(module).should_receive('make_dump_path')
  672. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  673. flexmock(module).should_receive('execute_command_with_processes').with_args(
  674. (
  675. 'pg_restore',
  676. '--no-password',
  677. '--if-exists',
  678. '--exit-on-error',
  679. '--clean',
  680. '--dbname',
  681. 'foo',
  682. ),
  683. processes=[extract_process],
  684. output_log_level=logging.DEBUG,
  685. input_file=extract_process.stdout,
  686. environment={'PGSSLMODE': 'disable'},
  687. ).once()
  688. flexmock(module).should_receive('execute_command').with_args(
  689. (
  690. 'psql',
  691. '--no-password',
  692. '--no-psqlrc',
  693. '--quiet',
  694. '--dbname',
  695. 'foo',
  696. '--command',
  697. 'ANALYZE',
  698. ),
  699. environment={'PGSSLMODE': 'disable'},
  700. ).once()
  701. module.restore_data_source_dump(
  702. hook_config,
  703. {},
  704. data_source={'name': 'foo'},
  705. dry_run=False,
  706. extract_process=extract_process,
  707. connection_params={
  708. 'hostname': None,
  709. 'port': None,
  710. 'username': None,
  711. 'password': None,
  712. },
  713. borgmatic_runtime_directory='/run/borgmatic',
  714. )
  715. def test_restore_data_source_dump_runs_pg_restore_with_hostname_and_port():
  716. hook_config = [
  717. {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
  718. ]
  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_environment').and_return({'PGSSLMODE': 'disable'})
  724. flexmock(module).should_receive('make_dump_path')
  725. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  726. flexmock(module).should_receive('execute_command_with_processes').with_args(
  727. (
  728. 'pg_restore',
  729. '--no-password',
  730. '--if-exists',
  731. '--exit-on-error',
  732. '--clean',
  733. '--dbname',
  734. 'foo',
  735. '--host',
  736. 'database.example.org',
  737. '--port',
  738. '5433',
  739. ),
  740. processes=[extract_process],
  741. output_log_level=logging.DEBUG,
  742. input_file=extract_process.stdout,
  743. environment={'PGSSLMODE': 'disable'},
  744. ).once()
  745. flexmock(module).should_receive('execute_command').with_args(
  746. (
  747. 'psql',
  748. '--no-password',
  749. '--no-psqlrc',
  750. '--quiet',
  751. '--host',
  752. 'database.example.org',
  753. '--port',
  754. '5433',
  755. '--dbname',
  756. 'foo',
  757. '--command',
  758. 'ANALYZE',
  759. ),
  760. environment={'PGSSLMODE': 'disable'},
  761. ).once()
  762. module.restore_data_source_dump(
  763. hook_config,
  764. {},
  765. data_source=hook_config[0],
  766. dry_run=False,
  767. extract_process=extract_process,
  768. connection_params={
  769. 'hostname': None,
  770. 'port': None,
  771. 'username': None,
  772. 'password': None,
  773. },
  774. borgmatic_runtime_directory='/run/borgmatic',
  775. )
  776. def test_restore_data_source_dump_runs_pg_restore_with_username_and_password():
  777. hook_config = [
  778. {'name': 'foo', 'username': 'postgres', 'password': 'trustsome1', 'schemas': None}
  779. ]
  780. extract_process = flexmock(stdout=flexmock())
  781. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  782. 'resolve_credential'
  783. ).replace_with(lambda value, config: value)
  784. flexmock(module).should_receive('make_environment').and_return(
  785. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  786. )
  787. flexmock(module).should_receive('make_dump_path')
  788. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  789. flexmock(module).should_receive('execute_command_with_processes').with_args(
  790. (
  791. 'pg_restore',
  792. '--no-password',
  793. '--if-exists',
  794. '--exit-on-error',
  795. '--clean',
  796. '--dbname',
  797. 'foo',
  798. '--username',
  799. 'postgres',
  800. ),
  801. processes=[extract_process],
  802. output_log_level=logging.DEBUG,
  803. input_file=extract_process.stdout,
  804. environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  805. ).once()
  806. flexmock(module).should_receive('execute_command').with_args(
  807. (
  808. 'psql',
  809. '--no-password',
  810. '--no-psqlrc',
  811. '--quiet',
  812. '--username',
  813. 'postgres',
  814. '--dbname',
  815. 'foo',
  816. '--command',
  817. 'ANALYZE',
  818. ),
  819. environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  820. ).once()
  821. module.restore_data_source_dump(
  822. hook_config,
  823. {},
  824. data_source=hook_config[0],
  825. dry_run=False,
  826. extract_process=extract_process,
  827. connection_params={
  828. 'hostname': None,
  829. 'port': None,
  830. 'username': None,
  831. 'password': None,
  832. },
  833. borgmatic_runtime_directory='/run/borgmatic',
  834. )
  835. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  836. hook_config = [
  837. {
  838. 'name': 'foo',
  839. 'hostname': 'database.example.org',
  840. 'port': 5433,
  841. 'username': 'postgres',
  842. 'password': 'trustsome1',
  843. 'restore_hostname': 'restorehost',
  844. 'restore_port': 'restoreport',
  845. 'restore_username': 'restoreusername',
  846. 'restore_password': 'restorepassword',
  847. 'schemas': None,
  848. }
  849. ]
  850. extract_process = flexmock(stdout=flexmock())
  851. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  852. 'resolve_credential'
  853. ).replace_with(lambda value, config: value)
  854. flexmock(module).should_receive('make_environment').and_return(
  855. {'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'}
  856. )
  857. flexmock(module).should_receive('make_dump_path')
  858. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  859. flexmock(module).should_receive('execute_command_with_processes').with_args(
  860. (
  861. 'pg_restore',
  862. '--no-password',
  863. '--if-exists',
  864. '--exit-on-error',
  865. '--clean',
  866. '--dbname',
  867. 'foo',
  868. '--host',
  869. 'clihost',
  870. '--port',
  871. 'cliport',
  872. '--username',
  873. 'cliusername',
  874. ),
  875. processes=[extract_process],
  876. output_log_level=logging.DEBUG,
  877. input_file=extract_process.stdout,
  878. environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'},
  879. ).once()
  880. flexmock(module).should_receive('execute_command').with_args(
  881. (
  882. 'psql',
  883. '--no-password',
  884. '--no-psqlrc',
  885. '--quiet',
  886. '--host',
  887. 'clihost',
  888. '--port',
  889. 'cliport',
  890. '--username',
  891. 'cliusername',
  892. '--dbname',
  893. 'foo',
  894. '--command',
  895. 'ANALYZE',
  896. ),
  897. environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'},
  898. ).once()
  899. module.restore_data_source_dump(
  900. hook_config,
  901. {},
  902. data_source={'name': 'foo'},
  903. dry_run=False,
  904. extract_process=extract_process,
  905. connection_params={
  906. 'hostname': 'clihost',
  907. 'port': 'cliport',
  908. 'username': 'cliusername',
  909. 'password': 'clipassword',
  910. },
  911. borgmatic_runtime_directory='/run/borgmatic',
  912. )
  913. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  914. hook_config = [
  915. {
  916. 'name': 'foo',
  917. 'hostname': 'database.example.org',
  918. 'port': 5433,
  919. 'username': 'postgres',
  920. 'password': 'trustsome1',
  921. 'schemas': None,
  922. 'restore_hostname': 'restorehost',
  923. 'restore_port': 'restoreport',
  924. 'restore_username': 'restoreusername',
  925. 'restore_password': 'restorepassword',
  926. }
  927. ]
  928. extract_process = flexmock(stdout=flexmock())
  929. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  930. 'resolve_credential'
  931. ).replace_with(lambda value, config: value)
  932. flexmock(module).should_receive('make_environment').and_return(
  933. {'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'}
  934. )
  935. flexmock(module).should_receive('make_dump_path')
  936. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  937. flexmock(module).should_receive('execute_command_with_processes').with_args(
  938. (
  939. 'pg_restore',
  940. '--no-password',
  941. '--if-exists',
  942. '--exit-on-error',
  943. '--clean',
  944. '--dbname',
  945. 'foo',
  946. '--host',
  947. 'restorehost',
  948. '--port',
  949. 'restoreport',
  950. '--username',
  951. 'restoreusername',
  952. ),
  953. processes=[extract_process],
  954. output_log_level=logging.DEBUG,
  955. input_file=extract_process.stdout,
  956. environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'},
  957. ).once()
  958. flexmock(module).should_receive('execute_command').with_args(
  959. (
  960. 'psql',
  961. '--no-password',
  962. '--no-psqlrc',
  963. '--quiet',
  964. '--host',
  965. 'restorehost',
  966. '--port',
  967. 'restoreport',
  968. '--username',
  969. 'restoreusername',
  970. '--dbname',
  971. 'foo',
  972. '--command',
  973. 'ANALYZE',
  974. ),
  975. environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'},
  976. ).once()
  977. module.restore_data_source_dump(
  978. hook_config,
  979. {},
  980. data_source=hook_config[0],
  981. dry_run=False,
  982. extract_process=extract_process,
  983. connection_params={
  984. 'hostname': None,
  985. 'port': None,
  986. 'username': None,
  987. 'password': None,
  988. },
  989. borgmatic_runtime_directory='/run/borgmatic',
  990. )
  991. def test_restore_data_source_dump_runs_pg_restore_with_options():
  992. hook_config = [
  993. {
  994. 'name': 'foo',
  995. 'restore_options': '--harder',
  996. 'analyze_options': '--smarter',
  997. 'schemas': None,
  998. }
  999. ]
  1000. extract_process = flexmock(stdout=flexmock())
  1001. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1002. 'resolve_credential'
  1003. ).replace_with(lambda value, config: value)
  1004. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  1005. flexmock(module).should_receive('make_dump_path')
  1006. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  1007. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1008. (
  1009. 'pg_restore',
  1010. '--no-password',
  1011. '--if-exists',
  1012. '--exit-on-error',
  1013. '--clean',
  1014. '--dbname',
  1015. 'foo',
  1016. '--harder',
  1017. ),
  1018. processes=[extract_process],
  1019. output_log_level=logging.DEBUG,
  1020. input_file=extract_process.stdout,
  1021. environment={'PGSSLMODE': 'disable'},
  1022. ).once()
  1023. flexmock(module).should_receive('execute_command').with_args(
  1024. (
  1025. 'psql',
  1026. '--no-password',
  1027. '--no-psqlrc',
  1028. '--quiet',
  1029. '--dbname',
  1030. 'foo',
  1031. '--smarter',
  1032. '--command',
  1033. 'ANALYZE',
  1034. ),
  1035. environment={'PGSSLMODE': 'disable'},
  1036. ).once()
  1037. module.restore_data_source_dump(
  1038. hook_config,
  1039. {},
  1040. data_source=hook_config[0],
  1041. dry_run=False,
  1042. extract_process=extract_process,
  1043. connection_params={
  1044. 'hostname': None,
  1045. 'port': None,
  1046. 'username': None,
  1047. 'password': None,
  1048. },
  1049. borgmatic_runtime_directory='/run/borgmatic',
  1050. )
  1051. def test_restore_data_source_dump_runs_psql_for_all_database_dump():
  1052. hook_config = [{'name': 'all', 'schemas': None}]
  1053. extract_process = flexmock(stdout=flexmock())
  1054. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1055. 'resolve_credential'
  1056. ).replace_with(lambda value, config: value)
  1057. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  1058. flexmock(module).should_receive('make_dump_path')
  1059. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  1060. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1061. (
  1062. 'psql',
  1063. '--no-password',
  1064. '--no-psqlrc',
  1065. ),
  1066. processes=[extract_process],
  1067. output_log_level=logging.DEBUG,
  1068. input_file=extract_process.stdout,
  1069. environment={'PGSSLMODE': 'disable'},
  1070. ).once()
  1071. flexmock(module).should_receive('execute_command').with_args(
  1072. ('psql', '--no-password', '--no-psqlrc', '--quiet', '--command', 'ANALYZE'),
  1073. environment={'PGSSLMODE': 'disable'},
  1074. ).once()
  1075. module.restore_data_source_dump(
  1076. hook_config,
  1077. {},
  1078. data_source={'name': 'all'},
  1079. dry_run=False,
  1080. extract_process=extract_process,
  1081. connection_params={
  1082. 'hostname': None,
  1083. 'port': None,
  1084. 'username': None,
  1085. 'password': None,
  1086. },
  1087. borgmatic_runtime_directory='/run/borgmatic',
  1088. )
  1089. def test_restore_data_source_dump_runs_psql_for_plain_database_dump():
  1090. hook_config = [{'name': 'foo', 'format': 'plain', 'schemas': None}]
  1091. extract_process = flexmock(stdout=flexmock())
  1092. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1093. 'resolve_credential'
  1094. ).replace_with(lambda value, config: value)
  1095. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  1096. flexmock(module).should_receive('make_dump_path')
  1097. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  1098. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1099. ('psql', '--no-password', '--no-psqlrc', '--dbname', 'foo'),
  1100. processes=[extract_process],
  1101. output_log_level=logging.DEBUG,
  1102. input_file=extract_process.stdout,
  1103. environment={'PGSSLMODE': 'disable'},
  1104. ).once()
  1105. flexmock(module).should_receive('execute_command').with_args(
  1106. (
  1107. 'psql',
  1108. '--no-password',
  1109. '--no-psqlrc',
  1110. '--quiet',
  1111. '--dbname',
  1112. 'foo',
  1113. '--command',
  1114. 'ANALYZE',
  1115. ),
  1116. environment={'PGSSLMODE': 'disable'},
  1117. ).once()
  1118. module.restore_data_source_dump(
  1119. hook_config,
  1120. {},
  1121. data_source=hook_config[0],
  1122. dry_run=False,
  1123. extract_process=extract_process,
  1124. connection_params={
  1125. 'hostname': None,
  1126. 'port': None,
  1127. 'username': None,
  1128. 'password': None,
  1129. },
  1130. borgmatic_runtime_directory='/run/borgmatic',
  1131. )
  1132. def test_restore_data_source_dump_runs_non_default_pg_restore_and_psql():
  1133. hook_config = [
  1134. {
  1135. 'name': 'foo',
  1136. 'pg_restore_command': 'docker exec --workdir * mycontainer pg_restore',
  1137. 'psql_command': 'docker exec --workdir * mycontainer psql',
  1138. 'schemas': None,
  1139. }
  1140. ]
  1141. extract_process = flexmock(stdout=flexmock())
  1142. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1143. 'resolve_credential'
  1144. ).replace_with(lambda value, config: value)
  1145. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  1146. flexmock(module).should_receive('make_dump_path')
  1147. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  1148. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1149. (
  1150. 'docker',
  1151. 'exec',
  1152. '--workdir',
  1153. "'*'", # Should get shell escaped to prevent injection attacks.
  1154. 'mycontainer',
  1155. 'pg_restore',
  1156. '--no-password',
  1157. '--if-exists',
  1158. '--exit-on-error',
  1159. '--clean',
  1160. '--dbname',
  1161. 'foo',
  1162. ),
  1163. processes=[extract_process],
  1164. output_log_level=logging.DEBUG,
  1165. input_file=extract_process.stdout,
  1166. environment={'PGSSLMODE': 'disable'},
  1167. ).once()
  1168. flexmock(module).should_receive('execute_command').with_args(
  1169. (
  1170. 'docker',
  1171. 'exec',
  1172. '--workdir',
  1173. "'*'", # Should get shell escaped to prevent injection attacks.
  1174. 'mycontainer',
  1175. 'psql',
  1176. '--no-password',
  1177. '--no-psqlrc',
  1178. '--quiet',
  1179. '--dbname',
  1180. 'foo',
  1181. '--command',
  1182. 'ANALYZE',
  1183. ),
  1184. environment={'PGSSLMODE': 'disable'},
  1185. ).once()
  1186. module.restore_data_source_dump(
  1187. hook_config,
  1188. {},
  1189. data_source=hook_config[0],
  1190. dry_run=False,
  1191. extract_process=extract_process,
  1192. connection_params={
  1193. 'hostname': None,
  1194. 'port': None,
  1195. 'username': None,
  1196. 'password': None,
  1197. },
  1198. borgmatic_runtime_directory='/run/borgmatic',
  1199. )
  1200. def test_restore_data_source_dump_with_dry_run_skips_restore():
  1201. hook_config = [{'name': 'foo', 'schemas': None}]
  1202. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1203. 'resolve_credential'
  1204. ).replace_with(lambda value, config: value)
  1205. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  1206. flexmock(module).should_receive('make_dump_path')
  1207. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  1208. flexmock(module).should_receive('execute_command_with_processes').never()
  1209. module.restore_data_source_dump(
  1210. hook_config,
  1211. {},
  1212. data_source={'name': 'foo'},
  1213. dry_run=True,
  1214. extract_process=flexmock(),
  1215. connection_params={
  1216. 'hostname': None,
  1217. 'port': None,
  1218. 'username': None,
  1219. 'password': None,
  1220. },
  1221. borgmatic_runtime_directory='/run/borgmatic',
  1222. )
  1223. def test_restore_data_source_dump_without_extract_process_restores_from_disk():
  1224. hook_config = [{'name': 'foo', 'schemas': None}]
  1225. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1226. 'resolve_credential'
  1227. ).replace_with(lambda value, config: value)
  1228. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  1229. flexmock(module).should_receive('make_dump_path')
  1230. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  1231. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1232. (
  1233. 'pg_restore',
  1234. '--no-password',
  1235. '--if-exists',
  1236. '--exit-on-error',
  1237. '--clean',
  1238. '--dbname',
  1239. 'foo',
  1240. '/dump/path',
  1241. ),
  1242. processes=[],
  1243. output_log_level=logging.DEBUG,
  1244. input_file=None,
  1245. environment={'PGSSLMODE': 'disable'},
  1246. ).once()
  1247. flexmock(module).should_receive('execute_command').with_args(
  1248. (
  1249. 'psql',
  1250. '--no-password',
  1251. '--no-psqlrc',
  1252. '--quiet',
  1253. '--dbname',
  1254. 'foo',
  1255. '--command',
  1256. 'ANALYZE',
  1257. ),
  1258. environment={'PGSSLMODE': 'disable'},
  1259. ).once()
  1260. module.restore_data_source_dump(
  1261. hook_config,
  1262. {},
  1263. data_source={'name': 'foo'},
  1264. dry_run=False,
  1265. extract_process=None,
  1266. connection_params={
  1267. 'hostname': None,
  1268. 'port': None,
  1269. 'username': None,
  1270. 'password': None,
  1271. },
  1272. borgmatic_runtime_directory='/run/borgmatic',
  1273. )
  1274. def test_restore_data_source_dump_with_schemas_restores_schemas():
  1275. hook_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
  1276. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  1277. 'resolve_credential'
  1278. ).replace_with(lambda value, config: value)
  1279. flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'})
  1280. flexmock(module).should_receive('make_dump_path')
  1281. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  1282. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1283. (
  1284. 'pg_restore',
  1285. '--no-password',
  1286. '--if-exists',
  1287. '--exit-on-error',
  1288. '--clean',
  1289. '--dbname',
  1290. 'foo',
  1291. '/dump/path',
  1292. '--schema',
  1293. 'bar',
  1294. '--schema',
  1295. 'baz',
  1296. ),
  1297. processes=[],
  1298. output_log_level=logging.DEBUG,
  1299. input_file=None,
  1300. environment={'PGSSLMODE': 'disable'},
  1301. ).once()
  1302. flexmock(module).should_receive('execute_command').with_args(
  1303. (
  1304. 'psql',
  1305. '--no-password',
  1306. '--no-psqlrc',
  1307. '--quiet',
  1308. '--dbname',
  1309. 'foo',
  1310. '--command',
  1311. 'ANALYZE',
  1312. ),
  1313. environment={'PGSSLMODE': 'disable'},
  1314. ).once()
  1315. module.restore_data_source_dump(
  1316. hook_config,
  1317. {},
  1318. data_source=hook_config[0],
  1319. dry_run=False,
  1320. extract_process=None,
  1321. connection_params={
  1322. 'hostname': None,
  1323. 'port': None,
  1324. 'username': None,
  1325. 'password': None,
  1326. },
  1327. borgmatic_runtime_directory='/run/borgmatic',
  1328. )