Skip to content

Commit

Permalink
Update test logic
Browse files Browse the repository at this point in the history
  • Loading branch information
WStechura committed Jul 26, 2024
1 parent d768e31 commit 096e8f9
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

# This script acts as a self contained installer of the procuct

readonly DEFAULT_INSTALL_DIR="/opt/dynatrace/oneagent"
readonly INSTALLER_VERSION="##VERSION##"
readonly DEPLOYMENT_CONF_PATH="/var/lib/dynatrace/oneagent/agent/config"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

# This file simulates deployment functionalities of oneagentctl binary, used to configure installation.

readonly DIR_NAME="$(dirname "${0}")"
readonly INSTALLER_VERSION="##VERSION##"
readonly DEPLOYMENT_CONF_PATH="/var/lib/dynatrace/oneagent/agent/config"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

# This file simulates the basic behavior of the uninstall.sh script

main() {
rm -rf /opt/dynatrace
}
Expand Down
105 changes: 83 additions & 22 deletions roles/oneagent/tests/integration/run.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import argparse
import glob
import logging
import os
import shutil
import subprocess

from pathlib import Path
from typing import Dict, Any, List
from scripts.util.test_data_types import DeploymentPlatform

USER_KEY = "user"
PASS_KEY = "password"

TEST_DIR = Path("test_dir")
LOG_DIR = TEST_DIR / "logs"
INSTALLERS_DIR = TEST_DIR / "installers"
TEST_VARS = {"PYTHONPATH": "scripts/"}


class ServerWrapper(object):
def __init__(self, proc: subprocess.Popen):
self.proc = proc

def __enter__(self):
return self.proc

def __exit__(self, exc_type, exc_val, exc_tb):
self.proc.terminate()
save_log(self.proc.stdout, LOG_DIR / "server.log")


def get_env_vars():
# This is required to pass current venv vars down to the subprocess
env_vars = os.environ.copy()
env_vars.update(TEST_VARS)
return env_vars
Expand All @@ -24,15 +43,34 @@ def save_log(out, log_path: Path):
log.write(line)


def run_tests():
def get_test_args(args: Dict[str, Any]) -> List[str]:
test_args = [f"--{USER_KEY}='{args[USER_KEY]}'", f"--{PASS_KEY}='{args[PASS_KEY]}'"]
for platform in [p.value for p in DeploymentPlatform]:
hosts = ','.join(args[platform])
hosts and test_args.append(f"--{platform}={hosts}")
return test_args


def run_test(test: str, test_args: List[str]) -> bool:
test_name = Path(test).stem
proc = subprocess.run(["pytest", test] + test_args, env=get_env_vars(), encoding="utf-8",
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
save_log(proc.stdout, LOG_DIR / f"{test_name}.log")
return proc.returncode == 0


def run_tests(args: Dict[str, Any]) -> bool:
logging.info("Running tests...")

test_path = "scripts/tests"
for test in glob.glob(f"{test_path}/test_i*.py"):
test_name = Path(test).stem
proc = subprocess.run(["pytest", test, "--user=''", "--password=''", "--linux_x86=localhost"],
env=get_env_vars(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8")
save_log(proc.stdout, LOG_DIR / f"{test_name}.log")
test_args = get_test_args(args)
tests_failed = False

for test in glob.glob(f"{test_path}/test_*.py"):
if not run_test(test, test_args):
tests_failed = True

return tests_failed


def run_server():
Expand All @@ -47,10 +85,13 @@ def get_file_content(path: Path):
return f.readlines()


def replace_tag(source: List[str], old: str, new: str) -> List[str]:
return [line.replace(old, new) for line in source]


def prepare_installers():
logging.info("Preparing installers...")

# TODO: remove SH once oneagentctl in ready
oneagentctl_bin_name = "oneagentctl.sh"
uninstall_script_name = "uninstall.sh"
installer_partial_name = "Dynatrace-OneAgent-Linux"
Expand All @@ -61,36 +102,56 @@ def prepare_installers():

resource_dir = Path("resources") / "installers"

uninstall_code = get_file_content(resource_dir / uninstall_script_name)
uninstall_code = [line.replace("$", r"\$") for line in uninstall_code]
uninstall_template = get_file_content(resource_dir / uninstall_script_name)
uninstall_code = replace_tag(uninstall_template, "$", r"\$")

oneagentctl_code = get_file_content(resource_dir / oneagentctl_bin_name)
oneagentctl_code = [line.replace("$", r"\$") for line in oneagentctl_code]
oneagentctl_template = get_file_content(resource_dir / oneagentctl_bin_name)
oneagentctl_code = replace_tag(oneagentctl_template, "$", r"\$")

installer_template = get_file_content(resource_dir / f"{installer_partial_name}.sh")
installer_template = [line.replace(uninstall_code_tag, "".join(uninstall_code)) for line in installer_template]
installer_template = [line.replace(oneagentctl_code_tag, "".join(oneagentctl_code)) for line in installer_template]
installer_template = replace_tag(installer_template, uninstall_code_tag, "".join(uninstall_code))
installer_template = replace_tag(installer_template, oneagentctl_code_tag, "".join(oneagentctl_code))

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)
# Minimal supported version is 1.199
for version in ["1.199.0", "1.300.0"]:
installer_code = replace_tag(installer_template, version_tag, version)
with open(INSTALLERS_DIR / f"{installer_partial_name}-{version}.sh", "w") as f:
f.writelines(installer_code)


def prepare_environment():
logging.basicConfig(
format="%(asctime)s [server] %(levelname)s: %(message)s", datefmt="%H:%M:%S", level=logging.INFO
)
shutil.rmtree(TEST_DIR, ignore_errors=True)
os.makedirs(INSTALLERS_DIR, exist_ok=True)
os.makedirs(LOG_DIR, exist_ok=True)
prepare_installers()


def parse_args() -> Dict[str, Any]:
parser = argparse.ArgumentParser(
description="Run integration tests for OneAgent role. If any of specified platform contains"
" 'localhost' as an IP address, the script will perform tests on the local machine."
" In such case, user and password arguments can be empty"
)

parser.add_argument(f"--{USER_KEY}", type=str, help="User for remote hosts", default="")
parser.add_argument(f"--{PASS_KEY}", type=str, help="Password for remote hosts user", default="")

for platform in DeploymentPlatform:
parser.add_argument(
f"--{platform.value}", type=str, nargs="+", default=[], help="List of IPs for specified platform"
)

return vars(parser.parse_args())


def main():
prepare_environment()
server = run_server()
run_tests()

server.terminate()
save_log(server.stdout, LOG_DIR / "server.log")
with ServerWrapper(run_server()):
result = run_tests(parse_args())
return result


if __name__ == "__main__":
Expand Down
1 change: 0 additions & 1 deletion roles/oneagent/tests/integration/scripts/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ 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
Expand Up @@ -47,11 +47,8 @@ def _assert_oneagentctl_getter(
expected_values: Set[str],
):
oneagentctl = f"{get_oneagentctl_path(platform)}"
tags = wrapper.run_command(platform, address, oneagentctl, ctl_param)
logging.info("AAAAAAAAAAAAAAAAAAAAA")
logging.info(set(tags.stdout.strip().splitlines()))
logging.info(expected_values)
assert tags.returncode == 0 and expected_values == set(tags.stdout.strip().splitlines())
result = wrapper.run_command(platform, address, oneagentctl, ctl_param)
assert result.returncode == 0 and expected_values == set(result.stdout.strip().splitlines())


def _check_install_args(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _get_versions_for_platforms(platforms: PlatformCollection, latest: bool) ->
for platform, _ in platforms.items():
installers = get_installers(platform.system(), platform.arch())
versioned_installer = installers[-1 if latest else 0]
versions[platform] = re.search(r"\d.\d+.\d+.\d+-\d+", str(versioned_installer)).group()
versions[platform] = re.search(r"\d.\d+.\d+", str(versioned_installer)).group()
return versions


Expand All @@ -33,6 +33,7 @@ def test_versioned_installation(_set_up, runner, configurator, platforms, wrappe
logging.info("Running versioned installation test")

set_installer_download_params(configurator)
configurator.set_common_parameter(configurator.VERIFY_SIGNATURE_KEY, False)

versions = _get_versions_for_platforms(platforms, False)
for platform, version in versions.items():
Expand All @@ -53,6 +54,7 @@ def test_upgrade(_set_up, runner, configurator, platforms, wrapper, _tear_down):
configurator.clear_parameters_section()
set_installer_download_params(configurator)
configurator.set_common_parameter(configurator.INSTALLER_VERSION_KEY, "latest")
configurator.set_common_parameter(configurator.VERIFY_SIGNATURE_KEY, False)

versions = _get_versions_for_platforms(platforms, True)

Expand Down

0 comments on commit 096e8f9

Please sign in to comment.