From 654820ac31658c2102d54cde50cb3937977cf825 Mon Sep 17 00:00:00 2001 From: mprahl Date: Mon, 19 Feb 2018 13:51:35 -0500 Subject: [PATCH] Fix error that occurs when a batch hasn't started but a repo regen message is received Fixes #864 --- .../scheduler/handlers/repos.py | 5 +- tests/test_build/test_build.py | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/module_build_service/scheduler/handlers/repos.py b/module_build_service/scheduler/handlers/repos.py index 274ba62b..0d1eac05 100644 --- a/module_build_service/scheduler/handlers/repos.py +++ b/module_build_service/scheduler/handlers/repos.py @@ -37,7 +37,7 @@ def done(config, session, msg): # First, find our ModuleBuild associated with this repo, if any. tag = msg.repo_tag - if config.system == "koji" and not tag.endswith('-build'): + if config.system in ('koji', 'test') and not tag.endswith('-build'): log.debug("Tag %r does not end with '-build' suffix, ignoring" % tag) return tag = tag[:-6] if tag.endswith('-build') else tag @@ -64,6 +64,9 @@ def done(config, session, msg): if untagged_components: log.info("Ignoring repo regen, because not all components are tagged.") return + if all([c.state is None for c in module_build.current_batch()]): + log.info("Ignoring repo regen because no components have started in the batch.") + return # If there are no components in this module build, then current_batch will be empty if not module_build.component_builds: diff --git a/tests/test_build/test_build.py b/tests/test_build/test_build.py index b6c8e616..553567e5 100644 --- a/tests/test_build/test_build.py +++ b/tests/test_build/test_build.py @@ -1215,6 +1215,60 @@ class TestBuild: } assert data == expected + @patch('module_build_service.auth.get_user', return_value=user) + @patch('module_build_service.scm.SCM') + def test_submit_build_repo_regen_not_started_batch(self, mocked_scm, mocked_get_user, + conf_system, dbg, pdc_module_inactive): + """ + Tests that if MBS starts a new batch, the concurrent component threshold is met before a + build can start, and an unexpected repo regen occurs, the build will not fail. + + See: https://pagure.io/fm-orchestrator/issue/864 + """ + FakeSCM(mocked_scm, 'testmodule', 'testmodule.yaml', + '620ec77321b2ea7b0d67d82992dda3e1d67055b4') + + rv = self.client.post('/module-build-service/1/module-builds/', data=json.dumps( + {'branch': 'master', 'scmurl': 'git://pkgs.stg.fedoraproject.org/modules/' + 'testmodule.git?#620ec77321b2ea7b0d67d82992dda3e1d67055b4'})) + + data = json.loads(rv.data) + module_build_id = data['id'] + + def _stop_condition(message): + # Stop the backend if the module batch is 2 (where we simulate the concurrent threshold + # being met). For safety, also stop the backend if the module erroneously completes. + module = db.session.query(models.ModuleBuild).get(module_build_id) + return module.batch == 2 or module.state >= models.BUILD_STATES['done'] + + with patch('module_build_service.utils.at_concurrent_component_threshold') as mock_acct: + # Once we get to batch 2, then simulate the concurrent threshold being met + def _at_concurrent_component_threshold(config, session): + return db.session.query(models.ModuleBuild).get(module_build_id).batch == 2 + mock_acct.side_effect = _at_concurrent_component_threshold + module_build_service.scheduler.main([], _stop_condition) + + # Only module-build-macros should be built + for build in db.session.query(models.ComponentBuild).filter_by( + module_id=module_build_id).all(): + if build.package == 'module-build-macros': + assert build.state == koji.BUILD_STATES['COMPLETE'] + else: + assert build.state is None + assert build.module_build.state == models.BUILD_STATES['build'] + + # Simulate a random repo regen message that MBS didn't expect + cleanup_moksha() + module = db.session.query(models.ModuleBuild).first() + msgs = [module_build_service.messaging.KojiRepoChange( + msg_id='a faked internal message', repo_tag=module.koji_tag + '-build')] + db.session.expire_all() + # Stop after processing the seeded message + module_build_service.scheduler.main(msgs, lambda message: True) + # Make sure the module build didn't fail so that the poller can resume it later + module = db.session.query(models.ModuleBuild).get(module_build_id) + assert module.state == models.BUILD_STATES['build'] + @patch("module_build_service.config.Config.system", new_callable=PropertyMock, return_value="testlocal")