certbot/certbot-compatibility-test/certbot_compatibility_test/util.py

52 lines
1.5 KiB
Python
Raw Permalink Normal View History

2016-04-14 20:04:23 -04:00
"""Utility functions for Certbot plugin tests."""
2015-07-16 19:57:47 -04:00
import argparse
2015-07-16 02:00:18 -04:00
import copy
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
2015-07-10 14:42:10 -04:00
import tarfile
import josepy as jose
from certbot._internal import constants
from certbot.tests import util as test_util
from certbot_compatibility_test import errors
2015-07-10 14:42:10 -04:00
_KEY_BASE = "rsa2048_key.pem"
2015-07-21 21:14:57 -04:00
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
def create_le_config(parent_dir: str) -> argparse.Namespace:
2015-07-16 02:00:18 -04:00
"""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, "certbot")
os.mkdir(le_dir)
for dir_name in ("config", "logs", "work"):
full_path = os.path.join(le_dir, dir_name)
os.mkdir(full_path)
full_name = dir_name + "_dir"
config[full_name] = full_path
2015-07-16 02:00:18 -04:00
config["domains"] = None
return argparse.Namespace(**config)
2015-07-16 02:00:18 -04:00
2015-07-22 22:51:05 -04:00
def extract_configs(configs: str, parent_dir: str) -> str:
2015-07-16 02:00:18 -04:00
"""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