def test_read_yaml_file_bad_decode(tmpdir, caplog): (flexmock(json).should_receive('load').and_raise(ValueError)) config_path = os.path.join(str(tmpdir), 'config.yaml') with open(config_path, 'w'): pass with pytest.raises(ValueError): read_yaml_from_file_path(config_path, 'schemas/container.json') assert "unable to decode JSON schema, cannot validate" in caplog.text
def test_read_yaml_file_bad_extract(tmpdir, caplog): class FakeProvider(object): def get_resource_stream(self, pkg, rsc): raise IOError # pkg_resources.resource_stream() cannot be mocked directly # Instead mock the module-level function it calls. (flexmock(pkg_resources).should_receive('get_provider').and_return( FakeProvider())) config_path = os.path.join(str(tmpdir), 'config.yaml') with open(config_path, 'w'): pass with pytest.raises(IOError): read_yaml_from_file_path(config_path, 'schemas/container.json') assert "unable to extract JSON schema, cannot validate" in caplog.text
def _validate_container_config_file(self): try: container_config_path = os.path.join(self.dir_path, REPO_CONTAINER_CONFIG) self.container = read_yaml_from_file_path(container_config_path, 'schemas/container.json') or {} except Exception as e: msg = ('Failed to load or validate container file "{file}": {reason}' .format(file=container_config_path, reason=e)) raise OsbsException(msg)
def __init__(self, dir_path='', file_name=REPO_CONFIG_FILE, depth=None, git_uri=None, git_branch=None, git_ref=None): self._config_parser = ConfigParser() self.container = {} self.depth = depth or 0 self.autorebuild = {} # Keep track of the repo metadata in the repo configuration self.git_uri = git_uri self.git_branch = git_branch self.git_ref = git_ref # Set default options self._config_parser.readfp(StringIO(self.DEFAULT_CONFIG)) # pylint: disable=W1505; py2 config_path = os.path.join(dir_path, file_name) if os.path.exists(config_path): self._config_parser.read(config_path) file_path = os.path.join(dir_path, REPO_CONTAINER_CONFIG) if os.path.exists(file_path): try: self.container = read_yaml_from_file_path( file_path, 'schemas/container.json') or {} except Exception as e: msg = ( 'Failed to load or validate container file "{file}": {reason}' .format(file=file_path, reason=e)) raise OsbsException(msg) # container values may be set to None container_compose = self.container.get('compose') or {} modules = container_compose.get('modules') or [] self.autorebuild = self.container.get('autorebuild') or {} self.container_module_specs = [] value_errors = [] for module in modules: try: self.container_module_specs.append(ModuleSpec.from_str(module)) except ValueError as e: value_errors.append(e) if value_errors: raise ValueError(value_errors) flatpak = self.container.get('flatpak') or {} self.is_flatpak = bool(flatpak) self.flatpak_base_image = flatpak.get('base_image') self.flatpak_component = flatpak.get('component') self.flatpak_name = flatpak.get('name')
def test_read_yaml_file_or_yaml(tmpdir, from_file, config): expected = yaml.safe_load(config) if from_file: config_path = os.path.join(str(tmpdir), 'config.yaml') with open(config_path, 'w') as fp: fp.write(config) output = read_yaml_from_file_path(config_path, 'schemas/container.json') else: output = read_yaml(config, 'schemas/container.json') assert output == expected
def test_read_yaml_file_ioerrors(tmpdir): config_path = os.path.join(str(tmpdir), 'nosuchfile.yaml') with pytest.raises(IOError): read_yaml_from_file_path(config_path, 'schemas/nosuchfile.json')