#324 Move the global _FAKE_COMPOSE_ID to _fake_odcs_new_compose and fix traceback caused by missing `builds` kwarg.
Merged 10 months ago by jkaluza. Opened 10 months ago by jkaluza.
jkaluza/freshmaker odcs-builds-kwarg  into  master

@@ -63,15 +63,6 @@ 

          if event.dry_run:

              self.force_dry_run()

  

-         # In case we run in DRY_RUN mode, we need to initialize

-         # FAKE_COMPOSE_ID to the id of last ODCS compose to give the IDs

-         # increasing and unique even between Freshmaker restarts.

-         if self.dry_run:

-             ErrataAdvisoryRPMsSignedHandler._FAKE_COMPOSE_ID = \

-                 Compose.get_lowest_compose_id(db.session) - 1

-             if ErrataAdvisoryRPMsSignedHandler._FAKE_COMPOSE_ID >= 0:

-                 ErrataAdvisoryRPMsSignedHandler._FAKE_COMPOSE_ID = -1

- 

          self.event = event

  

          # Generate the Database representation of `event`, it can be

file modified
+12 -7

@@ -89,9 +89,6 @@ 

      This class is intended to be used in the BaseHandler scope.

      """

  

-     # Used to generate incremental compose id in dry run mode.

-     _FAKE_COMPOSE_ID = 0

- 

      def __init__(self, handler):

          """

          Creates new FreshmakerODCSClient.

@@ -102,7 +99,8 @@ 

          self.handler = handler

  

      def _fake_odcs_new_compose(

-             self, compose_source, tag, packages=None, results=[]):

+             self, compose_source, tag, packages=None, results=[],

+             builds=None):

          """

          Fake odcs.new_compose(...) method used in the dry run mode.

  

@@ -115,15 +113,22 @@ 

              "DRY RUN: Calling fake odcs.new_compose with args: %r",

              (compose_source, tag, packages, results))

  

-         # Generate the new_compose dict.

-         FreshmakerODCSClient._FAKE_COMPOSE_ID -= 1

+         # In case we run in DRY_RUN mode, we need to initialize

+         # FAKE_COMPOSE_ID to the id of last ODCS compose to give the IDs

+         # increasing and unique even between Freshmaker restarts.

+         fake_compose_id = Compose.get_lowest_compose_id(db.session) - 1

+         if fake_compose_id >= 0:

+             fake_compose_id = -1

+ 

          new_compose = {}

-         new_compose['id'] = FreshmakerODCSClient._FAKE_COMPOSE_ID

+         new_compose['id'] = fake_compose_id

          new_compose['result_repofile'] = "http://localhost/%d.repo" % (

              new_compose['id'])

          new_compose['state'] = COMPOSE_STATES['done']

          if results:

              new_compose['results'] = ['boot.iso']

+         if builds:

+             new_compose['builds'] = builds

  

          # Generate and inject the ODCSComposeStateChangeEvent event.

          event = ODCSComposeStateChangeEvent(

@@ -313,23 +313,6 @@ 

              'image': {'advisory_name': 'RHBA-2017'}

          }

      })

-     @patch.object(freshmaker.conf, 'dry_run', new=True)

-     def test_setting_fake_compose_id_dry_run_mode(self):

-         compose_4 = Compose(odcs_compose_id=4)

-         db.session.add(compose_4)

-         db.session.commit()

- 

-         self.mock_find_images_to_rebuild.return_value = [[]]

-         handler = ErrataAdvisoryRPMsSignedHandler()

-         handler.handle(self.rhba_event)

- 

-         self.assertEqual(ErrataAdvisoryRPMsSignedHandler._FAKE_COMPOSE_ID, -1)

- 

-     @patch.object(freshmaker.conf, 'handler_build_whitelist', new={

-         'ErrataAdvisoryRPMsSignedHandler': {

-             'image': {'advisory_name': 'RHBA-2017'}

-         }

-     })

      def test_event_state_updated_when_no_images_to_rebuild(self):

          self.mock_find_images_to_rebuild.return_value = [[]]

          handler = ErrataAdvisoryRPMsSignedHandler()

@@ -298,6 +298,7 @@ 

              'freshmaker.handlers.errata.ErrataAdvisoryRPMsSignedHandler.')

  

      def tearDown(self):

+         super(TestBatches, self).tearDown()

          self.patcher.unpatch_all()

  

      def _mock_build(self, build, parent=None, error=None):

file modified
+34 -2

@@ -20,17 +20,21 @@ 

  # SOFTWARE.

  #

  # Written by Chenxiong Qi <cqi@redhat.com>

+ #            Jan kaluza <jkaluza@redhat.com>

  

+ import fedmsg

  import six

+ import queue

  

- from mock import patch, Mock

+ from mock import patch, Mock, MagicMock

  from odcs.client.odcs import AuthMech

  

  from freshmaker import conf, db

- from freshmaker.models import Event, ArtifactBuild

+ from freshmaker.models import Event, ArtifactBuild, Compose

  from freshmaker.odcsclient import create_odcs_client

  from freshmaker.types import ArtifactBuildState, EventState, ArtifactType

  from freshmaker.handlers import ContainerBuildHandler

+ from freshmaker.consumer import FreshmakerConsumer

  from tests import helpers

  

  

@@ -298,6 +302,34 @@ 

              '', 'build', builds=set(['avalon-logkit-2.1-14.el7', 'apache-commons-lang-2.6-15.el7']),

              flags=['no_deps'], packages=set([u'avalon-logkit', u'apache-commons-lang']), sigkeys=[])

  

+     def _create_consumer(self):

+         hub = MagicMock()

+         hub.config = fedmsg.config.load_config()

+         hub.config['freshmakerconsumer'] = True

+         consumer = FreshmakerConsumer(hub)

+         consumer.incoming = queue.Queue()

+         return consumer

+ 

+     @patch("freshmaker.consumer.get_global_consumer")

+     def test_prepare_odcs_compose_with_image_rpms_dry_run(self, global_consumer):

+         consumer = self._create_consumer()

+         global_consumer.return_value = consumer

+         image = self._get_fake_container_image()

+ 

+         # Run multiple times, so we can verify that id of fake compose is set

+         # properly and is not repeating.

+         for i in range(1, 3):

+             handler = MyHandler()

+             handler.force_dry_run()

+             compose = handler.odcs.prepare_odcs_compose_with_image_rpms(image)

+             db_compose = Compose(odcs_compose_id=compose['id'])

+             db.session.add(db_compose)

+             db.session.commit()

+ 

+             self.assertEqual(-i, compose['id'])

+             event = consumer.incoming.get()

+             self.assertEqual(event.msg_id, "fake_compose_msg")

+ 

      def test_prepare_odcs_compose_with_image_rpms_no_rpm_manifest(self):

          handler = MyHandler()