Skip to content

Commit 899101c

Browse files
committed
Speed up test_upstream
Earlier, one batch of advisories was requested from updated_advisories method of the respective importers. This was inefficient as not all importers respect batching internally. Eventually, we wish to eliminate batches as well ( # 338 ). Now, the updated_advisories method of each importer is expected to create at least one Advisory object. If it does so, the importer is marked working. This brings major performance improvement. It is a necessity to improve this test as GitHub only allows 6 hrs of workflow time. Before: ~6hrs, now ~9 minutes Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent 7d3e3d3 commit 899101c

1 file changed

Lines changed: 26 additions & 23 deletions

File tree

vulnerabilities/tests/test_upstream.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,54 @@
1-
import pytest
1+
import inspect
22
from unittest.mock import patch
3-
from inspect import getmodule
4-
from inspect import getmro
3+
4+
import pytest
55

66
from vulnerabilities import importers
7-
from vulnerabilities.importer_yielder import IMPORTER_REGISTRY
87
from vulnerabilities.data_source import Advisory
8+
from vulnerabilities.importer_yielder import IMPORTER_REGISTRY
9+
10+
MAX_ADVISORIES = 1
911

10-
MAX_ADVISORIES = 2000
1112

12-
class MaxAdvisoriesCreatedInterrupt(Exception):
13+
class MaxAdvisoriesCreatedInterrupt(BaseException):
14+
# Inheriting BaseException is intentional because the function being tested might catch Exception
1315
pass
1416

1517

1618
@pytest.mark.webtest
1719
@pytest.mark.parametrize(
1820
("data_source", "config"),
19-
((data["data_source"], data["data_source_cfg"]) for data in IMPORTER_REGISTRY[0:1]),
21+
((data["data_source"], data["data_source_cfg"]) for data in IMPORTER_REGISTRY),
2022
)
2123
def test_updated_advisories(data_source, config):
2224
if not data_source == "GitHubAPIDataSource":
2325
data_src = getattr(importers, data_source)
2426
data_src = data_src(batch_size=MAX_ADVISORIES, config=config)
25-
module = getmodule(data_src).__name__
2627
advisory_counter = 0
2728

28-
def patched_advisory(**kwargs):
29+
def patched_advisory(*args, **kwargs):
2930
nonlocal advisory_counter
30-
ueatnhasnuth
31-
advisory_counter += 1
3231

3332
if advisory_counter >= MAX_ADVISORIES:
3433
raise MaxAdvisoriesCreatedInterrupt
3534

36-
x = Advisory(**kwargs)
37-
with open("/tmp/x") as f:
38-
f.write(x)
39-
return x
40-
41-
with patch(f"{module}.Advisory", side_effect=patched_advisory, create=True):
35+
advisory_counter += 1
36+
return Advisory(*args, **kwargs)
37+
38+
module = inspect.getmodule(data_src)
39+
module_members = [m[0] for m in inspect.getmembers(module)]
40+
advisory_class = f"{module.__name__}.Advisory"
41+
if "Advisory" not in module_members:
42+
advisory_class = "vulnerabilities.data_source.Advisory"
43+
44+
# Either
45+
# 1) Advisory class is successfully patched and MaxAdvisoriesCreatedInterrupt is thrown when
46+
# an importer tries to create an Advisory or
47+
# 2) Importer somehow bypasses the patch / handles BaseException internally, then
48+
# updated_advisories is required to return non zero advisories
49+
with patch(advisory_class, side_effect=patched_advisory):
4250
try:
4351
with data_src:
44-
print(list(data_src.updated_advisories()))
45-
for i in data_src.updated_advisories():
46-
print("EE:")
47-
print(i)
52+
assert len(list(data_src.updated_advisories())) > 0
4853
except MaxAdvisoriesCreatedInterrupt:
4954
pass
50-
51-

0 commit comments

Comments
 (0)