test_postgresql.py 50 KB

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