Skip to content

Commit

Permalink
Fix pylint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubrak committed Dec 13, 2024
1 parent a5c7d7e commit 563e7aa
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 68 deletions.
6 changes: 1 addition & 5 deletions roles/oneagent/tests/ansible/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@
ANSIBLE_PASS_KEY,
ANSIBLE_RESOURCE_DIR,
ANSIBLE_USER_KEY,
COLLECTION_NAME,
COLLECTION_NAMESPACE,
CREDENTIALS_FILE_NAME,
HOSTS_TEMPLATE_FILE_NAME,
INSTALLED_COLLECTIONS_DIR,
INVENTORY_FILE,
PLAYBOOK_FILE,
PLAYBOOK_TEMPLATE_FILE_NAME,
ROLE_NAME,
TEST_COLLECTIONS_DIR,
TEST_SIGNATURE_FILE,
)

from util.common_utils import read_yaml_file, write_yaml_file
from util.constants.common_constants import TEST_DIRECTORY, INSTALLERS_DIRECTORY, INSTALLER_CERTIFICATE_FILE_NAME
from util.constants.common_constants import TEST_DIRECTORY
from util.test_data_types import DeploymentPlatform, PlatformCollection


Expand Down
12 changes: 5 additions & 7 deletions roles/oneagent/tests/ansible/runner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import subprocess
import logging

from ansible.constants import HOSTS_TEMPLATE_FILE_NAME, PLAYBOOK_TEMPLATE_FILE_NAME, CREDENTIALS_FILE_NAME, TEST_DIRECTORY
from ansible.constants import (HOSTS_TEMPLATE_FILE_NAME, PLAYBOOK_TEMPLATE_FILE_NAME, CREDENTIALS_FILE_NAME,
TEST_DIRECTORY)
from util.test_data_types import CommandResult, DeploymentResult


Expand All @@ -13,18 +14,15 @@ def __init__(self, user: str, password: str):
def run_deployment(self) -> DeploymentResult:
with open(TEST_DIRECTORY / PLAYBOOK_TEMPLATE_FILE_NAME, 'r') as f:
logging.debug(
f"Running playbook ({PLAYBOOK_TEMPLATE_FILE_NAME}):\n{
f.read()}")
"Running playbook (%s):\n%s", PLAYBOOK_TEMPLATE_FILE_NAME, f.read())

with open(TEST_DIRECTORY / HOSTS_TEMPLATE_FILE_NAME, 'r') as f:
logging.debug(
f"Inventory file ({HOSTS_TEMPLATE_FILE_NAME}):\n{
f.read()}")
"Inventory file (%s):\n%s", HOSTS_TEMPLATE_FILE_NAME, f.read())

with open(TEST_DIRECTORY / CREDENTIALS_FILE_NAME, 'r') as f:
logging.debug(
f"Credentials file ({CREDENTIALS_FILE_NAME}):\n{
f.read()}")
"Credentials file (%s):\n%s", CREDENTIALS_FILE_NAME, f.read())

res = subprocess.run(["ansible-playbook",
"-i",
Expand Down
3 changes: 1 addition & 2 deletions roles/oneagent/tests/command/unix/unix_command_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ def run_command(
command: str,
*args: str) -> CommandResult:
return self._execute(
address, f"echo {
self.password} | sudo -S {command}", *args)
address, f"echo {self.password} | sudo -S {command}", *args)
12 changes: 6 additions & 6 deletions roles/oneagent/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@


def is_local_deployment(platforms: PlatformCollection) -> bool:
return any("localhost" in hosts for _, hosts in platforms.items())
return any("localhost" in hosts for platform, hosts in platforms.items())


def parse_platforms_from_options(
Expand All @@ -54,7 +54,7 @@ def parse_platforms_from_options(
if key in deployment_platforms and hosts:
if "localhost" in hosts:
logging.info(
f"Local deployment detected for {key}, only this host will be used")
"Local deployment detected for %s, only this host will be used", key)
return {DeploymentPlatform.from_str(key): hosts}
platforms[DeploymentPlatform.from_str(key)] = hosts
return platforms
Expand Down Expand Up @@ -107,18 +107,18 @@ def prepare_installers(request) -> None:
@pytest.fixture(scope="session", autouse=True)
def installer_server_url(request) -> None:
port = 8021
ip_address = socket.gethostbyname(socket.gethostname())
url = f"https://{ip_address}:{port}"
ipaddress = socket.gethostbyname(socket.gethostname())
url = f"https://{ipaddress}:{port}"

logging.info(f"Running server on {url}...")
logging.info("Running server on %s...", url)

proc = subprocess.Popen([sys.executable,
"-m",
"server",
"--port",
f"{port}",
"--ip-address",
ip_address],
ipaddress],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
Expand Down
17 changes: 11 additions & 6 deletions roles/oneagent/tests/resources/installers/oneagentctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ saveToConfig() {
while [ $# -gt 0 ]; do
# example command: --set-host-property=TENANT=tenant1
# setter: --set-host-property
local setter="$(cutVariable "${1}" "=" 1)"
local setter
setter="$(cutVariable "${1}" "=" 1)"
# setterType: property
local setterType="$(cutVariable "${setter}" "-" "5-")"
local setterType
setterType="$(cutVariable "${setter}" "-" "5-")"
# value: TENANT=tenant1
local value="$(cutVariable "${1}" "=" "2-")"
local value
value="$(cutVariable "${1}" "=" "2-")"
# property: TENANT
local property="$(cutVariable "${value}" "=" "1")"
local property
property="$(cutVariable "${value}" "=" "1")"
local setterFile="${DEPLOYMENT_CONF_PATH}/${setterType}"
if grep -q "${property}" "${setterFile}"; then
sed -i "s/${property}.*/${value}/" "${setterFile}"
Expand All @@ -36,7 +40,8 @@ saveToConfig() {
}

readFromConfig() {
local getterType="$(cutVariable "${1}" "-" "5-")"
local getterType
getterType="$(cutVariable "${1}" "-" "5-")"

if [ "${getterType}" = "properties" ]; then
getterType="property"
Expand All @@ -49,7 +54,7 @@ main() {
if [ "${1}" = '--version' ]; then
printf '%s\n' "${INSTALLER_VERSION}"
elif printf "%s" "${1}" | grep -q "^--get"; then
readFromConfig ${1}
readFromConfig "${1}"
elif printf "%s" "${1}" | grep -q "^--set"; then
saveToConfig "$@"
fi
Expand Down
9 changes: 4 additions & 5 deletions roles/oneagent/tests/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

def get_installer(system: str, arch: str, version: str) -> TransferResult:
logging.info(
f"Getting installer for system {system}_{arch} in {version} version")
"Getting installer for system %s_%s in %s version", system, arch, version)

installers = get_installers(system, arch, version, True)

Expand All @@ -41,7 +41,7 @@ def get_installer(system: str, arch: str, version: str) -> TransferResult:
def get_ca_certificate() -> TransferResult:
cert_file = INSTALLERS_DIRECTORY / INSTALLER_CERTIFICATE_FILE_NAME
if not cert_file.exists():
logging.warning(f"{cert_file} not found")
logging.warning("%s not found", cert_file)
return f"{cert_file} not found", HTTPStatus.NOT_FOUND
return send_file(cert_file)

Expand Down Expand Up @@ -78,9 +78,8 @@ def main() -> None:
organization_name="Dynatrace",
common_name=args.ip_address
)
generator.generate_and_save(f"{SERVER_DIRECTORY /
SERVER_PRIVATE_KEY_FILE_NAME}", f"{SERVER_DIRECTORY /
SERVER_CERTIFICATE_FILE_NAME}")
generator.generate_and_save(f"{SERVER_DIRECTORY /SERVER_PRIVATE_KEY_FILE_NAME}",
f"{SERVER_DIRECTORY / SERVER_CERTIFICATE_FILE_NAME}")

context = (f"{SERVER_DIRECTORY / SERVER_CERTIFICATE_FILE_NAME}",
f"{SERVER_DIRECTORY / SERVER_PRIVATE_KEY_FILE_NAME}")
Expand Down
2 changes: 1 addition & 1 deletion roles/oneagent/tests/test_installAndConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_basic_installation(
f"{CTL_OPTION_SET_HOST_TAG}={dummy_common_tag}",
f"{CTL_OPTION_SET_HOST_PROPERTY}={dummy_common_property}"])

for platform, _ in platforms.items():
for platform, hosts in platforms.items():
download_dir: Path = get_platform_argument(
platform, UNIX_DOWNLOAD_PATH, WINDOWS_DOWNLOAD_PATH)
configurator.set_platform_parameter(
Expand Down
2 changes: 1 addition & 1 deletion roles/oneagent/tests/test_localInstaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_local_installer(

set_ca_cert_download_params(configurator, installer_server_url)

for platform, _ in platforms.items():
for platform, hosts in platforms.items():
installers_location = LOCAL_INSTALLERS_LOCATION
latest_installer_name = get_installers(
platform.system(), platform.arch(), "latest")[-1]
Expand Down
9 changes: 4 additions & 5 deletions roles/oneagent/tests/test_upgrade.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import re
from typing import Dict

from command.platform_command_wrapper import PlatformCommandWrapper
from util.common_utils import get_oneagentctl_path, get_installers
Expand All @@ -15,7 +16,7 @@
def _get_versions_for_platforms(
platforms: PlatformCollection, latest: bool) -> dict[DeploymentPlatform, str]:
versions: Dict[DeploymentPlatform, str] = {}
for platform, _ in platforms.items():
for platform, hosts in platforms.items():
installers = get_installers(platform.system(), platform.arch())
versioned_installer = installers[-1 if latest else 0]
versions[platform] = re.search(
Expand All @@ -27,11 +28,9 @@ def _get_versions_for_platforms(
def _check_agent_version(platform: DeploymentPlatform,
address: str,
wrapper: PlatformCommandWrapper,
versions: dict[DeploymentPlatform,
str]) -> None:
versions: dict[DeploymentPlatform, str]) -> None:
installed_version = wrapper.run_command(
platform, address, f"{
get_oneagentctl_path(platform)}", "--version")
platform, address, f"{get_oneagentctl_path(platform)}", "--version")
assert installed_version.stdout.strip() == versions[platform]


Expand Down
6 changes: 3 additions & 3 deletions roles/oneagent/tests/util/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import shutil
from pathlib import Path
from typing import Any
from typing import Any, Dict

import yaml

Expand Down Expand Up @@ -74,7 +74,7 @@ def _get_platform_by_installer(installer: Path) -> DeploymentPlatform:
return DeploymentPlatform.LINUX_X86


def _get_available_installers() -> dict[DeploymentPlatform, list[Path]]:
def _get_available_installers() -> Dict[DeploymentPlatform, list[Path]]:
installers: dict[DeploymentPlatform, list[Path]] = {
k: [] for k in DeploymentPlatform}
for installer in sorted(
Expand Down Expand Up @@ -104,5 +104,5 @@ def get_installers(system: str, arch: str, version: str = "",

except Exception as ex:
logging.error(
f"Failed to get installer for {system}_{arch} in {version} version: {ex}")
"Failed to get installer for %s_%s in %s version: %s", system, arch, version, ex)
return []
31 changes: 14 additions & 17 deletions roles/oneagent/tests/util/installer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def sign_installer(installer: list[str]) -> list[str]:

proc = subprocess.run(
cmd,
input=f"{
''.join(installer)}",
input=f"{''.join(installer)}",
encoding="utf-8",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT,
check=False)
if proc.returncode != 0:
logging.error(f"Failed to sign installer: {proc.stdout}")
logging.error("Failed to sign installer: %s")
return []

signed_installer = proc.stdout.splitlines()
Expand All @@ -50,11 +50,10 @@ def sign_installer(installer: list[str]) -> list[str]:
signed_installer = signed_installer[index + 1:]

custom_delimiter = "----SIGNED-INSTALLER"
return [
f"{l}\n" if not l.startswith(delimiter) else f"{
l.replace(
delimiter,
custom_delimiter)}\n" for l in signed_installer]

return [f"{l}\n" if not l.startswith(delimiter)
else f"{l.replace(delimiter, custom_delimiter)}\n"
for l in signed_installer]


def generate_installers() -> bool:
Expand Down Expand Up @@ -86,9 +85,8 @@ def generate_installers() -> bool:
organization_name="Dynatrace",
common_name="127.0.0.1"
)
generator.generate_and_save(f"{INSTALLERS_DIRECTORY /
INSTALLER_PRIVATE_KEY_FILE_NAME}", f"{INSTALLERS_DIRECTORY /
INSTALLER_CERTIFICATE_FILE_NAME}")
generator.generate_and_save(f"{INSTALLERS_DIRECTORY /INSTALLER_PRIVATE_KEY_FILE_NAME}",
f"{INSTALLERS_DIRECTORY /INSTALLER_CERTIFICATE_FILE_NAME}")

# REMOVE INSTALLER VERSION
for version in InstallerVersion:
Expand Down Expand Up @@ -117,21 +115,20 @@ def get_installers_versions_from_tenant(
versions = resp.json()["availableVersions"]
if len(versions) < 2:
logging.error(
f"List of available installers is too short: {versions}")
"List of available installers is too short: %s", versions)
else:
return [versions[0], versions[-1]]
except KeyError:
logging.error(
f"Failed to get list of installer versions: {
resp.content}")
"Failed to get list of installer versions: %s", resp.content)
return []


def download_and_save(path: Path, url: str, headers: dict[str, str]) -> bool:
resp = requests.get(url, headers=headers)

if not resp.ok:
logging.error(f"Failed to download file {path}: {resp.text}")
logging.error("Failed to download file %s: %s", path, resp.text)
return False

with path.open("wb") as f:
Expand Down Expand Up @@ -165,7 +162,7 @@ def download_installers(
tenant,
tenant_token,
platforms: PlatformCollection) -> bool:
for platform, _ in platforms.items():
for platform, hosts in platforms.items():
versions = get_installers_versions_from_tenant(
tenant, tenant_token, platform.family())
if not versions:
Expand Down
2 changes: 1 addition & 1 deletion roles/oneagent/tests/util/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def arch(self) -> str:
return str(self.value).split("_")[1]

def system(self) -> str:
return str(self.value).split("_")[0]
return str(self.value).split("_", maxsplit=1)[0]

@staticmethod
def from_str(param: str) -> "DeploymentPlatform":
Expand Down
13 changes: 4 additions & 9 deletions roles/oneagent/tests/util/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def params_wrapper(*args, **kwargs):
config.set_deployment_hosts(family)
func(*args, **kwargs)
else:
logging.info(f"Skipping test for specified platform")
logging.info("Skipping test for specified platform")

return params_wrapper

Expand All @@ -59,8 +59,7 @@ def set_ca_cert_download_params(
config.CA_CERT_DOWNLOAD_URL_KEY,
f"{installer_server_url}/{INSTALLER_CERTIFICATE_FILE_NAME}")
config.set_common_parameter(
config.CA_CERT_DOWNLOAD_CERT_KEY, f"{
SERVER_DIRECTORY / SERVER_CERTIFICATE_FILE_NAME}")
config.CA_CERT_DOWNLOAD_CERT_KEY, f"{SERVER_DIRECTORY / SERVER_CERTIFICATE_FILE_NAME}")
config.set_common_parameter(config.FORCE_CERT_DOWNLOAD_KEY, True)


Expand All @@ -72,8 +71,7 @@ def set_installer_download_params(
installer_server_url)
config.set_common_parameter(config.PAAS_TOKEN_KEY, INSTALLER_SERVER_TOKEN)
config.set_common_parameter(
config.INSTALLER_DOWNLOAD_CERT_KEY, f"{
SERVER_DIRECTORY / SERVER_CERTIFICATE_FILE_NAME}")
config.INSTALLER_DOWNLOAD_CERT_KEY, f"{SERVER_DIRECTORY / SERVER_CERTIFICATE_FILE_NAME}")
for platform in DeploymentPlatform:
config.set_platform_parameter(
platform, config.INSTALLER_ARCH_KEY, platform.arch())
Expand All @@ -88,10 +86,7 @@ def run_deployment(
logging.info("Deployment finished")
for result in results:
logging.debug(
f"Exit code: {
result.returncode}\nOutput: {
result.stdout}, Error: {
result.stderr}")
"Exit code: %s\nOutput: %s, Error: %s", result.returncode, result.stdout, result.stderr)

if not ignore_errors:
logging.info("Check exit codes")
Expand Down

0 comments on commit 563e7aa

Please sign in to comment.