Skip to content

Commit

Permalink
test: mock-insights: fix/improve handling of IDs
Browse files Browse the repository at this point in the history
Currently, the fake systems (or better, only one) is kept in the helper
"systems" dictionary by the machine ID; while this seems to work fine,
in practice it will not work for upcoming changes, and it does not match
what Inventory actually does.

Change the ID handling to represent better what Inventory does:
- assign "id" as Inventory ID for each newly registered system; in
  practice we have only one, and keep hardcoding "123-nice-id" for now
  (the "testInsights" test checks for it)
- use the "id" as key in the "systems" dictionary, rather than the
  "machine_id"
- when registering a new system, copy "machine_id" as "insights_id";
  this will help later on when implementing the non-legacy API endpoints
- adapt endpoints to search for the ID they need

Even with all the changes, there should be no behaviour changes.
  • Loading branch information
ptoscano authored and martinpitt committed Aug 6, 2024
1 parent 08418fc commit b066e12
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions test/verify/files/mock-insights
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ class handler(BaseHTTPRequestHandler):
m = self.match("/r/insights/v1/systems/([^/]+)")
if m:
machine_id = m[1]
if machine_id not in systems:
self.send_response(404)
self.end_headers()
return
self.send_response(200)
for system in systems.values():
if system["machine_id"] == machine_id:
self.send_response(200)
self.end_headers()
self.wfile.write(json.dumps(system).encode() + b"\n")
return
self.send_response(404)
self.end_headers()
self.wfile.write(json.dumps(systems[machine_id]).encode() + b"\n")
return

m = self.match("/r/insights/v1/branch_info")
Expand All @@ -77,9 +78,10 @@ class handler(BaseHTTPRequestHandler):
"total": 0,
"results": [],
}
if insights_id in systems:
res["total"] += 1
res["results"].append({"id": "123-nice-id"})
for system in systems.values():
if system["insights_id"] == insights_id:
res["total"] += 1
res["results"].append(system)
self.wfile.write(json.dumps(res).encode("utf-8") + b"\n")
return

Expand All @@ -88,7 +90,7 @@ class handler(BaseHTTPRequestHandler):
inventory_id = m[1]
self.send_response(200)
self.end_headers()
if inventory_id == "123-nice-id":
if inventory_id in systems:
self.wfile.write(b'[ { "rule": { "total_risk": 3 } }, { "rule": { "total_risk": 2 } }, { "rule": { "total_risk": 1 } }]\n')
else:
self.wfile.write(b'[ ]\n')
Expand All @@ -106,8 +108,10 @@ class handler(BaseHTTPRequestHandler):
s = json.loads(data)
s["unregistered_at"] = None
s["account_number"] = "123456"
s["insights_id"] = s["machine_id"]
s["id"] = "123-nice-id"
print(s)
systems[s["machine_id"]] = s
systems[s["id"]] = s
self.send_response(200)
self.end_headers()
self.wfile.write(data)
Expand Down

0 comments on commit b066e12

Please sign in to comment.