Skip to content

Commit

Permalink
test: moves negative test cases for compdef from E2E to unit
Browse files Browse the repository at this point in the history
The negative test case exit codes and output can be easily verified
through unit test and should not change when not running in a container.
Saving E2E to verify functionality inside a container environment.

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Feb 27, 2024
1 parent 8a6248d commit 4f68b7e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 81 deletions.
100 changes: 19 additions & 81 deletions tests/e2e/test_e2e_compdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@

from tests.e2e.e2e_testutils import E2ETestRunner
from tests.testutils import load_from_json, setup_for_profile, setup_rules_view
from trestlebot.const import (
ERROR_EXIT_CODE,
INVALID_ARGS_EXIT_CODE,
RULES_VIEW_DIR,
SUCCESS_EXIT_CODE,
)
from trestlebot.const import RULES_VIEW_DIR, SUCCESS_EXIT_CODE


logger = logging.getLogger(__name__)
Expand All @@ -35,7 +30,7 @@

@pytest.mark.slow
@pytest.mark.parametrize(
"test_name, command_args, response",
"test_name, command_args",
[
(
"success/happy path",
Expand All @@ -45,7 +40,6 @@
"committer-name": "test",
"committer-email": "[email protected]",
},
SUCCESS_EXIT_CODE,
),
(
"success/happy path with model skipping",
Expand All @@ -56,15 +50,6 @@
"committer-email": "test",
"skip-items": test_comp_name,
},
SUCCESS_EXIT_CODE,
),
(
"failure/missing args",
{
"branch": "test",
"rules-view-path": RULES_VIEW_DIR,
},
INVALID_ARGS_EXIT_CODE,
),
],
)
Expand All @@ -73,7 +58,6 @@ def test_rules_transform_e2e(
e2e_runner: E2ETestRunner,
test_name: str,
command_args: Dict[str, str],
response: int,
) -> None:
"""Test the trestlebot rules transform command."""
logger.info(f"Running test: {test_name}")
Expand All @@ -88,7 +72,7 @@ def test_rules_transform_e2e(
tmp_repo_str, "rules-transform", command_args
)
exit_code, response_stdout = e2e_runner.invoke_command(command)
assert exit_code == response
assert exit_code == SUCCESS_EXIT_CODE

# Check that the component definition was created
if exit_code == SUCCESS_EXIT_CODE:
Expand All @@ -106,7 +90,7 @@ def test_rules_transform_e2e(

@pytest.mark.slow
@pytest.mark.parametrize(
"test_name, command_args, response",
"test_name, command_args",
[
(
"success/happy path",
Expand All @@ -120,7 +104,6 @@ def test_rules_transform_e2e(
"committer-name": "test",
"committer-email": "[email protected]",
},
SUCCESS_EXIT_CODE,
),
(
"success/happy path with filtering",
Expand All @@ -135,49 +118,6 @@ def test_rules_transform_e2e(
"committer-email": "[email protected]",
"filter-by-profile": test_filter_prof,
},
SUCCESS_EXIT_CODE,
),
(
"failure/missing args",
{
"component-title": "test-comp",
"compdef-name": "test-compdef",
"component-description": "test",
"markdown-path": "markdown",
"branch": "test",
"committer-name": "test",
"committer-email": "[email protected]",
},
INVALID_ARGS_EXIT_CODE,
),
(
"failure/missing profile",
{
"profile-name": "fake",
"component-title": "test-comp",
"compdef-name": "test-compdef",
"component-description": "test",
"markdown-path": "markdown",
"branch": "test",
"committer-name": "test",
"committer-email": "[email protected]",
},
ERROR_EXIT_CODE,
),
(
"failure/missing filter profile",
{
"profile-name": test_prof,
"component-title": "test-comp",
"compdef-name": "test-compdef",
"component-description": "test",
"markdown-path": "markdown",
"branch": "test",
"committer-name": "test",
"committer-email": "test",
"filter-by-profile": "fake",
},
ERROR_EXIT_CODE,
),
],
)
Expand All @@ -186,7 +126,6 @@ def test_create_cd_e2e(
e2e_runner: E2ETestRunner,
test_name: str,
command_args: Dict[str, str],
response: int,
) -> None:
"""Test the trestlebot create-cd command."""
logger.info(f"Running test: {test_name}")
Expand All @@ -200,21 +139,20 @@ def test_create_cd_e2e(

command = e2e_runner.build_test_command(tmp_repo_str, "create-cd", command_args)
exit_code, _ = e2e_runner.invoke_command(command, tmp_repo_path)
assert exit_code == response
assert exit_code == SUCCESS_EXIT_CODE

# Check that all expected files were created
if response == SUCCESS_EXIT_CODE:
comp_path: pathlib.Path = ModelUtils.get_model_path_for_name_and_class(
tmp_repo_path,
command_args["compdef-name"],
ComponentDefinition,
FileContentType.JSON,
)
assert comp_path.exists()
assert (tmp_repo_path / command_args["markdown-path"]).exists()
assert (
tmp_repo_path
/ RULES_VIEW_DIR
/ command_args["compdef-name"]
/ command_args["component-title"]
).exists()
comp_path: pathlib.Path = ModelUtils.get_model_path_for_name_and_class(
tmp_repo_path,
command_args["compdef-name"],
ComponentDefinition,
FileContentType.JSON,
)
assert comp_path.exists()
assert (tmp_repo_path / command_args["markdown-path"]).exists()
assert (
tmp_repo_path
/ RULES_VIEW_DIR
/ command_args["compdef-name"]
/ command_args["component-title"]
).exists()
95 changes: 95 additions & 0 deletions tests/trestlebot/entrypoints/test_create_cd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2024 Red Hat, Inc.


"""Test for Create CD CLI"""

import logging
import pathlib
from typing import Any, Dict
from unittest.mock import patch

import pytest

from tests.testutils import args_dict_to_list, setup_for_compdef
from trestlebot.entrypoints.create_cd import main as cli_main


@pytest.fixture
def valid_args_dict() -> Dict[str, str]:
return {
"profile-name": "simplified_nist_profile",
"component-title": "test-comp",
"compdef-name": "test-compdef",
"component-description": "test",
"markdown-path": "markdown",
"branch": "test",
"committer-name": "test",
"committer-email": "[email protected]",
}


test_comp_name = "test_comp"
test_ssp_cd = "md_cd"


def test_create_cd_with_missing_args(
tmp_trestle_dir: str, valid_args_dict: Dict[str, str]
) -> None:
"""Test create cd and trigger error."""
tmp_repo_path = pathlib.Path(tmp_trestle_dir)

args_dict = valid_args_dict
del args_dict["profile-name"]

_ = setup_for_compdef(tmp_repo_path, test_comp_name, test_ssp_cd)

with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]):
with pytest.raises(SystemExit, match="2"):
cli_main()


def test_create_cd_with_missing_profile(
tmp_trestle_dir: str, valid_args_dict: Dict[str, str], caplog: Any
) -> None:
"""Test create cd and trigger error."""
tmp_repo_path = pathlib.Path(tmp_trestle_dir)

args_dict = valid_args_dict
args_dict["profile-name"] = "invalid_prof"
args_dict["working-dir"] = tmp_trestle_dir

_ = setup_for_compdef(tmp_repo_path, test_comp_name, test_ssp_cd)

with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]):
with pytest.raises(SystemExit, match="1"):
cli_main()

assert any(
record.levelno == logging.ERROR
and "Profile invalid_prof does not exist in the workspace" in record.message
for record in caplog.records
)


def test_create_cd_with_missing_filter_profile(
tmp_trestle_dir: str, valid_args_dict: Dict[str, str], caplog: Any
) -> None:
"""Test create cd and trigger error."""
tmp_repo_path = pathlib.Path(tmp_trestle_dir)

args_dict = valid_args_dict
args_dict["filter-by-profile"] = "invalid_prof"
args_dict["working-dir"] = tmp_trestle_dir

_ = setup_for_compdef(tmp_repo_path, test_comp_name, test_ssp_cd)

with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]):
with pytest.raises(SystemExit, match="1"):
cli_main()

assert any(
record.levelno == logging.ERROR
and "Profile invalid_prof does not exist in the workspace" in record.message
for record in caplog.records
)

0 comments on commit 4f68b7e

Please sign in to comment.