From 67fc5ee668f08fe8160a563c661654612448932d Mon Sep 17 00:00:00 2001 From: Eric Fahlgren Date: Fri, 27 Sep 2024 08:59:53 -0700 Subject: [PATCH] tests: improve coverage and reporting Add tests for missing functions with better response checking. Add context output for html report. Signed-off-by: Eric Fahlgren --- pyproject.toml | 11 ++++++++ tests/conftest.py | 2 ++ tests/test_api.py | 7 +++++ tests/test_util.py | 69 +++++++++++++++++++++++++++++++++------------- 4 files changed, 70 insertions(+), 19 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bfad9bc0..7ed577e8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,17 @@ isort = "^5.13.2" fakeredis = "^2.24.1" +[tool.coverage.run] +dynamic_context = "test_function" + +[tool.coverage.report] +precision = 1 + +[tool.coverage.html] +show_contexts = "true" +title = "ASU Server Regression Test Coverage" + + [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" diff --git a/tests/conftest.py b/tests/conftest.py index eddecbb9..15b1d8f8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -33,6 +33,8 @@ def redis_load_mock_data(redis): redis.sadd("packages:21.02:21.02.7:ath79/generic", "vim", "tmux") redis.sadd("packages:21.02:21.02.7:x86/64", "vim", "tmux") + redis.set("revision:21.02.7:x86/64", "r16847-f8282da11e") + redis.hset( "mapping:1.2:1.2.3:testtarget/testsubtarget", mapping={"testvendor,testprofile": "testprofile"}, diff --git a/tests/test_api.py b/tests/test_api.py index bde07ade..b00f28a6 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -719,6 +719,13 @@ def test_api_version_update(app): assert response.status_code == 403 +def test_api_revision(client): + response = client.get("/api/v1/revision/21.02.7/x86/64", follow_redirects=False) + assert response.status_code == 200 + data = response.json() + assert data["revision"] == "r16847-f8282da11e" + + def test_api_stats(client): response = client.get("/api/v1/stats", follow_redirects=False) assert response.status_code == 200 diff --git a/tests/test_util.py b/tests/test_util.py index d47140e9..42547f71 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -6,6 +6,7 @@ from asu.build_request import BuildRequest from asu.util import ( + httpx, # For monkeypatching. check_manifest, diff_packages, fingerprint_pubkey_usign, @@ -15,7 +16,8 @@ get_podman, get_request_hash, get_str_hash, - parse_packages_versions, + parse_packages_file, + parse_feeds_conf, run_cmd, verify_usign, ) @@ -100,24 +102,29 @@ def test_get_version_container_tag(): assert get_container_version_tag("SNAPP-SNAPSHOT") == "openwrt-SNAPP" -def test_get_packages_versions(): - text = ( - "Package: libusb-1.0-0\n" - "ABIVersion: -0\n" - "Version: 1.2.3\n" - "Architecture: x86_64\n" - "\n" - "Package: libpython-3.3-3\n" - "ABIVersion: -3\n" - "Version: 1.2.3\n" - "\n" - "Package: bort\n" - "Version: 9.9.9\n" - "\n" - "\n" # Add two more to fake malformed input. - "\n" - ) - index = parse_packages_versions(text) +def test_get_packages_versions(monkeypatch): + class Response: + status_code = 200 + text = ( + "Package: libusb-1.0-0\n" + "ABIVersion: -0\n" + "Version: 1.2.3\n" + "Architecture: x86_64\n" + "\n" + "Package: libpython-3.3-3\n" + "ABIVersion: -3\n" + "Version: 1.2.3\n" + "\n" + "Package: bort\n" + "Version: 9.9.9\n" + "\n" + "\n" # Add two more to fake malformed input. + "\n" + ) + + monkeypatch.setattr(httpx, "get", lambda url: Response()) + + index = parse_packages_file("httpx://fake_url") packages = index["packages"] assert index["architecture"] == "x86_64" @@ -126,6 +133,30 @@ def test_get_packages_versions(): assert packages["libpython-3.3"] == "1.2.3" assert packages["bort"] == "9.9.9" + Response.status_code = 404 + index = parse_packages_file("abc://fake") + assert index == {} + + +def test_get_feeds(monkeypatch): + class Response: + status_code = 200 + text = ( + "src-git packages https://git.openwrt.org/feed/packages.git^b1635b8\n" + "src-git luci https://git.openwrt.org/project/luci.git^63d8b79\n" + ) + + monkeypatch.setattr(httpx, "get", lambda url: Response()) + + feeds = parse_feeds_conf("httpx://fake_url") + assert len(feeds) == 2 + assert feeds[0] == "packages" + assert feeds[1] == "luci" + + Response.status_code = 404 + feeds = parse_feeds_conf("httpx://fake_url") + assert feeds == [] + def test_check_manifest(): assert check_manifest({"test": "1.0"}, {"test": "1.0"}) is None