2021-06-21 14:51:43 +02:00
|
|
|
# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
#
|
2021-06-21 14:51:43 +02:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
#
|
|
|
|
# See the COPYRIGHT file distributed with this work for additional
|
|
|
|
# information regarding copyright ownership.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2023-01-12 15:48:55 +01:00
|
|
|
# ======================= LEGACY=COMPATIBLE FIXTURES =========================
|
|
|
|
# The following fixtures are designed to work with both pytest system test
|
|
|
|
# runner and the legacy system test framework.
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
2021-06-21 14:51:43 +02:00
|
|
|
def named_port():
|
2022-06-07 16:27:23 +02:00
|
|
|
return int(os.environ.get("PORT", default=5300))
|
2022-03-14 08:59:32 +01:00
|
|
|
|
|
|
|
|
2023-01-12 15:48:55 +01:00
|
|
|
@pytest.fixture(scope="module")
|
2022-03-14 08:59:32 +01:00
|
|
|
def named_tlsport():
|
2022-06-07 16:27:23 +02:00
|
|
|
return int(os.environ.get("TLSPORT", default=8853))
|
2022-03-14 08:59:32 +01:00
|
|
|
|
|
|
|
|
2023-01-12 15:48:55 +01:00
|
|
|
@pytest.fixture(scope="module")
|
2022-06-27 22:50:00 +02:00
|
|
|
def named_httpsport():
|
|
|
|
return int(os.environ.get("HTTPSPORT", default=4443))
|
|
|
|
|
|
|
|
|
2023-01-12 15:48:55 +01:00
|
|
|
@pytest.fixture(scope="module")
|
2022-03-14 08:59:32 +01:00
|
|
|
def control_port():
|
2022-06-07 16:27:23 +02:00
|
|
|
return int(os.environ.get("CONTROLPORT", default=9953))
|
2023-01-12 15:48:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ======================= PYTEST SYSTEM TEST RUNNER ==========================
|
|
|
|
# From this point onward, any setting, fixtures or functions only apply to the
|
|
|
|
# new pytest runner. Ideally, these would be in a separate file. However, due
|
|
|
|
# to how pytest works and how it's used by the legacy runner, the best approach
|
|
|
|
# is to have everything in this file to avoid duplication and set the
|
|
|
|
# LEGACY_TEST_RUNNER if pytest is executed from the legacy framework.
|
|
|
|
#
|
|
|
|
# FUTURE: Once legacy runner is no longer supported, remove the env var and
|
|
|
|
# don't branch the code.
|
|
|
|
|
|
|
|
if os.getenv("LEGACY_TEST_RUNNER", "0") == "0":
|
2023-01-12 16:41:41 +01:00
|
|
|
import logging
|
|
|
|
from pathlib import Path
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
# ----------------------- Globals definition -----------------------------
|
|
|
|
|
|
|
|
FILE_DIR = os.path.abspath(Path(__file__).parent)
|
|
|
|
ENV_RE = re.compile("([^=]+)=(.*)")
|
|
|
|
|
|
|
|
# ---------------------- Module initialization ---------------------------
|
|
|
|
|
|
|
|
def parse_env(env_text):
|
|
|
|
"""Parse the POSIX env format into Python dictionary."""
|
|
|
|
out = {}
|
|
|
|
for line in env_text.splitlines():
|
|
|
|
match = ENV_RE.match(line)
|
|
|
|
if match:
|
|
|
|
out[match.groups()[0]] = match.groups()[1]
|
|
|
|
return out
|
|
|
|
|
|
|
|
def get_env(cmd):
|
|
|
|
try:
|
|
|
|
proc = subprocess.run(
|
|
|
|
[cmd],
|
|
|
|
shell=True,
|
|
|
|
check=True,
|
|
|
|
cwd=FILE_DIR,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
logging.error("failed to get shell env: %s", exc)
|
|
|
|
raise exc
|
|
|
|
env_text = proc.stdout.decode("utf-8")
|
|
|
|
return parse_env(env_text)
|
|
|
|
|
|
|
|
# Read common environment variables for running tests from conf.sh.
|
|
|
|
# FUTURE: Remove conf.sh entirely and define all variables in pytest only.
|
|
|
|
CONF_ENV = get_env(". ./conf.sh && env")
|
|
|
|
os.environ.update(CONF_ENV)
|
|
|
|
logging.debug("conf.sh env: %s", CONF_ENV)
|