Skip to content

Commit

Permalink
Replacing tests calling private methods with tests not doing that
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmerold committed Nov 24, 2023
1 parent 2aa17bd commit 85e05bd
Show file tree
Hide file tree
Showing 3 changed files with 504 additions and 268 deletions.
23 changes: 13 additions & 10 deletions lib/charms/kubernetes_charm_libraries/v0/multus.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, *args):
container_name=self._container_name,
cap_net_admin=True,
privileged=True,
network_annotations=[
network_annotations_func=[
NetworkAnnotation(
name=NETWORK_ATTACHMENT_DEFINITION_NAME,
interface=INTERFACE_NAME,
Expand Down Expand Up @@ -148,6 +148,8 @@ def __eq__(self, other):
class NetworkAnnotation:
"""NetworkAnnotation."""

NETWORK_ANNOTATION_RESOURCE_KEY = "k8s.v1.cni.cncf.io/networks"

name: str
interface: str
mac: Optional[str] = None
Expand Down Expand Up @@ -359,7 +361,7 @@ def patch_statefulset(
template=PodTemplateSpec(
metadata=ObjectMeta(
annotations={
"k8s.v1.cni.cncf.io/networks": json.dumps(
NetworkAnnotation.NETWORK_ANNOTATION_RESOURCE_KEY: json.dumps(
[
network_annotation.dict()
for network_annotation in network_annotations
Expand Down Expand Up @@ -458,10 +460,10 @@ def _pod_is_patched(
def _annotations_contains_multus_networks(
annotations: dict, network_annotations: list[NetworkAnnotation]
) -> bool:
if "k8s.v1.cni.cncf.io/networks" not in annotations:
if NetworkAnnotation.NETWORK_ANNOTATION_RESOURCE_KEY not in annotations:
return False
try:
if json.loads(annotations["k8s.v1.cni.cncf.io/networks"]) != [
if json.loads(annotations[NetworkAnnotation.NETWORK_ANNOTATION_RESOURCE_KEY]) != [
network_annotation.dict() for network_annotation in network_annotations
]:
return False
Expand Down Expand Up @@ -503,7 +505,7 @@ def __init__(
self,
charm: CharmBase,
network_attachment_definitions_func: Callable[[], list[NetworkAttachmentDefinition]],
network_annotations: list[NetworkAnnotation],
network_annotations_func: Callable[[], list[NetworkAnnotation]],
container_name: str,
refresh_event: BoundEvent,
cap_net_admin: bool = False,
Expand All @@ -515,7 +517,8 @@ def __init__(
charm: Charm object
network_attachment_definitions_func: A callable to a function returning a list of
`NetworkAttachmentDefinition` to be created.
network_annotations: List of NetworkAnnotation.
network_annotations_func: A callable to a function returning a list
of `NetworkAnnotation` to be added to the container.
container_name: Container name
cap_net_admin: Container requires NET_ADMIN capability
privileged: Container requires privileged security context
Expand All @@ -525,7 +528,7 @@ def __init__(
super().__init__(charm, "kubernetes-multus")
self.kubernetes = KubernetesClient(namespace=self.model.name)
self.network_attachment_definitions_func = network_attachment_definitions_func
self.network_annotations = network_annotations
self.network_annotations_func = network_annotations_func
self.container_name = container_name
self.cap_net_admin = cap_net_admin
self.privileged = privileged
Expand All @@ -543,7 +546,7 @@ def _configure_multus(self, event: BoundEvent) -> None:
if not self._statefulset_is_patched():
self.kubernetes.patch_statefulset(
name=self.model.app.name,
network_annotations=self.network_annotations,
network_annotations=self.network_annotations_func(),
container_name=self.container_name,
cap_net_admin=self.cap_net_admin,
privileged=self.privileged,
Expand Down Expand Up @@ -617,7 +620,7 @@ def _statefulset_is_patched(self) -> bool:
"""Returns whether statefuset is patched with network annotations and capabilities."""
return self.kubernetes.statefulset_is_patched(
name=self.model.app.name,
network_annotations=self.network_annotations,
network_annotations=self.network_annotations_func(),
container_name=self.container_name,
cap_net_admin=self.cap_net_admin,
privileged=self.privileged,
Expand All @@ -627,7 +630,7 @@ def _pod_is_ready(self) -> bool:
"""Returns whether pod is ready with network annotations and capabilities."""
return self.kubernetes.pod_is_ready(
pod_name=self._pod,
network_annotations=self.network_annotations,
network_annotations=self.network_annotations_func(),
container_name=self.container_name,
cap_net_admin=self.cap_net_admin,
privileged=self.privileged,
Expand Down
2 changes: 1 addition & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def __init__(self, *args):
charm=self,
container_name=self._bessd_container_name,
cap_net_admin=True,
network_annotations=self._generate_network_annotations(),
network_annotations_func=self._generate_network_annotations,
network_attachment_definitions_func=self._network_attachment_definitions_from_config,
refresh_event=self.on.nad_config_changed,
)
Expand Down
Loading

0 comments on commit 85e05bd

Please sign in to comment.