test_mysql.py 22 KB

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