test_mysql.py 15 KB

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