From b066e1251cb9005489ed86b80921b0892d814000 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Mon, 5 Aug 2024 07:45:36 +0200 Subject: [PATCH] test: mock-insights: fix/improve handling of IDs 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. --- test/verify/files/mock-insights | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/test/verify/files/mock-insights b/test/verify/files/mock-insights index 608e4b8df30..086ef6fc5ea 100755 --- a/test/verify/files/mock-insights +++ b/test/verify/files/mock-insights @@ -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") @@ -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 @@ -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') @@ -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)