Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Feb 19, 2024
1 parent 0670225 commit 0ec89b0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
24 changes: 7 additions & 17 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pathlib import Path

from typing import Set, List, Optional, Dict
from exceptions import ApplicationHostNotFoundError

from charms.mongodb.v0.mongodb_secrets import SecretCache
from charms.mongos.v0.mongos_client_interface import MongosProvider
Expand All @@ -25,7 +24,7 @@
from config import Config

import ops
from ops.model import BlockedStatus, MaintenanceStatus, WaitingStatus, Relation, Unit
from ops.model import BlockedStatus, MaintenanceStatus, WaitingStatus, Relation
from ops.charm import InstallEvent, StartEvent, RelationDepartedEvent

import logging
Expand Down Expand Up @@ -355,7 +354,7 @@ def get_mongos_host(self) -> str:
the client wishes to connect to mongos (inside Juju or outside).
"""
if self.is_external_client:
return self._unit_ip(self.unit)
return self._unit_ip
else:
return Config.MONGOS_SOCKET_URI_FMT

Expand All @@ -364,20 +363,6 @@ def _generate_relation_departed_key(rel_id: int) -> str:
"""Generates the relation departed key for a specified relation id."""
return f"relation_{rel_id}_departed"

def _unit_ip(self, unit: Unit) -> str:
"""Returns the ip address of a given unit."""
# check if host is current host
if unit == self.unit:
return str(
self.model.get_binding(Config.Relations.PEERS).network.bind_address
)
# check if host is a peer
elif unit in self._peers.data:
return str(self._peers.data[unit].get("private-address"))
# raise exception if host not found
else:
raise ApplicationHostNotFoundError

def open_mongos_port():
try:
logger.debug("opening tcp port")
Expand All @@ -389,6 +374,11 @@ def open_mongos_port():
# END: helper functions

# BEGIN: properties
@property
def _unit_ip(self) -> str:
"""Returns the ip address of the unit."""
return str(self.model.get_binding(Config.Relations.PEERS).network.bind_address)

@property
def is_external_client(self) -> Optional[str]:
"""Returns the database requested by the hosting application of the subordinate charm."""
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from charms.data_platform_libs.v0.data_interfaces import DatabaseRequiresEvents

CLUSTER_ALIAS = "cluster"
MONGOS_SOCKET_URI_FMT = (
"%2Fvar%2Fsnap%2Fcharmed-mongodb%2Fcommon%2Fvar%2Fmongodb-27018.sock"
)


class TestCharm(unittest.TestCase):
Expand Down Expand Up @@ -143,3 +146,14 @@ def test_status_shows_mongos_waiting(self):
self.harness.add_relation("cluster", "config-server")
self.harness.charm.on.update_status.emit()
self.assertTrue(isinstance(self.harness.charm.unit.status, WaitingStatus))

@patch_network_get(private_address="1.1.1.1")
def test_mongos_host(self):
"""TBD."""
self.harness.charm.app_peer_data["external-connectivity"] = json.dumps(False)
mongos_host = self.harness.charm.get_mongos_host()
self.assertEqual(mongos_host, MONGOS_SOCKET_URI_FMT)

self.harness.charm.app_peer_data["external-connectivity"] = json.dumps(True)
mongos_host = self.harness.charm.get_mongos_host()
self.assertEqual(mongos_host, "1.1.1.1")
4 changes: 3 additions & 1 deletion tests/unit/test_config_server_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def setUp(self):
# related to duplicated events.
delattr(DatabaseRequiresEvents, f"{CLUSTER_ALIAS}_database_created")
delattr(DatabaseRequiresEvents, f"{CLUSTER_ALIAS}_endpoints_changed")
delattr(DatabaseRequiresEvents, f"{CLUSTER_ALIAS}_read_only_endpoints_changed")
delattr(
DatabaseRequiresEvents, f"{CLUSTER_ALIAS}_read_only_endpoints_changed"
)
except AttributeError:
# Ignore the events not existing before the first test.
pass
Expand Down

0 comments on commit 0ec89b0

Please sign in to comment.