提交 e2c56967 编写于 作者: A Ademar de Souza Reis Jr

core.job.id: improve create_unique_job_id()

The previous implementation was mixing non-random information with
random data before applying a hash on top of everything. Given that,
we couldn't use the hash later to validate the information and the
entropy was limited to 64 bits, which was the random part.

So I've got rid of the sha1 code and just used a 160 random number.

A future improvement would be the use a random number in the first
part of the ID (a salt) and a hash of relevant information in the
second part. In pseudo-code:

    info = hostname, date, list of tests, etc
    salt =  str(hex(getrandbits(120)))[[2:-1]
    hash = sha1(info).hexdigest()[:10]
    return salt+hash

Includes a functional test to check the ID is indeed a 40 digit hex
number.
Signed-off-by: NAdemar de Souza Reis Jr <areis@redhat.com>
上级 9ab8c0bb
......@@ -12,25 +12,19 @@
# Copyright: Red Hat Inc. 2013-2014
# Authors: Lucas Meneghel Rodrigues <lmr@redhat.com>
import hashlib
import random
import socket
import time
_RAND_POOL = random.SystemRandom()
_HOSTNAME = socket.gethostname()
def create_unique_job_id():
"""
Create a job ID SHA1.
Create a 40 digit hex number to be used as a job ID string.
(similar to SHA1)
:return: SHA1 string
:return: 40 digit hex number string
:rtype: str
"""
info = '%s-%s-%s' % (_HOSTNAME,
time.strftime('%Y-%m-%dT%H:%M:%S'),
_RAND_POOL.getrandbits(64))
h = hashlib.sha1()
h.update(info)
return h.hexdigest()
n = _RAND_POOL.getrandbits(160)
return str(hex(n))[2:-1]
......@@ -200,6 +200,14 @@ class RunnerOperationTest(unittest.TestCase):
self.assertNotIn('needs to be a 40 digit hex', result.stderr)
self.assertIn('SKIP', result.stderr)
def test_automatic_unique_id(self):
cmd_line = './scripts/avocado run skiptest --json -'
result = process.run(cmd_line, ignore_status=True)
self.assertEqual(0, result.exit_status)
r = json.loads(result.stdout)
int(r['job_id'], 16) # it's an hex number
self.assertEqual(len(r['job_id']), 40)
class RunnerDropinTest(unittest.TestCase):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册