certbot/letsencrypt-compatibility-test/letsencrypt_compatibility_test/util.py

63 lines
1.9 KiB
Python
Raw Normal View History

2015-07-16 02:00:18 -04:00
"""Utility functions for Let"s Encrypt plugin tests."""
2015-07-16 19:57:47 -04:00
import argparse
2015-07-16 02:00:18 -04:00
import copy
2015-07-14 21:04:43 -04:00
import contextlib
2015-07-10 14:42:10 -04:00
import os
2015-07-16 02:00:18 -04:00
import re
2015-07-14 21:04:43 -04:00
import shutil
import socket
2015-07-10 14:42:10 -04:00
import tarfile
2015-07-21 21:14:57 -04:00
from acme import jose
from acme import test_util
2015-07-16 02:00:18 -04:00
from letsencrypt import constants
from letsencrypt_compatibility_test import errors
2015-07-10 14:42:10 -04:00
2015-07-21 21:14:57 -04:00
_KEY_BASE = "rsa1024_key.pem"
KEY_PATH = test_util.vector_path(_KEY_BASE)
KEY = test_util.load_pyopenssl_private_key(_KEY_BASE)
JWK = jose.JWKRSA(key=test_util.load_rsa_private_key(_KEY_BASE))
2015-07-16 02:00:18 -04:00
IP_REGEX = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
2015-07-10 14:42:10 -04:00
2015-07-16 02:00:18 -04:00
def create_le_config(parent_dir):
"""Sets up LE dirs in parent_dir and returns the config dict"""
config = copy.deepcopy(constants.CLI_DEFAULTS)
le_dir = os.path.join(parent_dir, "letsencrypt")
config["config_dir"] = os.path.join(le_dir, "config")
config["work_dir"] = os.path.join(le_dir, "work")
config["logs_dir"] = os.path.join(le_dir, "logs_dir")
os.makedirs(config["config_dir"])
os.mkdir(config["work_dir"])
os.mkdir(config["logs_dir"])
2015-09-06 05:21:28 -04:00
return argparse.Namespace(**config) # pylint: disable=star-args
2015-07-16 02:00:18 -04:00
2015-07-22 22:51:05 -04:00
2015-07-16 02:00:18 -04:00
def extract_configs(configs, parent_dir):
"""Extracts configs to a new dir under parent_dir and returns it"""
2015-10-11 20:33:00 -04:00
config_dir = os.path.join(parent_dir, "configs")
2015-07-10 14:42:10 -04:00
2015-07-14 21:04:43 -04:00
if os.path.isdir(configs):
shutil.copytree(configs, config_dir, symlinks=True)
elif tarfile.is_tarfile(configs):
2015-07-16 02:00:18 -04:00
with tarfile.open(configs, "r") as tar:
2015-07-14 21:04:43 -04:00
tar.extractall(config_dir)
else:
2015-07-16 02:00:18 -04:00
raise errors.Error("Unknown configurations file type")
2015-07-14 21:04:43 -04:00
2015-07-16 02:00:18 -04:00
return config_dir
2015-07-14 21:04:43 -04:00
def get_two_free_ports():
"""Returns two free ports to use for the tests"""
with contextlib.closing(socket.socket()) as sock1:
with contextlib.closing(socket.socket()) as sock2:
2015-07-16 02:00:18 -04:00
sock1.bind(("", 0))
sock2.bind(("", 0))
2015-07-14 21:04:43 -04:00
return sock1.getsockname()[1], sock2.getsockname()[1]