From 6a9fe871e29361ca0738f8d458d099405e9b66cb Mon Sep 17 00:00:00 2001 From: Jeny Sadadia Date: Wed, 3 Jan 2024 18:19:03 +0530 Subject: [PATCH] tests/e2e_tests: fix pylint errors Fix below errors reported by `pylint`: - fix `duplicate-code` error for duplicate fields of node and regression objects in multiple tests - disable `no-member` error for pytest variables like `pytest.BEARER_TOKEN` - fix `trailing-whitespace` error Signed-off-by: Jeny Sadadia --- tests/e2e_tests/conftest.py | 10 ++- tests/e2e_tests/listen_handler.py | 4 +- tests/e2e_tests/test_node_handler.py | 77 +++------------------ tests/e2e_tests/test_password_handler.py | 2 +- tests/e2e_tests/test_pipeline.py | 6 +- tests/e2e_tests/test_pubsub_handler.py | 4 +- tests/e2e_tests/test_regression_handler.py | 29 ++------ tests/e2e_tests/test_subscribe_handler.py | 6 +- tests/e2e_tests/test_unsubscribe_handler.py | 8 +-- tests/e2e_tests/test_user_group_handler.py | 19 ++--- 10 files changed, 46 insertions(+), 119 deletions(-) diff --git a/tests/e2e_tests/conftest.py b/tests/e2e_tests/conftest.py index 4437adee4..e7a79e806 100644 --- a/tests/e2e_tests/conftest.py +++ b/tests/e2e_tests/conftest.py @@ -13,6 +13,7 @@ from fastapi.testclient import TestClient from api.main import versioned_app +from api.models import Node, Regression BASE_URL = 'http://api:8000/latest/' DB_URL = 'mongodb://db:27017' @@ -20,7 +21,14 @@ db_client = AsyncIOMotorClient(DB_URL) db = db_client[DB_NAME] - +node_model_fields = set(Node.__fields__.keys()) +regression_model_fields = set(Regression.__fields__.keys()) +paginated_response_keys = { + 'items', + 'total', + 'limit', + 'offset', +} @pytest.fixture(scope='session') def test_client(): diff --git a/tests/e2e_tests/listen_handler.py b/tests/e2e_tests/listen_handler.py index 94e99a733..9b5a7b883 100644 --- a/tests/e2e_tests/listen_handler.py +++ b/tests/e2e_tests/listen_handler.py @@ -6,8 +6,8 @@ """Helper function for KernelCI API listen handler""" -import pytest import asyncio +import pytest def create_listen_task(test_async_client, subscription_id): @@ -22,7 +22,7 @@ def create_listen_task(test_async_client, subscription_id): listen_path, headers={ "Accept": "application/json", - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, ) ) diff --git a/tests/e2e_tests/test_node_handler.py b/tests/e2e_tests/test_node_handler.py index eaeb1e7c9..2de3ae858 100644 --- a/tests/e2e_tests/test_node_handler.py +++ b/tests/e2e_tests/test_node_handler.py @@ -9,6 +9,8 @@ import json import pytest +from .conftest import node_model_fields, paginated_response_keys + async def create_node(test_async_client, node): """ @@ -21,30 +23,12 @@ async def create_node(test_async_client, node): "node", headers={ "Accept": "application/json", - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, data=json.dumps(node) ) assert response.status_code == 200 - assert response.json().keys() == { - 'id', - 'artifacts', - 'created', - 'data', - 'group', - 'holdoff', - 'kind', - 'name', - 'owner', - 'path', - 'parent', - 'result', - 'revision', - 'state', - 'timeout', - 'updated', - 'user_groups', - } + assert response.json().keys() == node_model_fields return response @@ -59,29 +43,11 @@ async def get_node_by_id(test_async_client, node_id): f"node/{node_id}", headers={ "Accept": "application/json", - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, ) assert response.status_code == 200 - assert response.json().keys() == { - 'id', - 'artifacts', - 'created', - 'data', - 'group', - 'holdoff', - 'kind', - 'name', - 'owner', - 'path', - 'parent', - 'result', - 'revision', - 'state', - 'timeout', - 'updated', - 'user_groups', - } + assert response.json().keys() == node_model_fields return response @@ -98,16 +64,11 @@ async def get_node_by_attribute(test_async_client, params): params=params, headers={ "Accept": "application/json", - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, ) assert response.status_code == 200 - assert response.json().keys() == { - 'items', - 'total', - 'limit', - 'offset', - } + assert response.json().keys() == paginated_response_keys assert response.json()['total'] >= 0 return response @@ -123,27 +84,9 @@ async def update_node(test_async_client, node): f"node/{node['id']}", headers={ "Accept": "application/json", - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, data=json.dumps(node) ) assert response.status_code == 200 - assert response.json().keys() == { - 'id', - 'artifacts', - 'created', - 'data', - 'group', - 'holdoff', - 'kind', - 'name', - 'owner', - 'path', - 'parent', - 'result', - 'revision', - 'state', - 'timeout', - 'updated', - 'user_groups', - } + assert response.json().keys() == node_model_fields diff --git a/tests/e2e_tests/test_password_handler.py b/tests/e2e_tests/test_password_handler.py index 19679b7a5..8be37fc24 100644 --- a/tests/e2e_tests/test_password_handler.py +++ b/tests/e2e_tests/test_password_handler.py @@ -27,7 +27,7 @@ async def test_password_endpoint(test_async_client): "user/me", headers={ "Accept": "application/json", - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, data=json.dumps( { diff --git a/tests/e2e_tests/test_pipeline.py b/tests/e2e_tests/test_pipeline.py index 56176c6f4..574131929 100644 --- a/tests/e2e_tests/test_pipeline.py +++ b/tests/e2e_tests/test_pipeline.py @@ -38,7 +38,7 @@ async def test_node_pipeline(test_async_client): # Create Task to listen pubsub event on 'node' channel task_listen = create_listen_task(test_async_client, - pytest.node_channel_subscription_id) + pytest.node_channel_subscription_id) # pylint: disable=no-member # Create a node node = { @@ -49,7 +49,7 @@ async def test_node_pipeline(test_async_client): "url": "https://git.kernel.org/pub/scm/linux/kernel/git/" "torvalds/linux.git", "branch": "master", - "commit": "2a987e65025e2b79c6d453b78cb5985ac6e5eb26", + "commit": "2a987e65025e2b79c6d453b78cb5985ac6e5eb28", "describe": "v5.16-rc4-31-g2a987e65025e" } } @@ -71,7 +71,7 @@ async def test_node_pipeline(test_async_client): # Create Task to listen 'updated' event on 'node' channel task_listen = create_listen_task(test_async_client, - pytest.node_channel_subscription_id) + pytest.node_channel_subscription_id) # pylint: disable=no-member # Update node.state node.update({"state": "done"}) diff --git a/tests/e2e_tests/test_pubsub_handler.py b/tests/e2e_tests/test_pubsub_handler.py index 22250fff0..d53b0299a 100644 --- a/tests/e2e_tests/test_pubsub_handler.py +++ b/tests/e2e_tests/test_pubsub_handler.py @@ -24,7 +24,7 @@ async def test_pubsub_handler(test_async_client): """ # Create Task to listen pubsub event on 'test_channel' channel task_listen = create_listen_task(test_async_client, - pytest.test_channel_subscription_id) + pytest.test_channel_subscription_id) # pylint: disable=no-member # Created and publish CloudEvent attributes = { @@ -34,7 +34,7 @@ async def test_pubsub_handler(test_async_client): data = {"message": "Test message"} event = CloudEvent(attributes, data) headers, body = to_structured(event) - headers['Authorization'] = f"Bearer {pytest.BEARER_TOKEN}" + headers['Authorization'] = f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member response = await test_async_client.post( "publish/test_channel", headers=headers, diff --git a/tests/e2e_tests/test_regression_handler.py b/tests/e2e_tests/test_regression_handler.py index 90806b78a..5689d1203 100644 --- a/tests/e2e_tests/test_regression_handler.py +++ b/tests/e2e_tests/test_regression_handler.py @@ -9,6 +9,7 @@ import json import pytest +from .conftest import regression_model_fields from .test_node_handler import create_node, get_node_by_attribute @@ -23,31 +24,12 @@ async def create_regression(test_async_client, regression_node): "regression", headers={ "Accept": "application/json", - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, data=json.dumps(regression_node) ) assert response.status_code == 200 - assert response.json().keys() == { - 'id', - 'artifacts', - 'created', - 'data', - 'group', - 'holdoff', - 'kind', - 'name', - 'owner', - 'path', - 'parent', - 'result', - 'revision', - 'regression_data', - 'state', - 'timeout', - 'updated', - 'user_groups', - } + assert response.json().keys() == regression_model_fields @pytest.mark.dependency( @@ -73,12 +55,12 @@ async def test_regression_handler(test_async_client): # Create a 'kver' passed node passed_node = { - "name": "passed_kver", + "name": "kver", "path": ["checkout", "kver"], "group": "kver", "parent": checkout_node["id"], "revision": { - "tree": "mainline", + "tree": "staging-next", "url": "https://git.kernel.org/pub/scm/linux/kernel/git/" "torvalds/linux.git", "branch": "master", @@ -95,7 +77,6 @@ async def test_regression_handler(test_async_client): # Create a 'kver' failed node failed_node = passed_node.copy() - failed_node["name"] = "failed_kver" failed_node["result"] = "fail" failed_node_obj = ( diff --git a/tests/e2e_tests/test_subscribe_handler.py b/tests/e2e_tests/test_subscribe_handler.py index 4413179ff..be40acb59 100644 --- a/tests/e2e_tests/test_subscribe_handler.py +++ b/tests/e2e_tests/test_subscribe_handler.py @@ -23,7 +23,7 @@ def test_subscribe_node_channel(test_client): response = test_client.post( "subscribe/node", headers={ - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, ) pytest.node_channel_subscription_id = response.json()['id'] @@ -46,7 +46,7 @@ def test_subscribe_test_channel(test_client): response = test_client.post( "subscribe/test_channel", headers={ - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, ) pytest.test_channel_subscription_id = response.json()['id'] @@ -70,7 +70,7 @@ def test_subscribe_user_group_channel(test_client): response = test_client.post( "subscribe/user_group", headers={ - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, ) pytest.user_group_channel_subscription_id = response.json()['id'] diff --git a/tests/e2e_tests/test_unsubscribe_handler.py b/tests/e2e_tests/test_unsubscribe_handler.py index c24b0f467..fd478f0cd 100644 --- a/tests/e2e_tests/test_unsubscribe_handler.py +++ b/tests/e2e_tests/test_unsubscribe_handler.py @@ -21,9 +21,9 @@ def test_unsubscribe_node_channel(test_client): HTTP Response Code 200 OK """ response = test_client.post( - f"unsubscribe/{pytest.node_channel_subscription_id}", + f"unsubscribe/{pytest.node_channel_subscription_id}", # pylint: disable=no-member headers={ - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, ) assert response.status_code == 200 @@ -41,9 +41,9 @@ def test_unsubscribe_test_channel(test_client): HTTP Response Code 200 OK """ response = test_client.post( - f"unsubscribe/{pytest.test_channel_subscription_id}", + f"unsubscribe/{pytest.test_channel_subscription_id}", # pylint: disable=no-member headers={ - "Authorization": f"Bearer {pytest.BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.BEARER_TOKEN}" # pylint: disable=no-member }, ) assert response.status_code == 200 diff --git a/tests/e2e_tests/test_user_group_handler.py b/tests/e2e_tests/test_user_group_handler.py index ce4bbe95f..c1e292559 100644 --- a/tests/e2e_tests/test_user_group_handler.py +++ b/tests/e2e_tests/test_user_group_handler.py @@ -5,13 +5,13 @@ """End-to-end test functions for KernelCI API user group handler""" +import json import pytest from cloudevents.http import from_json -import json +from e2e_tests.conftest import db_create, paginated_response_keys from api.models import UserGroup from api.db import Database -from e2e_tests.conftest import db_create from .listen_handler import create_listen_task @@ -45,12 +45,7 @@ async def test_get_user_group(test_async_client): "groups", ) assert response.status_code == 200 - assert response.json().keys() == { - 'items', - 'total', - 'limit', - 'offset', - } + assert response.json().keys() == paginated_response_keys assert response.json()['total'] == 1 assert response.json()['items'][0]['name'] == 'admin' @@ -71,17 +66,17 @@ async def test_create_and_get_user_group(test_async_client): The GET '/group/{group_id}' will be sent using group id from event data to get newly created user group object. """ - + # Create Task to listen pubsub event on 'user_group' channel task_listen = create_listen_task(test_async_client, - pytest.user_group_channel_subscription_id) - + pytest.user_group_channel_subscription_id) # pylint: disable=no-member + # Create a user group response = await test_async_client.post( "group", headers={ "Accept": "application/json", - "Authorization": f"Bearer {pytest.ADMIN_BEARER_TOKEN}" + "Authorization": f"Bearer {pytest.ADMIN_BEARER_TOKEN}" # pylint: disable=no-member }, data=json.dumps({"name": "kernelci"}) )