def test_target_dups(tmp_dir): """Test model validation.""" content_type = FileContentType.YAML models_dir_name = test_utils.TARGET_DEFS_DIR model_ref = ostarget.TargetDefinition test_utils.ensure_trestle_config_dir(tmp_dir) file_ext = FileContentType.to_file_extension(content_type) models_full_path = tmp_dir / models_dir_name / 'my_test_model' model_alias = utils.classname_to_alias(model_ref.__name__, 'json') model_def_file = models_full_path / f'{model_alias}{file_ext}' fs.ensure_directory(models_full_path) shutil.copyfile('tests/data/yaml/good_target.yaml', model_def_file) testcmd = f'trestle validate -f {model_def_file} -m duplicates -i uuid' with patch.object(sys, 'argv', testcmd.split()): with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code is None shutil.copyfile('tests/data/yaml/bad_target_dup_uuid.yaml', model_def_file) testcmd = f'trestle validate -f {model_def_file} -m duplicates -i uuid' with patch.object(sys, 'argv', testcmd.split()): with pytest.raises(TrestleValidationError) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == TrestleValidationError
def test_validation_happy(name, mode, parent, tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None: """Test successful validation runs.""" (tmp_trestle_dir / test_utils.CATALOGS_DIR / 'my_test_model').mkdir( exist_ok=True, parents=True) (tmp_trestle_dir / test_utils.CATALOGS_DIR / 'my_test_model2').mkdir( exist_ok=True, parents=True) shutil.copyfile( test_data_dir / 'json/minimal_catalog.json', tmp_trestle_dir / test_utils.CATALOGS_DIR / 'my_test_model/catalog.json') shutil.copyfile( test_data_dir / 'json/minimal_catalog.json', tmp_trestle_dir / test_utils.CATALOGS_DIR / 'my_test_model2/catalog.json') model_def_file = tmp_trestle_dir / test_utils.CATALOGS_DIR / ( 'my_test_model/catalog.json') if mode == '-f': if not parent: testcmd = f'trestle validate {mode} {model_def_file}' else: testcmd = f'trestle validate {mode} {model_def_file.parent}' elif mode == '-n': testcmd = f'trestle validate -t catalog -n {name}' elif mode == '-x': testcmd = f'trestle validate -t catalog -n {name}' else: testcmd = 'trestle validate -a' monkeypatch.setattr(sys, 'argv', testcmd.split()) with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 0
def test_config_copy_error(tmpdir): """Test error during init when a contents of .trestle cannot be created.""" owd = os.getcwd() os.chdir(tmpdir) os.mkdir(const.TRESTLE_CONFIG_DIR) open(os.path.join(const.TRESTLE_CONFIG_DIR, const.TRESTLE_CONFIG_FILE), 'a').close() os.chmod(os.path.join(const.TRESTLE_CONFIG_DIR, const.TRESTLE_CONFIG_FILE), stat.S_IREAD) testargs = ['trestle', 'init'] with patch.object(sys, 'argv', testargs): with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 1 for directory in const.MODELTYPE_TO_MODELMODULE.keys(): assert os.path.isdir(directory) assert os.path.isdir( os.path.join(const.TRESTLE_DIST_DIR, directory)) assert os.path.isdir(const.TRESTLE_CONFIG_DIR) assert os.path.isfile( os.path.join(const.TRESTLE_CONFIG_DIR, const.TRESTLE_CONFIG_FILE)) assert os.stat( os.path.join(const.TRESTLE_CONFIG_DIR, const.TRESTLE_CONFIG_FILE)).st_size == 0 os.chdir(owd)
def test_directory_creation_error(tmp_path, keep_cwd, monkeypatch: MonkeyPatch): """Test error during init when a directory cannot be created.""" # Windows read-only on dir does not prevent file creation in dir if file_utils.is_windows(): return os.chdir(tmp_path) config_dir = pathlib.Path(const.TRESTLE_CONFIG_DIR) config_dir.mkdir() config_dir.chmod(stat.S_IREAD) config_file = config_dir / const.TRESTLE_CONFIG_FILE testargs = ['trestle', 'init'] monkeypatch.setattr(sys, 'argv', testargs) with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 1 for directory in const.MODEL_DIR_LIST: dir_path = pathlib.Path(directory) assert not dir_path.exists() dist_dir_path = pathlib.Path(const.TRESTLE_DIST_DIR) / directory assert not dist_dir_path.exists() assert config_dir.exists() assert config_dir.is_dir() config_exists = False try: config_exists = config_file.exists() except Exception: pass assert not config_exists
def test_run() -> None: """Test cli call.""" testargs = ['trestle'] with patch.object(sys, 'argv', testargs): with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code > 0
def test_version(monkeypatch: MonkeyPatch) -> None: """Test version output.""" testcmd = 'trestle version' monkeypatch.setattr(sys, 'argv', testcmd.split()) with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 0
def test_run(monkeypatch: MonkeyPatch) -> None: """Test cli call.""" testargs = ['trestle'] monkeypatch.setattr(sys, 'argv', testargs) with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code > 0
def test_init(tmp_path, keep_cwd, monkeypatch: MonkeyPatch): """Test init happy path.""" os.chdir(tmp_path) testargs = ['trestle', 'init'] monkeypatch.setattr(sys, 'argv', testargs) with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 0 for directory in const.MODEL_DIR_LIST: assert os.path.isdir(directory) assert os.path.isdir(os.path.join(const.TRESTLE_DIST_DIR, directory)) assert os.path.isfile(os.path.join(directory, const.TRESTLE_KEEP_FILE)) assert os.path.isdir(const.TRESTLE_CONFIG_DIR) assert os.path.isfile(os.path.join(const.TRESTLE_CONFIG_DIR, const.TRESTLE_CONFIG_FILE))
def test_init(tmpdir): """Test init happy path.""" owd = os.getcwd() os.chdir(tmpdir) testargs = ['trestle', 'init'] with patch.object(sys, 'argv', testargs): with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 0 for directory in const.MODELTYPE_TO_MODELMODULE.keys(): assert os.path.isdir(directory) assert os.path.isdir(os.path.join(const.TRESTLE_DIST_DIR, directory)) assert os.path.isdir(const.TRESTLE_CONFIG_DIR) assert os.path.isfile(os.path.join(const.TRESTLE_CONFIG_DIR, const.TRESTLE_CONFIG_FILE)) os.chdir(owd)
def test_oscal_version_validator(tmp_trestle_dir: pathlib.Path, sample_catalog_minimal: Catalog, code: int, monkeypatch: MonkeyPatch) -> None: """ Test oscal version validator. There is no actual validator for oscal version but it happens in base_model when a file is loaded. """ mycat_dir = tmp_trestle_dir / 'catalogs/mycat' mycat_dir.mkdir() sample_catalog_minimal.oscal_write(mycat_dir / 'catalog.json') testcmd = 'trestle validate -t catalog' monkeypatch.setattr(sys, 'argv', testcmd.split()) with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == code
def test_config_copy_error(tmp_path, keep_cwd, monkeypatch: MonkeyPatch): """Test error during init when a contents of .trestle cannot be created.""" os.chdir(tmp_path) config_dir = pathlib.Path(const.TRESTLE_CONFIG_DIR) config_dir.mkdir() config_file = config_dir / const.TRESTLE_CONFIG_FILE config_file.touch() config_file.chmod(stat.S_IREAD) testargs = ['trestle', 'init'] monkeypatch.setattr(sys, 'argv', testargs) with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 1 for directory in const.MODEL_DIR_LIST: assert os.path.isdir(directory) assert os.path.isdir(os.path.join(const.TRESTLE_DIST_DIR, directory)) assert os.path.isdir(const.TRESTLE_CONFIG_DIR) assert os.path.isfile(os.path.join(const.TRESTLE_CONFIG_DIR, const.TRESTLE_CONFIG_FILE)) assert os.stat(os.path.join(const.TRESTLE_CONFIG_DIR, const.TRESTLE_CONFIG_FILE)).st_size == 0
def test_directory_creation_error(tmpdir): """Test error during init when a directory cannot be created.""" # Windows read-only on dir does not prevent file creation in dir if platform.system() == 'Windows': return owd = os.getcwd() os.chdir(tmpdir) os.mkdir(const.TRESTLE_CONFIG_DIR) os.chmod(const.TRESTLE_CONFIG_DIR, stat.S_IREAD) testargs = ['trestle', 'init'] with patch.object(sys, 'argv', testargs): with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 1 for directory in const.MODELTYPE_TO_MODELMODULE.keys(): assert os.path.isdir(directory) assert os.path.isdir(os.path.join(const.TRESTLE_DIST_DIR, directory)) assert os.path.isdir(const.TRESTLE_CONFIG_DIR) assert not os.path.isfile(os.path.join(const.TRESTLE_CONFIG_DIR, const.TRESTLE_CONFIG_FILE)) os.chdir(owd)
def test_role_refs_validator(name, mode, parent, test_id, code, tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None: """Test validation of roles and references to them in responsible-parties.""" (tmp_trestle_dir / 'assessment-plans/my_ap').mkdir(exist_ok=True, parents=True) roles = [ Role(id='id1', title='title1'), Role(id='id2', title='title2'), Role(id='id3', title='title3') ] party1 = ResponsibleParty(role_id=test_id, party_uuids=[PartyUuid(__root__=str(uuid4()))]) party2 = ResponsibleParty(role_id='id2', party_uuids=[PartyUuid(__root__=str(uuid4()))]) responsible_parties = [party1, party2] ap_obj = generate_sample_model(ap.AssessmentPlan) ap_obj.metadata.roles = roles ap_obj.metadata.responsible_parties = responsible_parties ap_path = tmp_trestle_dir / 'assessment-plans/my_ap/assessment-plan.json' ap_obj.oscal_write(ap_path) if mode == '-f': if not parent: testcmd = f'trestle validate {mode} {ap_path}' else: testcmd = f'trestle validate {mode} {ap_path.parent}' elif mode == '-n': testcmd = f'trestle validate -t assessment-plan -n {name}' elif mode == '-t': testcmd = 'trestle validate -t assessment-plan' else: testcmd = 'trestle validate -a' monkeypatch.setattr(sys, 'argv', testcmd.split()) with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == code
def test_validate_distributed(testdata_dir: pathlib.Path, tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None: """Check that validate will run correctly when exploiting load distributed.""" test_utils.ensure_trestle_config_dir(tmp_trestle_dir) # Clean up. test_data_source = testdata_dir / 'split_merge/step0-merged_catalog/catalogs' catalogs_dir = tmp_trestle_dir / 'catalogs' shutil.rmtree(catalogs_dir) shutil.copytree(test_data_source, catalogs_dir) args = argparse.Namespace(name='split', file='catalogs/mycatalog/catalog.json', verbose=1, element='catalog.groups.*.controls.*', trestle_root=tmp_trestle_dir) _ = SplitCmd()._run(args) test_args = 'trestle validate -a'.split(' ') monkeypatch.setattr(sys, 'argv', test_args) with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 0