From 93c0f8607c15ea7a5459752a71f04069de0d1b9d Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Mon, 9 Jul 2018 13:20:32 -0700 Subject: [PATCH] Require tenant in Pipeline constructor Pipelines are closely associated with their tenants, so that pipeline managers may find the currently active layout. To ensure that a Pipeline is always associated with exactly one tenant, require the tenant when constructing the pipeline, and raise an exception if we try to add the pipeline to a layout for a different tenant object. Change-Id: I9fafa0133d57c77ee172245d0897e375dcd0de15 --- tests/unit/test_model.py | 6 +++--- zuul/configloader.py | 4 +--- zuul/driver/github/githubreporter.py | 2 +- zuul/manager/__init__.py | 2 +- zuul/model.py | 14 +++++++++++--- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tests/unit/test_model.py b/tests/unit/test_model.py index 3786d49553..66eeb9999d 100644 --- a/tests/unit/test_model.py +++ b/tests/unit/test_model.py @@ -53,7 +53,7 @@ class TestJob(BaseTestCase): 'test', False) self.tpc = model.TenantProjectConfig(self.project) self.tenant.addUntrustedProject(self.tpc) - self.pipeline = model.Pipeline('gate', self.layout) + self.pipeline = model.Pipeline('gate', self.tenant) self.pipeline.source_context = self.context self.layout.addPipeline(self.pipeline) self.queue = model.ChangeQueue(self.pipeline) @@ -153,7 +153,7 @@ class TestJob(BaseTestCase): job.applyVariant(bad_final, self.layout) def test_job_inheritance_job_tree(self): - pipeline = model.Pipeline('gate', self.layout) + pipeline = model.Pipeline('gate', self.tenant) pipeline.source_context = self.context self.layout.addPipeline(pipeline) queue = model.ChangeQueue(pipeline) @@ -228,7 +228,7 @@ class TestJob(BaseTestCase): self.assertEqual(job.timeout, 70) def test_inheritance_keeps_matchers(self): - pipeline = model.Pipeline('gate', self.layout) + pipeline = model.Pipeline('gate', self.tenant) pipeline.source_context = self.context self.layout.addPipeline(pipeline) queue = model.ChangeQueue(pipeline) diff --git a/zuul/configloader.py b/zuul/configloader.py index 782fb05986..559e73c410 100644 --- a/zuul/configloader.py +++ b/zuul/configloader.py @@ -1040,7 +1040,7 @@ class PipelineParser(object): def fromYaml(self, conf): self.schema(conf) - pipeline = model.Pipeline(conf['name'], self.pcontext.tenant.name) + pipeline = model.Pipeline(conf['name'], self.pcontext.tenant) pipeline.source_context = conf['_source_context'] pipeline.description = conf.get('description') @@ -1722,8 +1722,6 @@ class TenantParser(object): # reference_exceptions has it; add tests if needed. if not skip_pipelines: for pipeline in parsed_config.pipelines: - if not pipeline.tenant: - pipeline.tenant = tenant layout.addPipeline(pipeline) for nodeset in parsed_config.nodesets: diff --git a/zuul/driver/github/githubreporter.py b/zuul/driver/github/githubreporter.py index 46de7f0f11..8be4bb1071 100644 --- a/zuul/driver/github/githubreporter.py +++ b/zuul/driver/github/githubreporter.py @@ -39,7 +39,7 @@ class GithubReporter(BaseReporter): self._unlabels = self.config.get('unlabel', []) if not isinstance(self._unlabels, list): self._unlabels = [self._unlabels] - self.context = "{}/{}".format(pipeline.tenant_name, pipeline.name) + self.context = "{}/{}".format(pipeline.tenant.name, pipeline.name) def report(self, item): """Report on an event.""" diff --git a/zuul/manager/__init__.py b/zuul/manager/__init__.py index d3f045f3c9..464891bfa3 100644 --- a/zuul/manager/__init__.py +++ b/zuul/manager/__init__.py @@ -47,7 +47,7 @@ class PipelineManager(object): def __init__(self, sched, pipeline): self.log = logging.getLogger("zuul.Pipeline.%s.%s" % - (pipeline.tenant_name, + (pipeline.tenant.name, pipeline.name,)) self.sched = sched self.pipeline = pipeline diff --git a/zuul/model.py b/zuul/model.py index 925f13e422..869662c678 100644 --- a/zuul/model.py +++ b/zuul/model.py @@ -180,11 +180,14 @@ class Pipeline(object): Reporter Communicates success and failure results somewhere """ - def __init__(self, name, tenant_name): + def __init__(self, name, tenant): self.name = name - self.tenant_name = tenant_name + # Note that pipelines are not portable across tenants (new + # pipeline objects must be made when a tenant is + # reconfigured). A pipeline requires a tenant in order to + # reach the currently active layout for that tenant. + self.tenant = tenant self.source_context = None - self.tenant = None self.description = None self.failure_message = None self.merge_failure_message = None @@ -2989,6 +2992,11 @@ class Layout(object): self.semaphores[semaphore.name] = semaphore def addPipeline(self, pipeline): + if pipeline.tenant is not self.tenant: + raise Exception("Pipeline created for tenant %s " + "may not be added to %s" % ( + pipeline.tenant, + self.tenant)) self.pipelines[pipeline.name] = pipeline def addProjectTemplate(self, project_template):