test_mariadb.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks import mariadb as module
  5. def test_database_names_to_dump_passes_through_name():
  6. extra_environment = flexmock()
  7. log_prefix = ''
  8. names = module.database_names_to_dump(
  9. {'name': 'foo'}, extra_environment, log_prefix, dry_run=False
  10. )
  11. assert names == ('foo',)
  12. def test_database_names_to_dump_bails_for_dry_run():
  13. extra_environment = flexmock()
  14. log_prefix = ''
  15. flexmock(module).should_receive('execute_command_and_capture_output').never()
  16. names = module.database_names_to_dump(
  17. {'name': 'all'}, extra_environment, log_prefix, dry_run=True
  18. )
  19. assert names == ()
  20. def test_database_names_to_dump_queries_mariadb_for_database_names():
  21. extra_environment = flexmock()
  22. log_prefix = ''
  23. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  24. ('mariadb', '--skip-column-names', '--batch', '--execute', 'show schemas'),
  25. extra_environment=extra_environment,
  26. ).and_return('foo\nbar\nmysql\n').once()
  27. names = module.database_names_to_dump(
  28. {'name': 'all'}, extra_environment, log_prefix, dry_run=False
  29. )
  30. assert names == ('foo', 'bar')
  31. def test_dump_data_sources_dumps_each_database():
  32. databases = [{'name': 'foo'}, {'name': 'bar'}]
  33. processes = [flexmock(), flexmock()]
  34. flexmock(module).should_receive('make_dump_path').and_return('')
  35. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  36. ('bar',)
  37. )
  38. for name, process in zip(('foo', 'bar'), processes):
  39. flexmock(module).should_receive('execute_dump_command').with_args(
  40. database={'name': name},
  41. log_prefix=object,
  42. dump_path=object,
  43. database_names=(name,),
  44. extra_environment=object,
  45. dry_run=object,
  46. dry_run_label=object,
  47. ).and_return(process).once()
  48. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
  49. def test_dump_data_sources_dumps_with_password():
  50. database = {'name': 'foo', 'username': 'root', 'password': 'trustsome1'}
  51. process = flexmock()
  52. flexmock(module).should_receive('make_dump_path').and_return('')
  53. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  54. ('bar',)
  55. )
  56. flexmock(module).should_receive('execute_dump_command').with_args(
  57. database=database,
  58. log_prefix=object,
  59. dump_path=object,
  60. database_names=('foo',),
  61. extra_environment={'MYSQL_PWD': 'trustsome1'},
  62. dry_run=object,
  63. dry_run_label=object,
  64. ).and_return(process).once()
  65. assert module.dump_data_sources([database], {}, 'test.yaml', dry_run=False) == [process]
  66. def test_dump_data_sources_dumps_all_databases_at_once():
  67. databases = [{'name': 'all'}]
  68. process = flexmock()
  69. flexmock(module).should_receive('make_dump_path').and_return('')
  70. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  71. flexmock(module).should_receive('execute_dump_command').with_args(
  72. database={'name': 'all'},
  73. log_prefix=object,
  74. dump_path=object,
  75. database_names=('foo', 'bar'),
  76. extra_environment=object,
  77. dry_run=object,
  78. dry_run_label=object,
  79. ).and_return(process).once()
  80. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  81. def test_dump_data_sources_dumps_all_databases_separately_when_format_configured():
  82. databases = [{'name': 'all', 'format': 'sql'}]
  83. processes = [flexmock(), flexmock()]
  84. flexmock(module).should_receive('make_dump_path').and_return('')
  85. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  86. for name, process in zip(('foo', 'bar'), processes):
  87. flexmock(module).should_receive('execute_dump_command').with_args(
  88. database={'name': name, 'format': 'sql'},
  89. log_prefix=object,
  90. dump_path=object,
  91. database_names=(name,),
  92. extra_environment=object,
  93. dry_run=object,
  94. dry_run_label=object,
  95. ).and_return(process).once()
  96. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
  97. def test_database_names_to_dump_runs_mariadb_with_list_options():
  98. database = {'name': 'all', 'list_options': '--defaults-extra-file=mariadb.cnf'}
  99. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  100. (
  101. 'mariadb',
  102. '--defaults-extra-file=mariadb.cnf',
  103. '--skip-column-names',
  104. '--batch',
  105. '--execute',
  106. 'show schemas',
  107. ),
  108. extra_environment=None,
  109. ).and_return(('foo\nbar')).once()
  110. assert module.database_names_to_dump(database, None, 'test.yaml', '') == ('foo', 'bar')
  111. def test_database_names_to_dump_runs_non_default_mariadb_with_list_options():
  112. database = {
  113. 'name': 'all',
  114. 'list_options': '--defaults-extra-file=mariadb.cnf',
  115. 'mariadb_command': 'custom_mariadb',
  116. }
  117. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  118. extra_environment=None,
  119. full_command=(
  120. 'custom_mariadb', # Custom MariaDB command
  121. '--defaults-extra-file=mariadb.cnf',
  122. '--skip-column-names',
  123. '--batch',
  124. '--execute',
  125. 'show schemas',
  126. ),
  127. ).and_return(('foo\nbar')).once()
  128. assert module.database_names_to_dump(database, None, 'test.yaml', '') == ('foo', 'bar')
  129. def test_execute_dump_command_runs_mariadb_dump():
  130. process = flexmock()
  131. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  132. flexmock(module.os.path).should_receive('exists').and_return(False)
  133. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  134. flexmock(module).should_receive('execute_command').with_args(
  135. (
  136. 'mariadb-dump',
  137. '--add-drop-database',
  138. '--databases',
  139. 'foo',
  140. '--result-file',
  141. 'dump',
  142. ),
  143. extra_environment=None,
  144. run_to_completion=False,
  145. ).and_return(process).once()
  146. assert (
  147. module.execute_dump_command(
  148. database={'name': 'foo'},
  149. log_prefix='log',
  150. dump_path=flexmock(),
  151. database_names=('foo',),
  152. extra_environment=None,
  153. dry_run=False,
  154. dry_run_label='',
  155. )
  156. == process
  157. )
  158. def test_execute_dump_command_runs_mariadb_dump_without_add_drop_database():
  159. process = flexmock()
  160. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  161. flexmock(module.os.path).should_receive('exists').and_return(False)
  162. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  163. flexmock(module).should_receive('execute_command').with_args(
  164. (
  165. 'mariadb-dump',
  166. '--databases',
  167. 'foo',
  168. '--result-file',
  169. 'dump',
  170. ),
  171. extra_environment=None,
  172. run_to_completion=False,
  173. ).and_return(process).once()
  174. assert (
  175. module.execute_dump_command(
  176. database={'name': 'foo', 'add_drop_database': False},
  177. log_prefix='log',
  178. dump_path=flexmock(),
  179. database_names=('foo',),
  180. extra_environment=None,
  181. dry_run=False,
  182. dry_run_label='',
  183. )
  184. == process
  185. )
  186. def test_execute_dump_command_runs_mariadb_dump_with_hostname_and_port():
  187. process = flexmock()
  188. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  189. flexmock(module.os.path).should_receive('exists').and_return(False)
  190. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  191. flexmock(module).should_receive('execute_command').with_args(
  192. (
  193. 'mariadb-dump',
  194. '--add-drop-database',
  195. '--host',
  196. 'database.example.org',
  197. '--port',
  198. '5433',
  199. '--protocol',
  200. 'tcp',
  201. '--databases',
  202. 'foo',
  203. '--result-file',
  204. 'dump',
  205. ),
  206. extra_environment=None,
  207. run_to_completion=False,
  208. ).and_return(process).once()
  209. assert (
  210. module.execute_dump_command(
  211. database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},
  212. log_prefix='log',
  213. dump_path=flexmock(),
  214. database_names=('foo',),
  215. extra_environment=None,
  216. dry_run=False,
  217. dry_run_label='',
  218. )
  219. == process
  220. )
  221. def test_execute_dump_command_runs_mariadb_dump_with_username_and_password():
  222. process = flexmock()
  223. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  224. flexmock(module.os.path).should_receive('exists').and_return(False)
  225. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  226. flexmock(module).should_receive('execute_command').with_args(
  227. (
  228. 'mariadb-dump',
  229. '--add-drop-database',
  230. '--user',
  231. 'root',
  232. '--databases',
  233. 'foo',
  234. '--result-file',
  235. 'dump',
  236. ),
  237. extra_environment={'MYSQL_PWD': 'trustsome1'},
  238. run_to_completion=False,
  239. ).and_return(process).once()
  240. assert (
  241. module.execute_dump_command(
  242. database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},
  243. log_prefix='log',
  244. dump_path=flexmock(),
  245. database_names=('foo',),
  246. extra_environment={'MYSQL_PWD': 'trustsome1'},
  247. dry_run=False,
  248. dry_run_label='',
  249. )
  250. == process
  251. )
  252. def test_execute_dump_command_runs_mariadb_dump_with_options():
  253. process = flexmock()
  254. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  255. flexmock(module.os.path).should_receive('exists').and_return(False)
  256. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  257. flexmock(module).should_receive('execute_command').with_args(
  258. (
  259. 'mariadb-dump',
  260. '--stuff=such',
  261. '--add-drop-database',
  262. '--databases',
  263. 'foo',
  264. '--result-file',
  265. 'dump',
  266. ),
  267. extra_environment=None,
  268. run_to_completion=False,
  269. ).and_return(process).once()
  270. assert (
  271. module.execute_dump_command(
  272. database={'name': 'foo', 'options': '--stuff=such'},
  273. log_prefix='log',
  274. dump_path=flexmock(),
  275. database_names=('foo',),
  276. extra_environment=None,
  277. dry_run=False,
  278. dry_run_label='',
  279. )
  280. == process
  281. )
  282. def test_execute_dump_command_runs_non_default_mariadb_dump_with_options():
  283. process = flexmock()
  284. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  285. flexmock(module.os.path).should_receive('exists').and_return(False)
  286. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  287. flexmock(module).should_receive('execute_command').with_args(
  288. (
  289. 'custom_mariadb_dump', # Custom MariaDB dump command
  290. '--stuff=such',
  291. '--add-drop-database',
  292. '--databases',
  293. 'foo',
  294. '--result-file',
  295. 'dump',
  296. ),
  297. extra_environment=None,
  298. run_to_completion=False,
  299. ).and_return(process).once()
  300. assert (
  301. module.execute_dump_command(
  302. database={
  303. 'name': 'foo',
  304. 'mariadb_dump_command': 'custom_mariadb_dump',
  305. 'options': '--stuff=such',
  306. }, # Custom MariaDB dump command specified
  307. log_prefix='log',
  308. dump_path=flexmock(),
  309. database_names=('foo',),
  310. extra_environment=None,
  311. dry_run=False,
  312. dry_run_label='',
  313. )
  314. == process
  315. )
  316. def test_execute_dump_command_with_duplicate_dump_skips_mariadb_dump():
  317. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  318. flexmock(module.os.path).should_receive('exists').and_return(True)
  319. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  320. flexmock(module).should_receive('execute_command').never()
  321. assert (
  322. module.execute_dump_command(
  323. database={'name': 'foo'},
  324. log_prefix='log',
  325. dump_path=flexmock(),
  326. database_names=('foo',),
  327. extra_environment=None,
  328. dry_run=True,
  329. dry_run_label='SO DRY',
  330. )
  331. is None
  332. )
  333. def test_execute_dump_command_with_dry_run_skips_mariadb_dump():
  334. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  335. flexmock(module.os.path).should_receive('exists').and_return(False)
  336. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  337. flexmock(module).should_receive('execute_command').never()
  338. assert (
  339. module.execute_dump_command(
  340. database={'name': 'foo'},
  341. log_prefix='log',
  342. dump_path=flexmock(),
  343. database_names=('foo',),
  344. extra_environment=None,
  345. dry_run=True,
  346. dry_run_label='SO DRY',
  347. )
  348. is None
  349. )
  350. def test_dump_data_sources_errors_for_missing_all_databases():
  351. databases = [{'name': 'all'}]
  352. flexmock(module).should_receive('make_dump_path').and_return('')
  353. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  354. 'databases/localhost/all'
  355. )
  356. flexmock(module).should_receive('database_names_to_dump').and_return(())
  357. with pytest.raises(ValueError):
  358. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False)
  359. def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
  360. databases = [{'name': 'all'}]
  361. flexmock(module).should_receive('make_dump_path').and_return('')
  362. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  363. 'databases/localhost/all'
  364. )
  365. flexmock(module).should_receive('database_names_to_dump').and_return(())
  366. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=True) == []
  367. def test_restore_data_source_dump_runs_mariadb_to_restore():
  368. hook_config = [{'name': 'foo'}, {'name': 'bar'}]
  369. extract_process = flexmock(stdout=flexmock())
  370. flexmock(module).should_receive('execute_command_with_processes').with_args(
  371. ('mariadb', '--batch'),
  372. processes=[extract_process],
  373. output_log_level=logging.DEBUG,
  374. input_file=extract_process.stdout,
  375. extra_environment=None,
  376. ).once()
  377. module.restore_data_source_dump(
  378. hook_config,
  379. {},
  380. 'test.yaml',
  381. data_source={'name': 'foo'},
  382. dry_run=False,
  383. extract_process=extract_process,
  384. connection_params={
  385. 'hostname': None,
  386. 'port': None,
  387. 'username': None,
  388. 'password': None,
  389. },
  390. )
  391. def test_restore_data_source_dump_runs_mariadb_with_options():
  392. hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
  393. extract_process = flexmock(stdout=flexmock())
  394. flexmock(module).should_receive('execute_command_with_processes').with_args(
  395. ('mariadb', '--batch', '--harder'),
  396. processes=[extract_process],
  397. output_log_level=logging.DEBUG,
  398. input_file=extract_process.stdout,
  399. extra_environment=None,
  400. ).once()
  401. module.restore_data_source_dump(
  402. hook_config,
  403. {},
  404. 'test.yaml',
  405. data_source=hook_config[0],
  406. dry_run=False,
  407. extract_process=extract_process,
  408. connection_params={
  409. 'hostname': None,
  410. 'port': None,
  411. 'username': None,
  412. 'password': None,
  413. },
  414. )
  415. def test_restore_data_source_dump_runs_non_default_mariadb_with_options():
  416. hook_config = [
  417. {'name': 'foo', 'restore_options': '--harder', 'mariadb_command': 'custom_mariadb'}
  418. ]
  419. extract_process = flexmock(stdout=flexmock())
  420. flexmock(module).should_receive('execute_command_with_processes').with_args(
  421. ('custom_mariadb', '--batch', '--harder'),
  422. processes=[extract_process],
  423. output_log_level=logging.DEBUG,
  424. input_file=extract_process.stdout,
  425. extra_environment=None,
  426. ).once()
  427. module.restore_data_source_dump(
  428. hook_config,
  429. {},
  430. 'test.yaml',
  431. data_source=hook_config[0],
  432. dry_run=False,
  433. extract_process=extract_process,
  434. connection_params={
  435. 'hostname': None,
  436. 'port': None,
  437. 'username': None,
  438. 'password': None,
  439. },
  440. )
  441. def test_restore_data_source_dump_runs_mariadb_with_hostname_and_port():
  442. hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  443. extract_process = flexmock(stdout=flexmock())
  444. flexmock(module).should_receive('execute_command_with_processes').with_args(
  445. (
  446. 'mariadb',
  447. '--batch',
  448. '--host',
  449. 'database.example.org',
  450. '--port',
  451. '5433',
  452. '--protocol',
  453. 'tcp',
  454. ),
  455. processes=[extract_process],
  456. output_log_level=logging.DEBUG,
  457. input_file=extract_process.stdout,
  458. extra_environment=None,
  459. ).once()
  460. module.restore_data_source_dump(
  461. hook_config,
  462. {},
  463. 'test.yaml',
  464. data_source=hook_config[0],
  465. dry_run=False,
  466. extract_process=extract_process,
  467. connection_params={
  468. 'hostname': None,
  469. 'port': None,
  470. 'username': None,
  471. 'password': None,
  472. },
  473. )
  474. def test_restore_data_source_dump_runs_mariadb_with_username_and_password():
  475. hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  476. extract_process = flexmock(stdout=flexmock())
  477. flexmock(module).should_receive('execute_command_with_processes').with_args(
  478. ('mariadb', '--batch', '--user', 'root'),
  479. processes=[extract_process],
  480. output_log_level=logging.DEBUG,
  481. input_file=extract_process.stdout,
  482. extra_environment={'MYSQL_PWD': 'trustsome1'},
  483. ).once()
  484. module.restore_data_source_dump(
  485. hook_config,
  486. {},
  487. 'test.yaml',
  488. data_source=hook_config[0],
  489. dry_run=False,
  490. extract_process=extract_process,
  491. connection_params={
  492. 'hostname': None,
  493. 'port': None,
  494. 'username': None,
  495. 'password': None,
  496. },
  497. )
  498. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  499. hook_config = [
  500. {
  501. 'name': 'foo',
  502. 'username': 'root',
  503. 'password': 'trustsome1',
  504. 'restore_hostname': 'restorehost',
  505. 'restore_port': 'restoreport',
  506. 'restore_username': 'restoreusername',
  507. 'restore_password': 'restorepassword',
  508. }
  509. ]
  510. extract_process = flexmock(stdout=flexmock())
  511. flexmock(module).should_receive('execute_command_with_processes').with_args(
  512. (
  513. 'mariadb',
  514. '--batch',
  515. '--host',
  516. 'clihost',
  517. '--port',
  518. 'cliport',
  519. '--protocol',
  520. 'tcp',
  521. '--user',
  522. 'cliusername',
  523. ),
  524. processes=[extract_process],
  525. output_log_level=logging.DEBUG,
  526. input_file=extract_process.stdout,
  527. extra_environment={'MYSQL_PWD': 'clipassword'},
  528. ).once()
  529. module.restore_data_source_dump(
  530. hook_config,
  531. {},
  532. 'test.yaml',
  533. data_source=hook_config[0],
  534. dry_run=False,
  535. extract_process=extract_process,
  536. connection_params={
  537. 'hostname': 'clihost',
  538. 'port': 'cliport',
  539. 'username': 'cliusername',
  540. 'password': 'clipassword',
  541. },
  542. )
  543. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  544. hook_config = [
  545. {
  546. 'name': 'foo',
  547. 'username': 'root',
  548. 'password': 'trustsome1',
  549. 'hostname': 'dbhost',
  550. 'port': 'dbport',
  551. 'restore_username': 'restoreuser',
  552. 'restore_password': 'restorepass',
  553. 'restore_hostname': 'restorehost',
  554. 'restore_port': 'restoreport',
  555. }
  556. ]
  557. extract_process = flexmock(stdout=flexmock())
  558. flexmock(module).should_receive('execute_command_with_processes').with_args(
  559. (
  560. 'mariadb',
  561. '--batch',
  562. '--host',
  563. 'restorehost',
  564. '--port',
  565. 'restoreport',
  566. '--protocol',
  567. 'tcp',
  568. '--user',
  569. 'restoreuser',
  570. ),
  571. processes=[extract_process],
  572. output_log_level=logging.DEBUG,
  573. input_file=extract_process.stdout,
  574. extra_environment={'MYSQL_PWD': 'restorepass'},
  575. ).once()
  576. module.restore_data_source_dump(
  577. hook_config,
  578. {},
  579. 'test.yaml',
  580. data_source=hook_config[0],
  581. dry_run=False,
  582. extract_process=extract_process,
  583. connection_params={
  584. 'hostname': None,
  585. 'port': None,
  586. 'username': None,
  587. 'password': None,
  588. },
  589. )
  590. def test_restore_data_source_dump_with_dry_run_skips_restore():
  591. hook_config = [{'name': 'foo'}]
  592. flexmock(module).should_receive('execute_command_with_processes').never()
  593. module.restore_data_source_dump(
  594. hook_config,
  595. {},
  596. 'test.yaml',
  597. data_source={'name': 'foo'},
  598. dry_run=True,
  599. extract_process=flexmock(),
  600. connection_params={
  601. 'hostname': None,
  602. 'port': None,
  603. 'username': None,
  604. 'password': None,
  605. },
  606. )