Ejemplo n.º 1
0
def test_to_json(mock_top_level_purl, app, package, subpath):
    request = Request()
    cm = content_manifest.ContentManifest(request)

    image_contents = []
    if package:
        pkg = Package.from_json(package)
        request_package = RequestPackage(package=pkg, subpath=subpath)
        request.request_packages.append(request_package)
        content = {
            "purl": mock_top_level_purl.return_value,
            "dependencies": [],
            "sources": [],
        }
        image_contents.append(content)

    expected = {
        "metadata": {
            "icm_version": 1,
            "icm_spec": content_manifest.JSON_SCHEMA_URL,
            "image_layer_index": -1,
        },
        "image_contents": image_contents,
    }
    assert cm.to_json() == expected

    if package:
        mock_top_level_purl.assert_called_once_with(request, subpath=subpath)
Ejemplo n.º 2
0
def test_process_pip(default_request, default_toplevel_purl):
    pkg = Package.from_json({
        "name": "requests",
        "type": "pip",
        "version": "2.24.0"
    })
    pkg.id = 1
    expected_purl = default_toplevel_purl

    dep_commit_id = "58c88e4952e95935c0dd72d4a24b0c44f2249f5b"
    dep = Package.from_json({
        "name":
        "cnr-server",
        "type":
        "pip",
        "version":
        f"git+https://github.com/quay/appr@{dep_commit_id}",
    })
    dep.id = 2
    expected_dep_purl = f"pkg:github/quay/appr@{dep_commit_id}"

    src = Package.from_json({
        "name": "setuptools",
        "type": "pip",
        "version": "49.1.1"
    })
    src.id = 3
    src.dev = True
    expected_src_purl = "pkg:pypi/[email protected]"

    cm = content_manifest.ContentManifest(default_request)

    # emulate to_json behavior to setup internal packages cache
    cm._pip_data.setdefault(pkg.id, {
        "purl": expected_purl,
        "dependencies": [],
        "sources": []
    })

    cm.process_pip_package(pkg, dep)
    cm.process_pip_package(pkg, src)

    expected_contents = {
        pkg.id: {
            "purl": expected_purl,
            "dependencies": [{
                "purl": expected_dep_purl
            }],
            "sources": [{
                "purl": expected_dep_purl
            }, {
                "purl": expected_src_purl
            }],
        }
    }

    assert cm._pip_data
    assert pkg.id in cm._pip_data
    assert cm._pip_data == expected_contents
Ejemplo n.º 3
0
def test_process_yarn(default_request, default_toplevel_purl):
    pkg = Package.from_json({
        "name": "grc-ui",
        "type": "yarn",
        "version": "1.0.0"
    })
    pkg.id = 1
    expected_purl = default_toplevel_purl

    dep_commit_id = "7762177aacfb1ddf5ca45cebfe8de1da3b24f0ff"
    dep = Package.from_json({
        "name":
        "security-middleware",
        "type":
        "yarn",
        "version":
        f"github:open-cluster-management/security-middleware#{dep_commit_id}",
    })
    dep.id = 2
    expected_dep_purl = f"pkg:github/open-cluster-management/security-middleware@{dep_commit_id}"

    src = Package.from_json({
        "name": "@types/events",
        "type": "yarn",
        "version": "3.0.0"
    })
    src.id = 3
    src.dev = True
    expected_src_purl = "pkg:npm/%40types/[email protected]"

    cm = content_manifest.ContentManifest(default_request)

    # emulate to_json behavior to setup internal packages cache
    cm._yarn_data.setdefault(pkg.id, {
        "purl": expected_purl,
        "dependencies": [],
        "sources": []
    })

    cm.process_yarn_package(pkg, dep)
    cm.process_yarn_package(pkg, src)

    expected_contents = {
        pkg.id: {
            "purl": expected_purl,
            "dependencies": [{
                "purl": expected_dep_purl
            }],
            "sources": [{
                "purl": expected_dep_purl
            }, {
                "purl": expected_src_purl
            }],
        }
    }

    assert cm._yarn_data
    assert pkg.id in cm._yarn_data
    assert cm._yarn_data == expected_contents
Ejemplo n.º 4
0
def test_process_go(default_request):
    pkg = Package.from_json({
        "name": "example.com/org/project",
        "type": "go-package",
        "version": "1.1.1"
    })
    pkg.id = 1
    expected_purl = "pkg:golang/example.com%2Forg%[email protected]"

    dep = Package.from_json({
        "name": "example.com/org/project/lib",
        "type": "go-package",
        "version": "2.2.2"
    })
    dep.id = 2
    expected_dep_purl = "pkg:golang/example.com%2Forg%2Fproject%[email protected]"

    src = Package.from_json({
        "name": "example.com/anotherorg/project",
        "type": "gomod",
        "version": "3.3.3"
    })
    src.id = 3
    expected_src_purl = "pkg:golang/example.com%2Fanotherorg%[email protected]"

    cm = content_manifest.ContentManifest(default_request)

    # emulate to_json behavior to setup internal packages cache
    cm._gomod_data.setdefault(pkg.name, {
        "purl": "not-important",
        "dependencies": []
    })
    cm._gopkg_data.setdefault(pkg.id, {
        "name": pkg.name,
        "purl": expected_purl,
        "dependencies": [],
        "sources": []
    })

    cm.process_go_package(pkg, dep)
    cm.process_gomod(pkg, src)
    cm.set_go_package_sources()

    expected_contents = {
        pkg.id: {
            "purl": expected_purl,
            "dependencies": [{
                "purl": expected_dep_purl
            }],
            "sources": [{
                "purl": expected_src_purl
            }],
        }
    }

    assert cm._gopkg_data
    assert pkg.id in cm._gopkg_data
    assert cm._gopkg_data == expected_contents
Ejemplo n.º 5
0
def test_generate_icm(contents, default_request):
    cm = content_manifest.ContentManifest(default_request)
    expected = OrderedDict({
        "image_contents":
        contents or [],
        "metadata":
        OrderedDict({
            "icm_spec": content_manifest.JSON_SCHEMA_URL,
            "icm_version": 1,
            "image_layer_index": -1,
        }),
    })
    assert cm.generate_icm(contents) == expected
Ejemplo n.º 6
0
    def content_manifest(self):
        """
        Get the Image Content Manifest for a request.

        :return: the ContentManifest object for the request
        :rtype: ContentManifest
        """
        packages_data = self._get_packages_data()
        packages = [
            content_manifest.Package.from_json(package)
            for package in packages_data.packages
        ]

        return content_manifest.ContentManifest(self, packages)
Ejemplo n.º 7
0
def test_to_json_with_multiple_packages(mock_generate_icm, app, packages):
    request = Request()
    cm = content_manifest.ContentManifest(request)

    image_contents = []
    for package in packages:
        pkg = Package.from_json(package)
        request_package = RequestPackage(package=pkg)
        request.request_packages.append(request_package)
        content = {"purl": pkg.to_purl(), "dependencies": [], "sources": []}
        image_contents.append(content)
    res = cm.to_json()
    mock_generate_icm.assert_called_once_with(image_contents)
    assert res == mock_generate_icm.return_value
Ejemplo n.º 8
0
def test_set_go_package_sources_replace_parent_purl(gopkg_name, gomod_data,
                                                    expected_parent_purl,
                                                    default_request):
    pre_replaced_dependencies = [
        {
            "purl":
            f"{content_manifest.PARENT_PURL_PLACEHOLDER}#staging/src/k8s.io/foo"
        },
        {
            "purl":
            f"{content_manifest.PARENT_PURL_PLACEHOLDER}#staging/src/k8s.io/bar"
        },
        {
            "purl": "pkg:golang/example.com/[email protected]"
        },
    ]
    post_replaced_dependencies = [
        {
            "purl": f"{expected_parent_purl}#staging/src/k8s.io/foo"
        },
        {
            "purl": f"{expected_parent_purl}#staging/src/k8s.io/bar"
        },
        {
            "purl": "pkg:golang/example.com/[email protected]"
        },
    ]

    cm = content_manifest.ContentManifest(default_request)
    cm._gomod_data = gomod_data
    cm._gopkg_data = {
        1: {
            "name": gopkg_name,
            "purl": "not-important",
            "dependencies": pre_replaced_dependencies,
            "sources": [],
        },
    }

    cm.set_go_package_sources()
    assert cm._gopkg_data == {
        1: {
            # name is popped by set_go_package_sources()
            "purl": "not-important",
            "dependencies": post_replaced_dependencies,
            "sources": [],
        },
    }
Ejemplo n.º 9
0
def test_to_json_properly_sets_internal_data(mock_set_go_sources,
                                             mock_top_level_purl, app, package,
                                             internal_attr, internal_data):
    # Half the unit tests "emulate to_json() behaviour" so we should probably test that behaviour
    request = Request()

    pkg = Package.from_json(package)
    pkg.id = 1

    request_package = RequestPackage(package=pkg)
    request.request_packages.append(request_package)

    mock_top_level_purl.return_value = "mock-package-purl"

    cm = content_manifest.ContentManifest(request)
    cm.to_json()

    # Here we are only interested in the setup part of to_json()
    # (sidenote: we really need to refactor to_json())
    assert getattr(cm, internal_attr) == internal_data
Ejemplo n.º 10
0
def test_set_go_package_sources(mock_warning, app, pkg_name, gomod_data, warn,
                                default_request):
    cm = content_manifest.ContentManifest(default_request)

    main_purl = "pkg:golang/a-package"
    main_package_id = 1

    cm._gopkg_data = {
        main_package_id: {
            "name": pkg_name,
            "purl": main_purl,
            "sources": [],
            "dependencies": []
        }
    }
    cm._gomod_data = gomod_data

    cm.set_go_package_sources()

    sources = []
    for v in gomod_data.values():
        if any(k in pkg_name for k in gomod_data.keys()):
            sources += v["dependencies"]

    expected = {
        main_package_id: {
            "purl": main_purl,
            "dependencies": [],
            "sources": sources
        }
    }

    assert cm._gopkg_data == expected

    if warn:
        mock_warning.assert_called_once_with(
            "Could not find a Go module for %s", main_purl)
    else:
        mock_warning.assert_not_called()
Ejemplo n.º 11
0
def test_process_gomod_replace_parent_purl(default_request):
    module = Package.from_json({
        "name": "example.com/org/project",
        "type": "gomod",
        "version": "1.1.1"
    })
    module.id = 1
    expected_module_purl = "pkg:golang/example.com%2Forg%[email protected]"

    module_dep = Package.from_json({
        "name":
        "example.com/anotherorg/project",
        "type":
        "gomod",
        "version":
        "./staging/src/anotherorg/project",
    })
    module_dep.id = 2
    expected_dependency_purl = f"{expected_module_purl}#staging/src/anotherorg/project"

    cm = content_manifest.ContentManifest(default_request)

    # emulate to_json behavior to setup internal packages cache
    cm._gomod_data.setdefault(module.name, {
        "purl": expected_module_purl,
        "dependencies": []
    })

    cm.process_gomod(module, module_dep)
    assert cm._gomod_data == {
        module.name: {
            "purl": expected_module_purl,
            "dependencies": [{
                "purl": expected_dependency_purl
            }],
        },
    }