def run(self):
        """
        run the plugin
        """
        if self.delegate_enabled:
            # will be used in koji_import
            self.workflow.triggered_after_koji_task = self.triggered_after_koji_task

        task_running = False
        koji_task_id = self.metadata.get('labels', {}).get('koji-task-id')
        if koji_task_id:
            task_info = self.kojisession.getTaskInfo(koji_task_id,
                                                     request=True)
            if task_info:
                task_running = koji.TASK_STATES[task_info['state']] == 'OPEN'
            else:
                self.log.warning(
                    "koji-task-id label on build, doesn't exist in koji")
        else:
            self.log.warning("koji-task-id label doesn't exist on build")

        if not self.delegate_enabled:
            self.log.info("delegate_task not enabled, skipping plugin")
            return
        elif not is_rebuild(self.workflow):
            self.log.info("not autorebuild, skipping plugin")
            return
        elif (self.triggered_after_koji_task and task_running):
            # The buildConfig will already have triggered_after_koji_task in user_params
            # after the first autorebuild performed with the delegating feature enabled.
            # If koji-task-id for the build is a running task,
            # it means it is a new, already delegated task
            self.log.info("koji task already delegated, skipping plugin")
            return

        self.osbs = get_openshift_session(self.workflow, NO_FALLBACK)

        # Do not run exit plugins. Especially sendmail
        self.workflow.exit_plugins_conf = []

        if self.workflow.cancel_isolated_autorebuild:  # this is set by the koji_parent plugin
            self.log.info(
                "ignoring isolated build for autorebuild, the build will be cancelled"
            )
            self.cancel_build()
            raise BuildCanceledException("Build was canceled")

        self.delegate_task()

        # We cancel the build so it does not inerfere with real failed builds
        self.cancel_build()
        self.log.info('Build was delegated, the build will be cancelled')
        raise BuildCanceledException("Build was canceled")
def test_orchestrate_build_failed_to_list_builds(tmpdir, fail_at):
    workflow = mock_workflow(tmpdir)
    mock_osbs()  # Current builds is a constant 2

    mock_reactor_config(
        tmpdir, {
            'x86_64': [{
                'name': 'spam',
                'max_concurrent_builds': 5
            }, {
                'name': 'eggs',
                'max_concurrent_builds': 5
            }],
        })

    flexmock_chain = flexmock(OSBS).should_receive('list_builds').and_raise(
        OsbsException("foo"))

    if fail_at == 'all':
        flexmock_chain.and_raise(OsbsException("foo"))

    if fail_at == 'first':
        flexmock_chain.and_return(['a', 'b'])

    if fail_at == 'build_canceled':
        flexmock_chain.and_raise(OsbsException(cause=BuildCanceledException()))

    runner = BuildStepPluginsRunner(
        workflow.builder.tasker, workflow, [{
            'name': OrchestrateBuildPlugin.key,
            'args': {
                'platforms': ['x86_64'],
                'build_kwargs': make_worker_build_kwargs(),
                'osbs_client_config': str(tmpdir),
                'find_cluster_retry_delay': .1,
                'max_cluster_fails': 2
            }
        }])
    if fail_at == 'first':
        build_result = runner.run()
        assert not build_result.is_failed()

        annotations = build_result.annotations
        assert annotations['worker-builds']['x86_64']['build'][
            'cluster-url'] == 'https://eggs.com/'
    else:
        build_result = runner.run()
        assert build_result.is_failed()
        if fail_at == 'all':
            assert 'Could not find appropriate cluster for worker build.' \
                in build_result.fail_reason
        elif fail_at == 'build_canceled':
            assert 'BuildCanceledException()' in str(exc)
    def run(self):
        """
        run the plugin
        """
        if self.delegate_enabled:
            # will be used in koji_import
            self.workflow.triggered_after_koji_task = self.triggered_after_koji_task

        task_running = False
        koji_task_id = self.metadata.get('labels', {}).get('koji-task-id')
        if koji_task_id:
            task_info = self.kojisession.getTaskInfo(koji_task_id,
                                                     request=True)
            if task_info:
                task_running = koji.TASK_STATES[task_info['state']] == 'OPEN'
            else:
                self.log.warning(
                    "koji-task-id label on build, doesn't exist in koji")
        else:
            self.log.warning("koji-task-id label doesn't exist on build")

        # we don't want to plugin continue when:
        # delegate_task isn't enabled
        # build isn't autorebuild
        # triggered_after_koji_task was provided, but task is running,
        # reason for this is, when we once enable delegating, after first autorebuild
        # buildConfig will already have triggered_after_koji_task in user_params
        # so when koji-task-id for build is running task, that means it is that new
        # already delegated task
        if not self.delegate_enabled:
            self.log.info("delegate_task not enabled, skipping plugin")
            return
        elif not is_rebuild(self.workflow):
            self.log.info("not autorebuild, skipping plugin")
            return
        elif (self.triggered_after_koji_task and task_running):
            self.log.info("koji task already delegated, skipping plugin")
            return

        self.osbs = get_openshift_session(self.workflow, NO_FALLBACK)

        self.delegate_task()

        # we will remove all exit plugins, as we don't want any of them running,
        # mainly sendmail
        self.workflow.exit_plugins_conf = []
        # we will cancel build and raise exception,
        # without canceling build build would end up as failed build, and we don't want
        # to have this build as failed but cancelled so it doesn't inerfere with real failed builds
        self.cancel_build()
        self.log.info('Build was delegated, will cancel itself')
        raise BuildCanceledException("Build was canceled")
Example #4
0
 def throw_canceled_build_exception(self, *args, **kwargs):
     self.build_canceled = True
     raise BuildCanceledException("Build was canceled")
Example #5
0
 def _cancel_build(*args, **kwargs):
     raise BuildCanceledException()
Example #6
0
 def wait(self, timeout=None):
     time.sleep(0.1)
     if not self.exception_raised:
         self.exception_raised = True
         raise BuildCanceledException()