certbot/letsencrypt-apache/letsencrypt_apache/tests/util.py

94 lines
3.5 KiB
Python
Raw Normal View History

2015-05-10 06:47:58 -04:00
"""Common utilities for letsencrypt_apache."""
2014-12-19 18:49:29 -05:00
import os
import pkg_resources
2015-01-24 08:12:45 -05:00
import unittest
2014-12-19 18:49:29 -05:00
import mock
from letsencrypt.plugins import common
Merge remote-tracking branch 'github/letsencrypt/master' into test-dirs-chmods Conflicts: letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/foo.conf letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/nginx.conf letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/nginx.new.conf letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/server.conf letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/sites-enabled/default letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/sites-enabled/example.com letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/fastcgi_params letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-utf letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-win letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/mime.types letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi-ui.conf.1.4.1 letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi.rules letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi_core.rules letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/nginx.conf letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/proxy_params letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/scgi_params letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-available/default letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-enabled/default letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/uwsgi_params letsencrypt/client/plugins/nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/win-utf letsencrypt/client/plugins/nginx/tests/testdata/foo.conf letsencrypt/client/plugins/nginx/tests/testdata/nginx.conf letsencrypt/client/plugins/nginx/tests/testdata/nginx.new.conf letsencrypt/client/plugins/nginx/tests/testdata/server.conf letsencrypt/client/plugins/nginx/tests/testdata/sites-enabled/default letsencrypt/client/plugins/nginx/tests/testdata/sites-enabled/example.com letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/fastcgi_params letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-utf letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-win letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/mime.types letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi-ui.conf.1.4.1 letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi.rules letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi_core.rules letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/nginx.conf letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/proxy_params letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/scgi_params letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-available/default letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-enabled/default letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/uwsgi_params letsencrypt/client/plugins/nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/win-utf letsencrypt_apache/tests/util.py letsencrypt_nginx/tests/testdata/foo.conf letsencrypt_nginx/tests/testdata/nginx.conf letsencrypt_nginx/tests/testdata/nginx.new.conf letsencrypt_nginx/tests/testdata/server.conf letsencrypt_nginx/tests/testdata/sites-enabled/default letsencrypt_nginx/tests/testdata/sites-enabled/example.com letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/fastcgi_params letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-utf letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-win letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/mime.types letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi-ui.conf.1.4.1 letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi.rules letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi_core.rules letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/nginx.conf letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/proxy_params letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/scgi_params letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-available/default letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-enabled/default letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/uwsgi_params letsencrypt_nginx/tests/testdata/ubuntu_nginx_1_4_6/default_vhost/nginx/win-utf letsencrypt_nginx/tests/util.py
2015-05-12 17:06:17 -04:00
2015-05-10 06:47:58 -04:00
from letsencrypt_apache import configurator
from letsencrypt_apache import constants
from letsencrypt_apache import obj
2014-12-19 18:49:29 -05:00
2015-01-28 08:02:14 -05:00
class ApacheTest(unittest.TestCase): # pylint: disable=too-few-public-methods
2015-01-24 08:12:45 -05:00
def setUp(self):
super(ApacheTest, self).setUp()
self.temp_dir, self.config_dir, self.work_dir = common.dir_setup(
test_dir="debian_apache_2_4/two_vhost_80",
pkg="letsencrypt_apache.tests")
2015-01-24 08:12:45 -05:00
self.ssl_options = common.setup_ssl_options(
self.config_dir, constants.MOD_SSL_CONF_SRC,
constants.MOD_SSL_CONF_DEST)
2015-01-24 08:12:45 -05:00
self.config_path = os.path.join(
self.temp_dir, "debian_apache_2_4/two_vhost_80/apache2")
2015-01-24 08:12:45 -05:00
self.rsa256_file = pkg_resources.resource_filename(
"letsencrypt.tests", os.path.join("testdata", "rsa256_key.pem"))
2015-01-24 08:12:45 -05:00
self.rsa256_pem = pkg_resources.resource_string(
"letsencrypt.tests", os.path.join("testdata", "rsa256_key.pem"))
2015-01-24 08:12:45 -05:00
2014-12-19 18:49:29 -05:00
def get_apache_configurator(
2015-06-01 21:01:29 -04:00
config_path, config_dir, work_dir, version=(2, 4, 7)):
2014-12-19 18:49:29 -05:00
"""Create an Apache Configurator with the specified options."""
backups = os.path.join(work_dir, "backups")
2015-05-10 06:47:58 -04:00
with mock.patch("letsencrypt_apache.configurator."
2014-12-19 18:49:29 -05:00
"subprocess.Popen") as mock_popen:
# This just states that the ssl module is already loaded
mock_popen().communicate.return_value = ("ssl_module", "")
config = configurator.ApacheConfigurator(
config=mock.MagicMock(
apache_server_root=config_path,
apache_le_vhost_ext=constants.CLI_DEFAULTS["le_vhost_ext"],
2015-06-01 20:40:33 -04:00
backup_dir=backups,
config_dir=config_dir,
2015-06-01 20:40:33 -04:00
temp_checkpoint_dir=os.path.join(work_dir, "temp_checkpoints"),
in_progress_dir=os.path.join(backups, "IN_PROGRESS"),
work_dir=work_dir),
name="apache",
version=version)
2014-12-19 18:49:29 -05:00
2015-02-23 07:26:43 -05:00
config.prepare()
2014-12-19 18:49:29 -05:00
return config
def get_vh_truth(temp_dir, config_name):
"""Return the ground truth for the specified directory."""
if config_name == "debian_apache_2_4/two_vhost_80":
prefix = os.path.join(
temp_dir, config_name, "apache2/sites-available")
aug_pre = "/files" + prefix
vh_truth = [
obj.VirtualHost(
os.path.join(prefix, "encryption-example.conf"),
os.path.join(aug_pre, "encryption-example.conf/VirtualHost"),
set([common.Addr.fromstring("*:80")]),
2014-12-19 18:49:29 -05:00
False, True, set(["encryption-example.demo"])),
obj.VirtualHost(
os.path.join(prefix, "default-ssl.conf"),
os.path.join(aug_pre, "default-ssl.conf/IfModule/VirtualHost"),
set([common.Addr.fromstring("_default_:443")]), True, False),
2014-12-19 18:49:29 -05:00
obj.VirtualHost(
os.path.join(prefix, "000-default.conf"),
os.path.join(aug_pre, "000-default.conf/VirtualHost"),
set([common.Addr.fromstring("*:80")]), False, True,
2014-12-19 18:49:29 -05:00
set(["ip-172-30-0-17"])),
obj.VirtualHost(
os.path.join(prefix, "letsencrypt.conf"),
os.path.join(aug_pre, "letsencrypt.conf/VirtualHost"),
set([common.Addr.fromstring("*:80")]), False, True,
2014-12-19 18:49:29 -05:00
set(["letsencrypt.demo"])),
]
return vh_truth
return None