def test_create_override_for_newer_build(self, publish): session = DBSession() old_build = Build.get(u'bodhi-2.0-1.fc17', session) build = Build(nvr=u'bodhi-2.0-2.fc17', package=old_build.package, release=old_build.release) session.add(build) session.flush() expiration_date = datetime.utcnow() + timedelta(days=1) data = {'nvr': build.nvr, 'notes': u'blah blah blah', 'expiration_date': expiration_date, 'csrf_token': self.get_csrf_token()} res = self.app.post('/overrides/', data) publish.assert_any_call(topic='buildroot_override.tag', msg=mock.ANY) publish.assert_any_call( topic='buildroot_override.untag', msg=mock.ANY) o = res.json_body self.assertEquals(o['build_id'], build.id) self.assertEquals(o['notes'], 'blah blah blah') self.assertEquals(o['expiration_date'], expiration_date.strftime("%Y-%m-%d %H:%M:%S")) self.assertEquals(o['expired_date'], None) old_build = Build.get(u'bodhi-2.0-1.fc17', session) self.assertNotEquals(old_build.override['expired_date'], None)
def test_create_override_for_newer_build(self, publish): old_build = Build.get(u"bodhi-2.0-1.fc17", self.db) build = Build(nvr=u"bodhi-2.0-2.fc17", package=old_build.package, release=old_build.release) self.db.add(build) self.db.flush() expiration_date = datetime.utcnow() + timedelta(days=1) data = { "nvr": build.nvr, "notes": u"blah blah blah", "expiration_date": expiration_date, "csrf_token": self.get_csrf_token(), } res = self.app.post("/overrides/", data) publish.assert_any_call(topic="buildroot_override.tag", msg=mock.ANY) publish.assert_any_call(topic="buildroot_override.untag", msg=mock.ANY) o = res.json_body self.assertEquals(o["build_id"], build.id) self.assertEquals(o["notes"], "blah blah blah") self.assertEquals(o["expiration_date"], expiration_date.strftime("%Y-%m-%d %H:%M:%S")) self.assertEquals(o["expired_date"], None) old_build = Build.get(u"bodhi-2.0-1.fc17", self.db) self.assertNotEquals(old_build.override["expired_date"], None)
def test_create_override_for_newer_build(self, publish): old_build = Build.get(u'bodhi-2.0-1.fc17', self.db) build = Build(nvr=u'bodhi-2.0-2.fc17', package=old_build.package, release=old_build.release) self.db.add(build) self.db.flush() expiration_date = datetime.utcnow() + timedelta(days=1) data = { 'nvr': build.nvr, 'notes': u'blah blah blah', 'expiration_date': expiration_date, 'csrf_token': self.get_csrf_token() } res = self.app.post('/overrides/', data) publish.assert_any_call(topic='buildroot_override.tag', msg=mock.ANY) publish.assert_any_call(topic='buildroot_override.untag', msg=mock.ANY) o = res.json_body self.assertEquals(o['build_id'], build.id) self.assertEquals(o['notes'], 'blah blah blah') self.assertEquals(o['expiration_date'], expiration_date.strftime("%Y-%m-%d %H:%M:%S")) self.assertEquals(o['expired_date'], None) old_build = Build.get(u'bodhi-2.0-1.fc17', self.db) self.assertNotEquals(old_build.override['expired_date'], None)
def test_unexpire_override(self, publish): session = DBSession() # First expire a buildroot override old_nvr = u'bodhi-2.0-1.fc17' override = Build.get(old_nvr, session).override override.expire() session.add(override) session.flush() publish.assert_called_once_with( topic='buildroot_override.untag', msg=mock.ANY) publish.reset_mock() # And now push its expiration_date into the future res = self.app.get('/overrides/%s' % old_nvr) o = res.json_body['override'] expiration_date = datetime.now() + timedelta(days=1) expiration_date = expiration_date.strftime("%Y-%m-%d %H:%M:%S") o.update({'nvr': o['build']['nvr'], 'edited': old_nvr, 'expiration_date': expiration_date, 'csrf_token': self.get_csrf_token()}) res = self.app.post('/overrides/', o) override = res.json_body self.assertEquals(override['build'], o['build']) self.assertEquals(override['notes'], o['notes']) self.assertEquals(override['expiration_date'], o['expiration_date']) self.assertEquals(override['expired_date'], None) publish.assert_called_once_with( topic='buildroot_override.tag', msg=mock.ANY)
def test_unexpire_override(self, publish): # First expire a buildroot override old_nvr = u"bodhi-2.0-1.fc17" override = Build.get(old_nvr, self.db).override override.expire() self.db.add(override) self.db.flush() publish.assert_called_once_with(topic="buildroot_override.untag", msg=mock.ANY) publish.reset_mock() # And now push its expiration_date into the future res = self.app.get("/overrides/%s" % old_nvr) o = res.json_body["override"] expiration_date = datetime.now() + timedelta(days=1) expiration_date = expiration_date.strftime("%Y-%m-%d %H:%M:%S") o.update( { "nvr": o["build"]["nvr"], "edited": old_nvr, "expiration_date": expiration_date, "csrf_token": self.get_csrf_token(), } ) res = self.app.post("/overrides/", o) override = res.json_body self.assertEquals(override["build"], o["build"]) self.assertEquals(override["notes"], o["notes"]) self.assertEquals(override["expiration_date"], o["expiration_date"]) self.assertEquals(override["expired_date"], None) publish.assert_called_once_with(topic="buildroot_override.tag", msg=mock.ANY)
def test_unexpire_override(self, publish): # First expire a buildroot override old_nvr = u'bodhi-2.0-1.fc17' override = Build.get(old_nvr, self.db).override override.expire() self.db.add(override) self.db.flush() publish.assert_called_once_with(topic='buildroot_override.untag', msg=mock.ANY) publish.reset_mock() # And now push its expiration_date into the future res = self.app.get('/overrides/%s' % old_nvr) o = res.json_body['override'] expiration_date = datetime.now() + timedelta(days=1) expiration_date = expiration_date.strftime("%Y-%m-%d %H:%M:%S") o.update({ 'nvr': o['build']['nvr'], 'edited': old_nvr, 'expiration_date': expiration_date, 'csrf_token': self.get_csrf_token() }) res = self.app.post('/overrides/', o) override = res.json_body self.assertEquals(override['build'], o['build']) self.assertEquals(override['notes'], o['notes']) self.assertEquals(override['expiration_date'], o['expiration_date']) self.assertEquals(override['expired_date'], None) publish.assert_called_once_with(topic='buildroot_override.tag', msg=mock.ANY)
def get_build(request): nvr = request.matchdict.get('nvr') build = Build.get(nvr, request.db) if not build: request.errors.add('body', 'nvr', 'No such build') request.errors.status = HTTPNotFound.code return return build
def get_override(request): db = request.db nvr = request.matchdict.get('nvr') build = Build.get(nvr, db) if not build: request.errors.add('url', 'nvr', 'No such build') request.errors.status = HTTPNotFound.code return if not build.override: request.errors.add('url', 'nvr', 'No buildroot override for this build') request.errors.status = HTTPNotFound.code return return dict(override=build.override)
def new_update(request): """ Save an update. This entails either creating a new update, or editing an existing one. To edit an existing update, the update's original title must be specified in the ``edited`` parameter. """ data = request.validated log.debug('validated = %s' % data) # This has already been validated at this point, but we need to ditch # it since the models don't care about a csrf argument. data.pop('csrf_token') caveats = [] try: releases = set() builds = [] # Create the Package and Build entities for nvr in data['builds']: name, version, release = request.buildinfo[nvr]['nvr'] package = request.db.query(Package).filter_by(name=name).first() if not package: package = Package(name=name) request.db.add(package) request.db.flush() build = Build.get(nvr, request.db) if build is None: log.debug("Adding nvr %s", nvr) build = Build(nvr=nvr, package=package) request.db.add(build) request.db.flush() build.package = package build.release = request.buildinfo[build.nvr]['release'] builds.append(build) releases.add(request.buildinfo[build.nvr]['release']) if data.get('edited'): log.info('Editing update: %s' % data['edited']) assert len(releases) == 1, "Updates may not span multiple releases" data['release'] = list(releases)[0] data['builds'] = [b.nvr for b in builds] result, _caveats = Update.edit(request, data) caveats.extend(_caveats) else: if len(releases) > 1: caveats.append({ 'name': 'releases', 'description': 'Your update is being split ' 'into %i, one for each release.' % len(releases) }) updates = [] for release in releases: _data = copy.copy(data) # Copy it because .new(..) mutates it _data['builds'] = [b for b in builds if b.release == release] _data['release'] = release log.info('Creating new update: %r' % _data['builds']) result, _caveats = Update.new(request, _data) log.debug('%s update created', result.title) updates.append(result) caveats.extend(_caveats) if len(releases) > 1: result = dict(updates=updates) except LockedUpdateException as e: log.warn(str(e)) request.errors.add('body', 'builds', "%s" % str(e)) return except Exception as e: log.exception('Failed to create update') request.errors.add('body', 'builds', 'Unable to create update. %s' % str(e)) return # Obsolete older updates for three different cases... # editing an update, submitting a new single update, submitting multiple. if isinstance(result, dict): updates = result['updates'] else: updates = [result] for update in updates: try: caveats.extend(update.obsolete_older_updates(request.db)) except Exception as e: caveats.append({ 'name': 'update', 'description': 'Problem obsoleting older updates: %s' % str(e), }) if not isinstance(result, dict): result = result.__json__() result['caveats'] = caveats return result
def save_override(request): """Save a buildroot override This entails either creating a new buildroot override, or editing an existing one. To edit an existing buildroot override, the buildroot override's original id needs to be specified in the ``edited`` parameter. """ data = request.validated edited = data.pop("edited") caveats = [] try: submitter = User.get(request.user.name, request.db) if edited is None: builds = data['builds'] overrides = [] if len(builds) > 1: caveats.append({ 'name': 'nvrs', 'description': 'Your override submission was ' 'split into %i.' % len(builds) }) for build in builds: log.info("Creating a new buildroot override: %s" % build.nvr) if BuildrootOverride.get(build.id, request.db): request.errors.add( 'body', 'builds', 'Buildroot override for %s already exists' % build.nvr) return else: overrides.append( BuildrootOverride.new( request, build=build, submitter=submitter, notes=data['notes'], expiration_date=data['expiration_date'], )) if len(builds) > 1: result = dict(overrides=overrides) else: result = overrides[0] else: log.info("Editing buildroot override: %s" % edited) edited = Build.get(edited, request.db) if edited is None: request.errors.add('body', 'edited', 'No such build') return result = BuildrootOverride.edit( request, edited=edited, submitter=submitter, notes=data["notes"], expired=data["expired"], expiration_date=data["expiration_date"]) if not result: # Some error inside .edit(...) return except Exception as e: log.exception(e) request.errors.add('body', 'override', 'Unable to save buildroot override: %s' % e) return if not isinstance(result, dict): result = result.__json__() result['caveats'] = caveats return result
def new_update(request): """ Save an update. This entails either creating a new update, or editing an existing one. To edit an existing update, the update's original title must be specified in the ``edited`` parameter. """ data = request.validated log.debug('validated = %s' % data) # This has already been validated at this point, but we need to ditch # it since the models don't care about a csrf argument. data.pop('csrf_token') caveats = [] try: releases = set() builds = [] # Create the Package and Build entities for nvr in data['builds']: name, version, release = request.buildinfo[nvr]['nvr'] package = request.db.query(Package).filter_by(name=name).first() if not package: package = Package(name=name) request.db.add(package) request.db.flush() build = Build.get(nvr, request.db) if build is None: log.debug("Adding nvr %s", nvr) build = Build(nvr=nvr, package=package) request.db.add(build) request.db.flush() build.package = package build.release = request.buildinfo[build.nvr]['release'] builds.append(build) releases.add(request.buildinfo[build.nvr]['release']) if data.get('edited'): log.info('Editing update: %s' % data['edited']) assert len(releases) == 1, "Updates may not span multiple releases" data['release'] = list(releases)[0] data['builds'] = [b.nvr for b in builds] result, _caveats = Update.edit(request, data) caveats.extend(_caveats) else: if len(releases) > 1: caveats.append({ 'name': 'releases', 'description': 'Your update is being split ' 'into %i, one for each release.' % len(releases) }) updates = [] for release in releases: _data = copy.copy(data) # Copy it because .new(..) mutates it _data['builds'] = [b for b in builds if b.release == release] _data['release'] = release log.info('Creating new update: %r' % _data['builds']) result, _caveats = Update.new(request, _data) log.debug('update = %r' % result) updates.append(result) caveats.extend(_caveats) if len(releases) > 1: result = dict(updates=updates) except LockedUpdateException as e: log.warn(str(e)) request.errors.add('body', 'builds', "%s" % str(e)) return except Exception as e: log.exception('Failed to create update') request.errors.add( 'body', 'builds', 'Unable to create update. %s' % str(e)) return # Obsolete older updates for three different cases... # editing an update, submitting a new single update, submitting multiple. if isinstance(result, dict): updates = result['updates'] else: updates = [result] for update in updates: try: caveats.extend(update.obsolete_older_updates(request)) except Exception as e: caveats.append({ 'name': 'update', 'description': 'Problem obsoleting older updates: %s' % str(e), }) if not isinstance(result, dict): result = result.__json__() result['caveats'] = caveats return result
def save_override(request): """Save a buildroot override This entails either creating a new buildroot override, or editing an existing one. To edit an existing buildroot override, the buildroot override's original id needs to be specified in the ``edited`` parameter. """ data = request.validated edited = data.pop("edited") caveats = [] try: submitter = User.get(request.user.name, request.db) if edited is None: builds = data['builds'] overrides = [] if len(builds) > 1: caveats.append({ 'name': 'nvrs', 'description': 'Your override submission was ' 'split into %i.' % len(builds) }) for build in builds: log.info("Creating a new buildroot override: %s" % build.nvr) if BuildrootOverride.get(build.id, request.db): request.errors.add('body', 'builds', 'Buildroot override for %s already exists' % build.nvr) return else: overrides.append(BuildrootOverride.new( request, build=build, submitter=submitter, notes=data['notes'], expiration_date=data['expiration_date'], )) if len(builds) > 1: result = dict(overrides=overrides) else: result = overrides[0] else: log.info("Editing buildroot override: %s" % edited) edited = Build.get(edited, request.db) if edited is None: request.errors.add('body', 'edited', 'No such build') return result = BuildrootOverride.edit( request, edited=edited, submitter=submitter, notes=data["notes"], expired=data["expired"], expiration_date=data["expiration_date"] ) if not result: # Some error inside .edit(...) return except Exception as e: log.exception(e) request.errors.add('body', 'override', 'Unable to save buildroot override: %s' % e) return if not isinstance(result, dict): result = result.__json__() result['caveats'] = caveats return result