diff --git a/.gitignore b/.gitignore index 681ba60a..deda5656 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ venv/ build/ *.charm +.coverage* coverage* __pycache__/ *.py[cod] diff --git a/tests/unit/test_nodeport.py b/tests/unit/test_nodeport.py index b5c30f86..6922255d 100644 --- a/tests/unit/test_nodeport.py +++ b/tests/unit/test_nodeport.py @@ -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 @@ -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()