Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functional tests #69

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions must_gather_explorer/tests/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MUST_GATHER_PATH_FOR_TESTS = "must_gather_explorer/tests/manifests/must-gather-data"
6 changes: 3 additions & 3 deletions must_gather_explorer/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sys

from cmd2.utils import StdSim

from must_gather_explorer.tests.constants import MUST_GATHER_PATH_FOR_TESTS
from must_gather_explorer.utils import get_all_resources, get_all_yaml_and_log_files, read_aliases_file
import pytest
from must_gather_explorer.prompt_handler import MustGatherExplorerPrompt
Expand Down Expand Up @@ -39,9 +41,7 @@ def aliases_file():

@pytest.fixture(scope="module")
def yaml_files():
all_yaml_files, _ = get_all_yaml_and_log_files(
must_gather_path="must_gather_explorer/tests/manifests/must-gather-data"
)
all_yaml_files, _ = get_all_yaml_and_log_files(must_gather_path=MUST_GATHER_PATH_FOR_TESTS)
yield get_all_resources(all_yaml_files=all_yaml_files)


Expand Down
74 changes: 74 additions & 0 deletions must_gather_explorer/tests/test_functional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import sys

from must_gather_explorer.tests.constants import MUST_GATHER_PATH_FOR_TESTS
from must_gather_explorer.utils import (
get_aliases_file_path,
read_aliases_file,
get_all_yaml_and_log_files,
get_all_resources,
get_resources,
get_cluster_resources_raw_data,
get_resource_kind_by_alias,
)

from io import StringIO


class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self

def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout


def test_get_aliases_file_path():
get_aliases_file_path()


def test_read_aliases_file():
assert read_aliases_file()


def test_get_all_yaml_and_log_files():
all_yaml_files, all_log_files = get_all_yaml_and_log_files(must_gather_path=MUST_GATHER_PATH_FOR_TESTS)
assert all_yaml_files, "No yaml files found"
assert all_log_files, "No log files found"


def test_get_all_resources():
all_yaml_files, _ = get_all_yaml_and_log_files(must_gather_path=MUST_GATHER_PATH_FOR_TESTS)
all_resources_dict = get_all_resources(all_yaml_files=all_yaml_files)

for pod in all_resources_dict["pod"]:
assert pod.get("name"), f"Pod name is missing: {pod}"
assert pod.get("namespace"), f"Pod namespace is missing: {pod}"
assert pod.get("yaml_file"), f"Pod yaml_file is missing: {pod}"

# TODO: add test for all_log_files


def test_get_resource_kind_by_alias():
assert get_resource_kind_by_alias(resources_aliases=read_aliases_file(), requested_kind="Pod")


# TODO add parameters for different querries (pod, node, with yaml, with yaml fields, with name, with partial name)
def test_get_resources():
all_yaml_files, _ = get_all_yaml_and_log_files(must_gather_path=MUST_GATHER_PATH_FOR_TESTS)
all_resources_dict = get_all_resources(all_yaml_files=all_yaml_files)
aliases = read_aliases_file()
cluster_resources_raw_data = get_cluster_resources_raw_data(
all_resources=all_resources_dict, resources_aliases=aliases, kind="pod"
)

with Capturing() as output:
get_resources(resources_raw_data=cluster_resources_raw_data)

output_string = "\n".join(output)

for item in ("NAMESPACE", "NAME", "kube-system", "csi-nfs-controller-c54d89cd5-gbd5s", "csi-nfs-node-2r5tx"):
assert item in output_string
2 changes: 1 addition & 1 deletion must_gather_explorer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def get_describe(**kwargs: dict[Any, Any]) -> bool:


def get_cluster_resources_raw_data(
resources_aliases: Any, all_resources: dict[str, Any], kind: str, name: str, namespace: str
resources_aliases: Any, all_resources: dict[str, Any], kind: str, name: str = "", namespace: str = ""
) -> list[dict[str, Any]]:
resources_list: list[dict[str, Any]] = []

Expand Down