コード例 #1
0
    def test_rebuild_if_not_exists_already_exists(self, advisories_from_event):
        handler = UpdateDBOnAdvisoryChange()

        db_event = Event.create(db.session, "msg124", "123",
                                ErrataAdvisoryRPMsSignedEvent)
        db.session.commit()

        for manual in [True, False]:
            for db_event_state in [
                    EventState.INITIALIZED, EventState.BUILDING,
                    EventState.COMPLETE, EventState.FAILED, EventState.SKIPPED
            ]:
                db_event.state = db_event_state
                db.session.commit()
                for state in [
                        "REL_PREP", "PUSH_READY", "IN_PUSH", "SHIPPED_LIVE"
                ]:
                    advisories_from_event.return_value = [
                        ErrataAdvisory(123, "RHSA-2017", state, ["rpm"],
                                       "Critical")
                    ]
                    ev = ErrataAdvisoryStateChangedEvent(
                        "msg123",
                        ErrataAdvisory(123, 'RHSA-2017', state, ['rpm']))
                    ev.manual = manual
                    ev.dry_run = manual  # use also manual just for the sake of test.
                    ret = handler.handle(ev)

                    if db_event_state == EventState.FAILED or ev.manual:
                        self.assertEqual(len(ret), 1)
                        self.assertEqual(ret[0].manual, manual)
                        self.assertEqual(ret[0].dry_run, manual)
                    else:
                        self.assertEqual(len(ret), 0)
コード例 #2
0
    def test_rebuild_if_not_exists_unknown_states(self, advisories_from_event):
        handler = UpdateDBOnAdvisoryChange()

        for state in ["NEW_FILES", "QE", "UNKNOWN"]:
            advisories_from_event.return_value = [
                ErrataAdvisory(123, "RHSA-2017", state, ["rpm"], "Critical")
            ]
            ev = ErrataAdvisoryStateChangedEvent(
                "msg123", ErrataAdvisory(123, 'RHSA-2017', state, ['rpm']))
            ret = handler.handle(ev)

            self.assertEqual(len(ret), 0)
コード例 #3
0
    def test_rebuild_if_not_exists(self, advisories_from_event):
        handler = UpdateDBOnAdvisoryChange()

        for state in ["REL_PREP", "PUSH_READY", "IN_PUSH", "SHIPPED_LIVE"]:
            advisories_from_event.return_value = [
                ErrataAdvisory(123, "RHSA-2017", state, ["rpm"], "Critical")
            ]
            ev = ErrataAdvisoryStateChangedEvent(
                "msg123", ErrataAdvisory(123, "RHSA-2017", state, ['rpm']))
            ret = handler.handle(ev)

            self.assertEqual(len(ret), 1)
            self.assertEqual(ret[0].advisory.errata_id, 123)
            self.assertEqual(ret[0].advisory.security_impact, "Critical")
            self.assertEqual(ret[0].advisory.name, "RHSA-2017")
コード例 #4
0
    def parse_post_data(self, data):
        """
        Method shared between Frontend and Backend to parse the POST data
        of manual rebuild JSON and generate the BaseEvent representation
        of the rebuild request.

        :param dict data: Dict generated from JSON from HTTP POST or parsed
            from the UMB message sent from Frontend to Backend.
        """
        msg_id = data.get('msg_id', "manual_rebuild_%s" % (str(time.time())))
        dry_run = data.get('dry_run', False)

        if data.get('errata_id', None):
            errata_id = data.get('errata_id')
            errata = Errata()
            advisory = ErrataAdvisory.from_advisory_id(errata, errata_id)

            event = ManualRebuildWithAdvisoryEvent(
                msg_id,
                advisory,
                data.get("container_images", []),
                data.get("metadata", None),
                freshmaker_event_id=data.get('freshmaker_event_id'),
                manual=True,
                dry_run=dry_run,
                requester=data.get('requester', None))
        else:
            event = ManualBundleRebuild(msg_id,
                                        data.get('container_images', []),
                                        data.get('bundle_images'),
                                        data.get('metadata', None),
                                        dry_run=dry_run,
                                        requester=data.get('requester', None))

        return event
コード例 #5
0
 def test_ensure_to_handle_rpm_and_module_adivsory(self):
     for content_type in ['rpm', 'module']:
         event = ErrataAdvisoryStateChangedEvent(
             'msg-id-1',
             ErrataAdvisory(123, 'name', 'REL_PREP',
                            [content_type, 'jar', 'pom']))
         handler = UpdateDBOnAdvisoryChange()
         self.assertTrue(handler.can_handle(event))
コード例 #6
0
    def parse(self, topic, msg):
        msg_id = msg.get('msg_id')
        inner_msg = msg.get('msg')
        errata_id = int(inner_msg.get('errata_id'))

        errata = Errata()
        return ErrataAdvisoryStateChangedEvent(
            msg_id, ErrataAdvisory.from_advisory_id(errata, errata_id))
コード例 #7
0
    def test_passing_dry_run(self):
        ev = ErrataAdvisoryStateChangedEvent("msg123",
                                             ErrataAdvisory(
                                                 123, "name", "SHIPPED_LIVE",
                                                 ["rpm"]),
                                             dry_run=True)
        self.assertEqual(ev.dry_run, True)

        ev = ErrataAdvisoryRPMsSignedEvent("123",
                                           ErrataAdvisory(
                                               123,
                                               "RHBA-2017",
                                               "REL_PREP", [],
                                               security_impact="",
                                               product_short_name="product"),
                                           dry_run=True)
        self.assertEqual(ev.dry_run, True)
コード例 #8
0
 def test_advisories_from_event_errata_state_change_event(
         self, errata_http_get, errata_rest_get):
     MockedErrataAPI(errata_rest_get, errata_http_get)
     event = ErrataAdvisoryStateChangedEvent(
         "msgid", ErrataAdvisory(28484, "name", "SHIPPED_LIVE", ['rpm']))
     advisories = self.errata.advisories_from_event(event)
     self.assertEqual(len(advisories), 1)
     self.assertEqual(advisories[0].errata_id, 28484)
コード例 #9
0
 def test_can_handle_manual_event(self):
     event = ManualRebuildWithAdvisoryEvent(
         "123",
         ErrataAdvisory(123,
                        "RHBA-2017",
                        "SHIPPED_LIVE", ["docker"],
                        security_impact="",
                        product_short_name="product"), [])
     ret = self.handler.can_handle(event)
     self.assertTrue(ret)
コード例 #10
0
    def test_rebuild_if_not_exists_unknown_errata_id(self,
                                                     advisories_from_event):
        advisories_from_event.return_value = []
        handler = UpdateDBOnAdvisoryChange()

        for state in ["REL_PREP", "PUSH_READY", "IN_PUSH", "SHIPPED_LIVE"]:
            ev = ErrataAdvisoryStateChangedEvent(
                "msg123", ErrataAdvisory(123, 'RHSA-2017', state, ['rpm']))
            ret = handler.handle(ev)

            self.assertEqual(len(ret), 0)
コード例 #11
0
    def test_mark_as_released_wrong_advisory_status(self):
        db_event = Event.create(db.session, "msg124", "123",
                                ErrataAdvisoryRPMsSignedEvent, False)
        db.session.commit()

        for state in ["NEW_FILES", "QE", "REL_PREP", "PUSH_READY", "IN_PUSH"]:
            ev = ErrataAdvisoryStateChangedEvent(
                "msg123", ErrataAdvisory(123, "name", state, ['rpm']))

            handler = UpdateDBOnAdvisoryChange()
            handler.handle(ev)

            db.session.refresh(db_event)
            self.assertEqual(db_event.released, False)
コード例 #12
0
    def setUp(self):
        super(RebuildImagesOnImageAdvisoryChangeTest, self).setUp()

        self.event = ErrataAdvisoryStateChangedEvent(
            "123",
            ErrataAdvisory(123,
                           "RHBA-2017",
                           "SHIPPED_LIVE", [],
                           security_impact="",
                           product_short_name="product"))
        self.handler = RebuildImagesOnImageAdvisoryChange()
        self.db_event = models.Event.get_or_create(db.session,
                                                   self.event.msg_id,
                                                   self.event.search_key,
                                                   self.event.__class__)
    def test_allow_build_false(self, handler_build_whitelist, builds_signed,
                               advisories_from_event):
        """
        Tests that allow_build filters out advisories based on advisory_name.
        """
        advisories_from_event.return_value = [
            ErrataAdvisory(123, "RHBA-2017", "REL_PREP", ["rpm"])]
        builds_signed.return_value = False

        event = MagicMock()
        handler = GenerateAdvisorySignedEventOnRPMSign()
        ret = handler.handle(event)

        self.assertTrue(not ret)
        builds_signed.assert_not_called()
コード例 #14
0
    def test_not_rebuild_if_errata_state_is_not_allowed(
            self, rebuild_if_not_exists):
        rebuild_if_not_exists.return_value = [Mock(), Mock()]

        Event.create(db.session, "msg-id-123", "123456",
                     ErrataAdvisoryRPMsSignedEvent, False)
        db.session.commit()

        event = ErrataAdvisoryStateChangedEvent(
            'msg-id-123',
            ErrataAdvisory(123456, 'name', 'SHIPPED_LIVE', ['rpm']))
        handler = UpdateDBOnAdvisoryChange()
        msgs = handler.handle(event)

        self.assertEqual([], msgs)
コード例 #15
0
    def test_mark_as_released(self):
        db_event = Event.create(db.session, "msg124", "123",
                                ErrataAdvisoryRPMsSignedEvent, False)
        db.session.commit()

        self.assertEqual(db_event.released, False)

        ev = ErrataAdvisoryStateChangedEvent(
            "msg123", ErrataAdvisory(123, "name", "SHIPPED_LIVE", ["rpm"]))

        handler = UpdateDBOnAdvisoryChange()
        handler.handle(ev)

        db.session.refresh(db_event)
        self.assertEqual(db_event.released, True)
コード例 #16
0
    def parse(self, topic, msg):
        msg_id = msg.get('msg_id')
        inner_msg = msg.get('msg')
        errata_id = int(inner_msg.get('errata_id'))

        errata = Errata()
        advisory = ErrataAdvisory.from_advisory_id(errata, errata_id)
        # If advisory created by BOTAS and it's shipped,
        # then return BotasErrataShippedEvent event
        if advisory.state == "SHIPPED_LIVE" and \
           advisory.reporter.startswith('botas'):
            event = BotasErrataShippedEvent(msg_id, advisory)
        else:
            event = ErrataAdvisoryStateChangedEvent(msg_id, advisory)
        return event
    def test_allow_build_true_global(self, handler_build_whitelist,
                                     builds_signed, advisories_from_event):
        """
        Tests that allow_build does not filter out advisories based on
        advisory_name.
        """
        advisories_from_event.return_value = [
            ErrataAdvisory(123, "RHSA-2017", "REL_PREP", ["rpm"])]
        builds_signed.return_value = False

        event = MagicMock()
        handler = GenerateAdvisorySignedEventOnRPMSign()
        handler.handle(event)

        builds_signed.assert_called_once()
    def test_allow_security_impact_important_false(
            self, handler_build_whitelist, builds_signed,
            advisories_from_event):
        """
        Tests that allow_build dost filter out advisories based on
        advisory_security_impact.
        """
        advisories_from_event.return_value = [
            ErrataAdvisory(123, "RHSA-2017", "REL_PREP", ["rpm"], "None")]
        builds_signed.return_value = False

        event = MagicMock()
        handler = GenerateAdvisorySignedEventOnRPMSign()
        handler.handle(event)

        builds_signed.assert_not_called()
    def test_return_value(self, handler_build_whitelist, builds_signed,
                          advisories_from_event):
        """
        Tests that handle method returns ErrataAdvisoryRPMsSignedEvent.
        """
        advisories_from_event.return_value = [
            ErrataAdvisory(123, "RHSA-2017", "REL_PREP", ["rpm"])]
        builds_signed.return_value = True

        event = MagicMock()
        event.msg_id = "msg_123"
        handler = GenerateAdvisorySignedEventOnRPMSign()
        ret = handler.handle(event)

        self.assertTrue(len(ret), 1)
        self.assertEqual(ret[0].advisory.name, "RHSA-2017")
        self.assertEqual(ret[0].advisory.errata_id, 123)
コード例 #20
0
    def setUp(self):
        super(TestBotasShippedAdvisory, self).setUp()

        # Each time when recording a build into database, freshmaker has to
        # request a pulp repo from ODCS. This is not necessary for running
        # tests.
        self.patcher = helpers.Patcher(
            'freshmaker.handlers.botas.botas_shipped_advisory.')
        self.pyxis = self.patcher.patch("Pyxis")

        # We do not want to send messages to message bus while running tests
        self.mock_messaging_publish = self.patcher.patch(
            'freshmaker.messaging.publish')

        self.handler = HandleBotasAdvisory()

        self.botas_advisory = ErrataAdvisory(123, "RHBA-2020", "SHIPPED_LIVE",
                                             ['docker'])
        self.botas_advisory._reporter = "botas/[email protected]"
    def test_do_not_create_already_handled_event(
            self, handler_build_whitelist, builds_signed,
            advisories_from_event):
        """
        Tests that GenerateAdvisorySignedEventOnRPMSign don't return Event which already exists
        in Freshmaker DB.
        """
        builds_signed.return_value = True
        advisories_from_event.return_value = [
            ErrataAdvisory(123, "RHSA-2017", "REL_PREP", ["rpm"])]

        event = MagicMock()
        event.msg_id = "msg_123"
        handler = GenerateAdvisorySignedEventOnRPMSign()
        handler.handle(event)

        builds_signed.assert_called_once()
        builds_signed.reset_mock()

        handler.handle(event)
        builds_signed.assert_not_called()
コード例 #22
0
    def parse(self, topic, msg):
        msg_id = msg.get('msg_id')
        inner_msg = msg.get('msg')
        errata_id = int(inner_msg.get('errata_id'))
        new_state = inner_msg.get('to')

        errata = Errata()
        advisory = ErrataAdvisory.from_advisory_id(errata, errata_id)
        # When there is message delay, state change messages can arrive after
        # advisory has been changed to a different state other than the one
        # in message, so we override advisory state with the state in message
        advisory.state = new_state

        # If advisory created by BOTAS and it's shipped,
        # then return BotasErrataShippedEvent event
        if advisory.state == "SHIPPED_LIVE" and advisory.reporter.startswith(
                'botas'):
            event = BotasErrataShippedEvent(msg_id, advisory)
        else:
            event = ErrataAdvisoryStateChangedEvent(msg_id, advisory)
        return event
コード例 #23
0
dictConfig(fedmsg_config.get('logging', {'version': 1}))

if len(sys.argv) < 2:
    print("Queries Lightblue to find out all the images Freshmaker rebuilds.")
    print("Usage: ./lightblue.py ERRATA_ID [[CONTAINER_IMAGE], ...]")
    sys.exit(1)

container_images = sys.argv[2:]

app_context = app.app_context()
app_context.__enter__()

db.drop_all()
db.create_all()
db.session.commit()

errata = Errata()
kwargs = {}
if container_images:
    EventClass = ManualRebuildWithAdvisoryEvent
    kwargs['container_images'] = container_images
else:
    EventClass = ErrataAdvisoryStateChangedEvent
event = EventClass(
    "fake_message", ErrataAdvisory.from_advisory_id(errata, sys.argv[1]),
    dry_run=True, **kwargs)

handler = RebuildImagesOnRPMAdvisoryChange()
with patch("freshmaker.consumer.get_global_consumer"):
    handler.handle(event)
コード例 #24
0
 def test_not_handle_non_rpm_advisory(self):
     event = ErrataAdvisoryStateChangedEvent(
         'msg-id-1', ErrataAdvisory(123, 'name', 'REL_PREP', ['docker']))
     handler = UpdateDBOnAdvisoryChange()
     self.assertFalse(handler.can_handle(event))
コード例 #25
0
    def test_mark_as_released_unknown_event(self, advisories_from_event):
        ev = ErrataAdvisoryStateChangedEvent(
            "msg123", ErrataAdvisory(123, "name", "SHIPPED_LIVE", ["rpm"]))

        handler = UpdateDBOnAdvisoryChange()
        handler.handle(ev)
コード例 #26
0
    kwargs['container_images'] = args.container_img
else:
    EventClass = ErrataAdvisoryStateChangedEvent

event_id = None
if args.cassette:
    cassette_name = os.path.splitext(os.path.basename(args.cassette))[0]
    extension = os.path.splitext(os.path.basename(args.cassette))[1]
    try:
        event_id = int(cassette_name)
        if extension != ".yml":
            raise ValueError
    except ValueError:
        print(
            "The input cassette must be named in the format of <event ID>.yml")
        sys.exit(1)

    conf.vcrpy_path = os.path.dirname(args.cassette)
    conf.vcrpy_mode = 'none'

event = EventClass("fake_message",
                   ErrataAdvisory.from_advisory_id(errata, sys.argv[1]),
                   dry_run=True,
                   freshmaker_event_id=event_id,
                   **kwargs)

handler = RebuildImagesOnRPMAdvisoryChange()

with patch("freshmaker.consumer.get_global_consumer"):
    handler.handle(event)