def get_validation_dict_from_manifests(self, manifests, ui_validate_io=False, repository=None): """ Given a dict of manifest files where the key is the version of the manifest (or FLAT_KEY if the manifest files are not grouped by version), the function returns a dict containing validation info (warnings/errors). :param manifests: a dict of manifest files where the key is the version of the manifest (or FLAT_KEY if the manifest files are not grouped by version) :param ui_validate_io: the ui_validate_io flag specified from CLI :param repository: the repository value specified from CLI :return: a dict containing validation info (warnings/errors). """ bundle_dict = None # validate on all bundles files and combine log messages validation_dict = ValidateCmd(ui_validate_io).validation_json for version, manifest_files_info in manifests.items(): bundle_dict = BuildCmd().build_bundle(manifest_files_info) if version != FLAT_KEY: logger.info("Parsing version: %s", version) _, validation_dict_temp = ValidateCmd(ui_validate_io, self.nested) \ .validate(bundle_dict, repository) for log_level, msg_list in validation_dict_temp.items(): validation_dict[log_level].extend(msg_list) if not self.nested: self.bundle_dict = bundle_dict return validation_dict
def test_invalid_bundle(): bundles = ["tests/test_files/bundles/verification/invalid.bundle.yaml"] for bundle in bundles: with open(bundle) as f: bundle = yaml.safe_load(f) valid = ValidateCmd().validate(bundle) assert valid == False
def test_valid_bundles_with_package_name_and_repo_name_match( file_name, correct_repo_name): with open(file_name) as f: bundle = yaml.safe_load(f) bundle = unformat_bundle(bundle) valid, _ = ValidateCmd().validate(bundle, repository=correct_repo_name) assert valid
def build_and_verify(source_dir=None, yamls=None): """Build and verify constructs an operator bundle from a set of files and then verifies it for usefulness and accuracy. It returns the bundle as a string. :param source_dir: Path to local directory of yaml files to be read. :param yamls: List of yaml strings to create bundle with """ if source_dir is not None and yamls is not None: logger.error("Both source_dir and yamls cannot be defined.") raise TypeError( "Both source_dir and yamls cannot be specified on function call.") yaml_files = [] if source_dir is not None: for filename in os.listdir(source_dir): if filename.endswith(".yaml") or filename.endswith(".yml"): with open(source_dir + "/" + filename) as f: yaml_files.append(f.read()) elif yamls is not None: yaml_files = yamls bundle = BuildCmd().build_bundle(yaml_files) valid = ValidateCmd().validate(bundle) if not valid: bundle = None logger.error("Bundle failed validation.") return bundle
def test_valid_crd_versions_field(file_name): with open(file_name) as f: bundle = yaml.safe_load(f) bundle = unformat_bundle(bundle) valid, _ = ValidateCmd().validate(bundle) assert valid
def test_valid_bundles_with_multiple_packages(file_name): with open(file_name) as f: bundle = yaml.safe_load(f) bundle = unformat_bundle(bundle) valid, _ = ValidateCmd().validate(bundle) assert not valid
def build_and_verify(source_dir=None, yamls=None, ui_validate_io=False, validation_output=None, repository=None, output=None): """Build and verify constructs an operator bundle from a set of files and then verifies it for usefulness and accuracy. It returns the bundle as a string. :param source_dir: Path to local directory of yaml files to be read. :param yamls: List of yaml strings to create bundle with :param ui_validate_io: Optional flag to test operatorhub.io specific validation :param validation_output: Path to optional output file for validation logs :param repository: Repository name for the application :raises TypeError: When called with both source_dir and yamls specified :raises OpCourierBadYaml: When an invalid yaml file is encountered :raises OpCourierBadArtifact: When a file is not any of {CSV, CRD, Package} :raises OpCourierBadBundle: When the resulting bundle fails validation """ if source_dir is not None and yamls is not None: logger.error("Both source_dir and yamls cannot be defined.") raise TypeError( "Both source_dir and yamls cannot be specified on function call.") yaml_files = [] if source_dir is not None: for filename in os.listdir(source_dir): if filename.endswith(".yaml") or filename.endswith(".yml"): with open(source_dir + "/" + filename) as f: yaml_files.append(f.read()) elif yamls is not None: yaml_files = yamls bundle = BuildCmd().build_bundle(yaml_files) valid, validation_results_dict = ValidateCmd(ui_validate_io).validate( bundle, repository) if validation_output is not None: with open(validation_output, 'w') as f: f.write(json.dumps(validation_results_dict) + "\n") if valid: bundle = format_bundle(bundle) if output is not None: with open(output, 'w') as f: yaml.dump(bundle, f, default_flow_style=False) return bundle else: logger.error("Bundle failed validation.") raise OpCourierBadBundle( "Resulting bundle is invalid, input yaml is improperly defined.", validation_info=validation_results_dict)
def test_valid_bundles(): bundles = [ "tests/test_files/bundles/verification/valid.bundle.yaml", "tests/test_files/bundles/verification/nocrd.valid.bundle.yaml" ] for bundle in bundles: with open(bundle) as f: bundle = yaml.safe_load(f) valid = ValidateCmd().validate(bundle) assert valid == True
def _test_invalid_bundle_with_log(bundleFile, logInfo): with open(bundleFile) as f: bundle = yaml.safe_load(f) bundle = unformat_bundle(bundle) module, level, message = logInfo[0], logInfo[1], logInfo[2] with LogCapture() as logs: valid, _ = ValidateCmd().validate(bundle) assert not valid # check if the input log info is present among all logs captured logs.check_present((module, level, message), )
def test_valid_bundles_with_multiple_packages(file_name): with open(file_name) as f: bundle = yaml.safe_load(f) bundle = unformat_bundle(bundle) with LogCapture() as logs: valid, _ = ValidateCmd().validate(bundle) assert not valid logs.check_present( ('operatorcourier.validate', 'ERROR', 'Only 1 package is expected to exist per bundle, but got 2.'))
def get_validation_results(bundle): return ValidateCmd().validate(get_bundle(bundle))
def get_ui_validation_results(bundle): return ValidateCmd(ui_validate_io=True).validate(get_bundle(bundle))
def _test_invalid_bundle(bundleFile): with open(bundleFile) as f: bundle = yaml.safe_load(f) unformatted_bundle = unformat_bundle(bundle) valid, _ = ValidateCmd().validate(unformatted_bundle) assert not valid