2014-05-21 13:05:41 +00:00
|
|
|
from django.db.backends.mysql.client import DatabaseClient
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class MySqlDbshellCommandTestCase(SimpleTestCase):
|
2020-10-04 22:25:29 +00:00
|
|
|
def settings_to_cmd_args_env(self, settings_dict, parameters=None):
|
|
|
|
if parameters is None:
|
|
|
|
parameters = []
|
|
|
|
return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters)
|
2014-05-21 13:05:41 +00:00
|
|
|
|
|
|
|
def test_fails_with_keyerror_on_incomplete_config(self):
|
|
|
|
with self.assertRaises(KeyError):
|
2020-10-04 22:25:29 +00:00
|
|
|
self.settings_to_cmd_args_env({})
|
2014-05-21 13:05:41 +00:00
|
|
|
|
|
|
|
def test_basic_params_specified_in_settings(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
|
|
|
'mysql',
|
|
|
|
'--user=someuser',
|
|
|
|
'--password=somepassword',
|
|
|
|
'--host=somehost',
|
|
|
|
'--port=444',
|
|
|
|
'somedbname',
|
|
|
|
]
|
|
|
|
expected_env = None
|
2014-05-21 13:05:41 +00:00
|
|
|
self.assertEqual(
|
2020-10-04 22:25:29 +00:00
|
|
|
self.settings_to_cmd_args_env({
|
2014-05-21 13:05:41 +00:00
|
|
|
'NAME': 'somedbname',
|
|
|
|
'USER': 'someuser',
|
|
|
|
'PASSWORD': 'somepassword',
|
|
|
|
'HOST': 'somehost',
|
|
|
|
'PORT': 444,
|
|
|
|
'OPTIONS': {},
|
2020-10-04 22:25:29 +00:00
|
|
|
}),
|
|
|
|
(expected_args, expected_env),
|
|
|
|
)
|
2014-05-21 13:05:41 +00:00
|
|
|
|
|
|
|
def test_options_override_settings_proper_values(self):
|
|
|
|
settings_port = 444
|
|
|
|
options_port = 555
|
|
|
|
self.assertNotEqual(settings_port, options_port, 'test pre-req')
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
|
|
|
'mysql',
|
|
|
|
'--user=optionuser',
|
|
|
|
'--password=optionpassword',
|
|
|
|
'--host=optionhost',
|
|
|
|
'--port=%s' % options_port,
|
|
|
|
'optiondbname',
|
|
|
|
]
|
|
|
|
expected_env = None
|
2014-05-21 13:05:41 +00:00
|
|
|
self.assertEqual(
|
2020-10-04 22:25:29 +00:00
|
|
|
self.settings_to_cmd_args_env({
|
2014-05-21 13:05:41 +00:00
|
|
|
'NAME': 'settingdbname',
|
|
|
|
'USER': 'settinguser',
|
|
|
|
'PASSWORD': 'settingpassword',
|
|
|
|
'HOST': 'settinghost',
|
|
|
|
'PORT': settings_port,
|
|
|
|
'OPTIONS': {
|
|
|
|
'db': 'optiondbname',
|
|
|
|
'user': 'optionuser',
|
|
|
|
'passwd': 'optionpassword',
|
|
|
|
'host': 'optionhost',
|
|
|
|
'port': options_port,
|
|
|
|
},
|
2020-10-04 22:25:29 +00:00
|
|
|
}),
|
|
|
|
(expected_args, expected_env),
|
|
|
|
)
|
2014-05-21 13:05:41 +00:00
|
|
|
|
2020-06-11 18:12:35 +00:00
|
|
|
def test_options_password(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
|
|
|
'mysql',
|
|
|
|
'--user=someuser',
|
|
|
|
'--password=optionpassword',
|
|
|
|
'--host=somehost',
|
|
|
|
'--port=444',
|
|
|
|
'somedbname',
|
|
|
|
]
|
|
|
|
expected_env = None
|
2020-06-11 18:12:35 +00:00
|
|
|
self.assertEqual(
|
2020-10-04 22:25:29 +00:00
|
|
|
self.settings_to_cmd_args_env({
|
2020-06-11 18:12:35 +00:00
|
|
|
'NAME': 'somedbname',
|
|
|
|
'USER': 'someuser',
|
|
|
|
'PASSWORD': 'settingpassword',
|
|
|
|
'HOST': 'somehost',
|
|
|
|
'PORT': 444,
|
|
|
|
'OPTIONS': {'password': 'optionpassword'},
|
|
|
|
}),
|
2020-10-04 22:25:29 +00:00
|
|
|
(expected_args, expected_env),
|
2020-06-11 18:12:35 +00:00
|
|
|
)
|
|
|
|
|
2020-10-14 18:07:57 +00:00
|
|
|
def test_options_charset(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
|
|
|
'mysql',
|
|
|
|
'--user=someuser',
|
|
|
|
'--password=somepassword',
|
|
|
|
'--host=somehost',
|
|
|
|
'--port=444',
|
|
|
|
'--default-character-set=utf8',
|
|
|
|
'somedbname',
|
|
|
|
]
|
|
|
|
expected_env = None
|
2020-10-14 18:07:57 +00:00
|
|
|
self.assertEqual(
|
2020-10-04 22:25:29 +00:00
|
|
|
self.settings_to_cmd_args_env({
|
2020-10-14 18:07:57 +00:00
|
|
|
'NAME': 'somedbname',
|
|
|
|
'USER': 'someuser',
|
|
|
|
'PASSWORD': 'somepassword',
|
|
|
|
'HOST': 'somehost',
|
|
|
|
'PORT': 444,
|
|
|
|
'OPTIONS': {'charset': 'utf8'},
|
|
|
|
}),
|
2020-10-04 22:25:29 +00:00
|
|
|
(expected_args, expected_env),
|
2020-10-14 18:07:57 +00:00
|
|
|
)
|
|
|
|
|
2014-05-21 13:05:41 +00:00
|
|
|
def test_can_connect_using_sockets(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
|
|
|
'mysql',
|
|
|
|
'--user=someuser',
|
|
|
|
'--password=somepassword',
|
|
|
|
'--socket=/path/to/mysql.socket.file',
|
|
|
|
'somedbname',
|
|
|
|
]
|
|
|
|
expected_env = None
|
2014-05-21 13:05:41 +00:00
|
|
|
self.assertEqual(
|
2020-10-04 22:25:29 +00:00
|
|
|
self.settings_to_cmd_args_env({
|
2014-05-21 13:05:41 +00:00
|
|
|
'NAME': 'somedbname',
|
|
|
|
'USER': 'someuser',
|
|
|
|
'PASSWORD': 'somepassword',
|
|
|
|
'HOST': '/path/to/mysql.socket.file',
|
|
|
|
'PORT': None,
|
|
|
|
'OPTIONS': {},
|
2020-10-04 22:25:29 +00:00
|
|
|
}),
|
|
|
|
(expected_args, expected_env),
|
|
|
|
)
|
2014-05-21 13:05:41 +00:00
|
|
|
|
|
|
|
def test_ssl_certificate_is_added(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
|
|
|
'mysql',
|
|
|
|
'--user=someuser',
|
|
|
|
'--password=somepassword',
|
|
|
|
'--host=somehost',
|
|
|
|
'--port=444',
|
|
|
|
'--ssl-ca=sslca',
|
|
|
|
'--ssl-cert=sslcert',
|
|
|
|
'--ssl-key=sslkey',
|
|
|
|
'somedbname',
|
|
|
|
]
|
|
|
|
expected_env = None
|
2014-05-21 13:05:41 +00:00
|
|
|
self.assertEqual(
|
2020-10-04 22:25:29 +00:00
|
|
|
self.settings_to_cmd_args_env({
|
2014-05-21 13:05:41 +00:00
|
|
|
'NAME': 'somedbname',
|
|
|
|
'USER': 'someuser',
|
|
|
|
'PASSWORD': 'somepassword',
|
|
|
|
'HOST': 'somehost',
|
|
|
|
'PORT': 444,
|
2017-06-19 22:11:25 +00:00
|
|
|
'OPTIONS': {
|
|
|
|
'ssl': {
|
|
|
|
'ca': 'sslca',
|
|
|
|
'cert': 'sslcert',
|
|
|
|
'key': 'sslkey',
|
|
|
|
},
|
|
|
|
},
|
2020-10-04 22:25:29 +00:00
|
|
|
}),
|
|
|
|
(expected_args, expected_env),
|
|
|
|
)
|
2014-05-21 13:05:41 +00:00
|
|
|
|
2020-04-14 07:56:40 +00:00
|
|
|
def test_parameters(self):
|
|
|
|
self.assertEqual(
|
2020-10-04 22:25:29 +00:00
|
|
|
self.settings_to_cmd_args_env(
|
2020-04-14 07:56:40 +00:00
|
|
|
{
|
|
|
|
'NAME': 'somedbname',
|
|
|
|
'USER': None,
|
|
|
|
'PASSWORD': None,
|
|
|
|
'HOST': None,
|
|
|
|
'PORT': None,
|
|
|
|
'OPTIONS': {},
|
|
|
|
},
|
|
|
|
['--help'],
|
|
|
|
),
|
2020-10-04 22:25:29 +00:00
|
|
|
(['mysql', 'somedbname', '--help'], None),
|
2020-04-14 07:56:40 +00:00
|
|
|
)
|