Refactor test.rand_str to reuse later

Signed-off-by: Mathieu Le Marec - Pasquet <kiorky@cryptelium.net>
This commit is contained in:
Mathieu Le Marec - Pasquet 2015-05-24 15:52:55 +02:00
parent 7646cb16ea
commit 1b8e0d0483
2 changed files with 22 additions and 4 deletions

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
'''
Module for running arbitrary tests
'''
@ -14,6 +13,7 @@ import random
# Import Salt libs
import salt
import salt.utils
import salt.version
import salt.loader
import salt.ext.six as six
@ -469,18 +469,26 @@ def opts_pkg():
return ret
def rand_str(size=9999999999):
def rand_str(size=9999999999, hash_type='md5'):
'''
Return a random string
size
size of the string to generate
hash_type
hash type to use
.. versionadded:: 2015.5.2
CLI Example:
.. code-block:: bash
salt '*' test.rand_str
'''
hasher = getattr(hashlib, __opts__.get('hash_type', 'md5'))
return hasher(str(random.SystemRandom().randint(0, size))).hexdigest()
return salt.utils.rand_str(
hash_type= __opts__.get('hash_type', hash_type),
size=size)
def exception(message='Test Exception'):

View File

@ -437,6 +437,16 @@ def profile_func(filename=None):
return proffunc
def rand_str(size=9999999999, hash_type=None):
'''
Return a random string
'''
if not hash_type:
hash_type = 'md5'
hasher = getattr(hashlib, hash_type)
return hasher(str(random.SystemRandom().randint(0, size))).hexdigest()
def which(exe=None):
'''
Python clone of /usr/bin/which