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 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 unformat_bundle(formattedBundle): """ Converts a push-ready bundle into a structured object by changing stringified yaml of 'customResourceDefinitions', 'clusterServiceVersions', and 'packages' into lists of objects. Undoing the format helps simplify bundle validation. :param formattedBundle: A push-ready bundle """ bundle = BuildCmd()._get_empty_bundle() if 'data' in formattedBundle: if 'customResourceDefinitions' in formattedBundle['data']: customResourceDefinitions = yaml.safe_load( formattedBundle['data']['customResourceDefinitions']) if customResourceDefinitions and len( customResourceDefinitions) > 0: bundle['data'][ 'customResourceDefinitions'] = customResourceDefinitions if 'clusterServiceVersions' in formattedBundle['data']: clusterServiceVersions = yaml.safe_load( formattedBundle['data']['clusterServiceVersions']) if clusterServiceVersions and len(clusterServiceVersions) > 0: bundle['data'][ 'clusterServiceVersions'] = clusterServiceVersions if 'packages' in formattedBundle['data']: packages = yaml.safe_load(formattedBundle['data']['packages']) if packages and len(packages) > 0: bundle['data']['packages'] = packages return bundle
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_create_bundle(): paths = ["tests/test_files/csv.yaml", "tests/test_files/crd.yaml", "tests/test_files/package.yaml"] yamls = [] for path in paths: with open(path) as f: yamls.append(f.read()) bundle = BuildCmd().build_bundle(yamls) assert bool(bundle["data"]["packages"]) == True assert bool(bundle["data"]["clusterServiceVersions"]) == True assert bool(bundle["data"]["customResourceDefinitions"]) == True