Report friendly errors when nodeset/secrets missing
Also, apply the as_list method to secrets (so that they, like
most lists, are optional singletons). The validation already
supported this.
Change-Id: I76ca1d502b18f49054ce9f8bed07bfcbac9e8cee
diff --git a/tests/unit/test_v3.py b/tests/unit/test_v3.py
index 9a10e9d..94f169a 100755
--- a/tests/unit/test_v3.py
+++ b/tests/unit/test_v3.py
@@ -748,6 +748,48 @@
self.assertIn('appears multiple times', A.messages[0],
"A should have a syntax error reported")
+ def test_secret_not_found_error(self):
+ in_repo_conf = textwrap.dedent(
+ """
+ - job:
+ name: test
+ secrets: does-not-exist
+ """)
+
+ file_dict = {'.zuul.yaml': in_repo_conf}
+ A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
+ files=file_dict)
+ A.addApproval('Code-Review', 2)
+ self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
+ self.waitUntilSettled()
+
+ self.assertEqual(A.data['status'], 'NEW')
+ self.assertEqual(A.reported, 1,
+ "A should report failure")
+ self.assertIn('secret "does-not-exist" was not found', A.messages[0],
+ "A should have a syntax error reported")
+
+ def test_nodeset_not_found_error(self):
+ in_repo_conf = textwrap.dedent(
+ """
+ - job:
+ name: test
+ nodeset: does-not-exist
+ """)
+
+ file_dict = {'.zuul.yaml': in_repo_conf}
+ A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
+ files=file_dict)
+ A.addApproval('Code-Review', 2)
+ self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
+ self.waitUntilSettled()
+
+ self.assertEqual(A.data['status'], 'NEW')
+ self.assertEqual(A.reported, 1,
+ "A should report failure")
+ self.assertIn('nodeset "does-not-exist" was not found', A.messages[0],
+ "A should have a syntax error reported")
+
def test_multi_repo(self):
downstream_repo_conf = textwrap.dedent(
"""
diff --git a/zuul/configloader.py b/zuul/configloader.py
index ef14f76..b70ea59 100644
--- a/zuul/configloader.py
+++ b/zuul/configloader.py
@@ -106,6 +106,24 @@
super(ProjectNotFoundError, self).__init__(message)
+class SecretNotFoundError(Exception):
+ def __init__(self, secret):
+ message = textwrap.dedent("""\
+ The secret "{secret}" was not found.
+ """)
+ message = textwrap.fill(message.format(secret=secret))
+ super(SecretNotFoundError, self).__init__(message)
+
+
+class NodesetNotFoundError(Exception):
+ def __init__(self, nodeset):
+ message = textwrap.dedent("""\
+ The nodeset "{nodeset}" was not found.
+ """)
+ message = textwrap.fill(message.format(nodeset=nodeset))
+ super(NodesetNotFoundError, self).__init__(message)
+
+
class PipelineNotPermittedError(Exception):
def __init__(self):
message = textwrap.dedent("""\
@@ -484,13 +502,15 @@
# Secrets are part of the playbook context so we must establish
# them earlier than playbooks.
secrets = []
- for secret_config in conf.get('secrets', []):
+ for secret_config in as_list(conf.get('secrets', [])):
if isinstance(secret_config, str):
secret_name = secret_config
- secret = layout.secrets[secret_name]
+ secret = layout.secrets.get(secret_name)
else:
secret_name = secret_config['name']
- secret = layout.secrets[secret_config['secret']]
+ secret = layout.secrets.get(secret_config['secret'])
+ if secret is None:
+ raise SecretNotFoundError(secret_name)
if secret_name == 'zuul':
raise Exception("Secrets named 'zuul' are not allowed.")
if secret.source_context != job.source_context:
@@ -571,7 +591,9 @@
conf_nodeset = conf['nodeset']
if isinstance(conf_nodeset, str):
# This references an existing named nodeset in the layout.
- ns = layout.nodesets[conf_nodeset]
+ ns = layout.nodesets.get(conf_nodeset)
+ if ns is None:
+ raise NodesetNotFoundError(conf_nodeset)
else:
ns = NodeSetParser.fromYaml(conf_nodeset, anonymous=True)
if tenant.max_nodes_per_job != -1 and \