Skip to content

Commit

Permalink
tests/e2e_tests: fix pylint errors
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Jeny Sadadia committed Jan 3, 2024
1 parent 2adeddc commit 6a9fe87
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 119 deletions.
10 changes: 9 additions & 1 deletion tests/e2e_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@
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'
DB_NAME = 'kernelci'

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():
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e_tests/listen_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
},
)
)
Expand Down
77 changes: 10 additions & 67 deletions tests/e2e_tests/test_node_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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


Expand All @@ -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


Expand All @@ -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

Expand All @@ -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
2 changes: 1 addition & 1 deletion tests/e2e_tests/test_password_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e_tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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"
}
}
Expand All @@ -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"})
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e_tests/test_pubsub_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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,
Expand Down
29 changes: 5 additions & 24 deletions tests/e2e_tests/test_regression_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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(
Expand All @@ -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",
Expand All @@ -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 = (
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e_tests/test_subscribe_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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']
Expand All @@ -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']
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e_tests/test_unsubscribe_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
19 changes: 7 additions & 12 deletions tests/e2e_tests/test_user_group_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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'

Expand All @@ -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"})
)
Expand Down

0 comments on commit 6a9fe87

Please sign in to comment.