test_mysql.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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_databases_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_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  49. def test_dump_databases_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_databases([database], 'test.yaml', {}, dry_run=False) == [process]
  66. def test_dump_databases_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_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  81. def test_dump_databases_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_databases(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_database_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. ('mysqldump', '--add-drop-database', '--databases', 'foo', '--result-file', 'dump',),
  118. extra_environment=None,
  119. run_to_completion=False,
  120. ).and_return(process).once()
  121. assert (
  122. module.execute_dump_command(
  123. database={'name': 'foo'},
  124. log_prefix='log',
  125. dump_path=flexmock(),
  126. database_names=('foo',),
  127. extra_environment=None,
  128. dry_run=False,
  129. dry_run_label='',
  130. )
  131. == process
  132. )
  133. def test_execute_dump_command_runs_mysqldump_without_add_drop_database():
  134. process = flexmock()
  135. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('dump')
  136. flexmock(module.os.path).should_receive('exists').and_return(False)
  137. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  138. flexmock(module).should_receive('execute_command').with_args(
  139. ('mysqldump', '--databases', 'foo', '--result-file', 'dump',),
  140. extra_environment=None,
  141. run_to_completion=False,
  142. ).and_return(process).once()
  143. assert (
  144. module.execute_dump_command(
  145. database={'name': 'foo', 'add_drop_database': False},
  146. log_prefix='log',
  147. dump_path=flexmock(),
  148. database_names=('foo',),
  149. extra_environment=None,
  150. dry_run=False,
  151. dry_run_label='',
  152. )
  153. == process
  154. )
  155. def test_execute_dump_command_runs_mysqldump_with_hostname_and_port():
  156. process = flexmock()
  157. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('dump')
  158. flexmock(module.os.path).should_receive('exists').and_return(False)
  159. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  160. flexmock(module).should_receive('execute_command').with_args(
  161. (
  162. 'mysqldump',
  163. '--add-drop-database',
  164. '--host',
  165. 'database.example.org',
  166. '--port',
  167. '5433',
  168. '--protocol',
  169. 'tcp',
  170. '--databases',
  171. 'foo',
  172. '--result-file',
  173. 'dump',
  174. ),
  175. extra_environment=None,
  176. run_to_completion=False,
  177. ).and_return(process).once()
  178. assert (
  179. module.execute_dump_command(
  180. database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},
  181. log_prefix='log',
  182. dump_path=flexmock(),
  183. database_names=('foo',),
  184. extra_environment=None,
  185. dry_run=False,
  186. dry_run_label='',
  187. )
  188. == process
  189. )
  190. def test_execute_dump_command_runs_mysqldump_with_username_and_password():
  191. process = flexmock()
  192. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('dump')
  193. flexmock(module.os.path).should_receive('exists').and_return(False)
  194. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  195. flexmock(module).should_receive('execute_command').with_args(
  196. (
  197. 'mysqldump',
  198. '--add-drop-database',
  199. '--user',
  200. 'root',
  201. '--databases',
  202. 'foo',
  203. '--result-file',
  204. 'dump',
  205. ),
  206. extra_environment={'MYSQL_PWD': 'trustsome1'},
  207. run_to_completion=False,
  208. ).and_return(process).once()
  209. assert (
  210. module.execute_dump_command(
  211. database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},
  212. log_prefix='log',
  213. dump_path=flexmock(),
  214. database_names=('foo',),
  215. extra_environment={'MYSQL_PWD': 'trustsome1'},
  216. dry_run=False,
  217. dry_run_label='',
  218. )
  219. == process
  220. )
  221. def test_execute_dump_command_runs_mysqldump_with_options():
  222. process = flexmock()
  223. flexmock(module.dump).should_receive('make_database_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. '--stuff=such',
  230. '--add-drop-database',
  231. '--databases',
  232. 'foo',
  233. '--result-file',
  234. 'dump',
  235. ),
  236. extra_environment=None,
  237. run_to_completion=False,
  238. ).and_return(process).once()
  239. assert (
  240. module.execute_dump_command(
  241. database={'name': 'foo', 'options': '--stuff=such'},
  242. log_prefix='log',
  243. dump_path=flexmock(),
  244. database_names=('foo',),
  245. extra_environment=None,
  246. dry_run=False,
  247. dry_run_label='',
  248. )
  249. == process
  250. )
  251. def test_execute_dump_command_with_duplicate_dump_skips_mysqldump():
  252. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('dump')
  253. flexmock(module.os.path).should_receive('exists').and_return(True)
  254. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  255. flexmock(module).should_receive('execute_command').never()
  256. assert (
  257. module.execute_dump_command(
  258. database={'name': 'foo'},
  259. log_prefix='log',
  260. dump_path=flexmock(),
  261. database_names=('foo',),
  262. extra_environment=None,
  263. dry_run=True,
  264. dry_run_label='SO DRY',
  265. )
  266. is None
  267. )
  268. def test_execute_dump_command_with_dry_run_skips_mysqldump():
  269. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('dump')
  270. flexmock(module.os.path).should_receive('exists').and_return(False)
  271. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  272. flexmock(module).should_receive('execute_command').never()
  273. assert (
  274. module.execute_dump_command(
  275. database={'name': 'foo'},
  276. log_prefix='log',
  277. dump_path=flexmock(),
  278. database_names=('foo',),
  279. extra_environment=None,
  280. dry_run=True,
  281. dry_run_label='SO DRY',
  282. )
  283. is None
  284. )
  285. def test_dump_databases_errors_for_missing_all_databases():
  286. databases = [{'name': 'all'}]
  287. flexmock(module).should_receive('make_dump_path').and_return('')
  288. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  289. 'databases/localhost/all'
  290. )
  291. flexmock(module).should_receive('database_names_to_dump').and_return(())
  292. with pytest.raises(ValueError):
  293. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False)
  294. def test_dump_databases_does_not_error_for_missing_all_databases_with_dry_run():
  295. databases = [{'name': 'all'}]
  296. flexmock(module).should_receive('make_dump_path').and_return('')
  297. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  298. 'databases/localhost/all'
  299. )
  300. flexmock(module).should_receive('database_names_to_dump').and_return(())
  301. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
  302. def test_restore_database_dump_runs_mysql_to_restore():
  303. database_config = [{'name': 'foo'}]
  304. extract_process = flexmock(stdout=flexmock())
  305. flexmock(module).should_receive('execute_command_with_processes').with_args(
  306. ('mysql', '--batch'),
  307. processes=[extract_process],
  308. output_log_level=logging.DEBUG,
  309. input_file=extract_process.stdout,
  310. extra_environment=None,
  311. ).once()
  312. module.restore_database_dump(
  313. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  314. )
  315. def test_restore_database_dump_errors_on_multiple_database_config():
  316. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  317. flexmock(module).should_receive('execute_command_with_processes').never()
  318. flexmock(module).should_receive('execute_command').never()
  319. with pytest.raises(ValueError):
  320. module.restore_database_dump(
  321. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  322. )
  323. def test_restore_database_dump_runs_mysql_with_options():
  324. database_config = [{'name': 'foo', 'restore_options': '--harder'}]
  325. extract_process = flexmock(stdout=flexmock())
  326. flexmock(module).should_receive('execute_command_with_processes').with_args(
  327. ('mysql', '--batch', '--harder'),
  328. processes=[extract_process],
  329. output_log_level=logging.DEBUG,
  330. input_file=extract_process.stdout,
  331. extra_environment=None,
  332. ).once()
  333. module.restore_database_dump(
  334. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  335. )
  336. def test_restore_database_dump_runs_mysql_with_hostname_and_port():
  337. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  338. extract_process = flexmock(stdout=flexmock())
  339. flexmock(module).should_receive('execute_command_with_processes').with_args(
  340. (
  341. 'mysql',
  342. '--batch',
  343. '--host',
  344. 'database.example.org',
  345. '--port',
  346. '5433',
  347. '--protocol',
  348. 'tcp',
  349. ),
  350. processes=[extract_process],
  351. output_log_level=logging.DEBUG,
  352. input_file=extract_process.stdout,
  353. extra_environment=None,
  354. ).once()
  355. module.restore_database_dump(
  356. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  357. )
  358. def test_restore_database_dump_runs_mysql_with_username_and_password():
  359. database_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  360. extract_process = flexmock(stdout=flexmock())
  361. flexmock(module).should_receive('execute_command_with_processes').with_args(
  362. ('mysql', '--batch', '--user', 'root'),
  363. processes=[extract_process],
  364. output_log_level=logging.DEBUG,
  365. input_file=extract_process.stdout,
  366. extra_environment={'MYSQL_PWD': 'trustsome1'},
  367. ).once()
  368. module.restore_database_dump(
  369. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  370. )
  371. def test_restore_database_dump_with_dry_run_skips_restore():
  372. database_config = [{'name': 'foo'}]
  373. flexmock(module).should_receive('execute_command_with_processes').never()
  374. module.restore_database_dump(
  375. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  376. )