From e8ac8038eddabd2a22bbe8a94231311cd5348aa4 Mon Sep 17 00:00:00 2001 From: Russell Vinegar Date: Mon, 21 Oct 2024 16:34:33 -0700 Subject: [PATCH] only check route name for building delete list --- microservices/kubeApi/routers/routes.py | 10 ++++- .../kubeApi/tests/routers/test_bulk_sync.py | 44 +++++++++++++++++++ .../routers/test_bulk_sync_session_cookie.py | 2 +- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/microservices/kubeApi/routers/routes.py b/microservices/kubeApi/routers/routes.py index 70d8c4d..a2bcc61 100644 --- a/microservices/kubeApi/routers/routes.py +++ b/microservices/kubeApi/routers/routes.py @@ -175,7 +175,7 @@ async def verify_and_create_routes(namespace: str, request: Request): ) insert_batch = [x for x in source_routes if not in_list(x, existing_routes)] - delete_batch = [y for y in existing_routes if not in_list(y, source_routes)] + delete_batch = [y for y in existing_routes if not in_list_by_name(y, source_routes)] logger.debug("insert batch: " + str(insert_batch)) @@ -250,7 +250,7 @@ def get_data_plane(ns_attributes): def get_template_version(ns_attributes): return ns_attributes.get('template-version', ["v2"])[0] -def in_list (match, list): +def in_list(match, list): match_ref = build_ref(match) for item in list: if build_ref(item) == match_ref: @@ -259,3 +259,9 @@ def in_list (match, list): def build_ref(v): return "%s%s%s%s%s" % (v['name'], v['selectTag'], v['host'], v['dataPlane'], v['sessionCookieEnabled']) + +def in_list_by_name(match, list): + for item in list: + if item['name'] == match['name']: + return True + return False \ No newline at end of file diff --git a/microservices/kubeApi/tests/routers/test_bulk_sync.py b/microservices/kubeApi/tests/routers/test_bulk_sync.py index 2eca86d..e793d47 100644 --- a/microservices/kubeApi/tests/routers/test_bulk_sync.py +++ b/microservices/kubeApi/tests/routers/test_bulk_sync.py @@ -38,3 +38,47 @@ def test_bulk_sync(client): assert response.json()['inserted_count'] == 0 assert response.json()['deleted_count'] == 0 +def test_bulk_sync_change_host(client): + with mock.patch("routers.routes.get_gwa_ocp_routes") as call: + call.return_value = [{ + "metadata": { + "name": "wild-ns-example-xyz", + "labels": { + "aps-select-tag": "ns.EXAMPLE-NS", + "aps-template-version": "v2" + } + }, + "spec": { + "host": "xyz.api.gov.bc.ca", + "to": { + "name": "data-plane-1" + } + } + }] + + + with mock.patch("routers.routes.prepare_apply_routes") as call_apply: + call_apply.return_value = 1 + + with mock.patch("routers.routes.apply_routes") as call_mismatch_routes: + call_mismatch_routes.return_value = None + + with mock.patch("routers.routes.delete_route") as mock_delete_route: + mock_delete_route.return_value = None # Simulate successful deletion + + with mock.patch("routers.routes.kubectl_delete") as mock_kubectl_delete: + mock_kubectl_delete.return_value = None # Simulate successful kubectl deletion + + data = [{ + "name": "wild-ns-example-abc", + "selectTag": "ns.EXAMPLE-NS", + "dataPlane": "data-plane-1", + "host": "abc.api.gov.bc.ca", + "sessionCookieEnabled": False + }] + response = client.post('/namespaces/examplens/routes/sync', json=data) + assert response.status_code == 200 + assert response.json()['message'] == 'synced' + assert response.json()['inserted_count'] == 1 + assert response.json()['deleted_count'] == 1 + diff --git a/microservices/kubeApi/tests/routers/test_bulk_sync_session_cookie.py b/microservices/kubeApi/tests/routers/test_bulk_sync_session_cookie.py index b1d1191..44be932 100644 --- a/microservices/kubeApi/tests/routers/test_bulk_sync_session_cookie.py +++ b/microservices/kubeApi/tests/routers/test_bulk_sync_session_cookie.py @@ -77,4 +77,4 @@ def test_bulk_sync_session_cookie_change(client): assert response.status_code == 200 assert response.json()['message'] == 'synced' assert response.json()['inserted_count'] == 1 - assert response.json()['deleted_count'] == 1 + assert response.json()['deleted_count'] == 0