test_mysql.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. dry_run_label = ''
  9. names = module.database_names_to_dump(
  10. {'name': 'foo'}, extra_environment, log_prefix, dry_run_label
  11. )
  12. assert names == ('foo',)
  13. def test_database_names_to_dump_queries_mysql_for_database_names():
  14. extra_environment = flexmock()
  15. log_prefix = ''
  16. dry_run_label = ''
  17. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  18. ('mysql', '--skip-column-names', '--batch', '--execute', 'show schemas'),
  19. extra_environment=extra_environment,
  20. ).and_return('foo\nbar\nmysql\n').once()
  21. names = module.database_names_to_dump(
  22. {'name': 'all'}, extra_environment, log_prefix, dry_run_label
  23. )
  24. assert names == ('foo', 'bar')
  25. def test_dump_databases_runs_mysqldump_for_each_database():
  26. databases = [{'name': 'foo'}, {'name': 'bar'}]
  27. processes = [flexmock(), flexmock()]
  28. flexmock(module).should_receive('make_dump_path').and_return('')
  29. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  30. 'databases/localhost/foo'
  31. ).and_return('databases/localhost/bar')
  32. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  33. ('bar',)
  34. )
  35. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  36. for name, process in zip(('foo', 'bar'), processes):
  37. flexmock(module).should_receive('execute_command').with_args(
  38. (
  39. 'mysqldump',
  40. '--add-drop-database',
  41. '--databases',
  42. name,
  43. '>',
  44. 'databases/localhost/{}'.format(name),
  45. ),
  46. shell=True,
  47. extra_environment=None,
  48. run_to_completion=False,
  49. ).and_return(process).once()
  50. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  51. def test_dump_databases_with_dry_run_skips_mysqldump():
  52. databases = [{'name': 'foo'}, {'name': 'bar'}]
  53. flexmock(module).should_receive('make_dump_path').and_return('')
  54. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  55. 'databases/localhost/foo'
  56. ).and_return('databases/localhost/bar')
  57. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  58. ('bar',)
  59. )
  60. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  61. flexmock(module).should_receive('execute_command').never()
  62. module.dump_databases(databases, 'test.yaml', {}, dry_run=True)
  63. def test_dump_databases_runs_mysqldump_with_hostname_and_port():
  64. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  65. process = flexmock()
  66. flexmock(module).should_receive('make_dump_path').and_return('')
  67. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  68. 'databases/database.example.org/foo'
  69. )
  70. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  71. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  72. flexmock(module).should_receive('execute_command').with_args(
  73. (
  74. 'mysqldump',
  75. '--add-drop-database',
  76. '--host',
  77. 'database.example.org',
  78. '--port',
  79. '5433',
  80. '--protocol',
  81. 'tcp',
  82. '--databases',
  83. 'foo',
  84. '>',
  85. 'databases/database.example.org/foo',
  86. ),
  87. shell=True,
  88. extra_environment=None,
  89. run_to_completion=False,
  90. ).and_return(process).once()
  91. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  92. def test_dump_databases_runs_mysqldump_with_username_and_password():
  93. databases = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  94. process = flexmock()
  95. flexmock(module).should_receive('make_dump_path').and_return('')
  96. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  97. 'databases/localhost/foo'
  98. )
  99. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  100. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  101. flexmock(module).should_receive('execute_command').with_args(
  102. (
  103. 'mysqldump',
  104. '--add-drop-database',
  105. '--user',
  106. 'root',
  107. '--databases',
  108. 'foo',
  109. '>',
  110. 'databases/localhost/foo',
  111. ),
  112. shell=True,
  113. extra_environment={'MYSQL_PWD': 'trustsome1'},
  114. run_to_completion=False,
  115. ).and_return(process).once()
  116. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  117. def test_dump_databases_runs_mysqldump_with_options():
  118. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  119. process = flexmock()
  120. flexmock(module).should_receive('make_dump_path').and_return('')
  121. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  122. 'databases/localhost/foo'
  123. )
  124. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  125. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  126. flexmock(module).should_receive('execute_command').with_args(
  127. (
  128. 'mysqldump',
  129. '--stuff=such',
  130. '--add-drop-database',
  131. '--databases',
  132. 'foo',
  133. '>',
  134. 'databases/localhost/foo',
  135. ),
  136. shell=True,
  137. extra_environment=None,
  138. run_to_completion=False,
  139. ).and_return(process).once()
  140. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  141. def test_dump_databases_runs_mysqldump_for_all_databases():
  142. databases = [{'name': 'all'}]
  143. process = flexmock()
  144. flexmock(module).should_receive('make_dump_path').and_return('')
  145. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  146. 'databases/localhost/all'
  147. )
  148. flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
  149. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  150. flexmock(module).should_receive('execute_command').with_args(
  151. (
  152. 'mysqldump',
  153. '--add-drop-database',
  154. '--databases',
  155. 'foo',
  156. 'bar',
  157. '>',
  158. 'databases/localhost/all',
  159. ),
  160. shell=True,
  161. extra_environment=None,
  162. run_to_completion=False,
  163. ).and_return(process).once()
  164. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  165. def test_database_names_to_dump_runs_mysql_with_list_options():
  166. database = {'name': 'all', 'list_options': '--defaults-extra-file=my.cnf'}
  167. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  168. (
  169. 'mysql',
  170. '--defaults-extra-file=my.cnf',
  171. '--skip-column-names',
  172. '--batch',
  173. '--execute',
  174. 'show schemas',
  175. ),
  176. extra_environment=None,
  177. ).and_return(('foo\nbar')).once()
  178. assert module.database_names_to_dump(database, None, 'test.yaml', '') == ('foo', 'bar')
  179. def test_dump_databases_errors_for_missing_all_databases():
  180. databases = [{'name': 'all'}]
  181. process = flexmock()
  182. flexmock(module).should_receive('make_dump_path').and_return('')
  183. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  184. 'databases/localhost/all'
  185. )
  186. flexmock(module).should_receive('database_names_to_dump').and_return(())
  187. with pytest.raises(ValueError):
  188. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  189. def test_restore_database_dump_runs_mysql_to_restore():
  190. database_config = [{'name': 'foo'}]
  191. extract_process = flexmock(stdout=flexmock())
  192. flexmock(module).should_receive('execute_command_with_processes').with_args(
  193. ('mysql', '--batch'),
  194. processes=[extract_process],
  195. output_log_level=logging.DEBUG,
  196. input_file=extract_process.stdout,
  197. extra_environment=None,
  198. ).once()
  199. module.restore_database_dump(
  200. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  201. )
  202. def test_restore_database_dump_errors_on_multiple_database_config():
  203. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  204. flexmock(module).should_receive('execute_command_with_processes').never()
  205. flexmock(module).should_receive('execute_command').never()
  206. with pytest.raises(ValueError):
  207. module.restore_database_dump(
  208. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  209. )
  210. def test_restore_database_dump_runs_mysql_with_hostname_and_port():
  211. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  212. extract_process = flexmock(stdout=flexmock())
  213. flexmock(module).should_receive('execute_command_with_processes').with_args(
  214. (
  215. 'mysql',
  216. '--batch',
  217. '--host',
  218. 'database.example.org',
  219. '--port',
  220. '5433',
  221. '--protocol',
  222. 'tcp',
  223. ),
  224. processes=[extract_process],
  225. output_log_level=logging.DEBUG,
  226. input_file=extract_process.stdout,
  227. extra_environment=None,
  228. ).once()
  229. module.restore_database_dump(
  230. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  231. )
  232. def test_restore_database_dump_runs_mysql_with_username_and_password():
  233. database_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  234. extract_process = flexmock(stdout=flexmock())
  235. flexmock(module).should_receive('execute_command_with_processes').with_args(
  236. ('mysql', '--batch', '--user', 'root'),
  237. processes=[extract_process],
  238. output_log_level=logging.DEBUG,
  239. input_file=extract_process.stdout,
  240. extra_environment={'MYSQL_PWD': 'trustsome1'},
  241. ).once()
  242. module.restore_database_dump(
  243. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  244. )
  245. def test_restore_database_dump_with_dry_run_skips_restore():
  246. database_config = [{'name': 'foo'}]
  247. flexmock(module).should_receive('execute_command_with_processes').never()
  248. module.restore_database_dump(
  249. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  250. )