Ejemplo n.º 1
0
def test_profile_to_osco_execute_no_overwrite(tmp_path):
    """Test execute call no overwrite."""
    config = configparser.ConfigParser()
    config_path = pathlib.Path(
        'tests/data/tasks/profile-to-osco/profile-to-osco-no-overwrite.config')
    config.read(config_path)
    section = config['task.profile-to-osco']
    d_expected = pathlib.Path(section['output-dir'])
    d_produced = tmp_path
    section['output-dir'] = str(d_produced)
    tgt = profile_to_osco.ProfileToOsco(section)
    retval = tgt.simulate()
    assert retval == TaskOutcome.SIM_SUCCESS
    assert len(os.listdir(str(tmp_path))) == 0
    retval = tgt.execute()
    assert retval == TaskOutcome.SUCCESS
    list_dir = os.listdir(d_produced)
    assert len(list_dir) == 1
    assert d_expected != d_produced
    for fn in list_dir:
        f_expected = d_expected / fn
        f_produced = d_produced / fn
        result = text_files_equal(f_expected, f_produced)
        assert (result)
    retval = tgt.execute()
    assert retval == TaskOutcome.FAILURE
Ejemplo n.º 2
0
def test_profile_to_osco_execute_set(tmp_path):
    """Test execute call with set variables."""
    config = configparser.ConfigParser()
    config_path = pathlib.Path(
        'tests/data/tasks/profile-to-osco/profile-to-osco-set.config')
    config.read(config_path)
    section = config['task.profile-to-osco']
    input_file = section['input-file']
    input_path = pathlib.Path(input_file)
    f_expected = pathlib.Path(section['output-dir'], 'osco-profile.yaml')
    d_produced = tmp_path
    section['output-dir'] = str(d_produced)
    f_produced = pathlib.Path(d_produced, 'osco-profile.yaml')
    # read input
    profile = Profile.oscal_read(input_path)
    # transform
    transformer = ProfileToOscoTransformer(extends='extends',
                                           api_version='api_version',
                                           kind='kind',
                                           name='name',
                                           namespace='namespace')
    ydata = json.loads(transformer.transform(profile))
    # write output
    yaml = YAML(typ='safe')
    yaml.default_flow_style = False
    with open(f_produced, 'w') as outfile:
        yaml.dump(ydata, outfile)
    assert f_expected != f_produced
    result = text_files_equal(f_expected, f_produced)
    assert (result)
def test_cis_to_component_definition_execute(tmp_path: pathlib.Path,
                                             monkeypatch: MonkeyPatch):
    """Test execute call."""
    monkeypatch.setattr(uuid, 'uuid4', monkey_uuid_1)
    monkeypatch.setattr(trestle, '__version__', monkey_trestle_version())
    config = configparser.ConfigParser()
    config_path = pathlib.Path(
        'tests/data/tasks/cis-to-component-definition/test-cis-to-component-definition.config'
    )
    config.read(config_path)
    section = config['task.cis-to-component-definition']
    d_expected = pathlib.Path(section['output-dir'])
    d_produced = tmp_path
    section['output-dir'] = str(tmp_path)
    tgt = cis_to_component_definition.CisToComponentDefinition(section)
    tgt.set_timestamp('2021-07-19T14:03:03.000+00:00')
    retval = tgt.execute()
    assert retval == TaskOutcome.SUCCESS
    list_dir = os.listdir(d_produced)
    assert len(list_dir) == 1
    assert d_expected != d_produced
    for fn in list_dir:
        f_expected = d_expected / fn
        f_produced = d_produced / fn
        result = text_files_equal(f_expected, f_produced)
        assert (result)
Ejemplo n.º 4
0
def test_control_objective(tmp_path: pathlib.Path) -> None:
    """Test read and write of control with objective."""
    # write the control directly as raw markdown text
    md_path = tmp_path / 'xy-9.md'
    with open(md_path, 'w') as f:
        f.write(control_text)
    # read it in as markdown to an OSCAL control in memory
    control, group_title = ControlIOReader.read_control(md_path, True)
    assert group_title == 'My Group Title'
    sub_dir = tmp_path / 'sub_dir'
    sub_dir.mkdir(exist_ok=True)
    # write it out as markdown in a separate directory to avoid name clash
    control_writer = ControlIOWriter()
    control_writer.write_control(sub_dir, control, 'My Group Title', None,
                                 None, False, False, None, False, None, None)
    # confirm the newly written markdown text is identical to what was read originally
    assert test_utils.text_files_equal(md_path, sub_dir / 'xy-9.md')
Ejemplo n.º 5
0
def _test_profile_to_osco_execute_common(tmp_path, config):
    """Test execute call."""
    section = config['task.profile-to-osco']
    d_expected = pathlib.Path(section['output-dir'])
    d_produced = tmp_path
    section['output-dir'] = str(d_produced)
    tgt = profile_to_osco.ProfileToOsco(section)
    retval = tgt.simulate()
    assert retval == TaskOutcome.SIM_SUCCESS
    assert len(os.listdir(str(tmp_path))) == 0
    retval = tgt.execute()
    assert retval == TaskOutcome.SUCCESS
    list_dir = os.listdir(d_produced)
    assert len(list_dir) == 1
    assert d_expected != d_produced
    for fn in list_dir:
        f_expected = d_expected / fn
        f_produced = d_produced / fn
        result = text_files_equal(f_expected, f_produced)
        assert (result)
def test_xlsx_execute(tmp_path):
    """Test execute call."""
    config = configparser.ConfigParser()
    config_path = pathlib.Path(CONFIG_PATH)
    config.read(config_path)
    section = config['task.xlsx-to-oscal-profile']
    d_expected = pathlib.Path(section['output-dir'])
    d_produced = tmp_path
    section['output-dir'] = str(tmp_path)
    tgt = xlsx_to_oscal_profile.XlsxToOscalProfile(section)
    tgt.set_timestamp('2021-07-19T14:03:03.000+00:00')
    retval = tgt.execute()
    assert retval == TaskOutcome.SUCCESS
    list_dir = os.listdir(d_produced)
    assert len(list_dir) == 1
    assert d_expected != d_produced
    for fn in list_dir:
        f_expected = d_expected / fn
        f_produced = d_produced / fn
        result = text_files_equal(f_expected, f_produced)
        assert (result)
Ejemplo n.º 7
0
def test_osco_execute_input_configmaps(tmp_path, monkeypatch: MonkeyPatch):
    """Test execute call OSCO configmaps data."""
    monkeybusiness = MonkeyBusiness()
    monkeypatch.setattr(uuid, 'uuid4', monkeybusiness.uuid_mock1)
    osco.OscoTransformer.set_timestamp('2021-02-24T19:31:13+00:00')
    config = configparser.ConfigParser()
    config_path = pathlib.Path(
        'tests/data/tasks/osco/test-osco-to-oscal-configmaps.config')
    config.read(config_path)
    section = config['task.osco-to-oscal']
    d_expected = pathlib.Path(section['output-dir'])
    d_produced = tmp_path
    section['output-dir'] = str(d_produced)
    tgt = osco_to_oscal.OscoToOscal(section)
    retval = tgt.execute()
    assert retval == TaskOutcome.SUCCESS
    list_dir = os.listdir(d_produced)
    assert len(list_dir) == 1
    for fn in list_dir:
        f_expected = d_expected / fn
        f_produced = d_produced / fn
        result = text_files_equal(f_expected, f_produced)
        assert (result)