test_mysql.py 11 KB

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