Error when enqueue-ref doesn't get long enough rev

The zuul executors (and the python git bindings they use) need full
length sha1 commit hashes. If you provide a short (or too long) hash
when attempting to enqueue a ref via the rpcclient the executors will
all fail and eventually you'll get a retry limit. We can avoid this
entirely if we error quickly and provide the user a nice error message.

Add a check for the old rev and new rev commit hash length. Additionally
check if the hash is a valid base 16 number. If not return a nice error
message.

Change-Id: Ia1cf4ac2bb87ce3248b34f246a6d6353df079dbf
This commit is contained in:
Clark Boylan 2018-02-20 16:29:02 -08:00
parent ec1f54deb3
commit 885e1423ca
2 changed files with 66 additions and 0 deletions

View File

@ -3577,6 +3577,56 @@ class TestScheduler(ZuulTestCase):
self.assertEqual(len(self.history), 0)
self.assertEqual(len(self.builds), 0)
def test_client_enqueue_ref_negative(self):
"Test that the RPC client returns errors"
client = zuul.rpcclient.RPCClient('127.0.0.1',
self.gearman_server.port)
self.addCleanup(client.shutdown)
with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
"New rev must be 40 character sha1"):
r = client.enqueue_ref(
tenant='tenant-one',
pipeline='post',
project='org/project',
trigger='gerrit',
ref='master',
oldrev='90f173846e3af9154517b88543ffbd1691f31366',
newrev='10054041')
self.assertEqual(r, False)
with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
"Old rev must be 40 character sha1"):
r = client.enqueue_ref(
tenant='tenant-one',
pipeline='post',
project='org/project',
trigger='gerrit',
ref='master',
oldrev='10054041',
newrev='90f173846e3af9154517b88543ffbd1691f31366')
self.assertEqual(r, False)
with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
"New rev must be base16 hash"):
r = client.enqueue_ref(
tenant='tenant-one',
pipeline='post',
project='org/project',
trigger='gerrit',
ref='master',
oldrev='90f173846e3af9154517b88543ffbd1691f31366',
newrev='notbase16')
self.assertEqual(r, False)
with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
"Old rev must be base16 hash"):
r = client.enqueue_ref(
tenant='tenant-one',
pipeline='post',
project='org/project',
trigger='gerrit',
ref='master',
oldrev='notbase16',
newrev='90f173846e3af9154517b88543ffbd1691f31366')
self.assertEqual(r, False)
def test_client_promote(self):
"Test that the RPC client can promote a change"
self.executor_server.hold_jobs_in_build = True

View File

@ -235,6 +235,22 @@ class RPCListener(object):
event.ref = args['ref']
event.oldrev = args['oldrev']
event.newrev = args['newrev']
try:
int(event.oldrev, 16)
if len(event.oldrev) != 40:
errors += 'Old rev must be 40 character sha1: ' \
'%s\n' % event.oldrev
except Exception:
errors += 'Old rev must be base16 hash: ' \
'%s\n' % event.oldrev
try:
int(event.newrev, 16)
if len(event.newrev) != 40:
errors += 'New rev must be 40 character sha1: ' \
'%s\n' % event.newrev
except Exception:
errors += 'New rev must be base16 hash: ' \
'%s\n' % event.newrev
if errors:
job.sendWorkException(errors.encode('utf8'))