Skip to content

Commit 019d1e0

Browse files
fix: test_update_ecosystems (#4929)
* fixes #4633 * fix: test_source_osv.py #4633 * Update test_source_osv.py to remove dwf and js
1 parent ab1c4fd commit 019d1e0

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

cve_bin_tool/data_sources/osv_source.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,17 @@ def __init__(
6161
self.session = None
6262

6363
async def update_ecosystems(self):
64-
"""Gets names of all ecosystems that OSV provides."""
65-
66-
ecosystems = []
67-
gsutil_path = find_gsutil() # use helper function
64+
"""Gets names of all ecosystems from OSV's ecosystems.txt."""
65+
ecosystems_url = (
66+
"https://osv-vulnerabilities.storage.googleapis.com/ecosystems.txt"
67+
)
6868

69-
# Inspect the list of files and folders at the top level in the GS bucket.
70-
stdout, _, _ = await aio_run_command([gsutil_path, "ls", self.gs_url])
71-
lines = stdout.split(b"\n")
72-
73-
# For each line in the directory listing determine if it is a folder that
74-
# contains all.zip.
75-
for line in lines:
76-
ecosystem_zip = line + b"all.zip"
77-
stdout, _, _ = await aio_run_command([gsutil_path, "ls", ecosystem_zip])
78-
if stdout.strip(b"\n") == ecosystem_zip:
79-
# Found a valid ecosystem
80-
ecosystem = str(line).split("/")[-2]
81-
ecosystems.append(ecosystem)
82-
83-
self.ecosystems = ecosystems
69+
async with aiohttp.ClientSession() as session:
70+
async with session.get(ecosystems_url, timeout=300) as response:
71+
response.raise_for_status()
72+
ecosystems_txt = await response.text()
73+
self.ecosystems = set(ecosystems_txt.strip().split("\n"))
74+
self.ecosystems.discard("[EMPTY]")
8475

8576
async def get_ecosystem(self, ecosystem_url, session, mode="json"):
8677
"""Fetches either a specific CVE or all.zip(containing all CVEs) file from an ecosystem."""

test/test_source_osv.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (C) 2022 Intel Corporation
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

4-
4+
import asyncio
55
import io
66
import shutil
77
import tempfile
@@ -169,21 +169,26 @@ def teardown_class(cls):
169169
@pytest.mark.skipif(not EXTERNAL_SYSTEM(), reason="Needs network connection.")
170170
async def test_update_ecosystems(self):
171171
await self.osv.update_ecosystems()
172-
173-
ecosystems_txt = make_http_requests(
174-
"text", url=self.ecosystems_url, timeout=300
175-
).strip("\n")
176-
expected_ecosystems = set(ecosystems_txt.split("\n"))
177-
178-
# Because ecosystems.txt does not contain the complete list, this must be
179-
# manually fixed up.
180-
expected_ecosystems.add("DWF")
181-
expected_ecosystems.add("JavaScript")
182-
183-
# Assert that there are no missing ecosystems
184-
assert all(x in self.osv.ecosystems for x in expected_ecosystems)
185-
# Assert that there are no extra ecosystems
186-
assert all(x in expected_ecosystems for x in self.osv.ecosystems)
172+
loop = asyncio.get_running_loop()
173+
ecosystems_txt = await loop.run_in_executor(
174+
None,
175+
lambda: make_http_requests("text", url=self.ecosystems_url, timeout=300),
176+
)
177+
expected_top_level = set(ecosystems_txt.strip().split("\n"))
178+
179+
# Validate parent ecosystems
180+
code_parent_ecosystems = {e.split(":")[0] for e in self.osv.ecosystems}
181+
expected_top_level.discard("[EMPTY]")
182+
missing_parents = expected_top_level - code_parent_ecosystems
183+
extra_parents = code_parent_ecosystems - expected_top_level
184+
185+
if missing_parents or extra_parents:
186+
error_msg = []
187+
if missing_parents:
188+
error_msg.append(f"Missing parent ecosystems: {missing_parents}")
189+
if extra_parents:
190+
error_msg.append(f"Unexpected parent ecosystems: {extra_parents}")
191+
pytest.fail("\n".join(error_msg))
187192

188193
@pytest.mark.asyncio
189194
@pytest.mark.skipif(not EXTERNAL_SYSTEM(), reason="Needs network connection.")

0 commit comments

Comments
 (0)