Ejemplo n.º 1
0
def test_split_run(tmp_path: pathlib.Path,
                   sample_target_def: ostarget.TargetDefinition) -> None:
    """Test split run."""
    # common variables
    target_def_dir: pathlib.Path = tmp_path / 'target-definitions' / 'mytarget'
    target_def_file: pathlib.Path = target_def_dir / 'target-definition.yaml'
    cwd = os.getcwd()
    args = {}
    cmd = SplitCmd()

    # inner function for checking split files
    def check_split_files():
        assert target_def_dir.joinpath(
            'target-definition/metadata.yaml').exists()
        assert target_def_dir.joinpath('target-definition.yaml').exists()
        assert target_def_dir.joinpath('target-definition/targets').exists()
        assert target_def_dir.joinpath('target-definition/targets').is_dir()

        targets: Dict = Element(sample_target_def).get_at(
            ElementPath('target-definition.targets.*'))
        for uuid in targets:
            target_file = target_def_dir / f'target-definition/targets/{uuid}{const.IDX_SEP}defined-target.yaml'
            assert target_file.exists()

        assert trash.to_trash_file_path(target_def_file).exists()

    # prepare trestle project dir with the file
    def prepare_target_def_file() -> None:
        test_utils.ensure_trestle_config_dir(tmp_path)
        target_def_dir.mkdir(exist_ok=True, parents=True)
        sample_target_def.oscal_write(target_def_file)

    # test
    prepare_target_def_file()
    args = argparse.Namespace(
        file='target-definition.yaml',
        element='target-definition.targets.*,target-definition.metadata',
        verbose=0)

    os.chdir(target_def_dir)
    cmd._run(args)
    os.chdir(cwd)
    check_split_files()

    # clean before the next test
    test_utils.clean_tmp_path(target_def_dir)

    # reverse order test
    prepare_target_def_file()
    args = argparse.Namespace(
        file='target-definition.yaml',
        element='target-definition.metadata,target-definition.targets.*',
        verbose=0)
    os.chdir(target_def_dir)
    cmd._run(args)
    os.chdir(cwd)
    check_split_files()
Ejemplo n.º 2
0
def test_split_run(tmp_dir, sample_target_def: ostarget.TargetDefinition):
    """Test split run."""
    # common variables
    target_def_dir: pathlib.Path = tmp_dir / 'target-definitions' / 'mytarget'
    target_def_file: pathlib.Path = target_def_dir / 'target-definition.yaml'
    cwd = os.getcwd()
    args = {}
    cmd = SplitCmd()
    parser = cmd.parser

    # inner function for checking split files
    def check_split_files():
        assert target_def_dir.joinpath(
            'target-definition/metadata.yaml').exists()
        assert target_def_dir.joinpath('target-definition.yaml').exists()
        assert target_def_dir.joinpath('target-definition/targets').exists()
        assert target_def_dir.joinpath('target-definition/targets').is_dir()

        targets: dict = Element(sample_target_def).get_at(
            ElementPath('target-definition.targets.*'))
        for uuid in targets:
            target_file = target_def_dir / f'target-definition/targets/{uuid}{const.IDX_SEP}target.yaml'
            assert target_file.exists()

        assert trash.to_trash_file_path(target_def_file).exists()

    # prepare trestle project dir with the file
    def prepare_target_def_file():
        test_utils.ensure_trestle_config_dir(tmp_dir)
        fs.ensure_directory(target_def_dir)
        sample_target_def.oscal_write(target_def_file)

    # test
    prepare_target_def_file()
    args = parser.parse_args([
        '-f', 'target-definition.yaml', '-e',
        'target-definition.targets.*,target-definition.metadata'
    ])
    os.chdir(target_def_dir)
    cmd._run(args)
    os.chdir(cwd)
    check_split_files()

    # clean before the next test
    test_utils.clean_tmp_dir(target_def_dir)

    # reverse order test
    prepare_target_def_file()
    args = parser.parse_args([
        '-f', 'target-definition.yaml', '-e',
        'target-definition.metadata,target-definition.targets.*'
    ])
    os.chdir(target_def_dir)
    cmd._run(args)
    os.chdir(cwd)
    check_split_files()
Ejemplo n.º 3
0
def test_split_run(
        keep_cwd: pathlib.Path, tmp_path: pathlib.Path,
        sample_nist_component_def: component.ComponentDefinition) -> None:
    """Test split run."""
    # common variables
    owd = keep_cwd
    component_def_dir: pathlib.Path = tmp_path / 'component-definitions' / 'mytarget'
    component_def_file: pathlib.Path = component_def_dir / 'component-definition.yaml'
    args = {}
    cmd = SplitCmd()

    # inner function for checking split files
    def check_split_files():
        assert component_def_dir.joinpath(
            'component-definition/metadata.yaml').exists()
        assert component_def_dir.joinpath('component-definition.yaml').exists()
        assert component_def_dir.joinpath(
            'component-definition/components').exists()
        assert component_def_dir.joinpath(
            'component-definition/components').is_dir()
        # Confirm that the list items are written with the expected numbered names
        components: list = Element(sample_nist_component_def).get_at(
            ElementPath('component-definition.components.*'))
        for index in range(len(components)):
            comp_fname = f'{str(index).zfill(const.FILE_DIGIT_PREFIX_LENGTH)}{const.IDX_SEP}defined-component.yaml'
            component_file = component_def_dir / 'component-definition' / 'components' / comp_fname
            assert component_file.exists()

        assert trash.to_trash_file_path(component_def_file).exists()

    # prepare trestle project dir with the file
    def prepare_component_def_file() -> None:
        test_utils.ensure_trestle_config_dir(tmp_path)
        component_def_dir.mkdir(exist_ok=True, parents=True)
        sample_nist_component_def.oscal_write(component_def_file)

    # test
    prepare_component_def_file()
    args = argparse.Namespace(
        file='component-definition.yaml',
        element=
        'component-definition.components.*,component-definition.metadata',
        verbose=0,
        trestle_root=tmp_path)

    os.chdir(component_def_dir)
    assert cmd._run(args) == 0
    os.chdir(owd)
    check_split_files()

    # clean before the next test
    test_utils.clean_tmp_path(component_def_dir)

    # reverse order test
    prepare_component_def_file()
    args = argparse.Namespace(
        file='component-definition.yaml',
        element=
        'component-definition.metadata,component-definition.components.*',
        verbose=0,
        trestle_root=tmp_path)
    os.chdir(component_def_dir)
    assert cmd._run(args) == 0
    os.chdir(owd)
    check_split_files()