Skip to content

Commit 0fbc0d8

Browse files
committed
test patch_finalizers directly
1 parent 0a45b91 commit 0fbc0d8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

capi_janitor/tests/openstack/test_operator.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import easykube
77
import httpx
8+
import kopf
89

910
from capi_janitor.openstack import openstack
1011
from capi_janitor.openstack import operator
@@ -469,3 +470,34 @@ async def test_purge_openstack_resources_raises(self, mock_from_clouds):
469470

470471
# # Example: Validate if appcred deletion was attempted
471472
# mock_identityapi.resource.assert_any_call("application_credentials")
473+
474+
async def test_successful_patch(self):
475+
resource = mock.AsyncMock()
476+
await operator.patch_finalizers(
477+
resource, "test-name", "default", ["finalizer.k8s.io"]
478+
)
479+
resource.patch.assert_awaited_once_with(
480+
"test-name",
481+
{"metadata": {"finalizers": ["finalizer.k8s.io"]}},
482+
namespace="default",
483+
)
484+
485+
async def test_patch_finalizers_error_handling(self):
486+
resource = mock.AsyncMock()
487+
488+
response_mock = mock.Mock()
489+
response_mock.status_code = 422
490+
response_mock.json.return_value = {"message": "error"}
491+
492+
api_error_422 = easykube.ApiError(mock.Mock(response=response_mock))
493+
api_error_422.status_code = 422
494+
resource.patch.side_effect = api_error_422
495+
496+
with self.assertRaises(kopf.TemporaryError):
497+
await operator.patch_finalizers(resource, "name", "ns", [])
498+
499+
exc_404 = easykube.ApiError("not found")
500+
exc_404.status_code = 404
501+
resource.patch.side_effect = exc_404
502+
503+
await operator.patch_finalizers(resource, "name", "ns", [])

0 commit comments

Comments
 (0)