Skip to content

Commit

Permalink
add rest of unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Sep 2, 2024
1 parent 823d875 commit fcfac44
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/node_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def on_deployed_without_trust(self) -> None:
"""Blocks the application and returns a specific error message for deployments made without --trust."""
logger.error("Could not apply service, application needs `juju trust`")
self.charm.unit.status = BlockedStatus(
f"Insufficient permissions, try: `juju trust {self.app.name} --scope=cluster`"
f"Insufficient permissions, try: `juju trust {self.app_name} --scope=cluster`"
)

def build_node_port_services(self, port: str) -> Service:
Expand Down
30 changes: 19 additions & 11 deletions tests/unit/test_nodeport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
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
from node_port import ApiError
from charms.data_platform_libs.v0.data_interfaces import DatabaseRequiresEvents
from charm import MongosCharm

Expand Down Expand Up @@ -78,22 +77,31 @@ def test_delete_unit_service_raises_ApiError(self, get_service):
with self.assertRaises(ApiError):
self.harness.charm.node_port_manager.delete_unit_service()

@patch("src.node_port.NodePortManager.get_service")
@patch("charm.NodePortManager.client", new_callable=PropertyMock)
def test_delete_unit_service_needs_juju_trust(self, get_service, client):
@patch("charm.NodePortManager.get_service")
def test_delete_unit_service_needs_juju_trust(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

api_error = mock.Mock()
api_error.status.code = 403
# 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", "code": 403}),
)

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

NodePortManager.delete_unit_service()
self.harness.charm.node_port_manager.delete_unit_service()

self.assertTrue(
isinstance(self.harness.charm.unit.status, BlockedStatus(STATUS_JUJU_TRUST))
self.harness.charm.unit.status == BlockedStatus(STATUS_JUJU_TRUST)
)

0 comments on commit fcfac44

Please sign in to comment.