Skip to content

Update Ecosystems Mock Test #5073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cve_bin_tool/data_sources/osv_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,8 @@ async def get_cve_data(self):
await self.update_cve_entries()

return self.format_data(self.all_cve_entries), self.source_name
import pytest
from unittest.mock import AsyncMock, patch

from cve_bin_tool.data_sources.osv_source import OSV_Source

36 changes: 36 additions & 0 deletions test/test_osv_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest
from unittest.mock import patch
from cve_bin_tool.data_sources.osv_source import OSV_Source

@pytest.mark.asyncio
async def test_update_ecosystems_mocked():
# Create an instance of OSV_Source
osv = OSV_Source()

with patch("cve_bin_tool.data_sources.osv_source.aio_run_command") as mock_run:
# Mock gsutil ls and gsutil ls for all.zip
mock_run.side_effect = [
(b"gs://osv-vulnerabilities/ecosystem1/\n", b"", b""), # gsutil ls base
(b"gs://osv-vulnerabilities/ecosystem1/all.zip\n", b"", b""), # gsutil ls all.zip
(b"", b"", b"") # fallback mock result
]

await osv.update_ecosystems()

assert osv.ecosystems == ["ecosystem1"]

@pytest.mark.asyncio
@patch("cve_bin_tool.data_sources.osv_source.aio_run_command", new_callable=AsyncMock)
async def test_update_ecosystems(mock_aio_run_command):
# Mock gsutil output: two folders, one of them contains all.zip
mock_aio_run_command.side_effect = [
b"https://osv-vulnerabilities/python/\nhttps://osv-vulnerabilities/rust/\n",
b"https://osv-vulnerabilities/python/all.zip\n",
b"", # rust does not contain all.zip
]

osv = OSV_Source()
await osv.update_ecosystems()

assert "python" in osv.ecosystems
assert "rust" not in osv.ecosystems