Ejemplo n.º 1
0
def test_split_relative_path(tmp_path, keep_cwd: pathlib.Path,
                             simplified_nist_catalog: oscatalog.Catalog,
                             monkeypatch: MonkeyPatch) -> None:
    """Test split with relative path."""
    # prepare trestle project dir with the file
    cat_name = 'mycat'
    trestle_root = test_utils.create_trestle_project_with_model(
        tmp_path, simplified_nist_catalog, cat_name, monkeypatch)

    orig_model: oscatalog.Catalog = simplified_nist_catalog

    os.chdir(trestle_root)
    catalog_dir = trestle_root / 'catalogs' / cat_name
    catalog_file: pathlib.Path = catalog_dir / 'catalog.json'

    args = argparse.Namespace(file='catalogs/mycat/catalog.json',
                              element='catalog.metadata',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 0

    # merge receives an element path not a file path
    # so need to chdir to where the file is
    os.chdir(catalog_dir)
    args = argparse.Namespace(element='catalog.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert MergeCmd()._run(args) == 0

    new_model: oscatalog.Catalog = oscatalog.Catalog.oscal_read(catalog_file)
    assert test_utils.models_are_equivalent(orig_model, new_model)
Ejemplo n.º 2
0
def test_split_deep(tmp_path, keep_cwd: pathlib.Path,
                    simplified_nist_catalog: oscatalog.Catalog,
                    monkeypatch: MonkeyPatch) -> None:
    """Test deep split of model."""
    # prepare trestle project dir with the file
    cat_name = 'mycat'
    trestle_root = test_utils.create_trestle_project_with_model(
        tmp_path, simplified_nist_catalog, cat_name, monkeypatch)

    orig_model: oscatalog.Catalog = simplified_nist_catalog

    catalog_dir = trestle_root / 'catalogs' / cat_name
    catalog_file: pathlib.Path = catalog_dir / 'catalog.json'

    os.chdir(catalog_dir)
    args = argparse.Namespace(file='catalog.json',
                              element='catalog.groups.*.controls.*.controls.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 0

    args = argparse.Namespace(element='catalog.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert MergeCmd()._run(args) == 0

    new_model: oscatalog.Catalog = oscatalog.Catalog.oscal_read(catalog_file)
    assert test_utils.models_are_equivalent(orig_model, new_model)
Ejemplo n.º 3
0
def test_split_tutorial_workflow(tmp_path, keep_cwd: pathlib.Path,
                                 simplified_nist_catalog: oscatalog.Catalog,
                                 monkeypatch: MonkeyPatch) -> None:
    """Test split operations and final re-merge in workflow tutorial."""
    # prepare trestle project dir with the file
    cat_name = 'mycat'
    trestle_root = test_utils.create_trestle_project_with_model(
        tmp_path, simplified_nist_catalog, cat_name, monkeypatch)

    catalog_dir = trestle_root / 'catalogs' / cat_name
    catalog_file: pathlib.Path = catalog_dir / 'catalog.json'
    orig_model = oscatalog.Catalog.oscal_read(catalog_file)

    # step0
    os.chdir(catalog_dir)
    args = argparse.Namespace(
        file='catalog.json',
        element='catalog.metadata,catalog.groups,catalog.back-matter',
        verbose=1,
        trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 0

    # step1
    os.chdir('catalog')
    args = argparse.Namespace(file='metadata.json',
                              element='metadata.roles,metadata.parties',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 0

    # step2
    os.chdir('metadata')
    args = argparse.Namespace(file='roles.json',
                              element='roles.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 0
    args = argparse.Namespace(file='parties.json',
                              element='parties.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 0

    # step3
    os.chdir('..')
    args = argparse.Namespace(file='./groups.json',
                              element='groups.*.controls.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 0

    # step4
    os.chdir(catalog_dir)
    args = argparse.Namespace(element='catalog.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert MergeCmd()._run(args) == 0

    new_model = oscatalog.Catalog.oscal_read(catalog_file)
    assert test_utils.models_are_equivalent(orig_model, new_model)
Ejemplo n.º 4
0
def test_no_file_given(tmp_path, keep_cwd: pathlib.Path,
                       simplified_nist_catalog: oscatalog.Catalog,
                       monkeypatch: MonkeyPatch) -> None:
    """Test split with no file specified."""
    # prepare trestle project dir with the file
    cat_name = 'mycat'
    trestle_root = test_utils.create_trestle_project_with_model(
        tmp_path, simplified_nist_catalog, cat_name, monkeypatch)

    orig_model: oscatalog.Catalog = simplified_nist_catalog

    catalog_dir = trestle_root / 'catalogs' / cat_name
    catalog_file: pathlib.Path = catalog_dir / 'catalog.json'

    # no file given and cwd not in trestle directory should fail
    os.chdir(tmp_path)
    args = argparse.Namespace(file=None,
                              element='catalog.groups',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 1

    os.chdir(catalog_dir)
    args = argparse.Namespace(file=None,
                              element='catalog.groups,catalog.metadata',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 0
    assert (catalog_dir / 'catalog/groups.json').exists()
    assert (catalog_dir / 'catalog/metadata.json').exists()

    os.chdir('./catalog')
    args = argparse.Namespace(file=None,
                              element='groups.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 0
    assert (catalog_dir / 'catalog/groups/00000__group.json').exists()

    os.chdir('./groups')
    args = argparse.Namespace(file='00000__group.json',
                              element='group.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 0

    os.chdir(catalog_dir)
    args = argparse.Namespace(file=None,
                              element='catalog.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert MergeCmd()._run(args) == 0

    new_model: oscatalog.Catalog = oscatalog.Catalog.oscal_read(catalog_file)
    assert test_utils.models_are_equivalent(orig_model, new_model)
Ejemplo n.º 5
0
def test_split_comp_def(
        mode, tmp_path, keep_cwd: pathlib.Path,
        sample_component_definition: component.ComponentDefinition,
        monkeypatch: MonkeyPatch) -> None:
    """Test splitting of component definition and its dictionary."""
    compdef_name = 'mycomp'
    trestle_root = test_utils.create_trestle_project_with_model(
        tmp_path, sample_component_definition, compdef_name, monkeypatch)

    compdef_dir = trestle_root / 'component-definitions' / compdef_name
    compdef_file: pathlib.Path = compdef_dir / 'component-definition.json'
    original_model = sample_component_definition

    os.chdir(compdef_dir)
    # do the split in different ways - then re-merge
    if mode == 'normal_split.*':
        args = argparse.Namespace(file='component-definition.json',
                                  element='component-definition.components.*',
                                  verbose=1,
                                  trestle_root=trestle_root)
        assert SplitCmd()._run(args) == 0
    elif mode == 'split_two_steps':
        args = argparse.Namespace(file='component-definition.json',
                                  element='component-definition.components',
                                  verbose=1,
                                  trestle_root=trestle_root)
        assert SplitCmd()._run(args) == 0
        os.chdir('component-definition')
        args = argparse.Namespace(file='components.json',
                                  element='components.*',
                                  verbose=1,
                                  trestle_root=trestle_root)
        assert SplitCmd()._run(args) == 0
    elif mode == 'split_in_lower_dir':
        args = argparse.Namespace(
            file='component-definition.json',
            element='component-definition.components.*.props',
            verbose=1,
            trestle_root=trestle_root)
        assert SplitCmd()._run(args) == 0

    os.chdir(compdef_dir)
    args = argparse.Namespace(element='component-definition.*',
                              verbose=1,
                              trestle_root=trestle_root)
    assert MergeCmd()._run(args) == 0

    new_model = component.ComponentDefinition.oscal_read(compdef_file)
    assert test_utils.models_are_equivalent(new_model, original_model)
Ejemplo n.º 6
0
def test_split_stop_at_string(tmp_path, keep_cwd: pathlib.Path,
                              simplified_nist_catalog: oscatalog.Catalog,
                              monkeypatch: MonkeyPatch) -> None:
    """Test prevention of split at string level."""
    # prepare trestle project dir with the file

    cat_name = 'mycat'
    trestle_root = test_utils.create_trestle_project_with_model(
        tmp_path, simplified_nist_catalog, cat_name, monkeypatch)
    catalog_dir = trestle_root / 'catalogs' / cat_name

    os.chdir(catalog_dir)
    args = argparse.Namespace(
        file='catalog.json',
        element='catalog.groups.*.controls.*.controls.*.id',
        verbose=1,
        trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 1
    args = argparse.Namespace(file='catalog.json',
                              element='catalog.metadata.version',
                              verbose=1,
                              trestle_root=trestle_root)
    assert SplitCmd()._run(args) == 1