def generate_icm(self, image_contents=None): """ Generate a content manifest with the given image contents. :param list image_contents: List with components for the ICM's ``image_contents`` field :return: a valid Image Content Manifest :rtype: OrderedDict """ icm = deepcopy(BASE_ICM) icm["image_contents"] = image_contents or [] deep_sort_icm(icm) return icm
def test_deep_sort_icm(orig_items): expected = [{ "image_contents": [ { "dependencies": [], "purl": "0sample-URL", "sources": [{ "purl": "0sample-URL" }, { "purl": "1sample-URL" }], }, { "dependencies": [ { "purl": "0sample-URL" }, { "purl": "1sample-URL" }, { "purl": "2sample-URL" }, { "purl": "3sample-URL" }, { "purl": "4sample-URL" }, { "purl": "5sample-URL" }, ], "purl": "1sample-URL", "sources": [], }, ], "metadata": { "icm_spec": "sample-URL", "icm_version": 1, "image_layer_index": -1 }, }] deep_sort_icm(orig_items) assert orig_items == expected
def get_content_manifest_by_requests(): """ Retrieve the content manifest associated with the given requests. :return: a Flask JSON response :rtype: flask.Response :raise BadRequest: if any of the given request is not in the "complete" or "stale" state, If any of the given request cannot be found. """ arg = flask.request.args.get("requests") if not arg: return flask.jsonify(BASE_ICM) request_ids = set() item: str for item in arg.split(","): if not item.strip(): continue if not item.strip().isdigit(): raise BadRequest(f"{item} is not an integer.") request_ids.add(int(item)) requests = (Request.query.filter(Request.id.in_(request_ids)).options( load_only("id"), joinedload(Request.state)).all()) states = (RequestStateMapping.complete.name, RequestStateMapping.stale.name) request: Request for request in requests: if request.state.state_name not in states: raise BadRequest( f"Request {request.id} is in state {request.state.state_name}, " f"not complete or stale.") request_ids.remove(request.id) if request_ids: nonexistent_ids = ",".join(map(str, request_ids)) raise BadRequest(f"Cannot find request(s) {nonexistent_ids}.") assembled_icm = deepcopy(BASE_ICM) for request in requests: manifest = request.content_manifest.to_json() assembled_icm["image_contents"].extend(manifest["image_contents"]) if len(requests) > 1: deep_sort_icm(assembled_icm) return send_content_manifest_back(assembled_icm)
def generate_icm(self, image_contents=None): """ Generate a content manifest with the given image contents. :param list image_contents: List with components for the ICM's ``image_contents`` field :return: a valid Image Content Manifest :rtype: OrderedDict """ icm = { "metadata": { "icm_version": self.version, "icm_spec": self.json_schema_url, "image_layer_index": self.unknown_layer_index, }, } icm["image_contents"] = image_contents or [] return deep_sort_icm(icm)
def get_content_manifest_by_requests(): """ Retrieve the content manifest associated with the given requests. :return: a Flask JSON response :rtype: flask.Response :raise BadRequest: if any of the given request is not in the "complete" or "stale" state, If any of the given request cannot be found. """ arg = flask.request.args.get("requests") if not arg: return flask.jsonify(BASE_ICM) request_ids = [] item: str for item in arg.split(","): if not item.strip(): continue if not item.strip().isdigit(): raise BadRequest(f"{item} is not an integer.") request_ids.append(int(item)) requests = Request.query.filter(Request.id.in_(request_ids)) found_requests_ids = [request.id for request in requests] ids_diff = list(map(str, set(request_ids) - set(found_requests_ids))) if ids_diff: raise BadRequest(f"Cannot find request(s) {', '.join(ids_diff)}.") assembled_icm = deepcopy(BASE_ICM) request: Request for request in requests: if request.state.state_name not in ( RequestStateMapping.complete.name, RequestStateMapping.stale.name, ): raise BadRequest( f"Request {request.id} is in state {request.state.state_name}, " f"not complete or stale.") assembled_icm["image_contents"].extend( request.content_manifest.to_json()["image_contents"]) return flask.jsonify(deep_sort_icm(assembled_icm))
def test_deep_sort_icm(orig_items): expected = [ OrderedDict({ "image_contents": [ OrderedDict({ "dependencies": [], "purl": "0sample-URL", "sources": [ OrderedDict({"purl": "0sample-URL"}), OrderedDict({"purl": "1sample-URL"}), ], }), OrderedDict({ "dependencies": [ OrderedDict({"purl": "0sample-URL"}), OrderedDict({"purl": "1sample-URL"}), OrderedDict({"purl": "2sample-URL"}), OrderedDict({"purl": "3sample-URL"}), OrderedDict({"purl": "4sample-URL"}), OrderedDict({"purl": "5sample-URL"}), ], "purl": "1sample-URL", "sources": [], }), ], "metadata": OrderedDict({ "icm_spec": "sample-URL", "icm_version": 1, "image_layer_index": -1 }), }), ] assert deep_sort_icm(orig_items) == expected
def test_deep_sort_icm_raises_error_when_unknown_type_included(error_icm): with pytest.raises( TypeError, match="Unknown type is included in the content manifest"): deep_sort_icm(error_icm)