Skip to content

Commit

Permalink
testing discover happy path
Browse files Browse the repository at this point in the history
  • Loading branch information
Franr committed Nov 23, 2024
1 parent 0af4741 commit 7ca0580
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions tests/test_containers/test_kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from click.exceptions import Exit

from leverage.containers.kubectl import KubeCtlContainer
from leverage.containers.kubectl import KubeCtlContainer, ClusterInfo
from leverage.path import PathsHandler
from tests.test_containers import container_fixture_factory

Expand Down Expand Up @@ -125,17 +125,40 @@ def test_start_shell_sso(mock_refresh, kubectl_container):


def test_scan_clusters(kubectl_container: KubeCtlContainer):
"""
Test that we can find valid metadata.yaml presents in the down the path of the filesystem tree where we are staying.
"""
# mock and call
with mock.patch("os.walk") as mock_walk:
with mock.patch("ruamel.yaml.load") as mock_yaml:
mock_walk.return_value = [
("/foo", ["bar"], ("baz",)),
("/foo/bar", [], ("spam", "metadata.yaml")),
]
mock_yaml.return_value = {"type": "k8s-eks-cluster"}
with patch("builtins.open"):
with mock.patch("ruamel.yaml.safe_load") as mock_yaml:
mock_walk.return_value = [
("/foo", ["bar"], ("baz",)),
("/foo/bar", [], ("spam", "metadata.yaml")),
]
mock_yaml.return_value = {"type": "k8s-eks-cluster"}

first_found = next(kubectl_container._scan_clusters())
first_found = next(kubectl_container._scan_clusters())

# compare
assert first_found[0] == PosixPath("/foo/bar/")
assert first_found[1]["type"] == "k8s-eks-cluster"


def test_discover(kubectl_container: KubeCtlContainer):
"""
Test that, given a layer with a valid cluster file, we are able to call the k8s configuration routine.
"""
mocked_cluster_data = {
"type": "k8s-eks-cluster",
"data": {"cluster_name": "test", "profile": "test", "region": "us-east-1"},
}
with patch.object(kubectl_container, "_scan_clusters", return_value=[(Path.cwd(), mocked_cluster_data)]):
with patch("simple_term_menu.TerminalMenu") as mkd_show:
mkd_show.return_value.show.return_value = 0 # simulate choosing the first result
with patch.object(kubectl_container.paths, "update_cwd") as mkd_update:
with patch.object(kubectl_container, "configure") as mkd_configure:
kubectl_container.discover()

assert mkd_update.called
assert isinstance(mkd_configure.call_args_list[0][0][0], ClusterInfo)

0 comments on commit 7ca0580

Please sign in to comment.