def test_failed_to_build_images_never_built_before(self):
     """
     This test checks that trying to build an image that was never built before (for that
     branch) will make the build fail.
     """
     self.mock_find_images_to_rebuild.return_value = [self.image_a]
     self.mock_find_images_trees_to_rebuild = self.patcher.patch(
         'find_images_trees_to_rebuild', return_value=[[self.image_a]])
     event = FreshmakerAsyncManualBuildEvent(
         'msg-id-123', 'another-branch', ['image-a-container'])
     handler = RebuildImagesOnAsyncManualBuild()
     handler.handle(event)
     db_event = Event.get(db.session, 'msg-id-123')
     self.assertEqual(EventState.FAILED.value, db_event.state)
    def test_building_single_image(self):
        """
        This tests the successful build of a single image
        """
        self.mock_find_images_to_rebuild.return_value = [self.image_a]
        self.mock_find_images_trees_to_rebuild = self.patcher.patch(
            'find_images_trees_to_rebuild', return_value=[[self.image_a]])
        event = FreshmakerAsyncManualBuildEvent(
            'msg-id-123', 'test_branch', ['image-a-container'])
        handler = RebuildImagesOnAsyncManualBuild()
        handler.handle(event)

        db_event = Event.get(db.session, 'msg-id-123')
        self.assertEqual(EventState.BUILDING.value, db_event.state)
        self.mock_get_image_builds_in_first_batch.assert_called_once_with(db.session)
        self.assertEqual(len(db_event.builds.all()), 1)
        self.mock_start_to_build_images.assert_called_once()
    def test_multiple_nvrs_for_the_same_name(self):
        """
        This test checks that when for one name more nvrs are returned by lightblue, Freshmaker
        will pick the one with higher nvr.
        """
        self.mock_find_images_to_rebuild.return_value = [self.image_a, self.image_c]
        self.mock_find_images_trees_to_rebuild = self.patcher.patch(
            'find_images_trees_to_rebuild', return_value=[[self.image_c]])
        event = FreshmakerAsyncManualBuildEvent(
            'msg-id-123', 'test_branch', ['image-a-container'])
        handler = RebuildImagesOnAsyncManualBuild()
        handler.handle(event)

        db_event = Event.get(db.session, 'msg-id-123')
        self.assertEqual(EventState.BUILDING.value, db_event.state)
        self.mock_get_image_builds_in_first_batch.assert_called_once_with(db.session)
        self.mock_start_to_build_images.assert_called_once()
        self.assertEqual(len(db_event.builds.all()), 1)
        self.assertEqual(db_event.builds.one().original_nvr, 'image-a-container-1.0-3')
    def test_building_sibilings(self):
        """
        This test checks that when the users requests to rebuild 2 images that are sibilings
        (or other unrelated images) Freshmaker will rebuild them separately, without the need
        of rebuilding the parent.
        """
        self.mock_find_images_to_rebuild.return_value = [self.image_b, self.image_d]
        self.find_images_trees_to_rebuild = self.patcher.patch(
            'find_images_trees_to_rebuild', return_value=[
                [self.image_b, self.image_a, self.image_0],
                [self.image_d, self.image_a, self.image_0]])
        event = FreshmakerAsyncManualBuildEvent(
            'msg-id-123', 'test_branch', ['image-b-container', 'image-d-container'])
        handler = RebuildImagesOnAsyncManualBuild()
        handler.handle(event)

        db_event = Event.get(db.session, 'msg-id-123')
        self.assertEqual(EventState.BUILDING.value, db_event.state)
        self.mock_get_image_builds_in_first_batch.assert_called_once_with(db.session)
        self.assertEqual(len(db_event.builds.all()), 2)
        self.mock_start_to_build_images.assert_called_once()
    def test_related_images_are_built(self):
        self.mock_find_images_to_rebuild.return_value = [self.image_b, self.image_d, self.image_a]
        self.find_images_trees_to_rebuild = self.patcher.patch(
            'find_images_trees_to_rebuild', return_value=[
                [self.image_a, self.image_0],
                [self.image_b, self.image_a, self.image_0],
                [self.image_d, self.image_a, self.image_0]])
        self.mock_generate_batches = self.patcher.patch('generate_batches', return_value=[
            [self.image_a],
            [self.image_b, self.image_d]
        ])
        event = FreshmakerAsyncManualBuildEvent(
            'msg-id-123', 'test_branch',
            ['image-a-container', 'image-b-container', 'image-d-container'])
        handler = RebuildImagesOnAsyncManualBuild()
        handler.handle(event)

        db_event = Event.get(db.session, 'msg-id-123')
        self.assertEqual(EventState.BUILDING.value, db_event.state)
        self.mock_get_image_builds_in_first_batch.assert_called_once_with(db.session)
        self.assertEqual(len(db_event.builds.all()), 3)
        self.mock_start_to_build_images.assert_called_once()
    def test_parent_if_image_without_parent(self):
        """
        This tests if we get parent as brew build of single image to rebuild
        when image doesn't have "parent" key
        """
        self.mock_find_images_to_rebuild.return_value = [self.image_f]
        event = FreshmakerAsyncManualBuildEvent(
            'msg-id-123', 'test_branch', ['image-a-container'])
        find_parent_mock = MagicMock()
        find_parent_mock.find_parent_brew_build_nvr_from_child.return_value = 'ubi8-container-8.2-299'
        self.mock_lightblue.return_value = find_parent_mock
        RebuildImagesOnAsyncManualBuild().handle(event)

        db_event = Event.get(db.session, 'msg-id-123')

        # Check if build in DB corresponds to parent of the image
        build = db_event.builds.first().json()
        self.assertEqual(build['build_args'].get('original_parent', 0),
                         'ubi8-container-8.2-299')
        # check if we are calling Lightblue to get proper parent of image
        find_parent_mock.find_parent_brew_build_nvr_from_child.assert_called_once_with(self.image_f)
    def test_parent_if_image_with_parent(self):
        """
        This tests if we get parent of single image to rebuild, when image
        has "parent" key as None OR as some image
        """
        for index, image in enumerate([self.image_0, self.image_a], 1):
            self.mock_find_images_to_rebuild.return_value = [image]
            event_id = f'msg-id-{index}'
            event = FreshmakerAsyncManualBuildEvent(
                event_id, 'test_branch', ['image-a-container'])
            RebuildImagesOnAsyncManualBuild().handle(event)

            db_event = Event.get(db.session, event_id)

            if image['parent'] is not None:
                original_parent = image['parent']['brew']['build']
            else:
                original_parent = None

            # Check if build in DB corresponds to parent of the image
            build = db_event.builds.first().json()
            self.assertEqual(build['build_args'].get('original_parent', 0),
                             original_parent)
 def test_can_handle_event(self):
     event = FreshmakerAsyncManualBuildEvent(
         'msg-id-01', 'repo-branch', ['image1', 'image2'])
     handler = RebuildImagesOnAsyncManualBuild()
     self.assertTrue(handler.can_handle(event))
示例#9
0
fedmsg_config = fedmsg.config.load_config()
dictConfig(fedmsg_config.get('logging', {'version': 1}))

if len(sys.argv) < 3:
    print("Usage: ./test_async_rebuild.py BRANCH CSV_LIST_IMAGES")
    sys.exit(1)

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

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

branch = sys.argv[1]
images = sys.argv[2].split(',')
kwargs = {}

event = FreshmakerAsyncManualBuildEvent(msg_id='fake-msg',
                                        dist_git_branch=branch,
                                        container_images=images,
                                        dry_run=True)

handler = RebuildImagesOnAsyncManualBuild()
with patch("freshmaker.consumer.get_global_consumer"):
    with patch(
            "freshmaker.handlers.koji.RebuildImagesOnAsyncManualBuild.start_to_build_images"
    ):
        handler.handle(event)