Skip to content

Commit

Permalink
feat: add unit test for validation component
Browse files Browse the repository at this point in the history
Signed-off-by: Sophia Wang <huiwang@redhat.com>
  • Loading branch information
huiwangredhat committed Jan 15, 2025
1 parent 8ce4f15 commit 59016fb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
44 changes: 44 additions & 0 deletions tests/trestlebot/cli/test_sync_cac_content_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,47 @@ def test_sync_product(tmp_repo: Tuple[str, Repo]) -> None:
"file_groupownership_sshd_private_key",
"sshd_set_keepalive",
]


def test_sync_product_create_validation_component(tmp_repo: Tuple[str, Repo]) -> None:
"""Tests sync Cac content to create validation component."""
repo_dir, _ = tmp_repo
repo_path = pathlib.Path(repo_dir)
setup_for_catalog(repo_path, test_cat, "catalog")
setup_for_profile(repo_path, test_prof, "profile")

runner = CliRunner()
result = runner.invoke(
sync_cac_content_cmd,
[
"--product",
test_product,
"--repo-path",
str(repo_path.resolve()),
"--cac-content-root",
test_content_dir,
"--cac-profile",
test_cac_profile,
"--oscal-profile",
test_prof,
"--committer-email",
"test@email.com",
"--committer-name",
"test name",
"--branch",
"test",
"--dry-run",
"--component-definition-type",
"validation",
],
)
# Check the CLI sync-cac-content is successful
component_definition = repo_path.joinpath(test_comp_path)
assert result.exit_code == 0
# Check if the component definition is created
assert component_definition.exists()
compdef = ComponentDefinition.oscal_read(component_definition)
component = compdef.components[0]
assert len(component.props) == 12
assert component.title == "openscap"
assert component.type == "validation"
17 changes: 12 additions & 5 deletions trestlebot/transformers/cac_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ def get_component_info(product_name: str, cac_path: str) -> Tuple[str, str]:
raise ValueError("component_title is empty or None")


def get_validation_component_mapping(
props: List[Dict[str, str]]
) -> List[Dict[str, str]]:
def transform_property(prop: Property) -> Dict[str, str]:
return {
"name": prop.name,
"ns": prop.ns,
"value": prop.value,
"remarks": prop.remarks,
}


def get_validation_component_mapping(props: Property) -> List[Dict[str, str]]:
"""
Adds a new "Check_Id" and "Check_Description" to the props based on the
"Rule_Id" value and "Rule_Description".
Expand All @@ -49,10 +56,10 @@ def get_validation_component_mapping(
List[Dict]: The updated list with the new "Check_Id" and
"Check_Description" entry.
"""
props = props
transformed_list = [transform_property(prop) for prop in props]
rule_check_mapping = []
check_id_entry = {}
for prop in props:
for prop in transformed_list:
if prop["name"] == "Rule_Id":
rule_check_mapping.append(prop)
check_id_entry = {
Expand Down

0 comments on commit 59016fb

Please sign in to comment.