test_mysql.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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_execute_dump_command_runs_mysqldump():
  112. process = flexmock()
  113. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  114. flexmock(module.os.path).should_receive('exists').and_return(False)
  115. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  116. flexmock(module).should_receive('execute_command').with_args(
  117. (
  118. 'mysqldump',
  119. '--add-drop-database',
  120. '--databases',
  121. 'foo',
  122. '--result-file',
  123. 'dump',
  124. ),
  125. extra_environment=None,
  126. run_to_completion=False,
  127. ).and_return(process).once()
  128. assert (
  129. module.execute_dump_command(
  130. database={'name': 'foo'},
  131. log_prefix='log',
  132. dump_path=flexmock(),
  133. database_names=('foo',),
  134. extra_environment=None,
  135. dry_run=False,
  136. dry_run_label='',
  137. )
  138. == process
  139. )
  140. def test_execute_dump_command_runs_mysqldump_without_add_drop_database():
  141. process = flexmock()
  142. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  143. flexmock(module.os.path).should_receive('exists').and_return(False)
  144. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  145. flexmock(module).should_receive('execute_command').with_args(
  146. (
  147. 'mysqldump',
  148. '--databases',
  149. 'foo',
  150. '--result-file',
  151. 'dump',
  152. ),
  153. extra_environment=None,
  154. run_to_completion=False,
  155. ).and_return(process).once()
  156. assert (
  157. module.execute_dump_command(
  158. database={'name': 'foo', 'add_drop_database': False},
  159. log_prefix='log',
  160. dump_path=flexmock(),
  161. database_names=('foo',),
  162. extra_environment=None,
  163. dry_run=False,
  164. dry_run_label='',
  165. )
  166. == process
  167. )
  168. def test_execute_dump_command_runs_mysqldump_with_hostname_and_port():
  169. process = flexmock()
  170. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  171. flexmock(module.os.path).should_receive('exists').and_return(False)
  172. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  173. flexmock(module).should_receive('execute_command').with_args(
  174. (
  175. 'mysqldump',
  176. '--add-drop-database',
  177. '--host',
  178. 'database.example.org',
  179. '--port',
  180. '5433',
  181. '--protocol',
  182. 'tcp',
  183. '--databases',
  184. 'foo',
  185. '--result-file',
  186. 'dump',
  187. ),
  188. extra_environment=None,
  189. run_to_completion=False,
  190. ).and_return(process).once()
  191. assert (
  192. module.execute_dump_command(
  193. database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},
  194. log_prefix='log',
  195. dump_path=flexmock(),
  196. database_names=('foo',),
  197. extra_environment=None,
  198. dry_run=False,
  199. dry_run_label='',
  200. )
  201. == process
  202. )
  203. def test_execute_dump_command_runs_mysqldump_with_username_and_password():
  204. process = flexmock()
  205. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  206. flexmock(module.os.path).should_receive('exists').and_return(False)
  207. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  208. flexmock(module).should_receive('execute_command').with_args(
  209. (
  210. 'mysqldump',
  211. '--add-drop-database',
  212. '--user',
  213. 'root',
  214. '--databases',
  215. 'foo',
  216. '--result-file',
  217. 'dump',
  218. ),
  219. extra_environment={'MYSQL_PWD': 'trustsome1'},
  220. run_to_completion=False,
  221. ).and_return(process).once()
  222. assert (
  223. module.execute_dump_command(
  224. database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},
  225. log_prefix='log',
  226. dump_path=flexmock(),
  227. database_names=('foo',),
  228. extra_environment={'MYSQL_PWD': 'trustsome1'},
  229. dry_run=False,
  230. dry_run_label='',
  231. )
  232. == process
  233. )
  234. def test_execute_dump_command_runs_mysqldump_with_options():
  235. process = flexmock()
  236. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  237. flexmock(module.os.path).should_receive('exists').and_return(False)
  238. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  239. flexmock(module).should_receive('execute_command').with_args(
  240. (
  241. 'mysqldump',
  242. '--stuff=such',
  243. '--add-drop-database',
  244. '--databases',
  245. 'foo',
  246. '--result-file',
  247. 'dump',
  248. ),
  249. extra_environment=None,
  250. run_to_completion=False,
  251. ).and_return(process).once()
  252. assert (
  253. module.execute_dump_command(
  254. database={'name': 'foo', 'options': '--stuff=such'},
  255. log_prefix='log',
  256. dump_path=flexmock(),
  257. database_names=('foo',),
  258. extra_environment=None,
  259. dry_run=False,
  260. dry_run_label='',
  261. )
  262. == process
  263. )
  264. def test_execute_dump_command_runs_non_default_mysqldump():
  265. process = flexmock()
  266. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  267. flexmock(module.os.path).should_receive('exists').and_return(False)
  268. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  269. flexmock(module).should_receive('execute_command').with_args(
  270. (
  271. 'custom_mysqldump', # Custom MySQL dump command
  272. '--add-drop-database',
  273. '--databases',
  274. 'foo',
  275. '--result-file',
  276. 'dump',
  277. ),
  278. extra_environment=None,
  279. run_to_completion=False,
  280. ).and_return(process).once()
  281. assert (
  282. module.execute_dump_command(
  283. database={
  284. 'name': 'foo',
  285. 'mysql_dump_command': 'custom_mysqldump',
  286. }, # Custom MySQL dump command specified
  287. log_prefix='log',
  288. dump_path=flexmock(),
  289. database_names=('foo',),
  290. extra_environment=None,
  291. dry_run=False,
  292. dry_run_label='',
  293. )
  294. == process
  295. )
  296. def test_execute_dump_command_with_duplicate_dump_skips_mysqldump():
  297. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  298. flexmock(module.os.path).should_receive('exists').and_return(True)
  299. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  300. flexmock(module).should_receive('execute_command').never()
  301. assert (
  302. module.execute_dump_command(
  303. database={'name': 'foo'},
  304. log_prefix='log',
  305. dump_path=flexmock(),
  306. database_names=('foo',),
  307. extra_environment=None,
  308. dry_run=True,
  309. dry_run_label='SO DRY',
  310. )
  311. is None
  312. )
  313. def test_execute_dump_command_with_dry_run_skips_mysqldump():
  314. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
  315. flexmock(module.os.path).should_receive('exists').and_return(False)
  316. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  317. flexmock(module).should_receive('execute_command').never()
  318. assert (
  319. module.execute_dump_command(
  320. database={'name': 'foo'},
  321. log_prefix='log',
  322. dump_path=flexmock(),
  323. database_names=('foo',),
  324. extra_environment=None,
  325. dry_run=True,
  326. dry_run_label='SO DRY',
  327. )
  328. is None
  329. )
  330. def test_dump_data_sources_errors_for_missing_all_databases():
  331. databases = [{'name': 'all'}]
  332. flexmock(module).should_receive('make_dump_path').and_return('')
  333. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  334. 'databases/localhost/all'
  335. )
  336. flexmock(module).should_receive('database_names_to_dump').and_return(())
  337. with pytest.raises(ValueError):
  338. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False)
  339. def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
  340. databases = [{'name': 'all'}]
  341. flexmock(module).should_receive('make_dump_path').and_return('')
  342. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  343. 'databases/localhost/all'
  344. )
  345. flexmock(module).should_receive('database_names_to_dump').and_return(())
  346. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=True) == []
  347. def test_restore_data_source_dump_runs_mysql_to_restore():
  348. hook_config = [{'name': 'foo'}, {'name': 'bar'}]
  349. extract_process = flexmock(stdout=flexmock())
  350. flexmock(module).should_receive('execute_command_with_processes').with_args(
  351. ('mysql', '--batch'),
  352. processes=[extract_process],
  353. output_log_level=logging.DEBUG,
  354. input_file=extract_process.stdout,
  355. extra_environment=None,
  356. ).once()
  357. module.restore_data_source_dump(
  358. hook_config,
  359. {},
  360. 'test.yaml',
  361. data_source={'name': 'foo'},
  362. dry_run=False,
  363. extract_process=extract_process,
  364. connection_params={
  365. 'hostname': None,
  366. 'port': None,
  367. 'username': None,
  368. 'password': None,
  369. },
  370. )
  371. def test_restore_data_source_dump_runs_mysql_with_options():
  372. hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
  373. extract_process = flexmock(stdout=flexmock())
  374. flexmock(module).should_receive('execute_command_with_processes').with_args(
  375. ('mysql', '--batch', '--harder'),
  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=hook_config[0],
  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_non_default_mysql_with_options():
  396. hook_config = [{'name': 'foo', 'mysql_command': 'custom_mysql', 'restore_options': '--harder'}]
  397. extract_process = flexmock(stdout=flexmock())
  398. flexmock(module).should_receive('execute_command_with_processes').with_args(
  399. ('custom_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_mysql_with_hostname_and_port():
  420. hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  421. extract_process = flexmock(stdout=flexmock())
  422. flexmock(module).should_receive('execute_command_with_processes').with_args(
  423. (
  424. 'mysql',
  425. '--batch',
  426. '--host',
  427. 'database.example.org',
  428. '--port',
  429. '5433',
  430. '--protocol',
  431. 'tcp',
  432. ),
  433. processes=[extract_process],
  434. output_log_level=logging.DEBUG,
  435. input_file=extract_process.stdout,
  436. extra_environment=None,
  437. ).once()
  438. module.restore_data_source_dump(
  439. hook_config,
  440. {},
  441. 'test.yaml',
  442. data_source=hook_config[0],
  443. dry_run=False,
  444. extract_process=extract_process,
  445. connection_params={
  446. 'hostname': None,
  447. 'port': None,
  448. 'username': None,
  449. 'password': None,
  450. },
  451. )
  452. def test_restore_data_source_dump_runs_mysql_with_username_and_password():
  453. hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  454. extract_process = flexmock(stdout=flexmock())
  455. flexmock(module).should_receive('execute_command_with_processes').with_args(
  456. ('mysql', '--batch', '--user', 'root'),
  457. processes=[extract_process],
  458. output_log_level=logging.DEBUG,
  459. input_file=extract_process.stdout,
  460. extra_environment={'MYSQL_PWD': 'trustsome1'},
  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_with_connection_params_uses_connection_params_for_restore():
  477. hook_config = [
  478. {
  479. 'name': 'foo',
  480. 'username': 'root',
  481. 'password': 'trustsome1',
  482. 'restore_hostname': 'restorehost',
  483. 'restore_port': 'restoreport',
  484. 'restore_username': 'restoreusername',
  485. 'restore_password': 'restorepassword',
  486. }
  487. ]
  488. extract_process = flexmock(stdout=flexmock())
  489. flexmock(module).should_receive('execute_command_with_processes').with_args(
  490. (
  491. 'mysql',
  492. '--batch',
  493. '--host',
  494. 'clihost',
  495. '--port',
  496. 'cliport',
  497. '--protocol',
  498. 'tcp',
  499. '--user',
  500. 'cliusername',
  501. ),
  502. processes=[extract_process],
  503. output_log_level=logging.DEBUG,
  504. input_file=extract_process.stdout,
  505. extra_environment={'MYSQL_PWD': 'clipassword'},
  506. ).once()
  507. module.restore_data_source_dump(
  508. hook_config,
  509. {},
  510. 'test.yaml',
  511. data_source={'name': 'foo'},
  512. dry_run=False,
  513. extract_process=extract_process,
  514. connection_params={
  515. 'hostname': 'clihost',
  516. 'port': 'cliport',
  517. 'username': 'cliusername',
  518. 'password': 'clipassword',
  519. },
  520. )
  521. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  522. hook_config = [
  523. {
  524. 'name': 'foo',
  525. 'username': 'root',
  526. 'password': 'trustsome1',
  527. 'hostname': 'dbhost',
  528. 'port': 'dbport',
  529. 'restore_username': 'restoreuser',
  530. 'restore_password': 'restorepass',
  531. 'restore_hostname': 'restorehost',
  532. 'restore_port': 'restoreport',
  533. }
  534. ]
  535. extract_process = flexmock(stdout=flexmock())
  536. flexmock(module).should_receive('execute_command_with_processes').with_args(
  537. (
  538. 'mysql',
  539. '--batch',
  540. '--host',
  541. 'restorehost',
  542. '--port',
  543. 'restoreport',
  544. '--protocol',
  545. 'tcp',
  546. '--user',
  547. 'restoreuser',
  548. ),
  549. processes=[extract_process],
  550. output_log_level=logging.DEBUG,
  551. input_file=extract_process.stdout,
  552. extra_environment={'MYSQL_PWD': 'restorepass'},
  553. ).once()
  554. module.restore_data_source_dump(
  555. hook_config,
  556. {},
  557. 'test.yaml',
  558. data_source=hook_config[0],
  559. dry_run=False,
  560. extract_process=extract_process,
  561. connection_params={
  562. 'hostname': None,
  563. 'port': None,
  564. 'username': None,
  565. 'password': None,
  566. },
  567. )
  568. def test_restore_data_source_dump_with_dry_run_skips_restore():
  569. hook_config = [{'name': 'foo'}]
  570. flexmock(module).should_receive('execute_command_with_processes').never()
  571. module.restore_data_source_dump(
  572. hook_config,
  573. {},
  574. 'test.yaml',
  575. data_source={'name': 'foo'},
  576. dry_run=True,
  577. extract_process=flexmock(),
  578. connection_params={
  579. 'hostname': None,
  580. 'port': None,
  581. 'username': None,
  582. 'password': None,
  583. },
  584. )