diff --git a/roles/oneagent/tests/integration/resources/installers/Dynatrace-OneAgent-Linux.sh b/roles/oneagent/tests/integration/resources/installers/Dynatrace-OneAgent-Linux.sh index b5d50d8..5833f5c 100644 --- a/roles/oneagent/tests/integration/resources/installers/Dynatrace-OneAgent-Linux.sh +++ b/roles/oneagent/tests/integration/resources/installers/Dynatrace-OneAgent-Linux.sh @@ -11,7 +11,12 @@ deployOneagentCtl() { } main() { - deployOneagentCtl + if [ "${@}" = '--version' ]; then + printf '%s\n' "${INSTALLER_VERSION}" + return 0 + else + deployOneagentCtl + fi } ################## diff --git a/roles/oneagent/tests/integration/run.py b/roles/oneagent/tests/integration/run.py new file mode 100644 index 0000000..48b90ed --- /dev/null +++ b/roles/oneagent/tests/integration/run.py @@ -0,0 +1,77 @@ +import glob +import logging +import os +import shutil +import subprocess + +from pathlib import Path + +TEST_DIR = Path("test_dir") +LOG_DIR = TEST_DIR / "logs" +INSTALLERS_DIR = TEST_DIR / "installers" +TEST_VARS = {"PYTHONPATH": "scripts/"} + + +def get_env_vars(): + env_vars = os.environ.copy() + env_vars.update(TEST_VARS) + return env_vars + + +def save_log(out, log_path: str): + with open(log_path, "w") as log: + for line in out: + log.write(line) + + +def run_tests(): + logging.info("Running tests...") + + test_path = "scripts/tests" + for test in glob.glob(f"{test_path}/test_*.py"): + proc = subprocess.run(["pytest", test], env=get_env_vars(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8") + save_log(proc.stdout, f"{LOG_DIR}/{Path(test).stem}.log") + + +def run_server(): + logging.info("Running server...") + server_path = Path("scripts") / "server" / "server.py" + return subprocess.Popen(["python", server_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=get_env_vars(), encoding="utf-8") + + +def prepare_installers(): + logging.info("Preparing installers...") + + installer_partial_name = "Dynatrace-OneAgent-Linux" + src_dir = Path("resources") / "installers" + version_tag = "##VERSION##" + + installers = list(glob.glob(f"{src_dir}/*.sh")) + assert len(installers) == 1, "Only one installer is supported" + + with open(installers[0], "r") as i: + installer_template = i.readlines() + for ver in ["1.0.0", "2.0.0"]: + versioned_installer = [line.replace(version_tag, ver) for line in installer_template] + with open(INSTALLERS_DIR / f"{installer_partial_name}-{ver}.sh", "w") as f: + f.writelines(versioned_installer) + + +def prepare_environment(): + shutil.rmtree(TEST_DIR, ignore_errors=True) + os.makedirs(INSTALLERS_DIR, exist_ok=True) + os.makedirs(LOG_DIR, exist_ok=True) + prepare_installers() + + +def main(): + prepare_environment() + server = run_server() + run_tests() + + server.terminate() + save_log(server.stdout, f"{LOG_DIR}/server.log") + + +if __name__ == "__main__": + main() diff --git a/roles/oneagent/tests/integration/run.sh b/roles/oneagent/tests/integration/run.sh deleted file mode 100644 index 27ceab3..0000000 --- a/roles/oneagent/tests/integration/run.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/sh - -readonly TEST_DIR='test_dir' -readonly LOG_DIR="${TEST_DIR}/logs" -readonly SERVER_LOG_FILE="${LOG_DIR}/server.log" - -prepareInstallerFiles() { - local baseName='Dynatrace-OneAgent-Linux' - local srcInstallerDir="resources/installers" - local destInstallerDir="${TEST_DIR}/installers" - - printf 'Preparing installer files...\n' - mkdir -p "${destInstallerDir}" - cp "${srcInstallerDir}/${baseName}.sh" "${destInstallerDir}/${baseName}-1.0.0.sh" - sed -i "s/##VERSION##/1.0.0/g" "${destInstallerDir}/${baseName}-1.0.0.sh" - cp "${srcInstallerDir}/${baseName}.sh" "${destInstallerDir}/${baseName}-2.0.0.sh" - sed -i "s/##VERSION##/2.0.0/g" "${destInstallerDir}/${baseName}-2.0.0.sh" -} - -runServer() { - local serverScript='scripts/server/server.py' - - printf 'Running server...\n' - mkdir -p "${LOG_DIR}" - PYTHONPATH='scripts' python "${serverScript}" >>"${SERVER_LOG_FILE}" 2>&1 & -} - -runTests() { - printf 'Running tests...\n' -} - -main() { - rm -rf "${TEST_DIR}" - prepareInstallerFiles - runServer - runTests -} - -################## -## SCRIPT START ## -################## -main "${@}" \ No newline at end of file diff --git a/roles/oneagent/tests/integration/scripts/server/server.py b/roles/oneagent/tests/integration/scripts/server/server.py index 565404f..1985558 100644 --- a/roles/oneagent/tests/integration/scripts/server/server.py +++ b/roles/oneagent/tests/integration/scripts/server/server.py @@ -1,3 +1,4 @@ +import argparse import logging from http import HTTPStatus from pathlib import Path @@ -40,8 +41,17 @@ def get_agent_in_version(system, version): return get_installer(system, request.args["arch"], version) +# def parse_args(): +# parser = argparse.ArgumentParser() +# parser.add_argument("--log_path", type=str, default=None, help="Log file path") +# return parser.parse_args() + + def main() -> None: + # args = parse_args() + logging.basicConfig( + filename=None, #args.log_path if args.log_path else None, format="%(asctime)s [server] %(levelname)s: %(message)s", datefmt="%H:%M:%S", level=logging.INFO ) server_path = Path("scripts") / "server" diff --git a/roles/oneagent/tests/integration/scripts/util/constants/common_constants.py b/roles/oneagent/tests/integration/scripts/util/constants/common_constants.py index 20888c3..4ce8744 100644 --- a/roles/oneagent/tests/integration/scripts/util/constants/common_constants.py +++ b/roles/oneagent/tests/integration/scripts/util/constants/common_constants.py @@ -1,10 +1,9 @@ -import os from pathlib import Path -# Currently the base is `integration` directory -INTEGRATION_TEST_BASE = Path().cwd() -TEST_DIRECTORY = INTEGRATION_TEST_BASE / "test_dir" -RESOURCES_DIRECTORY = INTEGRATION_TEST_BASE / "resources" +# is cwd() correct? +INTEGRATION_TEST_BASE = Path().cwd() / "test_dir" +TEST_DIRECTORY = INTEGRATION_TEST_BASE / "working_dir" +RESOURCES_DIRECTORY = Path().cwd() / "resources" INSTALLERS_DIRECTORY = TEST_DIRECTORY / "installers" SIGNATURE_FILE_NAME = "dt-root.cert.pem"