Skip to content

Commit

Permalink
Builds local tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mab879 committed Feb 11, 2025
1 parent 0f151a1 commit f5b06a8
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 15 deletions.
78 changes: 78 additions & 0 deletions build-scripts/build_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/python3

import argparse
import pathlib

import ssg.environment
import ssg.jinja
import ssg.utils
import ssg.yaml
import ssg.templates
import tests.ssg_test_suite.common
import tests.ssg_test_suite.rule

SSG_ROOT = str(pathlib.Path(__file__).resolve().parent.parent.absolute())

def _create_arg_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Converts builds content tests to be rendered"
)
parser.add_argument(
"--build-config-yaml", required=True,
help="YAML file with information about the build configuration. "
"e.g.: ~/scap-security-guide/build/build_config.yml"
)
parser.add_argument(
"--product-yaml", required=True,
help="YAML file with information about the product we are building. "
"e.g.: ~/scap-security-guide/rhel10/product.yml"
)
parser.add_argument(
"--output", required=True,
help="Output path"
"e.g.: ~/scap-security-guide/build/rhel10/tests"
)
parser.add_argument(
"--resolved-rules-dir", required=True,
help="Directory with <rule-id>.yml resolved rule YAMLs"
"e.g.: ~/scap-security-guide/build/rhel10/rules"
)
parser.add_argument("--root", default=SSG_ROOT,
help=f"Path to the project. Defaults to {SSG_ROOT}")
return parser


def main() -> int:
args = _create_arg_parser().parse_args()
env_yaml = ssg.environment.open_environment(
args.build_config_yaml, args.product_yaml)

product = ssg.utils.required_key(env_yaml, "product")
benchmark_cpes = { env_yaml["cpes"][0][product]["name"], }
resolved_rules_dir = pathlib.Path(args.resolved_rules_dir)

for rule_file in resolved_rules_dir.iterdir(): # type: pathlib.Path
rendered_rule_obj = ssg.yaml.open_raw(str(rule_file))
rule_path = pathlib.Path(rendered_rule_obj["definition_location"])
rule_root = rule_path.parent
rule_id = rule_root.name
tests_root = rule_root / "tests"
output_path = pathlib.Path(args.output) / rule_id
if tests_root.exists():
for test in tests_root.iterdir(): # type: pathlib.Path
if not test.name.endswith(".sh"):
continue
output_path.mkdir(parents=True, exist_ok=True)
file_contents = open(str(test.absolute())).read()
s = tests.ssg_test_suite.rule.Scenario(test.name, file_contents)
if s.matches_platform(benchmark_cpes):
content = ssg.jinja.process_file_with_macros(str(test.absolute()), env_yaml)
with open(output_path / test.name, 'w') as file:
file.write(content)


return 0


if __name__ == "__main__":
raise SystemExit(main())
1 change: 0 additions & 1 deletion tests/ssg_test_suite/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from ssg.products import product_yaml_path, load_product_yaml
from ssg.rules import get_rule_dir_yaml
from ssg.utils import mkdir_p
from ssg_test_suite.log import LogHelper

import ssg.templates

Expand Down
7 changes: 3 additions & 4 deletions tests/ssg_test_suite/oscap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import os.path
import re
import socket
import subprocess
import sys
import time
import xml.etree.ElementTree

from ssg.constants import OSCAP_PROFILE_ALL_ID

from ssg_test_suite.log import LogHelper
from ssg_test_suite import test_env
from ssg_test_suite import common
from tests.ssg_test_suite.log import LogHelper
from tests.ssg_test_suite import test_env
from tests.ssg_test_suite import common

from ssg.shims import input_func

Expand Down
10 changes: 5 additions & 5 deletions tests/ssg_test_suite/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
from ssg.jinja import process_file_with_macros
from ssg.rules import is_rule_dir

from ssg_test_suite import oscap
from ssg_test_suite import xml_operations
from ssg_test_suite import test_env
from ssg_test_suite import common
from ssg_test_suite.log import LogHelper
from tests.ssg_test_suite import oscap
from tests.ssg_test_suite import xml_operations
from tests.ssg_test_suite import test_env
from tests.ssg_test_suite import common
from tests.ssg_test_suite.log import LogHelper


Rule = collections.namedtuple(
Expand Down
5 changes: 2 additions & 3 deletions tests/ssg_test_suite/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import sys
import time

import ssg_test_suite
from ssg_test_suite import common
from ssg_test_suite.log import LogHelper
from tests.ssg_test_suite import common
from tests.ssg_test_suite.log import LogHelper


class SavedState(object):
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/ssg_test_suite/test_rule.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
import pytest

from ssg_test_suite import rule
from tests.ssg_test_suite.rule import Scenario
from ssg.constants import OSCAP_PROFILE_ALL_ID

Expand Down

0 comments on commit f5b06a8

Please sign in to comment.