Skip to content

Commit

Permalink
test(APIError): Make test valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu1nness committed Sep 2, 2024
1 parent 710135e commit 823d875
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ venv/
build/
*.charm

.coverage*
coverage*
__pycache__/
*.py[cod]
Expand Down
21 changes: 17 additions & 4 deletions tests/unit/test_nodeport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import logging
import unittest
from unittest import mock
from httpx import Request, Response
from unittest.mock import patch, PropertyMock
import httpx
from ops.model import BlockedStatus
from ops.testing import Harness
from node_port import NodePortManager, ApiError
Expand Down Expand Up @@ -51,16 +53,27 @@ def test_delete_unit_service_has_no_metadata(self, get_service):
self.harness.charm.node_port_manager.delete_unit_service()

@patch("charm.NodePortManager.get_service")
@patch("charm.NodePortManager.client", new_callable=PropertyMock)
def test_delete_unit_service_raises_ApiError(self, client, get_service):
def test_delete_unit_service_raises_ApiError(self, get_service):
"""Verify that when charm needs juju trust a status is logged."""
metadata_mock = mock.Mock()
metadata_mock.name = "serice-name"
metadata_mock.name = "service-name"
service = mock.Mock()
service.metadata = metadata_mock
get_service.return_value = service

client.return_value.delete.side_effect = ApiError
# We need a valid API error due to error handling in lightkube
api_error = ApiError(
request=httpx.Request(url="http://controller/call", method="DELETE"),
response=httpx.Response(409, json={"message": "bad call"}),
)

mocked_client = PropertyMock()
delete_mock = mock.Mock()
delete_mock.side_effect = api_error
mocked_client.delete = delete_mock

# Patch the actual client here
self.harness.charm.node_port_manager.client = mocked_client

with self.assertRaises(ApiError):
self.harness.charm.node_port_manager.delete_unit_service()
Expand Down

0 comments on commit 823d875

Please sign in to comment.