Skip to content

Commit

Permalink
Add missing types
Browse files Browse the repository at this point in the history
  • Loading branch information
WStechura committed Sep 13, 2024
1 parent 8978ba4 commit 7ff1679
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ jobs:
uses: ./.github/actions/setup-build-environment
- name: Build the collection
uses: ./.github/actions/build-collection
- name: Debug
run: ls -alR
- name: Run tests
uses: ./.github/actions/run-tests
13 changes: 0 additions & 13 deletions .idea/misc.xml.orig

This file was deleted.

3 changes: 2 additions & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ tags:
repository: https://github.com/Dynatrace/Dynatrace-OneAgent-Ansible
documentation: https://docs.dynatrace.com/docs/setup-and-configuration/dynatrace-oneagent/deployment-orchestration/ansible
build_ignore:
- roles/oneagent/tests
- roles/oneagent/tests
- roles/oneagent/examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/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"

readonly UNINSTALL_SCRIPT="uninstall.sh"
readonly UNINSTALL_CODE="$(cat <<-ENDUNINSTALL
##UNINSTALL_CODE##
ENDUNINSTALL
)"

readonly ONEAGENTCTL_BIN="oneagentctl"
readonly ONEAGENTCTL_CODE="$(cat <<-ENDCTL
##ONEAGENTCTL_CODE##
ENDCTL
)"

CTL_PARAMS=
INSTALL_DIR="${DEFAULT_INSTALL_DIR}"

parseParams() {
while [ $# -gt 0 ]; do
local param="${1}"
if [ "${param}" = "--version" ]; then
printf "%s" "${INSTALLER_VERSION}"
exit 0
elif [ "${param}" = "INSTALL_PATH" ]; then
INSTALL_DIR="$(printf "%s" "${param}" | cut -d "=" -f "2-")"
elif printf "%s" "${param}" | grep -q -- "--set"; then
CTL_PARAMS="${CTL_PARAMS} ${1}"
fi
shift
done
}

uninstall() {
local uninstallScript="${INSTALL_DIR}/agent/${UNINSTALL_SCRIPT}"
if [ -f "${uninstallScript}" ]; then
"${uninstallScript}"
fi
}

deployOneagentCtl() {
local ONEAGENTCTL_DIR="${INSTALL_DIR}/agent/tools"
mkdir -p "${ONEAGENTCTL_DIR}"
mkdir -p "${DEPLOYMENT_CONF_PATH}"
printf '%s' "${ONEAGENTCTL_CODE}" > "${ONEAGENTCTL_DIR}/${ONEAGENTCTL_BIN}"
chmod +x "${ONEAGENTCTL_DIR}/${ONEAGENTCTL_BIN}"
}

deployUninstallScript() {
local UNINSTALL_DIR="${INSTALL_DIR}/agent"
mkdir -p "${UNINSTALL_DIR}"
printf '%s' "${UNINSTALL_CODE}" > "${UNINSTALL_DIR}/${UNINSTALL_SCRIPT}"
chmod +x "${UNINSTALL_DIR}/${UNINSTALL_SCRIPT}"
}

applyConfig() {
"${INSTALL_DIR}/agent/tools/${ONEAGENTCTL_BIN}" ${CTL_PARAMS}
}

main() {
parseParams "$@"
uninstall
deployOneagentCtl
deployUninstallScript
applyConfig
}

##################
## SCRIPT START ##
##################

main "$@"
21 changes: 11 additions & 10 deletions roles/oneagent/tests/component/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
USER_KEY = "user"
PASS_KEY = "password"

TEST_DIR = Path("test_dir")
BASE_DIR = Path(__file__).resolve().parent
TEST_DIR = BASE_DIR / "test_dir"
LOG_DIR = TEST_DIR / "logs"
INSTALLERS_DIR = TEST_DIR / "installers"
TEST_VARS = {"PYTHONPATH": "scripts/"}
Expand All @@ -32,14 +33,14 @@ def __exit__(self, exc_type, exc_val, exc_tb):
save_log(self.proc.stdout, LOG_DIR / "server.log")


def get_env_vars():
def get_env_vars() -> dict[str, str]:
# This is required to pass current venv vars down to the subprocess for tests and server
env_vars = os.environ.copy()
env_vars.update(TEST_VARS)
return env_vars


def save_log(out, log_path: Path):
def save_log(out, log_path: Path) -> None:
with log_path.open("w") as log:
for line in out:
log.write(line)
Expand Down Expand Up @@ -78,14 +79,14 @@ def run_all_tests(args: dict[str, Any]) -> bool:
return tests_failed


def run_server():
def run_server() -> subprocess.Popen:
logging.info("Running server...")
server_path = Path("scripts") / "server" / "server.py"
server_path = BASE_DIR / "scripts" / "server" / "server.py"
return subprocess.Popen(["python", server_path], env=get_env_vars(),
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8")


def get_file_content(path: Path):
def get_file_content(path: Path) -> list[str]:
with path.open("r") as f:
return f.readlines()

Expand All @@ -94,7 +95,7 @@ def replace_tag(source: list[str], old: str, new: str) -> list[str]:
return [line.replace(old, new) for line in source]


def prepare_installers():
def prepare_installers() -> None:
logging.info("Preparing installers...")

oneagentctl_bin_name = "oneagentctl.sh"
Expand All @@ -105,7 +106,7 @@ def prepare_installers():
uninstall_code_tag = "##UNINSTALL_CODE##"
oneagentctl_code_tag = "##ONEAGENTCTL_CODE##"

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

uninstall_template = get_file_content(resource_dir / uninstall_script_name)
uninstall_code = replace_tag(uninstall_template, "$", r"\$")
Expand All @@ -126,7 +127,7 @@ def prepare_installers():
f.writelines(installer_code)


def prepare_environment():
def prepare_environment() -> None:
logging.basicConfig(
format="%(asctime)s [server] %(levelname)s: %(message)s", datefmt="%H:%M:%S", level=logging.INFO
)
Expand Down Expand Up @@ -154,7 +155,7 @@ def parse_args() -> dict[str, Any]:
return vars(parser.parse_args())


def main():
def main() -> bool:
args = parse_args()
prepare_environment()
with ServerWrapper(run_server()):
Expand Down
4 changes: 2 additions & 2 deletions roles/oneagent/tests/component/scripts/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def get_installer(system: str, arch: str, version: str) -> TransferResult:


@bp.route("/<system>/default/latest")
def get_latest_agent(system):
def get_latest_agent(system) -> TransferResult:
return get_installer(system, request.args["arch"], "latest")


@bp.route("/<system>/default/version/<version>")
def get_agent_in_version(system, version):
def get_agent_in_version(system, version) -> TransferResult:
return get_installer(system, request.args["arch"], version)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
TECH_VERSION_KEY = "tech_version"
TECH_SCRIPT_VERSION_KEY = "script_version"

# Tags and properties for regular installation
INSTALLER_TAG = "install_tag"
INSTALLER_PROPERTY = "install_prop1=install_1"
PLATFORM_TAG = "platform_install_tag"
PLATFORM_PROPERTY = "platform_install_property"

# Tags and properties for intended configuration with oneagentctl
CONFIG_INSTALL_TAG = "config_install_tag"
CONFIG_INSTALL_PROPERTY = "install_prop2=config_install_prop"
CONFIG_INTENDED_TAG = "config_intended_tag"
Expand Down Expand Up @@ -87,7 +90,7 @@ def _check_output_for_secrets(result: DeploymentResult) -> None:
def test_basic_installation(_set_up, runner, configurator, constants, platforms, wrapper):
logging.info("Running basic installation test")

set_installer_download_params(configurator)
set_installer_download_params(configurator)
configurator.set_common_parameter(configurator.PRESERVE_INSTALLER_KEY, True)
configurator.set_common_parameter(configurator.INSTALLER_ARGS_KEY, INSTALLER_ARGS)
configurator.set_common_parameter(configurator.VERIFY_SIGNATURE_KEY, False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from technology.constants import INSTALLER_SIGNATURE_FILE
from util.test_data_types import DeploymentResult
from util.test_helpers import run_deployment, set_installer_download_params, enable_for_family
from util.test_helpers import run_deployment, set_installer_download_params, enable_for_system_family

MISSING_REQUIRED_PARAMETERS_KEY = "missing_mandatory_params"
UNKNOWN_ARCHITECTURE_KEY = "unknown_arch"
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_missing_local_installer(_error_messages, runner, configurator, constant
)


@enable_for_family(family="unix")
@enable_for_system_family(family="unix")
def test_directories_contain_spaces(_error_messages, runner, configurator, constants):
logging.info("Running directories contain spaces test")

Expand Down Expand Up @@ -150,7 +150,7 @@ def test_failed_download(_error_messages, runner, configurator, constants):


# noinspection PyUnusedLocal
@enable_for_family(family="unix")
@enable_for_system_family(family="unix")
def test_failed_signature_verification(_error_messages, runner, configurator, constants):
logging.info("Running failed signature verification test")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def system(self) -> str:
return str(self.value).split("_")[0]

@staticmethod
def from_str(param: str):
def from_str(param: str) -> "DeploymentPlatform":
try:
return DeploymentPlatform[param.upper()]
except KeyError:
raise ValueError(f"Invalid option passed: {param}") from KeyError

@staticmethod
def from_system_and_arch(system: str, arch: str):
def from_system_and_arch(system: str, arch: str) -> "DeploymentPlatform":
return DeploymentPlatform.from_str(f"{system}_{arch}")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def platforms_wrapper(*args, **kwargs):
return func_wrapper


def enable_for_family(family: str):
def enable_for_system_family(family: str):
def func_wrapper(func):
@functools.wraps(func)
def params_wrapper(*args, **kwargs):
Expand Down

0 comments on commit 7ff1679

Please sign in to comment.