Skip to content

Commit ec360c5

Browse files
committed
chore: update async tests
1 parent d01feb2 commit ec360c5

7 files changed

Lines changed: 43 additions & 29 deletions

File tree

tests/listener_tests/conftest.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
# -*- coding: utf-8 -*-
22
# pylint:disable=missing-docstring,redefined-outer-name
3-
import asyncio
4-
53
import psycopg2
64
import pytest
75

86

9-
@pytest.fixture(scope="class")
10-
def event_loop():
11-
return asyncio.new_event_loop()
12-
13-
147
@pytest.fixture(scope="module")
158
def pg_db_conn(pg_db_mod):
169
"""Returns connection to PostgreSQL database."""

tests/notificator_tests/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Fixtures for notificator tests
33
"""
44

5+
import asyncio
6+
57
import asyncpg
68
import pytest
79
import pytest_asyncio
@@ -13,14 +15,14 @@
1315

1416

1517
@pytest_asyncio.fixture(loop_scope="function", scope="function")
16-
async def asyncpg_pool(event_loop, pg_db_func):
18+
async def asyncpg_pool(pg_db_func):
1719
"""Creates asyncpg pool fixture"""
1820
dsn = pg_db_func.dsn()
1921
dsn_str = f"postgres://{dsn['user']}@{dsn['host']}:{dsn['port']}/{dsn['database']}"
2022

2123
pool = await asyncpg.create_pool(
2224
dsn=dsn_str,
23-
loop=event_loop,
25+
loop=asyncio.get_running_loop(),
2426
)
2527
yield pool
2628
await pool.close()

tests/notificator_tests/test_app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import asyncio
66

7+
import pytest
8+
79
from notificator.notificator import NOTIFICATOR_PERIOD
810
from notificator.notificator import NotificationType
911
from notificator.notificator import NotificatorConditions
@@ -23,6 +25,7 @@ async def _init_notificator(self, pool):
2325
await queue.init()
2426
return conditions, queue
2527

28+
@pytest.mark.asyncio()
2629
async def test_cache_handler(self, asyncpg_pool, http_client):
2730
"""Test cache refresh handler"""
2831
_, queue = await self._init_notificator(asyncpg_pool)

tests/notificator_tests/test_notificator.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Notificator unit tests
33
"""
44

5+
import asyncio
56
import json
67

78
import pytest
@@ -65,9 +66,10 @@ async def _build_notificator(pool, loop):
6566
return notificator
6667

6768
@staticmethod
68-
@pytest.mark.asyncio
69-
async def test_notificator_new(asyncpg_pool, event_loop, monkeypatch):
69+
@pytest.mark.asyncio(loop_scope="function")
70+
async def test_notificator_new(asyncpg_pool, monkeypatch):
7071
"""Tests new sys_vulns from evaluator to notificator"""
72+
event_loop = asyncio.get_running_loop()
7173
# update CVE-2014-1, CVE-2017-1, CVE-2017-6 to be fresh
7274
# leave out CVE-2016-1 to not be fresh because it has exploits
7375
async with asyncpg_pool.acquire() as conn:
@@ -100,9 +102,10 @@ async def test_notificator_new(asyncpg_pool, event_loop, monkeypatch):
100102
NotificatorQueue.delete()
101103

102104
@staticmethod
103-
@pytest.mark.asyncio
104-
async def test_notificator_mitigated(asyncpg_pool, event_loop, monkeypatch):
105+
@pytest.mark.asyncio(loop_scope="function")
106+
async def test_notificator_mitigated(asyncpg_pool, monkeypatch):
105107
"""Tests mitigated sys_vulns from evaluator to notificator"""
108+
event_loop = asyncio.get_running_loop()
106109
notificator = await TestNotificator._build_notificator(asyncpg_pool, event_loop)
107110
monkeypatch.setattr(json, "loads", lambda _: EVALUATOR_RESULT_MIT_VULNS)
108111

@@ -121,9 +124,10 @@ async def test_notificator_mitigated(asyncpg_pool, event_loop, monkeypatch):
121124
NotificatorQueue.delete()
122125

123126
@staticmethod
124-
@pytest.mark.asyncio
125-
async def test_notificator_unmitigated(asyncpg_pool, event_loop, monkeypatch):
127+
@pytest.mark.asyncio(loop_scope="function")
128+
async def test_notificator_unmitigated(asyncpg_pool, monkeypatch):
126129
"""Tests unmitigated sys_vulns from evaluator to notificator"""
130+
event_loop = asyncio.get_running_loop()
127131
# update CVE-2014-1, CVE-2017-1, CVE-2017-6, CVE-2017-8 to be fresh
128132
# leave out CVE-2016-1 to not be fresh because it has exploits
129133
async with asyncpg_pool.acquire() as conn:

tests/notificator_tests/test_notificator_queue.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"""
33
Notificator Queue unit tests
44
"""
5+
import asyncio
6+
57
import pytest
68

79
from common.peewee_model import NotificationType
@@ -33,9 +35,10 @@ async def _notified_users_cnt(pool):
3335
return res[0]
3436

3537
@staticmethod
36-
@pytest.mark.asyncio
37-
async def test_queue_empty(asyncpg_pool, event_loop):
38+
@pytest.mark.asyncio(loop_scope="function")
39+
async def test_queue_empty(asyncpg_pool):
3840
"""Tests queue single run when its empty"""
41+
event_loop = asyncio.get_running_loop()
3942
queue = await TestNotificatorQueue._build_queue(asyncpg_pool, event_loop)
4043
await queue._process_queue()
4144
# Queues should be empty
@@ -44,9 +47,10 @@ async def test_queue_empty(asyncpg_pool, event_loop):
4447
NotificatorQueue.delete()
4548

4649
@staticmethod
47-
@pytest.mark.asyncio
48-
async def test_queue_nonexisting(asyncpg_pool, event_loop):
50+
@pytest.mark.asyncio(loop_scope="function")
51+
async def test_queue_nonexisting(asyncpg_pool):
4952
"""Tests queue run with non existing sys_vuln"""
53+
event_loop = asyncio.get_running_loop()
5054
queue = await TestNotificatorQueue._build_queue(asyncpg_pool, event_loop)
5155
# add nonexisting sys vuln to queue
5256
queue.notif_cves_queue[1337] = QueueItem("CVE-NONEXISTING", 1337, 0, "0", [NotificationType.CVSS_NOTIFICATION])
@@ -60,9 +64,10 @@ async def test_queue_nonexisting(asyncpg_pool, event_loop):
6064
NotificatorQueue.delete()
6165

6266
@staticmethod
63-
@pytest.mark.asyncio
64-
async def test_queue_non_advisor_eval(asyncpg_pool, event_loop):
67+
@pytest.mark.asyncio(loop_scope="function")
68+
async def test_queue_non_advisor_eval(asyncpg_pool):
6569
"""Tests queue run with not yet advisor evaluated system"""
70+
event_loop = asyncio.get_running_loop()
6671
queue = await TestNotificatorQueue._build_queue(asyncpg_pool, event_loop)
6772

6873
# insert into db system which is not advisor evaluated and has single CVE
@@ -88,9 +93,10 @@ async def test_queue_non_advisor_eval(asyncpg_pool, event_loop):
8893
NotificatorQueue.delete()
8994

9095
@staticmethod
91-
@pytest.mark.asyncio
92-
async def test_queue_mitigated(asyncpg_pool, event_loop):
96+
@pytest.mark.asyncio(loop_scope="function")
97+
async def test_queue_mitigated(asyncpg_pool):
9398
"""Test queue run but cve gets mitigated"""
99+
event_loop = asyncio.get_running_loop()
94100
queue = await TestNotificatorQueue._build_queue(asyncpg_pool, event_loop)
95101

96102
# insert into db system which is not vulnerable anymore, based on the advisor listener
@@ -117,9 +123,10 @@ async def test_queue_mitigated(asyncpg_pool, event_loop):
117123
NotificatorQueue.delete()
118124

119125
@staticmethod
120-
@pytest.mark.asyncio
121-
async def test_queue_valid(asyncpg_pool, event_loop):
126+
@pytest.mark.asyncio(loop_scope="function")
127+
async def test_queue_valid(asyncpg_pool):
122128
"""Test queue run with valid system vulnerabilities, which should be notified"""
129+
event_loop = asyncio.get_running_loop()
123130
msgs = []
124131

125132
def _send_kafka_notif_mock(self, acc_id, event_type, *_, **__):
@@ -156,9 +163,10 @@ def _send_kafka_notif_mock(self, acc_id, event_type, *_, **__):
156163
NotificatorQueue.delete()
157164

158165
@staticmethod
159-
@pytest.mark.asyncio
160-
async def test_queue_unknown_cve(asyncpg_pool, event_loop):
166+
@pytest.mark.asyncio(loop_scope="function")
167+
async def test_queue_unknown_cve(asyncpg_pool):
161168
"""Test queue run with unknown cve vulnerabilities yet"""
169+
event_loop = asyncio.get_running_loop()
162170
queue = await TestNotificatorQueue._build_queue(asyncpg_pool, event_loop)
163171

164172
# insert into db system which vulnerable to CVE which vulnerability does not know yet

tests/taskomatic_tests/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# pylint:disable=missing-docstring,redefined-outer-name
33
import psycopg2
44
import pytest
5+
import pytest_asyncio
56
from aiohttp import test_utils
67
from aiohttp import web
78

@@ -27,7 +28,7 @@ def db_cleanup():
2728
request.addfinalizer(db_cleanup)
2829

2930

30-
@pytest.fixture
31+
@pytest_asyncio.fixture()
3132
async def taskomatic_server():
3233
app = taskomatic.TaskomaticApp()
3334
runner = web.AppRunner(app.app)
@@ -41,7 +42,7 @@ async def taskomatic_server():
4142
await runner.cleanup()
4243

4344

44-
@pytest.fixture()
45+
@pytest_asyncio.fixture()
4546
async def http_client(taskomatic_server, aiohttp_client):
4647
client = await aiohttp_client(taskomatic_server)
4748
yield client

tests/taskomatic_tests/test_taskomatic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
import asyncio
66

7+
import pytest
78
from psycopg2.extensions import connection
89

910
import taskomatic.jobs.common as tjc
@@ -90,11 +91,13 @@ def run_forever(self, *_, **__):
9091
class TestTaskomaticApp:
9192
"""Test taskomatic App handlers"""
9293

94+
@pytest.mark.asyncio
9395
async def test_run_job(self, http_client):
9496
"""Test run job handler"""
9597
response = await http_client.put("/api/v1/run/stale_systems", data={})
9698
assert response.status == 200
9799

100+
@pytest.mark.asyncio
98101
async def test_run_job_wrong(self, http_client):
99102
"""Test run job handler with wrong job"""
100103
response = await http_client.put("/api/v1/run/nonexisting_job", data={})

0 commit comments

Comments
 (0)