test_postgresql.py 40 KB

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