From 903fc494c51b1575a2cdaafc62a51380c5a62b80 Mon Sep 17 00:00:00 2001 From: Dawid Korzepa Date: Thu, 11 Jul 2024 08:53:55 +0200 Subject: [PATCH] test fix Signed-off-by: Dawid Korzepa --- .../framework/docker_helper.py | 2 +- .../framework/kubernetes_helper.py | 2 +- .../framework/propagating_thread.py | 2 +- .../framework/qtest_helper.py | 159 ++++++++++++------ tests/e2e-test-framework/framework/ssh.py | 3 +- .../framework/test_description_plugin.py | 2 +- tests/e2e-test-framework/framework/tunnel.py | 119 ------------- tests/e2e-test-framework/framework/utils.py | 9 +- 8 files changed, 122 insertions(+), 176 deletions(-) delete mode 100644 tests/e2e-test-framework/framework/tunnel.py diff --git a/tests/e2e-test-framework/framework/docker_helper.py b/tests/e2e-test-framework/framework/docker_helper.py index 752845679..2343ffbcd 100644 --- a/tests/e2e-test-framework/framework/docker_helper.py +++ b/tests/e2e-test-framework/framework/docker_helper.py @@ -13,4 +13,4 @@ def is_docker_running(cls): return True except Exception as exc: logging.error(f"Error: {exc}") - return False \ No newline at end of file + return False diff --git a/tests/e2e-test-framework/framework/kubernetes_helper.py b/tests/e2e-test-framework/framework/kubernetes_helper.py index b9a615d74..2b082d770 100644 --- a/tests/e2e-test-framework/framework/kubernetes_helper.py +++ b/tests/e2e-test-framework/framework/kubernetes_helper.py @@ -21,4 +21,4 @@ def load_kube_api(cls): apps_v1_api = client.AppsV1Api(api_client) network_v1_api = client.NetworkingV1Api(api_client) coordination_v1_api = client.CoordinationV1Api(api_client) - return core_v1_api, custom_objects_api, apps_v1_api, network_v1_api, coordination_v1_api \ No newline at end of file + return core_v1_api, custom_objects_api, apps_v1_api, network_v1_api, coordination_v1_api diff --git a/tests/e2e-test-framework/framework/propagating_thread.py b/tests/e2e-test-framework/framework/propagating_thread.py index 1a4411bff..fa64190e6 100644 --- a/tests/e2e-test-framework/framework/propagating_thread.py +++ b/tests/e2e-test-framework/framework/propagating_thread.py @@ -27,4 +27,4 @@ def has_failed(self): return self.exc is not None def get_target_name(self): - return self.target.__name__ \ No newline at end of file + return self.target.__name__ diff --git a/tests/e2e-test-framework/framework/qtest_helper.py b/tests/e2e-test-framework/framework/qtest_helper.py index 4c0bb2a38..186232c15 100644 --- a/tests/e2e-test-framework/framework/qtest_helper.py +++ b/tests/e2e-test-framework/framework/qtest_helper.py @@ -6,9 +6,9 @@ # https://qtest.dev.tricentis.com/ -class QTestHelper(): +class QTestHelper: @classmethod - def __init__(cls, qtest_token, bundle_version=''): + def __init__(cls, qtest_token, bundle_version=""): cls.api_base_url = "https://qtest.gtie.dell.com/api/v3" cls.project_id = 367 # CMO project @@ -20,14 +20,14 @@ def __init__(cls, qtest_token, bundle_version=''): cls.status_codes = { "passed": 601, "failed": 602, - "skipped": 603 # 603 - incomplete + "skipped": 603, # 603 - incomplete } cls.access_token = qtest_token cls.headers = { "Authorization": f"Bearer {cls.access_token}", - "Content-Type": "application/json" + "Content-Type": "application/json", } def link_test_case_to_requirement(self, jira_id, test_case_pid): @@ -41,13 +41,22 @@ def link_test_case_to_requirement(self, jira_id, test_case_pid): exception = None for _ in range(self.max_retries): try: - response = requests.post(req_link_endpoint, headers=self.headers, data=json.dumps(body), timeout=self.default_timeout) + response = requests.post( + req_link_endpoint, + headers=self.headers, + data=json.dumps(body), + timeout=self.default_timeout, + ) response.raise_for_status() - logging.info(f"[qTest] Test cases {test_case_pid} [{test_case_id}] linked to requirement {jira_id} [{requirement_id}] successfully.") + logging.info( + f"[qTest] Test cases {test_case_pid} [{test_case_id}] linked to requirement {jira_id} [{requirement_id}] successfully." + ) return except requests.HTTPError as exc: exception = exc - logging.warning(f"[qTest] Failed to link test case {test_case_pid} to requirement {jira_id}. Retrying...") + logging.warning( + f"[qTest] Failed to link test case {test_case_pid} to requirement {jira_id}. Retrying..." + ) raise exception @@ -58,40 +67,57 @@ def get_test_case_pid_by_id(self, test_case_pid): exception = None for _ in range(self.max_retries): try: - response = requests.get(test_cases_endpoint, headers=self.headers, timeout=self.default_timeout) + response = requests.get( + test_cases_endpoint, + headers=self.headers, + timeout=self.default_timeout, + ) response.raise_for_status() - logging.info(f"[qTest] Get test case {test_case_pid} by id completed.") - return response.json()['id'] + logging.info( + f"[qTest] Get test case {test_case_pid} by id completed." + ) + return response.json()["id"] except requests.HTTPError as exc: exception = exc - logging.warning(f"[qTest] Failed to get test case {test_case_pid} by id. Retrying...") + logging.warning( + f"[qTest] Failed to get test case {test_case_pid} by id. Retrying..." + ) raise exception def get_requirement_id_by_jira_id(self, jira_id): logging.info(f"[qTest] Get requirement id by jira id {jira_id}") - search_endpoint = f"{self.api_base_url}/projects/{self.project_id}/search" + search_endpoint = ( + f"{self.api_base_url}/projects/{self.project_id}/search" + ) payload = { "object_type": "requirements", - "fields": [ - "id" - ], - "query": f"'name' ~ '{jira_id}'" + "fields": ["id"], + "query": f"'name' ~ '{jira_id}'", } exception = None for _ in range(self.max_retries): try: - response = requests.post(search_endpoint, headers=self.headers, json=payload, timeout=self.default_timeout) + response = requests.post( + search_endpoint, + headers=self.headers, + json=payload, + timeout=self.default_timeout, + ) response.raise_for_status() - logging.info(f"[qTest] Get requirement id by jira id {jira_id} completed.") - return response.json()['items'][0]['id'] + logging.info( + f"[qTest] Get requirement id by jira id {jira_id} completed." + ) + return response.json()["items"][0]["id"] except requests.HTTPError as exc: exception = exc - logging.warning(f"[qTest] Failed to get requirement id by jira id {jira_id}. Retrying...") + logging.warning( + f"[qTest] Failed to get requirement id by jira id {jira_id}. Retrying..." + ) raise exception @@ -102,36 +128,54 @@ def get_tests_from_suite(self, test_suite_id): exception = None for _ in range(self.max_retries): try: - response = requests.get(test_runs_endpoint, headers=self.headers, timeout=self.default_timeout) + response = requests.get( + test_runs_endpoint, + headers=self.headers, + timeout=self.default_timeout, + ) response.raise_for_status() tests_in_suite = {} - for test_run in response.json()['items']: - tests_in_suite[test_run['testCaseId']] = test_run['id'] + for test_run in response.json()["items"]: + tests_in_suite[test_run["testCaseId"]] = test_run["id"] - logging.info(f"[qTest] Get tests from test suite {test_suite_id} completed.") + logging.info( + f"[qTest] Get tests from test suite {test_suite_id} completed." + ) return tests_in_suite except requests.HTTPError as exc: exception = exc - logging.warning(f"[qTest] Failed to get tests from test suite {test_suite_id}. Retrying...") + logging.warning( + f"[qTest] Failed to get tests from test suite {test_suite_id}. Retrying..." + ) raise exception def add_test_run_to_test_suite(self, test_case_pid, test_suite_id): - logging.info(f"[qTest] Add test case {test_case_pid} to test suite {test_suite_id}") + logging.info( + f"[qTest] Add test case {test_case_pid} to test suite {test_suite_id}" + ) get_test_case_endpoint = f"{self.api_base_url}/projects/{self.project_id}/test-cases/{test_case_pid}" exception = None for _ in range(self.max_retries): try: - response = requests.get(get_test_case_endpoint, headers=self.headers, timeout=self.default_timeout) + response = requests.get( + get_test_case_endpoint, + headers=self.headers, + timeout=self.default_timeout, + ) response.raise_for_status() - logging.info(f"[qTest] Test case {test_case_pid} added to test suite {test_suite_id}") + logging.info( + f"[qTest] Test case {test_case_pid} added to test suite {test_suite_id}" + ) break except requests.HTTPError as exc: exception = exc - logging.warning(f"[qTest] Failed to add test case {test_case_pid} to test suite {test_suite_id}. Retrying...") + logging.warning( + f"[qTest] Failed to add test case {test_case_pid} to test suite {test_suite_id}. Retrying..." + ) if exception is not None: raise exception @@ -139,54 +183,75 @@ def add_test_run_to_test_suite(self, test_case_pid, test_suite_id): add_test_case_to_test_suite_endpoint = f"{self.api_base_url}/projects/{self.project_id}/test-runs?parentId={test_suite_id}&parentType=test-suite" payload = { - "name": response.json()['name'], + "name": response.json()["name"], "test_case": { "id": test_case_pid, - } + }, } for _ in range(self.max_retries): try: - response = requests.post(add_test_case_to_test_suite_endpoint, headers=self.headers, data=json.dumps(payload), timeout=self.default_timeout) + response = requests.post( + add_test_case_to_test_suite_endpoint, + headers=self.headers, + data=json.dumps(payload), + timeout=self.default_timeout, + ) response.raise_for_status() - logging.info(f"[qTest] Test case {test_case_pid} added to test suite {test_suite_id}") - return response.json()['id'] + logging.info( + f"[qTest] Test case {test_case_pid} added to test suite {test_suite_id}" + ) + return response.json()["id"] except requests.HTTPError as exc: exception = exc - logging.warning(f"[qTest] Failed to add test case {test_case_pid} to test suite {test_suite_id}. Retrying...") + logging.warning( + f"[qTest] Failed to add test case {test_case_pid} to test suite {test_suite_id}. Retrying..." + ) raise exception - def update_test_run_status_in_test_suite(self, test_run_id, status, test_start_date, test_end_date): - logging.info(f"[qTest] Update test run {test_run_id} with status {status}") + def update_test_run_status_in_test_suite( + self, test_run_id, status, test_start_date, test_end_date + ): + logging.info( + f"[qTest] Update test run {test_run_id} with status {status}" + ) update_test_logs_endpoint = f"{self.api_base_url}/projects/{self.project_id}/test-runs/{test_run_id}/test-logs" payload = { - "exe_start_date": test_start_date.isoformat().split(".")[0] + "+00:00", + "exe_start_date": test_start_date.isoformat().split(".")[0] + + "+00:00", "exe_end_date": test_end_date.isoformat().split(".")[0] + "+00:00", - "status": { - "id": self.status_codes[status] - }, + "status": {"id": self.status_codes[status]}, } - if self.bundle_version != '': + if self.bundle_version != "": payload["properties"] = [ { "field_id": 103442, # bundle version - "field_value": self.bundle_version + "field_value": self.bundle_version, } ] exception = None for _ in range(self.max_retries): try: - response = requests.post(update_test_logs_endpoint, headers=self.headers, data=json.dumps(payload), timeout=self.default_timeout) + response = requests.post( + update_test_logs_endpoint, + headers=self.headers, + data=json.dumps(payload), + timeout=self.default_timeout, + ) response.raise_for_status() - logging.info(f"[qTest] Test run {test_run_id} status updated successfully.") + logging.info( + f"[qTest] Test run {test_run_id} status updated successfully." + ) return except requests.HTTPError as exc: exception = exc - logging.warning(f"[qTest] Failed to update test run {test_run_id} status. Retrying...") + logging.warning( + f"[qTest] Failed to update test run {test_run_id} status. Retrying..." + ) - raise exception \ No newline at end of file + raise exception diff --git a/tests/e2e-test-framework/framework/ssh.py b/tests/e2e-test-framework/framework/ssh.py index ed8adacbe..96f95e708 100644 --- a/tests/e2e-test-framework/framework/ssh.py +++ b/tests/e2e-test-framework/framework/ssh.py @@ -1,4 +1,5 @@ from typing import Any, List, Tuple + import logging import paramiko @@ -47,4 +48,4 @@ def exec(self, command: str) -> Tuple[str, List[Any]]: if len(error) > 0: logging.error(f"SSH command {command} failed: {error}") - return output, error \ No newline at end of file + return output, error diff --git a/tests/e2e-test-framework/framework/test_description_plugin.py b/tests/e2e-test-framework/framework/test_description_plugin.py index 65a6625d3..9020013bd 100644 --- a/tests/e2e-test-framework/framework/test_description_plugin.py +++ b/tests/e2e-test-framework/framework/test_description_plugin.py @@ -19,4 +19,4 @@ def pytest_runtest_logstart(self) -> None: self.terminal_reporter.write('\n\n') yield if self.desc: - self.terminal_reporter.write(f'\n{self.desc} \n') \ No newline at end of file + self.terminal_reporter.write(f'\n{self.desc} \n') diff --git a/tests/e2e-test-framework/framework/tunnel.py b/tests/e2e-test-framework/framework/tunnel.py deleted file mode 100644 index 9e77ebd29..000000000 --- a/tests/e2e-test-framework/framework/tunnel.py +++ /dev/null @@ -1,119 +0,0 @@ -import socket -import threading -import select -import logging -import time -import paramiko -import pytest - - -class Tunnel: - def __init__(self, jump_ip: str, target_ip: str, wiremock_ip: str, wiremock_port: int, username: str, password: str) -> None: - self.reverse_tunnel_port = 443 - self.ssh_port = 22 - self.enabled = False - self.accept_timeout = 2 - - self.jump_ip = jump_ip - self.target_ip = target_ip - self.wiremock_ip = wiremock_ip - self.wiremock_port = wiremock_port - self.username = username - self.password = password - - self.jump = None - self.target = None - self.acceptor = None - - def start(self, timeout: int = 300) -> None: - start_time = time.time() - time_interval = 10 - count = 0 - - while True: - count += time_interval - logging.info(f"Waiting for tunnel connection...{count}s") - time.sleep(time_interval) - - if time.time() - start_time > timeout: - pytest.fail(f"Failed to establish tunnel connection. Timeout {timeout}s exceeded.") - - try: - self.jump = paramiko.SSHClient() - self.jump.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.jump.connect(self.jump_ip, - username=self.username, - password=self.password) - - jump_transport = self.jump.get_transport() - jump_channel = jump_transport.open_channel("direct-tcpip", - (self.target_ip, self.ssh_port), - ('127.0.0.1', self.ssh_port)) - - self.target = paramiko.SSHClient() - self.target.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.target.connect(self.target_ip, - username=self.username, - password=self.password, - sock=jump_channel) - - target_transport = self.target.get_transport() - - self.enabled = True - self.acceptor = threading.Thread(target=self.__reverse_tunnel, - args=(self.reverse_tunnel_port, - self.wiremock_ip, - self.wiremock_port, - target_transport)) - self.acceptor.daemon = True - self.acceptor.start() - - logging.info("Tunnel connection established.") - return - - except Exception as exc: - logging.warning(f"Failed to establish tunnel connection. Reason: {str(exc)}") - - def stop(self) -> None: - self.enabled = False - self.acceptor.join() - self.target.close() - self.jump.close() - - def __reverse_tunnel(self, server_port: int, remote_host: str, remote_port: int, transport) -> None: - transport.request_port_forward("", server_port) - while self.enabled: - chan = transport.accept(self.accept_timeout) - if chan is None: - continue - thr = threading.Thread( - target=self.__reverse_tunnel_handler, args=(chan, remote_host, remote_port) - ) - thr.daemon = True - thr.start() - - def __reverse_tunnel_handler(self, chan, host: str, port: int) -> None: - sock = socket.socket() - try: - sock.connect((host, port)) - except Exception as exc: - logging.error(exc) - pytest.fail(f"Forwarding request to {host}:{port} failed: {exc}") - return - - logging.info(f"Connected! Tunnel open {chan.origin_addr} -> {chan.getpeername()} -> {host, port}") - while True: - read, _, _ = select.select([sock, chan], [], []) - if sock in read: - data = sock.recv(1024) - if len(data) == 0: - break - chan.send(data) - if chan in read: - data = chan.recv(1024) - if len(data) == 0: - break - sock.send(data) - chan.close() - sock.close() - logging.info(f"Tunnel closed from {chan.origin_addr}") \ No newline at end of file diff --git a/tests/e2e-test-framework/framework/utils.py b/tests/e2e-test-framework/framework/utils.py index f74e86bc2..81f9a4f4b 100644 --- a/tests/e2e-test-framework/framework/utils.py +++ b/tests/e2e-test-framework/framework/utils.py @@ -151,11 +151,10 @@ def is_pod_ready(self, pod_name: str, timeout=5): ) return False except Exception as exc: - - logging.warning( - f"Failed check if '{pod_name}' pod is ready. Reason: {str(exc)}" - ) - return False + logging.warning( + f"Failed check if '{pod_name}' pod is ready. Reason: {str(exc)}" + ) + return False def list_pods( self,