2015-06-17 21:37:09 +08:00
|
|
|
import os
|
2017-04-02 09:01:08 +08:00
|
|
|
import signal
|
2019-02-11 09:17:24 +08:00
|
|
|
import subprocess
|
2017-01-20 01:16:04 +08:00
|
|
|
from unittest import mock
|
2015-06-17 21:37:09 +08:00
|
|
|
|
2015-08-05 22:08:56 +08:00
|
|
|
from django.db.backends.postgresql.client import DatabaseClient
|
2017-01-20 01:16:04 +08:00
|
|
|
from django.test import SimpleTestCase
|
2015-06-17 21:37:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
|
|
|
|
|
|
|
|
def _run_it(self, dbinfo):
|
|
|
|
"""
|
|
|
|
That function invokes the runshell command, while mocking
|
2019-02-11 09:17:24 +08:00
|
|
|
subprocess.run(). It returns a 2-tuple with:
|
2015-06-17 21:37:09 +08:00
|
|
|
- The command line list
|
2019-02-11 09:17:24 +08:00
|
|
|
- The the value of the PGPASSWORD environment variable, or None.
|
2015-06-17 21:37:09 +08:00
|
|
|
"""
|
2019-02-11 09:17:24 +08:00
|
|
|
def _mock_subprocess_run(*args, env=os.environ, **kwargs):
|
2015-06-17 21:37:09 +08:00
|
|
|
self.subprocess_args = list(*args)
|
2019-02-11 09:17:24 +08:00
|
|
|
self.pgpassword = env.get('PGPASSWORD')
|
|
|
|
return subprocess.CompletedProcess(self.subprocess_args, 0)
|
|
|
|
with mock.patch('subprocess.run', new=_mock_subprocess_run):
|
2015-06-17 21:37:09 +08:00
|
|
|
DatabaseClient.runshell_db(dbinfo)
|
2019-02-11 09:17:24 +08:00
|
|
|
return self.subprocess_args, self.pgpassword
|
2015-06-17 21:37:09 +08:00
|
|
|
|
|
|
|
def test_basic(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_it({
|
2016-06-03 05:03:03 +08:00
|
|
|
'database': 'dbname',
|
|
|
|
'user': 'someuser',
|
|
|
|
'password': 'somepassword',
|
|
|
|
'host': 'somehost',
|
|
|
|
'port': '444',
|
2015-06-17 21:37:09 +08:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'],
|
2019-02-11 09:17:24 +08:00
|
|
|
'somepassword',
|
2015-06-17 21:37:09 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_nopass(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_it({
|
2016-06-03 05:03:03 +08:00
|
|
|
'database': 'dbname',
|
|
|
|
'user': 'someuser',
|
|
|
|
'host': 'somehost',
|
|
|
|
'port': '444',
|
2015-06-17 21:37:09 +08:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'someuser', '-h', 'somehost', '-p', '444', 'dbname'],
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_column(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_it({
|
2016-06-03 05:03:03 +08:00
|
|
|
'database': 'dbname',
|
|
|
|
'user': 'some:user',
|
|
|
|
'password': 'some:password',
|
|
|
|
'host': '::1',
|
|
|
|
'port': '444',
|
2015-06-17 21:37:09 +08:00
|
|
|
}), (
|
|
|
|
['psql', '-U', 'some:user', '-h', '::1', '-p', '444', 'dbname'],
|
2019-02-11 09:17:24 +08:00
|
|
|
'some:password',
|
2015-06-17 21:37:09 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_accent(self):
|
|
|
|
username = 'rôle'
|
|
|
|
password = 'sésame'
|
|
|
|
self.assertEqual(
|
|
|
|
self._run_it({
|
2016-06-03 05:03:03 +08:00
|
|
|
'database': 'dbname',
|
2017-01-12 06:17:25 +08:00
|
|
|
'user': username,
|
|
|
|
'password': password,
|
2016-06-03 05:03:03 +08:00
|
|
|
'host': 'somehost',
|
|
|
|
'port': '444',
|
2015-06-17 21:37:09 +08:00
|
|
|
}), (
|
2017-01-12 06:17:25 +08:00
|
|
|
['psql', '-U', username, '-h', 'somehost', '-p', '444', 'dbname'],
|
2019-02-11 09:17:24 +08:00
|
|
|
password,
|
2015-06-17 21:37:09 +08:00
|
|
|
)
|
|
|
|
)
|
2017-04-02 09:01:08 +08:00
|
|
|
|
|
|
|
def test_sigint_handler(self):
|
|
|
|
"""SIGINT is ignored in Python and passed to psql to abort quries."""
|
2019-02-11 09:17:24 +08:00
|
|
|
def _mock_subprocess_run(*args, **kwargs):
|
2017-04-02 09:01:08 +08:00
|
|
|
handler = signal.getsignal(signal.SIGINT)
|
|
|
|
self.assertEqual(handler, signal.SIG_IGN)
|
|
|
|
|
|
|
|
sigint_handler = signal.getsignal(signal.SIGINT)
|
|
|
|
# The default handler isn't SIG_IGN.
|
|
|
|
self.assertNotEqual(sigint_handler, signal.SIG_IGN)
|
2019-02-11 09:17:24 +08:00
|
|
|
with mock.patch('subprocess.run', new=_mock_subprocess_run):
|
2017-04-02 09:01:08 +08:00
|
|
|
DatabaseClient.runshell_db({})
|
2018-08-02 00:55:53 +08:00
|
|
|
# dbshell restores the original handler.
|
2017-04-02 09:01:08 +08:00
|
|
|
self.assertEqual(sigint_handler, signal.getsignal(signal.SIGINT))
|