From 5bc613318d2ff948cf4c7fbd0546b53ef2b3b487 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Mon, 5 Aug 2024 07:58:33 +0200 Subject: [PATCH] test: mock-insights: implement upload and delete platform endpoints Implement a couple of missing platform endpoints needed to make insights-client work in non-legacy-upload mode: - the upload endpoint, which needs to get the system metadata from the MIME data sent with POST: because of this, the implementation needs a bit more work; because of testing reasons, the Inventory ID is still hardcoded as "123-nice-id" - the delete endpoint, needed to unregister a system --- test/verify/files/mock-insights | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test/verify/files/mock-insights b/test/verify/files/mock-insights index 086ef6fc5ea..ce7dedfa205 100755 --- a/test/verify/files/mock-insights +++ b/test/verify/files/mock-insights @@ -17,6 +17,7 @@ # username=admin # password=foobar +import email import json import os import re @@ -124,6 +125,51 @@ class handler(BaseHTTPRequestHandler): self.wfile.write(b'{ "reports": [ "foo", "bar" ] }\n') return + m = self.match("/r/insights/platform/ingress/v1/upload") + if m: + # The metadata of the system is in the multipart MIME data + # sent by the system for the upload; to pick it and use it + # we need to unpack the multipart MIME data. + + # First, create prologue to the data, so it can be recognized + # as multipart MIME. + multipart_data = ( + b"""MIME-Version: 1.0 +Content-Type: """ + + self.headers.get("content-type").encode("utf-8") + + b""" + +""" + ) + multipart_data += data + + # Parse the multipart MIME data, and then look for a "metadata" + # file part which contains the system metadata as JSON. + message = email.message_from_bytes(multipart_data) + for part in message.walk(): + if part.get_filename() == "metadata": + s = json.loads(part.get_payload()) + s["id"] = "123-nice-id" + print(s) + systems[s["id"]] = s + + self.send_response(202) + self.end_headers() + self.wfile.write( + b"{" + b' "request_id": "some-upload", ' + b' "upload": { ' + b' "account": "123456", ' + b' "org_id": "123456" ' + b" }" + b"}\n" + ) + return + + self.send_response(400) + self.end_headers() + return + self.send_response(404) self.end_headers() @@ -138,6 +184,18 @@ class handler(BaseHTTPRequestHandler): del systems[machine_id] return + m = self.match("/r/insights/platform/inventory/v1/hosts/([^/]+)") + if m: + inventory_id = m[1] + if inventory_id in systems: + del systems[inventory_id] + self.send_response(200) + self.end_headers() + return + self.send_response(404) + self.end_headers() + return + self.send_response(404) self.end_headers()