Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
rokatyy committed May 8, 2024
1 parent 24a3eb2 commit 05a878e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/api/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ async def delete_function(
function, mlrun.common.constants.MLRUN_MODEL_CONF
)
if config_map:
k8s_helper.delete_config_map(config_map.items[0].metadata.name)
k8s_helper.delete_configmap(config_map.metadata.name)
return None
except Exception as exc:
# return tuple with failure info
Expand Down
34 changes: 34 additions & 0 deletions tests/api/api/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import json
import unittest.mock
from http import HTTPStatus
from unittest.mock import AsyncMock, MagicMock, patch

import kubernetes.client
import pytest
from deepdiff import DeepDiff
from fastapi.testclient import TestClient
from kubernetes.client.models import V1ConfigMap, V1ObjectMeta
from sqlalchemy.orm import Session

import mlrun
Expand All @@ -35,6 +37,7 @@
from mlrun.common.schemas import SecurityContextEnrichmentModes
from mlrun.utils import logger
from server.api.api.utils import (
_delete_nuclio_functions_in_batches,
_generate_function_and_task_from_submit_run_body,
_mask_v3io_access_key_env_var,
_mask_v3io_volume_credentials,
Expand Down Expand Up @@ -1658,3 +1661,34 @@ def _assert_env_var_from_secret(
)
assert env_var_value.secret_key_ref.name == secret_name
assert env_var_value.secret_key_ref.key == secret_key


@pytest.mark.asyncio
async def test_delete_function_calls_k8s_helper_methods():
function_names = ["function1"]
async_client_mock = AsyncMock()

k8s_helper_mock = MagicMock()
configmap = V1ConfigMap()
configmap.metadata = V1ObjectMeta(name="config-map-1")
k8s_helper_mock.get_configmap_for_function.return_value = configmap

with (
patch(
"server.api.utils.clients.async_nuclio.Client",
return_value=async_client_mock,
),
patch(
"server.api.utils.singletons.k8s.get_k8s_helper",
return_value=k8s_helper_mock,
),
):
failed_requests = await _delete_nuclio_functions_in_batches(
{}, "my-project", function_names
)

assert len(failed_requests) == 0
k8s_helper_mock.get_configmap_for_function.assert_called_with(
"function1", "model-conf"
)
k8s_helper_mock.delete_configmap.assert_called_with("config-map-1")

0 comments on commit 05a878e

Please sign in to comment.