Upgrade pylint (#8855)

This is part of https://github.com/certbot/certbot/issues/8782. I took it on now because the currently pinned version of `pylint` doesn't work with newer versions of `poetry` which I wanted to upgrade as part of https://github.com/certbot/certbot/issues/8787.

To say a bit more about the specific changes in this PR:

* Newer versions of `pylint` complain if `Popen` isn't used as a context manager. Instead of making this change, I switched to using `subprocess.run` which is simpler and [recommended in the Python docs](https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module). I also disabled this check in a few places where no longer using `Popen` would require significant refactoring.
* The deleted code in `certbot/certbot/_internal/renewal.py` is cruft since https://github.com/certbot/certbot/pull/8685.
* The unused argument to `enable_mod` in the Apache plugin is used in some over the override classes that subclass that class.

* unpin pylint and repin dependencies

* disable raise-missing-from

* disable wrong-input-order

* remove unused code

* misc lint fixes

* remove unused import

* various lint fixes
This commit is contained in:
Brad Warren
2021-05-24 10:02:55 -07:00
committed by GitHub
parent 2df279bc5b
commit 315ddb247f
26 changed files with 223 additions and 225 deletions
@@ -985,13 +985,14 @@ class NginxConfigurator(common.Installer):
Unable to run Nginx version command
"""
try:
proc = subprocess.Popen(
proc = subprocess.run(
[self.conf('ctl'), "-c", self.nginx_conf, "-V"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=False,
env=util.env_no_snap_for_external_calls())
text = proc.communicate()[1] # nginx prints output to stderr
text = proc.stderr # nginx prints output to stderr
except (OSError, ValueError) as error:
logger.debug(str(error), exc_info=True)
raise errors.PluginError(
@@ -1224,10 +1225,9 @@ def nginx_restart(nginx_ctl, nginx_conf, sleep_duration):
try:
reload_output: Text = u""
with tempfile.TemporaryFile() as out:
proc = subprocess.Popen([nginx_ctl, "-c", nginx_conf, "-s", "reload"],
env=util.env_no_snap_for_external_calls(),
stdout=out, stderr=out)
proc.communicate()
proc = subprocess.run([nginx_ctl, "-c", nginx_conf, "-s", "reload"],
env=util.env_no_snap_for_external_calls(),
stdout=out, stderr=out, check=False)
out.seek(0)
reload_output = out.read().decode("utf-8")
@@ -1237,9 +1237,8 @@ def nginx_restart(nginx_ctl, nginx_conf, sleep_duration):
# Write to temporary files instead of piping because of communication issues on Arch
# https://github.com/certbot/certbot/issues/4324
with tempfile.TemporaryFile() as out:
nginx_proc = subprocess.Popen([nginx_ctl, "-c", nginx_conf],
stdout=out, stderr=out, env=util.env_no_snap_for_external_calls())
nginx_proc.communicate()
nginx_proc = subprocess.run([nginx_ctl, "-c", nginx_conf],
stdout=out, stderr=out, env=util.env_no_snap_for_external_calls(), check=False)
if nginx_proc.returncode != 0:
out.seek(0)
# Enter recovery routine...
+68 -59
View File
@@ -42,15 +42,16 @@ class NginxConfiguratorTest(util.NginxTest):
self.assertEqual(13, len(self.config.parser.parsed))
@mock.patch("certbot_nginx._internal.configurator.util.exe_exists")
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
def test_prepare_initializes_version(self, mock_popen, mock_exe_exists):
mock_popen().communicate.return_value = (
"", "\n".join(["nginx version: nginx/1.6.2",
@mock.patch("certbot_nginx._internal.configurator.subprocess.run")
def test_prepare_initializes_version(self, mock_run, mock_exe_exists):
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = "\n".join(
["nginx version: nginx/1.6.2",
"built by clang 6.0 (clang-600.0.56)"
" (based on LLVM 3.5svn)",
"TLS SNI support enabled",
"configure arguments: --prefix=/usr/local/Cellar/"
"nginx/1.6.2 --with-http_ssl_module"]))
"nginx/1.6.2 --with-http_ssl_module"])
mock_exe_exists.return_value = True
@@ -347,141 +348,149 @@ class NginxConfiguratorTest(util.NginxTest):
self.assertEqual(mock_revert.call_count, 1)
self.assertEqual(mock_restart.call_count, 2)
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
def test_get_version(self, mock_popen):
mock_popen().communicate.return_value = (
"", "\n".join(["nginx version: nginx/1.4.2",
@mock.patch("certbot_nginx._internal.configurator.subprocess.run")
def test_get_version(self, mock_run):
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = "\n".join(
["nginx version: nginx/1.4.2",
"built by clang 6.0 (clang-600.0.56)"
" (based on LLVM 3.5svn)",
"TLS SNI support enabled",
"configure arguments: --prefix=/usr/local/Cellar/"
"nginx/1.6.2 --with-http_ssl_module"]))
"nginx/1.6.2 --with-http_ssl_module"])
self.assertEqual(self.config.get_version(), (1, 4, 2))
mock_popen().communicate.return_value = (
"", "\n".join(["nginx version: nginx/0.9",
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = "\n".join(
["nginx version: nginx/0.9",
"built by clang 6.0 (clang-600.0.56)"
" (based on LLVM 3.5svn)",
"TLS SNI support enabled",
"configure arguments: --with-http_ssl_module"]))
"configure arguments: --with-http_ssl_module"])
self.assertEqual(self.config.get_version(), (0, 9))
mock_popen().communicate.return_value = (
"", "\n".join(["blah 0.0.1",
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = "\n".join(
["blah 0.0.1",
"built by clang 6.0 (clang-600.0.56)"
" (based on LLVM 3.5svn)",
"TLS SNI support enabled",
"configure arguments: --with-http_ssl_module"]))
"configure arguments: --with-http_ssl_module"])
self.assertRaises(errors.PluginError, self.config.get_version)
mock_popen().communicate.return_value = (
"", "\n".join(["nginx version: nginx/1.4.2",
"TLS SNI support enabled"]))
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = "\n".join(
["nginx version: nginx/1.4.2",
"TLS SNI support enabled"])
self.assertRaises(errors.PluginError, self.config.get_version)
mock_popen().communicate.return_value = (
"", "\n".join(["nginx version: nginx/1.4.2",
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = "\n".join(
["nginx version: nginx/1.4.2",
"built by clang 6.0 (clang-600.0.56)"
" (based on LLVM 3.5svn)",
"configure arguments: --with-http_ssl_module"]))
"configure arguments: --with-http_ssl_module"])
self.assertRaises(errors.PluginError, self.config.get_version)
mock_popen().communicate.return_value = (
"", "\n".join(["nginx version: nginx/0.8.1",
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = "\n".join(
["nginx version: nginx/0.8.1",
"built by clang 6.0 (clang-600.0.56)"
" (based on LLVM 3.5svn)",
"TLS SNI support enabled",
"configure arguments: --with-http_ssl_module"]))
"configure arguments: --with-http_ssl_module"])
self.assertRaises(errors.NotSupportedError, self.config.get_version)
mock_popen.side_effect = OSError("Can't find program")
mock_run.side_effect = OSError("Can't find program")
self.assertRaises(errors.PluginError, self.config.get_version)
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
def test_get_openssl_version(self, mock_popen):
@mock.patch("certbot_nginx._internal.configurator.subprocess.run")
def test_get_openssl_version(self, mock_run):
# pylint: disable=protected-access
mock_popen().communicate.return_value = (
"", """
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = """
nginx version: nginx/1.15.5
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
built with OpenSSL 1.0.2g 1 Mar 2016
TLS SNI support enabled
configure arguments:
""")
"""
self.assertEqual(self.config._get_openssl_version(), "1.0.2g")
mock_popen().communicate.return_value = (
"", """
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = """
nginx version: nginx/1.15.5
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
built with OpenSSL 1.0.2-beta1 1 Mar 2016
TLS SNI support enabled
configure arguments:
""")
"""
self.assertEqual(self.config._get_openssl_version(), "1.0.2-beta1")
mock_popen().communicate.return_value = (
"", """
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = """
nginx version: nginx/1.15.5
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
built with OpenSSL 1.0.2 1 Mar 2016
TLS SNI support enabled
configure arguments:
""")
"""
self.assertEqual(self.config._get_openssl_version(), "1.0.2")
mock_popen().communicate.return_value = (
"", """
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = """
nginx version: nginx/1.15.5
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
built with OpenSSL 1.0.2g 1 Mar 2016 (running with OpenSSL 1.0.2a 1 Mar 2016)
TLS SNI support enabled
configure arguments:
""")
"""
self.assertEqual(self.config._get_openssl_version(), "1.0.2a")
mock_popen().communicate.return_value = (
"", """
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = """
nginx version: nginx/1.15.5
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
built with LibreSSL 2.2.2
TLS SNI support enabled
configure arguments:
""")
"""
self.assertEqual(self.config._get_openssl_version(), "")
mock_popen().communicate.return_value = (
"", """
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = """
nginx version: nginx/1.15.5
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
TLS SNI support enabled
configure arguments:
""")
"""
self.assertEqual(self.config._get_openssl_version(), "")
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
@mock.patch("certbot_nginx._internal.configurator.subprocess.run")
@mock.patch("certbot_nginx._internal.configurator.time")
def test_nginx_restart(self, mock_time, mock_popen):
mocked = mock_popen()
mocked.communicate.return_value = ('', '')
def test_nginx_restart(self, mock_time, mock_run):
mocked = mock_run.return_value
mocked.stdout = ''
mocked.stderr = ''
mocked.returncode = 0
self.config.restart()
self.assertEqual(mocked.communicate.call_count, 1)
self.assertEqual(mock_run.call_count, 1)
mock_time.sleep.assert_called_once_with(0.1234)
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
@mock.patch("certbot_nginx._internal.configurator.subprocess.run")
@mock.patch("certbot_nginx._internal.configurator.logger.debug")
def test_nginx_restart_fail(self, mock_log_debug, mock_popen):
mocked = mock_popen()
mocked.communicate.return_value = ('', '')
def test_nginx_restart_fail(self, mock_log_debug, mock_run):
mocked = mock_run.return_value
mocked.stdout = ''
mocked.stderr = ''
mocked.returncode = 1
self.assertRaises(errors.MisconfigurationError, self.config.restart)
self.assertEqual(mocked.communicate.call_count, 2)
self.assertEqual(mock_run.call_count, 2)
mock_log_debug.assert_called_once_with("nginx reload failed:\n%s", "")
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
def test_no_nginx_start(self, mock_popen):
mock_popen.side_effect = OSError("Can't find program")
@mock.patch("certbot_nginx._internal.configurator.subprocess.run")
def test_no_nginx_start(self, mock_run):
mock_run.side_effect = OSError("Can't find program")
self.assertRaises(errors.MisconfigurationError, self.config.restart)
@mock.patch("certbot.util.run_script")