Skip to content

Commit

Permalink
test: mock-insights: implement upload and delete platform endpoints
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ptoscano authored and martinpitt committed Aug 6, 2024
1 parent b066e12 commit 5bc6133
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions test/verify/files/mock-insights
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# username=admin
# password=foobar

import email
import json
import os
import re
Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand Down

0 comments on commit 5bc6133

Please sign in to comment.