Skip to content

Commit

Permalink
skip: fix lint issues and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DnPlas committed Feb 14, 2024
1 parent 239b4d9 commit c34af50
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
31 changes: 22 additions & 9 deletions lib/charms/mlops_libs/v0/k8s_service_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ def _some_event_handler(self, ...):
import logging
from typing import List, Optional, Union

from ops.framework import Object
from ops.model import CharmBase, Relation
from ops.framework import BoundEvent, EventBase, EventSource, Object, ObjectEvents
from ops.charm import CharmBase
from ops.framework import BoundEvent, Object
from ops.model import Relation

# The unique Charmhub library identifier, never change it
LIBID = "f5c3f6cc023e40468d6f9a871e8afcd0"

Expand Down Expand Up @@ -202,33 +203,45 @@ def get_k8s_svc_info(self) -> dict:
"svc_port": relation_data["svc_port"],
}


class KubernetesServiceInfoProvider(Object):
"""Base class that represents a provider relation end.
Args:
provider_charm (CharmBase): the provider application
relation_name (str, optional): the name of the relation
refresh_event: (list, optional): list of BoundEvents that this manager should handle. Use this to update
the data sent on this relation on demand.
svc_name (str): the name of the Kubernetes Service the provider knows about
svc_port (str): the port number of the Kubernetes Service the provider knows about
refresh_event: (list, optional): list of BoundEvents that this manager should handle. Use this to update
the data sent on this relation on demand.
Attributes:
provider_charm (CharmBase): variable for storing the provider application
relation_name (str): variable for storing the name of the relation
"""

def __init__(self, provider_charm, relation_name: str = DEFAULT_RELATION_NAME, refresh_event: Optional[Union[BoundEvent, List[BoundEvent]]] = None, svc_name: str, svc_port: str):
def __init__(
self,
provider_charm: CharmBase,
relation_name: str,
svc_name: str,
svc_port: str,
refresh_event: Optional[Union[BoundEvent, List[BoundEvent]]] = None,
):
super().__init__(provider_charm, relation_name)
self.provider_charm = provider_charm
self.relation_name = relation_name
self._requirer_wrapper = KubernetesServiceInfoProviderWrapper(self.provider_charm, self.relation_name)
self._requirer_wrapper = KubernetesServiceInfoProviderWrapper(
self.provider_charm, self.relation_name
)
self._svc_name = svc_name
self._svc_port = svc_port

self.framework.observe(self.provider_charm.on.leader_elected, self._send_k8s_svc_info)

self.framework.observe(self.provider_charm.on[self.relation_name].relation_crated, self._send_k8s_svc_info)
self.framework.observe(
self.provider_charm.on[self.relation_name].relation_crated, self._send_k8s_svc_info
)

if refresh_event:
if not isinstance(refresh_event, (tuple, list)):
Expand All @@ -253,7 +266,7 @@ class KubernetesServiceInfoProviderWrapper(Object):
relation_name (str): variable for storing the name of the relation
"""

def __init__(self, provider_charm, relation_name: str = DEFAULT_RELATION_NAME):
def __init__(self, provider_charm: CharmBase, relation_name: str):
super().__init__(provider_charm, relation_name)
self.provider_charm = provider_charm
self.relation_name = relation_name
Expand Down
13 changes: 7 additions & 6 deletions tests/unit/test_k8s_service_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
from charms.mlops_libs.v0.k8s_service_info import (
KubernetesServiceInfoProvider,
KubernetesServiceInfoProviderWrapper,
KubernetesServiceInfoRelationDataMissingError,
KubernetesServiceInfoRelationMissingError,
KubernetesServiceInfoRequirer,
Expand Down Expand Up @@ -117,7 +117,7 @@ def test_preflight_checks_raise_no_relation_data(requirer_charm_harness):

with pytest.raises(KubernetesServiceInfoRelationDataMissingError) as error:
requirer_charm_harness.charm._k8s_svc_info_requirer.get_k8s_svc_info()
assert str(error.value) == "No data found in relation data bag."
assert str(error.value) == f"No data found in relation {TEST_RELATION_NAME} data bag."


def test_preflight_checks_raises_data_missing_attribute(requirer_charm_harness):
Expand All @@ -140,7 +140,7 @@ def test_preflight_checks_raises_data_missing_attribute(requirer_charm_harness):

with pytest.raises(KubernetesServiceInfoRelationDataMissingError) as error:
requirer_charm_harness.charm._k8s_svc_info_requirer.get_k8s_svc_info()
assert str(error.value) == "Missing attributes: ['svc_port']"
assert str(error.value) == f"Missing attributes: ['svc_port'] in relation {TEST_RELATION_NAME}"


def test_send_relation_data_passes(provider_charm_harness):
Expand All @@ -153,16 +153,17 @@ def test_send_relation_data_passes(provider_charm_harness):
provider_charm_harness.add_relation(TEST_RELATION_NAME, "app")

# Instantiate KubernetesServiceInfoRequirer class
provider_charm_harness.charm._k8s_svc_info_provider = KubernetesServiceInfoProvider(
provider_charm_harness.charm, relation_name=TEST_RELATION_NAME
provider_charm_harness.charm._k8s_svc_info_provider = KubernetesServiceInfoProviderWrapper(
provider_charm_harness.charm,
relation_name=TEST_RELATION_NAME,
)

# Add and update relation
provider_charm_harness.add_relation_unit(relation_id, "app/0")

relation_data = {"svc_name": "some-service", "svc_port": "7878"}
provider_charm_harness.charm._k8s_svc_info_provider.send_k8s_svc_info_relation_data(
**relation_data
svc_name=relation_data["svc_name"], svc_port=relation_data["svc_port"]
)
relations = provider_charm_harness.model.relations[TEST_RELATION_NAME]
for relation in relations:
Expand Down

0 comments on commit c34af50

Please sign in to comment.