Skip to content

Commit

Permalink
feat: adds regenerate task task testing to trestlebot pretasks
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <barnabei.jennifer@gmail.com>
  • Loading branch information
jpower432 committed Jun 30, 2023
1 parent a2a3b7e commit 68cedab
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 97 deletions.
8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ inputs:
description: OSCAL Model type to assemble. Values can be catalog, profile, compdef, or ssp.
required: true
check_only:
description: "Runs tasks and check if exit with an error if there is a diff. Default to false"
description: "Runs tasks and exits with an error if there is a diff. Defaults to false"
required: false
default: false
skip-assemble:
skip_assemble:
description: "Skip assembly task. Defaults to false"
required: false
default: false
skip-regenerate:
skip_regenerate:
description: "Skip regenerate task. Defaults to false."
required: false
default: false
skip_items:
description: "Comma-separated list of content by Trestle name to skip during task execution. For example `profile_x,profile_y`".
description: "Space-separated list of content by Trestle name to skip during task execution. For example `profile_x profile_y`".
required: false
default: ""
ssp_index_path:
Expand Down
1 change: 1 addition & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ output=$(python3.8 -m trestlebot \
--skip-assemble="${INPUT_SKIP_ASSMEBLE}" \
--skip-regenerate="${INPUT_SKIP_REGENERATE}" \
--check-only="${INPUT_CHECK_ONLY}" \
--skip-items="${INPUT_SKIP_ITEMS}" \
--working-dir="${INPUT_REPOSITORY}" | tee /dev/fd/3)

commit=$(echo "$output" | grep "Commit Hash:" | sed 's/.*: //')
Expand Down
51 changes: 47 additions & 4 deletions tests/trestlebot/tasks/authored/test_ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pathlib

import pytest
from trestle.common import const
from trestle.common.model_utils import ModelUtils
from trestle.core.commands.author.ssp import SSPGenerate
from trestle.core.models.file_content_type import FileContentType
Expand Down Expand Up @@ -76,23 +77,65 @@ def test_assemble_no_ssp_entry(tmp_trestle_dir: str) -> None:

authored_ssp = AuthoredSSP(tmp_trestle_dir, ssp_index)

with pytest.raises(AuthoredObjectException):
with pytest.raises(AuthoredObjectException, match="SSP test-ssp does not exists in the index"):
authored_ssp.assemble(md_path)


def test_regenerate(tmp_trestle_dir: str) -> None:
"""Test to test regenerate functionality for SSPs"""
# Prepare the workspace and generate the markdown
trestle_root = pathlib.Path(tmp_trestle_dir)
md_path = os.path.join(markdown_dir, test_ssp_output)
_ = testutils.setup_for_ssp(trestle_root, test_prof, test_comp, md_path)

ssp_index_path = os.path.join(tmp_trestle_dir, "ssp-index.json")
testutils.write_index_json(ssp_index_path, test_ssp_output, test_prof, [test_comp])
ssp_index: SSPIndex = SSPIndex(ssp_index_path)

authored_ssp = AuthoredSSP(tmp_trestle_dir, ssp_index)

# Run to ensure no exceptions are raised
model_path = os.path.join(const.MODEL_DIR_SSP, test_ssp_output)
authored_ssp.regenerate(model_path, md_path)

assert os.path.exists(os.path.join(tmp_trestle_dir, markdown_dir, test_ssp_output))


def test_regenerate_no_ssp_entry(tmp_trestle_dir: str) -> None:
"""Test to trigger failure for missing SSP index"""
# Prepare the workspace and generate the markdown
trestle_root = pathlib.Path(tmp_trestle_dir)
md_path = os.path.join(markdown_dir, test_ssp_output)
_ = testutils.setup_for_ssp(trestle_root, test_prof, test_comp, md_path)

ssp_index_path = os.path.join(tmp_trestle_dir, "ssp-index.json")
testutils.write_index_json(ssp_index_path, "fake", test_prof, [test_comp])
ssp_index: SSPIndex = SSPIndex(ssp_index_path)

authored_ssp = AuthoredSSP(tmp_trestle_dir, ssp_index)

model_path = os.path.join(const.MODEL_DIR_SSP, test_ssp_output)
with pytest.raises(AuthoredObjectException, match="SSP test-ssp does not exists in the index"):
authored_ssp.regenerate(model_path, md_path)


# SSPIndex tests


def test_get_comps_by_ssp(tmp_trestle_dir: str) -> None:
"""Test to get formatted component definition string"""
"""Test to get component definition list from index"""
ssp_index_path = os.path.join(tmp_trestle_dir, "ssp-index.json")
testutils.write_index_json(
ssp_index_path, test_ssp_output, test_prof, [test_comp, "another_comp"]
)
ssp_index: SSPIndex = SSPIndex(ssp_index_path)

assert ssp_index.get_comps_by_ssp(test_ssp_output) == "test_comp,another_comp"
assert test_comp in ssp_index.get_comps_by_ssp(test_ssp_output)
assert "another_comp" in ssp_index.get_comps_by_ssp(test_ssp_output)


def test_get_profile_by_ssp(tmp_trestle_dir: str) -> None:
"""Test to get formatted component definition string"""
"""Test to get profile from index"""
ssp_index_path = os.path.join(tmp_trestle_dir, "ssp-index.json")
testutils.write_index_json(ssp_index_path, test_ssp_output, test_prof, [test_comp])
ssp_index: SSPIndex = SSPIndex(ssp_index_path)
Expand Down
12 changes: 6 additions & 6 deletions tests/trestlebot/tasks/test_assemble_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.

"""Test for Trestle Bot assemble"""
"""Test for Trestle Bot assemble task"""

import os
import pathlib
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_catalog_assemble_task(tmp_trestle_dir: str) -> None:

assemble_task = AssembleTask(
tmp_trestle_dir,
AuthoredType.CATALOG,
AuthoredType.CATALOG.value,
cat_md_dir,
"",
)
Expand All @@ -67,7 +67,7 @@ def test_profile_assemble_task(tmp_trestle_dir: str) -> None:
assert profile_generate._run(args) == 0
assemble_task = AssembleTask(
tmp_trestle_dir,
AuthoredType.PROFILE,
AuthoredType.PROFILE.value,
prof_md_dir,
"",
)
Expand All @@ -83,7 +83,7 @@ def test_compdef_assemble_task(tmp_trestle_dir: str) -> None:
assert comp_generate._run(args) == 0
assemble_task = AssembleTask(
tmp_trestle_dir,
AuthoredType.COMPDEF,
AuthoredType.COMPDEF.value,
compdef_md_dir,
"",
)
Expand All @@ -103,7 +103,7 @@ def test_ssp_assemble_task(tmp_trestle_dir: str) -> None:

assemble_task = AssembleTask(
tmp_trestle_dir,
AuthoredType.SSP,
AuthoredType.SSP.value,
ssp_md_dir,
ssp_index_path,
)
Expand All @@ -120,7 +120,7 @@ def test_ssp_assemble_task_no_index_path(tmp_trestle_dir: str) -> None:

assemble_task = AssembleTask(
tmp_trestle_dir,
AuthoredType.SSP,
AuthoredType.SSP.value,
ssp_md_dir,
"",
)
Expand Down
158 changes: 158 additions & 0 deletions tests/trestlebot/tasks/test_regenerate_task .py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#!/usr/bin/python

# Copyright 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Test for Trestle Bot regenerate task"""

import argparse
import os
import pathlib

import pytest
from trestle.core.commands.author.ssp import SSPAssemble, SSPGenerate

from tests import testutils
from trestlebot.tasks.authored.types import AuthoredType
from trestlebot.tasks.regenerate_task import RegenerateTask


test_prof = "simplified_nist_profile"
test_comp = "test_comp"
test_cat = "simplified_nist_catalog"
test_ssp_output = "my-ssp"

cat_md_dir = "md_cat"
prof_md_dir = "md_prof"
compdef_md_dir = "md_comp"
ssp_md_dir = "md_ssp"


def test_catalog_regenerate_task(tmp_trestle_dir: str) -> None:
"""Test catalog regenerate at the task level"""
trestle_root = pathlib.Path(tmp_trestle_dir)
md_path = os.path.join(cat_md_dir, test_cat)
_ = testutils.setup_for_catalog(trestle_root, test_cat, md_path)

regenerate_task = RegenerateTask(
tmp_trestle_dir,
AuthoredType.CATALOG.value,
cat_md_dir,
"",
)
assert regenerate_task.execute() == 0
assert os.path.exists(os.path.join(tmp_trestle_dir, md_path))


def test_profile_regenerate_task(tmp_trestle_dir: str) -> None:
"""Test profile regenerate at the task level"""
trestle_root = pathlib.Path(tmp_trestle_dir)
md_path = os.path.join(prof_md_dir, test_prof)
_ = testutils.setup_for_profile(trestle_root, test_prof, md_path)

regenerate_task = RegenerateTask(
tmp_trestle_dir,
AuthoredType.PROFILE.value,
prof_md_dir,
"",
)
assert regenerate_task.execute() == 0
assert os.path.exists(os.path.join(tmp_trestle_dir, md_path))


def test_compdef_regenerate_task(tmp_trestle_dir: str) -> None:
"""Test compdef regenerate at the task level"""
trestle_root = pathlib.Path(tmp_trestle_dir)
md_path = os.path.join(compdef_md_dir, test_comp)
_ = testutils.setup_for_compdef(trestle_root, test_comp, md_path)

regenerate_task = RegenerateTask(
tmp_trestle_dir,
AuthoredType.COMPDEF.value,
compdef_md_dir,
"",
)
assert regenerate_task.execute() == 0
assert os.path.exists(os.path.join(tmp_trestle_dir, md_path))


def test_ssp_regenerate_task(tmp_trestle_dir: str) -> None:
"""Test ssp regenerate at the task level"""
ssp_index_path = os.path.join(tmp_trestle_dir, "ssp-index.json")
testutils.write_index_json(ssp_index_path, test_ssp_output, test_prof, [test_comp])

trestle_root = pathlib.Path(tmp_trestle_dir)
md_path = os.path.join(ssp_md_dir, test_ssp_output)

# Create initial SSP for testing
args = testutils.setup_for_ssp(trestle_root, test_prof, test_comp, md_path)
ssp_generate = SSPGenerate()
assert ssp_generate._run(args) == 0

# create ssp from the markdown
ssp_assemble = SSPAssemble()
args = argparse.Namespace(
trestle_root=tmp_trestle_dir,
markdown=md_path,
output=test_ssp_output,
verbose=0,
name=None,
version=None,
regenerate=False,
compdefs=args.compdefs,
)
assert ssp_assemble._run(args) == 0

regenerate_task = RegenerateTask(
tmp_trestle_dir,
AuthoredType.SSP.value,
ssp_md_dir,
ssp_index_path,
)
assert regenerate_task.execute() == 0
assert os.path.exists(os.path.join(tmp_trestle_dir, md_path))


def test_ssp_regenerate_task_no_index_path(tmp_trestle_dir: str) -> None:
"""Test ssp regenerate at the task level with failure"""
trestle_root = pathlib.Path(tmp_trestle_dir)
md_path = os.path.join(ssp_md_dir, test_ssp_output)

# Create initial SSP for testing
args = testutils.setup_for_ssp(trestle_root, test_prof, test_comp, md_path)
ssp_generate = SSPGenerate()
assert ssp_generate._run(args) == 0

# create ssp from the markdown
ssp_assemble = SSPAssemble()
args = argparse.Namespace(
trestle_root=tmp_trestle_dir,
markdown=md_path,
output=test_ssp_output,
verbose=0,
name=None,
version=None,
regenerate=False,
compdefs=args.compdefs,
)
assert ssp_assemble._run(args) == 0
regenerate_task = RegenerateTask(
tmp_trestle_dir,
AuthoredType.SSP.value,
ssp_md_dir,
"",
)

with pytest.raises(FileNotFoundError):
regenerate_task.execute()
Loading

0 comments on commit 68cedab

Please sign in to comment.