test_mysql.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks import mysql 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_mysql_for_database_names():
  21. extra_environment = flexmock()
  22. log_prefix = ''
  23. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  24. ('mysql', '--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_mysql_with_list_options():
  98. database = {'name': 'all', 'list_options': '--defaults-extra-file=my.cnf'}
  99. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  100. (
  101. 'mysql',
  102. '--defaults-extra-file=my.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_mysql_with_list_options():
  112. database = {
  113. 'name': 'all',
  114. 'list_options': '--defaults-extra-file=my.cnf',
  115. 'mysql_command': 'custom_mysql',
  116. }
  117. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  118. extra_environment=None,
  119. full_command=(
  120. 'custom_mysql', # Custom MySQL command
  121. '--defaults-extra-file=my.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_mysqldump():
  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. 'mysqldump',
  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_mysqldump_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. 'mysqldump',
  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_mysqldump_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. 'mysqldump',
  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_mysqldump_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. 'mysqldump',
  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_mysqldump_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. 'mysqldump',
  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_mysqldump():
  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_mysqldump', # Custom MySQL dump command
  290. '--add-drop-database',
  291. '--databases',
  292. 'foo',
  293. '--result-file',
  294. 'dump',
  295. ),
  296. extra_environment=None,
  297. run_to_completion=False,
  298. ).and_return(process).once()
  299. assert (
  300. module.execute_dump_command(
  301. database={
  302. 'name': 'foo',
  303. 'mysql_dump_command': 'custom_mysqldump',
  304. }, # Custom MySQL dump command specified
  305. log_prefix='log',
  306. dump_path=flexmock(),
  307. database_names=('foo',),
  308. extra_environment=None,
  309. dry_run=False,
  310. dry_run_label='',
  311. )
  312. == process
  313. )
  314. def test_execute_dump_command_with_duplicate_dump_skips_mysqldump():
  315. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  316. flexmock(module.os.path).should_receive('exists').and_return(True)
  317. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  318. flexmock(module).should_receive('execute_command').never()
  319. assert (
  320. module.execute_dump_command(
  321. database={'name': 'foo'},
  322. log_prefix='log',
  323. dump_path=flexmock(),
  324. database_names=('foo',),
  325. extra_environment=None,
  326. dry_run=True,
  327. dry_run_label='SO DRY',
  328. )
  329. is None
  330. )
  331. def test_execute_dump_command_with_dry_run_skips_mysqldump():
  332. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  333. flexmock(module.os.path).should_receive('exists').and_return(False)
  334. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  335. flexmock(module).should_receive('execute_command').never()
  336. assert (
  337. module.execute_dump_command(
  338. database={'name': 'foo'},
  339. log_prefix='log',
  340. dump_path=flexmock(),
  341. database_names=('foo',),
  342. extra_environment=None,
  343. dry_run=True,
  344. dry_run_label='SO DRY',
  345. )
  346. is None
  347. )
  348. def test_dump_data_sources_errors_for_missing_all_databases():
  349. databases = [{'name': 'all'}]
  350. flexmock(module).should_receive('make_dump_path').and_return('')
  351. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  352. 'databases/localhost/all'
  353. )
  354. flexmock(module).should_receive('database_names_to_dump').and_return(())
  355. with pytest.raises(ValueError):
  356. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False)
  357. def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
  358. databases = [{'name': 'all'}]
  359. flexmock(module).should_receive('make_dump_path').and_return('')
  360. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  361. 'databases/localhost/all'
  362. )
  363. flexmock(module).should_receive('database_names_to_dump').and_return(())
  364. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=True) == []
  365. def test_restore_data_source_dump_runs_mysql_to_restore():
  366. hook_config = [{'name': 'foo'}, {'name': 'bar'}]
  367. extract_process = flexmock(stdout=flexmock())
  368. flexmock(module).should_receive('execute_command_with_processes').with_args(
  369. ('mysql', '--batch'),
  370. processes=[extract_process],
  371. output_log_level=logging.DEBUG,
  372. input_file=extract_process.stdout,
  373. extra_environment=None,
  374. ).once()
  375. module.restore_data_source_dump(
  376. hook_config,
  377. {},
  378. 'test.yaml',
  379. data_source={'name': 'foo'},
  380. dry_run=False,
  381. extract_process=extract_process,
  382. connection_params={
  383. 'hostname': None,
  384. 'port': None,
  385. 'username': None,
  386. 'password': None,
  387. },
  388. )
  389. def test_restore_data_source_dump_runs_mysql_with_options():
  390. hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
  391. extract_process = flexmock(stdout=flexmock())
  392. flexmock(module).should_receive('execute_command_with_processes').with_args(
  393. ('mysql', '--batch', '--harder'),
  394. processes=[extract_process],
  395. output_log_level=logging.DEBUG,
  396. input_file=extract_process.stdout,
  397. extra_environment=None,
  398. ).once()
  399. module.restore_data_source_dump(
  400. hook_config,
  401. {},
  402. 'test.yaml',
  403. data_source=hook_config[0],
  404. dry_run=False,
  405. extract_process=extract_process,
  406. connection_params={
  407. 'hostname': None,
  408. 'port': None,
  409. 'username': None,
  410. 'password': None,
  411. },
  412. )
  413. def test_restore_data_source_dump_runs_non_default_mysql_with_options():
  414. hook_config = [{'name': 'foo', 'mysql_command': 'custom_mysql', 'restore_options': '--harder'}]
  415. extract_process = flexmock(stdout=flexmock())
  416. flexmock(module).should_receive('execute_command_with_processes').with_args(
  417. ('custom_mysql', '--batch', '--harder'),
  418. processes=[extract_process],
  419. output_log_level=logging.DEBUG,
  420. input_file=extract_process.stdout,
  421. extra_environment=None,
  422. ).once()
  423. module.restore_data_source_dump(
  424. hook_config,
  425. {},
  426. 'test.yaml',
  427. data_source=hook_config[0],
  428. dry_run=False,
  429. extract_process=extract_process,
  430. connection_params={
  431. 'hostname': None,
  432. 'port': None,
  433. 'username': None,
  434. 'password': None,
  435. },
  436. )
  437. def test_restore_data_source_dump_runs_mysql_with_hostname_and_port():
  438. hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  439. extract_process = flexmock(stdout=flexmock())
  440. flexmock(module).should_receive('execute_command_with_processes').with_args(
  441. (
  442. 'mysql',
  443. '--batch',
  444. '--host',
  445. 'database.example.org',
  446. '--port',
  447. '5433',
  448. '--protocol',
  449. 'tcp',
  450. ),
  451. processes=[extract_process],
  452. output_log_level=logging.DEBUG,
  453. input_file=extract_process.stdout,
  454. extra_environment=None,
  455. ).once()
  456. module.restore_data_source_dump(
  457. hook_config,
  458. {},
  459. 'test.yaml',
  460. data_source=hook_config[0],
  461. dry_run=False,
  462. extract_process=extract_process,
  463. connection_params={
  464. 'hostname': None,
  465. 'port': None,
  466. 'username': None,
  467. 'password': None,
  468. },
  469. )
  470. def test_restore_data_source_dump_runs_mysql_with_username_and_password():
  471. hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  472. extract_process = flexmock(stdout=flexmock())
  473. flexmock(module).should_receive('execute_command_with_processes').with_args(
  474. ('mysql', '--batch', '--user', 'root'),
  475. processes=[extract_process],
  476. output_log_level=logging.DEBUG,
  477. input_file=extract_process.stdout,
  478. extra_environment={'MYSQL_PWD': 'trustsome1'},
  479. ).once()
  480. module.restore_data_source_dump(
  481. hook_config,
  482. {},
  483. 'test.yaml',
  484. data_source=hook_config[0],
  485. dry_run=False,
  486. extract_process=extract_process,
  487. connection_params={
  488. 'hostname': None,
  489. 'port': None,
  490. 'username': None,
  491. 'password': None,
  492. },
  493. )
  494. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  495. hook_config = [
  496. {
  497. 'name': 'foo',
  498. 'username': 'root',
  499. 'password': 'trustsome1',
  500. 'restore_hostname': 'restorehost',
  501. 'restore_port': 'restoreport',
  502. 'restore_username': 'restoreusername',
  503. 'restore_password': 'restorepassword',
  504. }
  505. ]
  506. extract_process = flexmock(stdout=flexmock())
  507. flexmock(module).should_receive('execute_command_with_processes').with_args(
  508. (
  509. 'mysql',
  510. '--batch',
  511. '--host',
  512. 'clihost',
  513. '--port',
  514. 'cliport',
  515. '--protocol',
  516. 'tcp',
  517. '--user',
  518. 'cliusername',
  519. ),
  520. processes=[extract_process],
  521. output_log_level=logging.DEBUG,
  522. input_file=extract_process.stdout,
  523. extra_environment={'MYSQL_PWD': 'clipassword'},
  524. ).once()
  525. module.restore_data_source_dump(
  526. hook_config,
  527. {},
  528. 'test.yaml',
  529. data_source={'name': 'foo'},
  530. dry_run=False,
  531. extract_process=extract_process,
  532. connection_params={
  533. 'hostname': 'clihost',
  534. 'port': 'cliport',
  535. 'username': 'cliusername',
  536. 'password': 'clipassword',
  537. },
  538. )
  539. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  540. hook_config = [
  541. {
  542. 'name': 'foo',
  543. 'username': 'root',
  544. 'password': 'trustsome1',
  545. 'hostname': 'dbhost',
  546. 'port': 'dbport',
  547. 'restore_username': 'restoreuser',
  548. 'restore_password': 'restorepass',
  549. 'restore_hostname': 'restorehost',
  550. 'restore_port': 'restoreport',
  551. }
  552. ]
  553. extract_process = flexmock(stdout=flexmock())
  554. flexmock(module).should_receive('execute_command_with_processes').with_args(
  555. (
  556. 'mysql',
  557. '--batch',
  558. '--host',
  559. 'restorehost',
  560. '--port',
  561. 'restoreport',
  562. '--protocol',
  563. 'tcp',
  564. '--user',
  565. 'restoreuser',
  566. ),
  567. processes=[extract_process],
  568. output_log_level=logging.DEBUG,
  569. input_file=extract_process.stdout,
  570. extra_environment={'MYSQL_PWD': 'restorepass'},
  571. ).once()
  572. module.restore_data_source_dump(
  573. hook_config,
  574. {},
  575. 'test.yaml',
  576. data_source=hook_config[0],
  577. dry_run=False,
  578. extract_process=extract_process,
  579. connection_params={
  580. 'hostname': None,
  581. 'port': None,
  582. 'username': None,
  583. 'password': None,
  584. },
  585. )
  586. def test_restore_data_source_dump_with_dry_run_skips_restore():
  587. hook_config = [{'name': 'foo'}]
  588. flexmock(module).should_receive('execute_command_with_processes').never()
  589. module.restore_data_source_dump(
  590. hook_config,
  591. {},
  592. 'test.yaml',
  593. data_source={'name': 'foo'},
  594. dry_run=True,
  595. extract_process=flexmock(),
  596. connection_params={
  597. 'hostname': None,
  598. 'port': None,
  599. 'username': None,
  600. 'password': None,
  601. },
  602. )