Fix and simplify arbitrary_string. lp#1085048
The previous behaviour:
In [4]: data_utils.arbitrary_string(size=3, base_text='test')
Out[4]: 'tes'
In [5]: data_utils.arbitrary_string(size=4, base_text='test')
Out[5]: 'testtesttesttest'
didn't seem sane. Fixed to more sane:
In [5]: data_utils.arbitrary_string(size=2, base_text='test')
Out[5]: 'te'
In [6]: data_utils.arbitrary_string(size=4, base_text='test')
Out[6]: 'test'
In [7]: data_utils.arbitrary_string(size=6, base_text='test')
Out[7]: 'testte
Change-Id: Idf38329b26846b15de929e7d00c6dd1353bde068
diff --git a/tempest/common/utils/data_utils.py b/tempest/common/utils/data_utils.py
index 15afd0a..9d422e1 100644
--- a/tempest/common/utils/data_utils.py
+++ b/tempest/common/utils/data_utils.py
@@ -18,6 +18,8 @@
import random
import re
import urllib
+import itertools
+
from tempest import exceptions
@@ -64,24 +66,10 @@
def arbitrary_string(size=4, base_text=None):
- """Return exactly size bytes worth of base_text as a string"""
-
- if (base_text is None) or (base_text == ''):
+ """
+ Return size characters from base_text, repeating the base_text infinitely
+ if needed.
+ """
+ if not base_text:
base_text = 'test'
-
- if size <= 0:
- return ''
-
- extra = size % len(base_text)
- body = ''
-
- if extra == 0:
- body = base_text * size
-
- if extra == size:
- body = base_text[:size]
-
- if extra > 0 and extra < size:
- body = (size / len(base_text)) * base_text + base_text[:extra]
-
- return body
+ return ''.join(itertools.islice(itertools.cycle(base_text), size))