Skip to content

Commit

Permalink
Fix missing await on Kubernetes API
Browse files Browse the repository at this point in the history
  • Loading branch information
raminqaf committed May 14, 2024
1 parent e761b40 commit f2bf594
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples
Submodule examples updated 1 files
+2 −0 config.yaml
2 changes: 1 addition & 1 deletion kpops/component_handlers/kubernetes/pvc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def create(cls, app_name: str, namespace: str) -> PVCHandler:
async def list_pvcs(self) -> list[str]:
async with ApiClient() as api:
core_v1_api = client.CoreV1Api(api)
pvc_list = core_v1_api.list_namespaced_persistent_volume_claim(
pvc_list = await core_v1_api.list_namespaced_persistent_volume_claim(
self.namespace, label_selector=f"app={self.app_name}"
)

Expand Down
37 changes: 29 additions & 8 deletions tests/kubernetes/test_pvc_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from unittest.mock import MagicMock
from unittest.mock import AsyncMock

import pytest
from kubernetes_asyncio.client import (
V1ObjectMeta,
V1PersistentVolumeClaim,
V1PersistentVolumeClaimList,
V1PersistentVolumeClaimSpec,
V1PersistentVolumeClaimStatus,
)
from pytest_mock import MockerFixture

from kpops.component_handlers.kubernetes.pvc_handler import PVCHandler
Expand All @@ -25,16 +32,30 @@ async def test_create(pvc_handler: PVCHandler, mocker: MockerFixture):

@pytest.mark.asyncio
async def test_pvc_names(pvc_handler: PVCHandler, mocker: MockerFixture):
mock_pvc = MagicMock()
mock_pvc.metadata.name = "test-pvc"
test_pvc1 = V1PersistentVolumeClaim(
api_version="v1",
kind="PersistentVolumeClaim",
metadata=V1ObjectMeta(name="datadir-test-app-1"),
spec=V1PersistentVolumeClaimSpec(),
status=V1PersistentVolumeClaimStatus(),
)
test_pvc2 = V1PersistentVolumeClaim(
api_version="v1",
kind="PersistentVolumeClaim",
metadata=V1ObjectMeta(name="datadir-test-app-2"),
spec=V1PersistentVolumeClaimSpec(),
status=V1PersistentVolumeClaimStatus(),
)
volume_claim_list = V1PersistentVolumeClaimList(items=[test_pvc1, test_pvc2])

async_mock = AsyncMock()
async_mock.list_namespaced_persistent_volume_claim.return_value = volume_claim_list

mocker.patch(f"{MODULE}.client.CoreV1Api", return_value=async_mock)

mock_core_v1_api = mocker.patch(f"{MODULE}.client.CoreV1Api")
mock_core_v1_api.return_value.list_namespaced_persistent_volume_claim.return_value.items = [
mock_pvc
]
pvcs = await pvc_handler.list_pvcs()

assert pvcs == ["test-pvc"]
assert pvcs == ["datadir-test-app-1", "datadir-test-app-2"]


@pytest.mark.asyncio
Expand Down

0 comments on commit f2bf594

Please sign in to comment.