Skip to content

Commit

Permalink
test: improves assemble task test by adding additional assertions
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Oct 16, 2023
1 parent f514b3a commit 226e8ce
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
14 changes: 14 additions & 0 deletions tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,17 @@ def write_index_json(

with open(file_path, "w") as file:
json.dump(data, file, indent=4)


def replace_string_in_file(file_path: str, old_string: str, new_string: str) -> None:
"""Replace a string in a file."""
# Read the content of the file
with open(file_path, "r") as file:
file_content = file.read()

# Replace the old string with the new string
updated_content = file_content.replace(old_string, new_string)

# Write the updated content back to the file
with open(file_path, "w") as file:
file.write(updated_content)
52 changes: 52 additions & 0 deletions tests/trestlebot/tasks/test_assemble_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
from unittest.mock import Mock, patch

import pytest
from trestle.common.model_utils import ModelUtils
from trestle.core.commands.author.catalog import CatalogGenerate
from trestle.core.commands.author.component import ComponentGenerate
from trestle.core.commands.author.profile import ProfileGenerate
from trestle.core.commands.author.ssp import SSPGenerate
from trestle.core.models.file_content_type import FileContentType
from trestle.oscal import catalog as oscal_cat
from trestle.oscal import component as oscal_comp
from trestle.oscal import profile as oscal_prof

from tests import testutils
from trestlebot.tasks.assemble_task import AssembleTask
Expand Down Expand Up @@ -114,6 +119,12 @@ def test_catalog_assemble_task(tmp_trestle_dir: str) -> None:
cat_generate = CatalogGenerate()
assert cat_generate._run(args) == 0

# Get original last modified time
cat, _ = ModelUtils.load_model_for_class(
trestle_root, test_cat, oscal_cat.Catalog, FileContentType.JSON
)
orig_time = cat.metadata.last_modified

assemble_task = AssembleTask(
tmp_trestle_dir,
AuthoredType.CATALOG.value,
Expand All @@ -122,6 +133,12 @@ def test_catalog_assemble_task(tmp_trestle_dir: str) -> None:
)
assert assemble_task.execute() == 0

# Get new last modified time and verify catalog was modified
cat, _ = ModelUtils.load_model_for_class(
trestle_root, test_cat, oscal_cat.Catalog, FileContentType.JSON
)
assert orig_time != cat.metadata.last_modified


def test_profile_assemble_task(tmp_trestle_dir: str) -> None:
"""Test profile assemble at the task level"""
Expand All @@ -130,6 +147,13 @@ def test_profile_assemble_task(tmp_trestle_dir: str) -> None:
args = testutils.setup_for_profile(trestle_root, test_prof, md_path)
profile_generate = ProfileGenerate()
assert profile_generate._run(args) == 0

# Get original last modified time
prof, _ = ModelUtils.load_model_for_class(
trestle_root, test_prof, oscal_prof.Profile, FileContentType.JSON
)
orig_time = prof.metadata.last_modified

assemble_task = AssembleTask(
tmp_trestle_dir,
AuthoredType.PROFILE.value,
Expand All @@ -138,6 +162,12 @@ def test_profile_assemble_task(tmp_trestle_dir: str) -> None:
)
assert assemble_task.execute() == 0

# Get new last modified time adn verify profile was modified
prof, _ = ModelUtils.load_model_for_class(
trestle_root, test_prof, oscal_prof.Profile, FileContentType.JSON
)
assert orig_time != prof.metadata.last_modified


def test_compdef_assemble_task(tmp_trestle_dir: str) -> None:
"""Test compdef assemble at the task level"""
Expand All @@ -146,6 +176,18 @@ def test_compdef_assemble_task(tmp_trestle_dir: str) -> None:
args = testutils.setup_for_compdef(trestle_root, test_comp, md_path)
comp_generate = ComponentGenerate()
assert comp_generate._run(args) == 0

# Get ac-1 markdown file
comp_path = os.path.join(trestle_root, compdef_md_dir, test_comp, test_comp)
ac1_md_path = os.path.join(comp_path, test_prof, "ac", "ac-1.md")
testutils.replace_string_in_file(ac1_md_path, "partial", "implemented")

# Get original last modified time
comp, _ = ModelUtils.load_model_for_class(
trestle_root, test_comp, oscal_comp.ComponentDefinition, FileContentType.JSON
)
orig_time = comp.metadata.last_modified

assemble_task = AssembleTask(
tmp_trestle_dir,
AuthoredType.COMPDEF.value,
Expand All @@ -154,6 +196,12 @@ def test_compdef_assemble_task(tmp_trestle_dir: str) -> None:
)
assert assemble_task.execute() == 0

# Get new last modified time and verify component was modified
comp, _ = ModelUtils.load_model_for_class(
trestle_root, test_comp, oscal_comp.ComponentDefinition, FileContentType.JSON
)
assert orig_time != comp.metadata.last_modified


def test_ssp_assemble_task(tmp_trestle_dir: str) -> None:
"""Test ssp assemble at the task level"""
Expand All @@ -174,6 +222,10 @@ def test_ssp_assemble_task(tmp_trestle_dir: str) -> None:
)
assert assemble_task.execute() == 0

assert os.path.exists(
os.path.join(tmp_trestle_dir, "system-security-plans", test_ssp_output)
)


def test_ssp_assemble_task_no_index_path(tmp_trestle_dir: str) -> None:
"""Test ssp assemble at the task level with failure"""
Expand Down
7 changes: 5 additions & 2 deletions tests/trestlebot/tasks/test_regenerate_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def test_profile_regenerate_task(tmp_trestle_dir: str) -> None:
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, test_comp)

md_path = os.path.join(compdef_md_dir, test_comp)
_ = testutils.setup_for_compdef(trestle_root, test_comp, md_path)

regenerate_task = RegenerateTask(
Expand All @@ -161,7 +162,9 @@ def test_compdef_regenerate_task(tmp_trestle_dir: str) -> None:
"",
)
assert regenerate_task.execute() == 0
assert os.path.exists(os.path.join(tmp_trestle_dir, md_path))

# The compdef is a special case where each component has a separate markdown directory
assert os.path.exists(os.path.join(tmp_trestle_dir, md_path, test_comp))


def test_ssp_regenerate_task(tmp_trestle_dir: str) -> None:
Expand Down

0 comments on commit 226e8ce

Please sign in to comment.