def main(): main_parser = argparse.ArgumentParser() subparser = main_parser.add_subparsers() for Cmd in Command.__subclasses__(): cmd_name = re.sub(r'([A-Z])', lambda s: '-' + s.group(0).lower(), Cmd.__name__)[1:] cmd = Cmd() parser = subparser.add_parser(cmd_name, help=cmd.__doc__) cmd.setup_parser(parser) parser.set_defaults(cmd=cmd) args = main_parser.parse_args() cmd = args.cmd kwargs = vars(args) del kwargs['cmd'] if cmd.needs_backend: primary = koji_util.KojiSession(anonymous=True) secondary = koji_util.KojiSession(anonymous=True, koji_id='secondary') backend = Backend(db=Session(), log=logging.getLogger(), koji_sessions={ 'primary': primary, 'secondary': secondary }) kwargs['backend'] = backend cmd.execute(**kwargs) if cmd.needs_backend: backend.db.commit() backend.db.close()
def koji(self, koji_id): """ Returns (and creates if necessary) current koji session for given koji_id (primary or secondary). """ if koji_id not in self._koji_sessions: if koji_id == 'primary': self._koji_sessions[koji_id] = \ koji_util.KojiSession(koji_id=koji_id, anonymous=False) elif koji_id == 'secondary': if get_config('secondary_mode'): self._koji_sessions[koji_id] = \ koji_util.KojiSession(koji_id=koji_id, anonymous=True) else: return self.koji('primary') else: raise AssertionError("Unknown koji_id: {}".format(koji_id)) return self._koji_sessions[koji_id]
def koji_cassette(self, *cassettes, secondary_mode=False): """ Creates a mock Koji session(s) backed by VCR-like cassette storage. When running for the first time with no cassette, the code under test accesses real Koji over the Internet, but interactions of the code with Koji are recorded to a YAML file called a cassette. Next time the test is run, the calls from the cassette are replayed and no Internet connection is made. Cassettes can also be written by hand or edited. To force a re-recoding of a cassette, delete the file on disk. To make the session authenticated when recording, set `TEST_ALLOW_LOGGED_IN=1` environment variable. To use different Koji instance than Fedora's production Koji when recording, set `koji_url` (and possibly `secondary_koji_url`) class attributes on the test suite class. The Koji session(s) is accessible via the `koji` method of this class. When using mock backend session (such as, when inheriting from `DBTest`), the mock backend session will automatically use the VCR-backed Koji session. :param cassettes: A list of cassette names. Only the last one will be used for recording. Slashes in names are interpreted as directory separator, nonexistent directories will be created. :param secondary_mode: Whether to create a separate secondary session with separate cassettes suffixed with `.secondary`. Otherwise, requests for secondary session will return primary session. """ koji_url = getattr(self, 'koji_url', 'https://koji.fedoraproject.org/kojihub') secondary_koji_url = getattr(self, 'secondary_koji_url', koji_url) logged_in = os.environ.get('TEST_ALLOW_LOGGED_IN') in ('1', 'true', 'y') with patch_config('koji_config.server', koji_url): with patch_config('secondary_koji_config.server', secondary_koji_url): primary_koji = koji_util.KojiSession('primary', anonymous=not logged_in) primary_vcr = koji_vcr.KojiVCR(primary_koji, cassettes) primary_mock = primary_vcr.create_mock() if secondary_mode: secondary_koji = koji_util.KojiSession('secondary') secondary_cassettes = [f'{c}.secondary' for c in cassettes] secondary_vcr = koji_vcr.KojiVCR(secondary_koji, secondary_cassettes) secondary_mock = secondary_vcr.create_mock() def get_koji(koji_id): assert koji_id in ('primary', 'secondary') if koji_id == 'secondary': if secondary_mode: return secondary_mock return DummyKoji('secondary') return primary_mock with patch.object(self, 'koji', new_callable=lambda: get_koji): try: yield finally: primary_vcr.write_cassette() if secondary_mode: secondary_vcr.write_cassette()
stream = re.match(r'ref: refs/heads/(.*)', f.readlines()[0]).group(1) config.load_config(['koschei.cfg'], ignore_env=True) full_refs = config.get_config('full_refs') default_ref = config.get_config('default_ref') include_build_deps = config.get_config('include_build_deps') api = config.get_config('api') profiles = config.get_config('profiles') includes = config.get_config('includes') excludes = config.get_config('excludes') frozen_refs = config.get_config('frozen_refs') stream_override = config.get_config('stream_override') macros = config.get_config('macros', None) ks = koji_util.KojiSession() tag_name = config.get_koji_config('primary', 'tag_name') repo_id = ks.getRepo(tag_name, state=koji.REPO_READY)['id'] repo_descriptor = KojiRepoDescriptor('primary', tag_name, repo_id) # Parse RPM name - split it into NVRA dict def parse_nvra(str): m = re.match(r'^(.+)-([^-]+)-([^-]+)\.([^-.]+).rpm$', str) return {'name': m.group(1), 'version': m.group(2), 'release': m.group(3), 'arch': m.group(4)} # Get RPM name def name(rpm):