Skip to content

Commit

Permalink
Add test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
WStechura committed Jul 2, 2024
1 parent f1449a5 commit fe46334
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ deployOneagentCtl() {
}

main() {
deployOneagentCtl
if [ "${@}" = '--version' ]; then
printf '%s\n' "${INSTALLER_VERSION}"
return 0
else
deployOneagentCtl
fi
}

##################
Expand Down
77 changes: 77 additions & 0 deletions roles/oneagent/tests/integration/run.py
Original file line number Diff line number Diff line change
@@ -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()
42 changes: 0 additions & 42 deletions roles/oneagent/tests/integration/run.sh

This file was deleted.

10 changes: 10 additions & 0 deletions roles/oneagent/tests/integration/scripts/server/server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
import logging
from http import HTTPStatus
from pathlib import Path
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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"

Expand Down

0 comments on commit fe46334

Please sign in to comment.