Merge pull request #2165 from twstrike/config_test_in_nginx_prepare

enable config_test in configurator prepare
This commit is contained in:
Peter Eckersley
2016-01-18 11:52:05 -08:00
4 changed files with 42 additions and 46 deletions
@@ -5,7 +5,6 @@ import re
import shutil import shutil
import socket import socket
import subprocess import subprocess
import sys
import time import time
import OpenSSL import OpenSSL
@@ -106,11 +105,18 @@ class NginxConfigurator(common.Plugin):
# This is called in determine_authenticator and determine_installer # This is called in determine_authenticator and determine_installer
def prepare(self): def prepare(self):
"""Prepare the authenticator/installer.""" """Prepare the authenticator/installer.
:raises .errors.NoInstallationError: If Nginx ctl cannot be found
:raises .errors.MisconfigurationError: If Nginx is misconfigured
"""
# Verify Nginx is installed # Verify Nginx is installed
if not le_util.exe_exists(self.conf('ctl')): if not le_util.exe_exists(self.conf('ctl')):
raise errors.NoInstallationError raise errors.NoInstallationError
# Make sure configuration is valid
self.config_test()
self.parser = parser.NginxParser( self.parser = parser.NginxParser(
self.conf('server-root'), self.mod_ssl_conf) self.conf('server-root'), self.mod_ssl_conf)
@@ -409,26 +415,13 @@ class NginxConfigurator(common.Plugin):
def config_test(self): # pylint: disable=no-self-use def config_test(self): # pylint: disable=no-self-use
"""Check the configuration of Nginx for errors. """Check the configuration of Nginx for errors.
:returns: Success :raises .errors.MisconfigurationError: If config_test fails
:rtype: bool
""" """
try: try:
proc = subprocess.Popen( le_util.run_script([self.conf('ctl'), "-c", self.nginx_conf, "-t"])
[self.conf('ctl'), "-c", self.nginx_conf, "-t"], except errors.SubprocessError as err:
stdout=subprocess.PIPE, raise errors.MisconfigurationError(str(err))
stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
except (OSError, ValueError):
logger.fatal("Unable to run nginx config test")
sys.exit(1)
if proc.returncode != 0:
# Enter recovery routine...
logger.error("Config test failed\n%s\n%s", stdout, stderr)
return False
return True
def _verify_setup(self): def _verify_setup(self):
"""Verify the setup to ensure safe operating environment. """Verify the setup to ensure safe operating environment.
@@ -54,6 +54,7 @@ class NginxConfiguratorTest(util.NginxTest):
mock_exe_exists.return_value = True mock_exe_exists.return_value = True
self.config.version = None self.config.version = None
self.config.config_test = mock.Mock()
self.config.prepare() self.config.prepare()
self.assertEquals((1, 6, 2), self.config.version) self.assertEquals((1, 6, 2), self.config.version)
@@ -361,12 +362,14 @@ class NginxConfiguratorTest(util.NginxTest):
mock_popen.side_effect = OSError("Can't find program") mock_popen.side_effect = OSError("Can't find program")
self.assertRaises(errors.MisconfigurationError, self.config.restart) self.assertRaises(errors.MisconfigurationError, self.config.restart)
@mock.patch("letsencrypt_nginx.configurator.subprocess.Popen") @mock.patch("letsencrypt.le_util.run_script")
def test_config_test(self, mock_popen): def test_config_test(self, _):
mocked = mock_popen() self.config.config_test()
mocked.communicate.return_value = ('', '')
mocked.returncode = 0 @mock.patch("letsencrypt.le_util.run_script")
self.assertTrue(self.config.config_test()) def test_config_test_bad_process(self, mock_run_script):
mock_run_script.side_effect = errors.SubprocessError
self.assertRaises(errors.MisconfigurationError, self.config.config_test)
def test_get_snakeoil_paths(self): def test_get_snakeoil_paths(self):
# pylint: disable=protected-access # pylint: disable=protected-access
@@ -49,10 +49,11 @@ def get_nginx_configurator(
backups = os.path.join(work_dir, "backups") backups = os.path.join(work_dir, "backups")
with mock.patch("letsencrypt_nginx.configurator.NginxConfigurator."
"config_test"):
with mock.patch("letsencrypt_nginx.configurator.le_util." with mock.patch("letsencrypt_nginx.configurator.le_util."
"exe_exists") as mock_exe_exists: "exe_exists") as mock_exe_exists:
mock_exe_exists.return_value = True mock_exe_exists.return_value = True
config = configurator.NginxConfigurator( config = configurator.NginxConfigurator(
config=mock.MagicMock( config=mock.MagicMock(
nginx_server_root=config_path, nginx_server_root=config_path,
+1 -2
View File
@@ -202,8 +202,7 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
# (we can only do that if letsencrypt-nginx is actually present) # (we can only do that if letsencrypt-nginx is actually present)
ret, _, _, _ = self._call(args) ret, _, _, _ = self._call(args)
self.assertTrue("The nginx plugin is not working" in ret) self.assertTrue("The nginx plugin is not working" in ret)
self.assertTrue("Could not find configuration root" in ret) self.assertTrue("MisconfigurationError" in ret)
self.assertTrue("NoInstallationError" in ret)
args = ["certonly", "--webroot"] args = ["certonly", "--webroot"]
ret, _, _, _ = self._call(args) ret, _, _, _ = self._call(args)