test_mysql.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.hooks import mysql as module
  4. def test_database_names_to_dump_passes_through_name():
  5. extra_environment = flexmock()
  6. log_prefix = ''
  7. dry_run_label = ''
  8. names = module.database_names_to_dump(
  9. {'name': 'foo'}, extra_environment, log_prefix, dry_run_label
  10. )
  11. assert names == ('foo',)
  12. def test_database_names_to_dump_queries_mysql_for_database_names():
  13. extra_environment = flexmock()
  14. log_prefix = ''
  15. dry_run_label = ''
  16. flexmock(module).should_receive('execute_command').with_args(
  17. ('mysql', '--skip-column-names', '--batch', '--execute', 'show schemas'),
  18. output_log_level=None,
  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. '--add-drop-database',
  130. '--stuff=such',
  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_restore_database_dump_runs_mysql_to_restore():
  166. database_config = [{'name': 'foo'}]
  167. extract_process = flexmock(stdout=flexmock())
  168. flexmock(module).should_receive('execute_command_with_processes').with_args(
  169. ('mysql', '--batch'),
  170. processes=[extract_process],
  171. input_file=extract_process.stdout,
  172. extra_environment=None,
  173. ).once()
  174. module.restore_database_dump(
  175. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  176. )
  177. def test_restore_database_dump_errors_on_multiple_database_config():
  178. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  179. flexmock(module).should_receive('execute_command_with_processes').never()
  180. flexmock(module).should_receive('execute_command').never()
  181. with pytest.raises(ValueError):
  182. module.restore_database_dump(
  183. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  184. )
  185. def test_restore_database_dump_runs_mysql_with_hostname_and_port():
  186. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  187. extract_process = flexmock(stdout=flexmock())
  188. flexmock(module).should_receive('execute_command_with_processes').with_args(
  189. (
  190. 'mysql',
  191. '--batch',
  192. '--host',
  193. 'database.example.org',
  194. '--port',
  195. '5433',
  196. '--protocol',
  197. 'tcp',
  198. ),
  199. processes=[extract_process],
  200. input_file=extract_process.stdout,
  201. extra_environment=None,
  202. ).once()
  203. module.restore_database_dump(
  204. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  205. )
  206. def test_restore_database_dump_runs_mysql_with_username_and_password():
  207. database_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
  208. extract_process = flexmock(stdout=flexmock())
  209. flexmock(module).should_receive('execute_command_with_processes').with_args(
  210. ('mysql', '--batch', '--user', 'root'),
  211. processes=[extract_process],
  212. input_file=extract_process.stdout,
  213. extra_environment={'MYSQL_PWD': 'trustsome1'},
  214. ).once()
  215. module.restore_database_dump(
  216. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  217. )