Esempio n. 1
0
class TestQueryAPIGetReturns200:
    _logger = get_logger(__name__)

    def test_query_image_by_content(self, add_alpine_latest_image):
        add_resp, api_conf = add_alpine_latest_image
        # Arbitrarily get the first package from the os content response
        first_package = (get_alpine_latest_image_os_content(
            get_image_id(add_resp), get_image_digest(add_resp),
            api_conf).body.get("content", [])[0].get("package", None))

        assert first_package is not None
        resp = http_get(["query", "images", "by_package"],
                        {"name": first_package},
                        config=api_conf)
        assert resp == APIResponse(200)

    def test_query_image_by_vuln(self, add_alpine_latest_image):
        """
        These tests seem to always return early because the system needs to be up and running for a while to gather
        feed data and analyze images. Good candidates for moving to an external test suite where an environment has
        been running for a while.
        """
        add_resp, api_conf = add_alpine_latest_image
        # Arbitrarily get the first vuln from the os vuln response
        try:
            first_vuln = (get_alpine_latest_image_os_vuln(
                get_image_id(add_resp), get_image_digest(add_resp),
                api_conf).body.get("vulnerabilities", [])[0].get("vuln", None))
        except IndexError:
            self._logger.warning(
                "No vulnerabilities found, cannot test query images by vulnerabilities"
            )
            return

        assert first_vuln is not None
        resp = http_get(
            ["query", "images", "by_vulnerability"],
            {"vulnerability_id": first_vuln},
            config=api_conf,
        )
        assert resp == APIResponse(200)

    def test_query_vuln(self, add_alpine_latest_image):
        add_resp, api_conf = add_alpine_latest_image
        # Arbitrarily get the first vuln from the os vuln response for alpine image
        try:
            first_vuln = (get_alpine_latest_image_os_vuln(
                get_image_id(add_resp), get_image_digest(add_resp),
                api_conf).body.get("vulnerabilities", [])[0].get("vuln", None))
        except IndexError:
            self._logger.warning(
                "No vulnerabilities found, cannot test query vulnerabilities")
            return

        assert first_vuln is not None
        resp = http_get(["query", "vulnerabilities"], {"id": first_vuln},
                        config=api_conf)
        assert resp == APIResponse(200)
Esempio n. 2
0
import pytest

from tests.functional.services.api.conftest import USER_API_CONFS
from tests.functional import get_logger
from tests.functional.services.utils.http_utils import (
    http_post,
    http_get,
    RequestFailedError,
    http_del,
)

ALPINE_LATEST_SUBSCRIPTION = {
    "subscription_key": "docker.io/alpine:latest",
    "subscription_type": "tag_update",
}
_logger = get_logger(__name__)


@pytest.fixture(scope="class", params=USER_API_CONFS)
def add_alpine_subscription(request):
    subscription = add_subscription(request.param)

    def remove_subscription():
        resp = http_del(
            ["subscriptions", subscription.get("subscription_id")], config=request.param
        )
        if resp.code != 200:
            raise RequestFailedError(resp.url, resp.code, resp.body)

    request.addfinalizer(remove_subscription)
    return subscription, request.param