2013-08-30 13:35:43 +03:00
|
|
|
# Copyright 2013: Mirantis Inc.
|
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
2014-06-27 14:53:32 +08:00
|
|
|
import string
|
2013-08-30 13:35:43 +03:00
|
|
|
import sys
|
2013-10-03 02:26:53 +04:00
|
|
|
import time
|
2013-08-30 13:35:43 +03:00
|
|
|
|
2015-09-22 09:13:51 -05:00
|
|
|
import ddt
|
2014-06-16 20:14:51 +03:00
|
|
|
import mock
|
Fix all py3 related issues
This patch makes output of `tox -epy34` to finish with following message:
> py34: commands succeeded
> congratulations :)
Issues:
* module "__builtin__" was renamed to "builtins" in Python 3
Related modules:
- rally.api
- tests.unit.benchmark.scenarios.vm.test_utils
* function "map"/"filter" returns "builtins.map"/"builtins.filter" object
in Python 3 instead of list in Python 2. "builtins.map" and
"builtins.filter" object is not subscriptable and has no len(), so list
comprehension is preferable to use and py2/py3 compatible way.
Related modules:
- rally.benchmark.context.sahara.sahara_edp
- rally.benchmark.processing.plot
- rally.benchmark.sla.base
- rally.benchmark.types
- rally.cmd.commands.task
- rally.cmd.commands.verify
- tests.unit.benchmark.scenarios.test_base
- tests.unit.benchmark.wrappers.test_keystone
- tests.unit.cmd.commands.test_task
- tests.unit.cmd.commands.test_verify
* dict.keys()/dict.values() returns "dict_keys"/"dict_values" object in
Python 3 instead of list in Python 2. so list(dict) and
list(dict.values()) should be used instead.
Related modules:
- rally.benchmark.scenarios.utils
- rally.benchmark.scenarios.vm.vmtasks
- tests.unit.cmd.commands.test_show
- tests.unit.common.test_broker
- tests.unit.deploy.engines.test_fuel
- tests.unit.fakes
* Some changes was made in Python 3 related to data model, so we should
change our inspect code. See code changes for more details
Related modules:
- rally.cmd.cliutils
- rally.common.utils
* ConfigParser is more strict for duplicate items in Python 3, so
duplicates are removed
Related files:
- rally/verification/tempest/config.ini
* Exception object doesn't have "message" attribute in Python 3, so
if we want to get it, the most proper way is using "getattr"
Related modules:
- rally.verification.tempest.config
* "mock.MagicMock" is not sortable in Python 3, so we should add required
attributes to fix that.
Related modules:
- tests.unit.benchmark.context.test_base
* assertSequenceEqual assertation method was added in tests.unit.test to
compare sequence objects
Related modules:
- tests.unit.benchmark.context.cleanup.test_resources
- tests.unit.benchmark.scenarios.nova.test_utils
* function "range" returns "range" object in Python 3 instead of list
in Python 2.
Related modules:
- tests.unit.benchmark.processing.test_utils
* keyword arguments should be transmitted to self.assertRaises as kwargs,
not like a dict
Related modules:
- tests.unit.benchmark.scenarios.dummy.test_dummy
Additional changes:
* Python 2.6 was added to setup.cfg, since Rally supports it.
* py33, py34 environments were added to tox.ini
* wrong ignore path was removed from tox.ini
* made items of bash complition sorted
Several tests are skipped in Python 3 env. For more details see notes in code:
- tests.unit.benchmark.processing.test_plot.PlotTestCase.test__process_main_time
- tests.unit.benchmark.processing.test_plot.PlotTestCase.test__process_atomic_time
- tests.unit.common.test_utils.MethodClassTestCase.test_method_class_for_class_level_method
During porting Rally to Python3, several issues found and fixed in
TempestContext and its unit tests:
- If process of cleanup is failed, exception is handled and cleanup is
marked as successfull. This issue was fixed and CleanUpException was
added to rally.exception module
- Cleanup was called with wrong path.
Change-Id: If04e873790dcb4c9c882d4be4bf40479deedd36d
2015-01-21 02:37:15 +02:00
|
|
|
import testtools
|
2014-06-16 20:14:51 +03:00
|
|
|
|
2014-12-24 10:20:26 +08:00
|
|
|
from rally.common.i18n import _
|
2014-12-25 15:02:44 +08:00
|
|
|
from rally.common import utils
|
2013-09-29 03:11:10 +04:00
|
|
|
from rally import exceptions
|
2014-10-06 21:18:24 +03:00
|
|
|
from tests.unit import test
|
2013-08-30 13:35:43 +03:00
|
|
|
|
|
|
|
|
2013-10-25 11:42:47 +03:00
|
|
|
class ImmutableMixinTestCase(test.TestCase):
|
2013-09-29 03:11:10 +04:00
|
|
|
|
|
|
|
def test_without_base_values(self):
|
|
|
|
im = utils.ImmutableMixin()
|
|
|
|
self.assertRaises(exceptions.ImmutableException,
|
2015-02-03 10:48:15 -06:00
|
|
|
im.__setattr__, "test", "test")
|
2013-09-29 03:11:10 +04:00
|
|
|
|
|
|
|
def test_with_base_values(self):
|
|
|
|
|
|
|
|
class A(utils.ImmutableMixin):
|
|
|
|
def __init__(self, test):
|
|
|
|
self.test = test
|
|
|
|
super(A, self).__init__()
|
|
|
|
|
2015-02-03 10:48:15 -06:00
|
|
|
a = A("test")
|
2013-09-29 03:11:10 +04:00
|
|
|
self.assertRaises(exceptions.ImmutableException,
|
2015-02-03 10:48:15 -06:00
|
|
|
a.__setattr__, "abc", "test")
|
|
|
|
self.assertEqual(a.test, "test")
|
2013-09-29 03:11:10 +04:00
|
|
|
|
|
|
|
|
2013-10-25 11:42:47 +03:00
|
|
|
class EnumMixinTestCase(test.TestCase):
|
2013-09-29 03:11:10 +04:00
|
|
|
|
|
|
|
def test_enum_mix_in(self):
|
|
|
|
|
|
|
|
class Foo(utils.EnumMixin):
|
|
|
|
a = 10
|
|
|
|
b = 20
|
|
|
|
CC = "2000"
|
|
|
|
|
|
|
|
self.assertEqual(set(list(Foo())), set([10, 20, "2000"]))
|
|
|
|
|
2015-07-10 17:34:45 +09:00
|
|
|
def test_with_underscore(self):
|
|
|
|
|
|
|
|
class Foo(utils.EnumMixin):
|
|
|
|
a = 10
|
|
|
|
b = 20
|
|
|
|
_CC = "2000"
|
|
|
|
|
|
|
|
self.assertEqual(set(list(Foo())), set([10, 20]))
|
|
|
|
|
2013-09-29 03:11:10 +04:00
|
|
|
|
2013-10-25 11:42:47 +03:00
|
|
|
class StdIOCaptureTestCase(test.TestCase):
|
2013-08-30 13:35:43 +03:00
|
|
|
|
|
|
|
def test_stdout_capture(self):
|
|
|
|
stdout = sys.stdout
|
2015-02-03 10:48:15 -06:00
|
|
|
messages = ["abcdef", "defgaga"]
|
2013-08-30 13:35:43 +03:00
|
|
|
with utils.StdOutCapture() as out:
|
|
|
|
for msg in messages:
|
|
|
|
print(msg)
|
|
|
|
|
2015-02-03 10:48:15 -06:00
|
|
|
self.assertEqual(out.getvalue().rstrip("\n").split("\n"), messages)
|
2013-08-30 13:35:43 +03:00
|
|
|
self.assertEqual(stdout, sys.stdout)
|
|
|
|
|
|
|
|
def test_stderr_capture(self):
|
|
|
|
stderr = sys.stderr
|
2015-02-03 10:48:15 -06:00
|
|
|
messages = ["abcdef", "defgaga"]
|
2013-08-30 13:35:43 +03:00
|
|
|
with utils.StdErrCapture() as err:
|
|
|
|
for msg in messages:
|
|
|
|
print(msg, file=sys.stderr)
|
|
|
|
|
2015-02-03 10:48:15 -06:00
|
|
|
self.assertEqual(err.getvalue().rstrip("\n").split("\n"), messages)
|
2013-08-30 13:35:43 +03:00
|
|
|
self.assertEqual(stderr, sys.stderr)
|
2013-09-01 02:10:18 +04:00
|
|
|
|
|
|
|
|
2013-10-25 11:42:47 +03:00
|
|
|
class TimerTestCase(test.TestCase):
|
2013-10-03 02:26:53 +04:00
|
|
|
|
|
|
|
def test_timer_duration(self):
|
|
|
|
start_time = time.time()
|
|
|
|
end_time = time.time()
|
|
|
|
|
2015-02-03 10:48:15 -06:00
|
|
|
with mock.patch("rally.common.utils.time") as mock_time:
|
2013-10-03 02:26:53 +04:00
|
|
|
mock_time.time = mock.MagicMock(return_value=start_time)
|
|
|
|
with utils.Timer() as timer:
|
|
|
|
mock_time.time = mock.MagicMock(return_value=end_time)
|
|
|
|
|
|
|
|
self.assertIsNone(timer.error)
|
|
|
|
self.assertEqual(end_time - start_time, timer.duration())
|
|
|
|
|
|
|
|
def test_timer_exception(self):
|
|
|
|
try:
|
|
|
|
with utils.Timer() as timer:
|
|
|
|
raise Exception()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
self.assertEqual(3, len(timer.error))
|
|
|
|
self.assertEqual(timer.error[0], type(Exception()))
|
|
|
|
|
|
|
|
|
2013-10-25 11:42:47 +03:00
|
|
|
class LogTestCase(test.TestCase):
|
2013-10-07 03:31:23 +04:00
|
|
|
|
2014-03-20 16:06:54 +08:00
|
|
|
def test_log_task_wrapper(self):
|
2013-10-07 03:31:23 +04:00
|
|
|
mock_log = mock.MagicMock()
|
|
|
|
msg = "test %(a)s %(b)s"
|
|
|
|
|
|
|
|
class TaskLog(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
2015-02-03 10:48:15 -06:00
|
|
|
self.task = {"uuid": "some_uuid"}
|
2013-10-07 03:31:23 +04:00
|
|
|
|
|
|
|
@utils.log_task_wrapper(mock_log, msg, a=10, b=20)
|
|
|
|
def some_method(self, x, y):
|
|
|
|
return x + y
|
|
|
|
|
|
|
|
t = TaskLog()
|
|
|
|
self.assertEqual(t.some_method.__name__, "some_method")
|
|
|
|
self.assertEqual(t.some_method(2, 2), 4)
|
2015-02-03 10:48:15 -06:00
|
|
|
params = {"msg": msg % {"a": 10, "b": 20}, "uuid": t.task["uuid"]}
|
2013-10-07 03:31:23 +04:00
|
|
|
expected = [
|
2014-03-20 16:06:54 +08:00
|
|
|
mock.call(_("Task %(uuid)s | Starting: %(msg)s") % params),
|
|
|
|
mock.call(_("Task %(uuid)s | Completed: %(msg)s") % params)
|
2013-10-07 03:31:23 +04:00
|
|
|
]
|
|
|
|
self.assertEqual(mock_log.mock_calls, expected)
|
2014-04-22 09:03:05 +03:00
|
|
|
|
2015-01-27 18:53:18 +03:00
|
|
|
def test_log_deprecated(self):
|
|
|
|
mock_log = mock.MagicMock()
|
|
|
|
|
2015-10-09 14:37:27 +03:00
|
|
|
@utils.log_deprecated("some alternative", "0.0.1", mock_log)
|
2015-01-27 18:53:18 +03:00
|
|
|
def some_method(x, y):
|
|
|
|
return x + y
|
|
|
|
|
|
|
|
self.assertEqual(some_method(2, 2), 4)
|
2015-10-09 14:37:27 +03:00
|
|
|
mock_log.assert_called_once_with("'some_method' is deprecated in "
|
|
|
|
"Rally v0.0.1: some alternative")
|
2015-01-27 18:53:18 +03:00
|
|
|
|
2015-04-16 15:40:43 +03:00
|
|
|
def test_log_deprecated_args(self):
|
|
|
|
mock_log = mock.MagicMock()
|
|
|
|
|
|
|
|
@utils.log_deprecated_args("Deprecated test", "0.0.1", ("z",),
|
|
|
|
mock_log, once=True)
|
|
|
|
def some_method(x, y, z):
|
|
|
|
return x + y + z
|
|
|
|
|
|
|
|
self.assertEqual(some_method(2, 2, z=3), 7)
|
|
|
|
mock_log.assert_called_once_with(
|
|
|
|
"Deprecated test (args `z' deprecated in Rally v0.0.1)")
|
|
|
|
|
|
|
|
mock_log.reset_mock()
|
|
|
|
self.assertEqual(some_method(2, 2, z=3), 7)
|
|
|
|
self.assertFalse(mock_log.called)
|
|
|
|
|
|
|
|
@utils.log_deprecated_args("Deprecated test", "0.0.1", ("z",),
|
|
|
|
mock_log, once=False)
|
|
|
|
def some_method(x, y, z):
|
|
|
|
return x + y + z
|
|
|
|
|
|
|
|
self.assertEqual(some_method(2, 2, z=3), 7)
|
|
|
|
mock_log.assert_called_once_with(
|
|
|
|
"Deprecated test (args `z' deprecated in Rally v0.0.1)")
|
|
|
|
|
|
|
|
mock_log.reset_mock()
|
|
|
|
self.assertEqual(some_method(2, 2, z=3), 7)
|
|
|
|
mock_log.assert_called_once_with(
|
|
|
|
"Deprecated test (args `z' deprecated in Rally v0.0.1)")
|
|
|
|
|
2014-04-22 09:03:05 +03:00
|
|
|
|
2014-06-25 13:39:27 +04:00
|
|
|
def module_level_method():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class MethodClassTestCase(test.TestCase):
|
|
|
|
|
Fix all py3 related issues
This patch makes output of `tox -epy34` to finish with following message:
> py34: commands succeeded
> congratulations :)
Issues:
* module "__builtin__" was renamed to "builtins" in Python 3
Related modules:
- rally.api
- tests.unit.benchmark.scenarios.vm.test_utils
* function "map"/"filter" returns "builtins.map"/"builtins.filter" object
in Python 3 instead of list in Python 2. "builtins.map" and
"builtins.filter" object is not subscriptable and has no len(), so list
comprehension is preferable to use and py2/py3 compatible way.
Related modules:
- rally.benchmark.context.sahara.sahara_edp
- rally.benchmark.processing.plot
- rally.benchmark.sla.base
- rally.benchmark.types
- rally.cmd.commands.task
- rally.cmd.commands.verify
- tests.unit.benchmark.scenarios.test_base
- tests.unit.benchmark.wrappers.test_keystone
- tests.unit.cmd.commands.test_task
- tests.unit.cmd.commands.test_verify
* dict.keys()/dict.values() returns "dict_keys"/"dict_values" object in
Python 3 instead of list in Python 2. so list(dict) and
list(dict.values()) should be used instead.
Related modules:
- rally.benchmark.scenarios.utils
- rally.benchmark.scenarios.vm.vmtasks
- tests.unit.cmd.commands.test_show
- tests.unit.common.test_broker
- tests.unit.deploy.engines.test_fuel
- tests.unit.fakes
* Some changes was made in Python 3 related to data model, so we should
change our inspect code. See code changes for more details
Related modules:
- rally.cmd.cliutils
- rally.common.utils
* ConfigParser is more strict for duplicate items in Python 3, so
duplicates are removed
Related files:
- rally/verification/tempest/config.ini
* Exception object doesn't have "message" attribute in Python 3, so
if we want to get it, the most proper way is using "getattr"
Related modules:
- rally.verification.tempest.config
* "mock.MagicMock" is not sortable in Python 3, so we should add required
attributes to fix that.
Related modules:
- tests.unit.benchmark.context.test_base
* assertSequenceEqual assertation method was added in tests.unit.test to
compare sequence objects
Related modules:
- tests.unit.benchmark.context.cleanup.test_resources
- tests.unit.benchmark.scenarios.nova.test_utils
* function "range" returns "range" object in Python 3 instead of list
in Python 2.
Related modules:
- tests.unit.benchmark.processing.test_utils
* keyword arguments should be transmitted to self.assertRaises as kwargs,
not like a dict
Related modules:
- tests.unit.benchmark.scenarios.dummy.test_dummy
Additional changes:
* Python 2.6 was added to setup.cfg, since Rally supports it.
* py33, py34 environments were added to tox.ini
* wrong ignore path was removed from tox.ini
* made items of bash complition sorted
Several tests are skipped in Python 3 env. For more details see notes in code:
- tests.unit.benchmark.processing.test_plot.PlotTestCase.test__process_main_time
- tests.unit.benchmark.processing.test_plot.PlotTestCase.test__process_atomic_time
- tests.unit.common.test_utils.MethodClassTestCase.test_method_class_for_class_level_method
During porting Rally to Python3, several issues found and fixed in
TempestContext and its unit tests:
- If process of cleanup is failed, exception is handled and cleanup is
marked as successfull. This issue was fixed and CleanUpException was
added to rally.exception module
- Cleanup was called with wrong path.
Change-Id: If04e873790dcb4c9c882d4be4bf40479deedd36d
2015-01-21 02:37:15 +02:00
|
|
|
@testtools.skipIf(sys.version_info > (2, 9), "Problems with access to "
|
|
|
|
"class from <locals>")
|
2014-06-25 13:39:27 +04:00
|
|
|
def test_method_class_for_class_level_method(self):
|
|
|
|
class A:
|
|
|
|
def m(self):
|
|
|
|
pass
|
2015-01-05 18:24:08 +02:00
|
|
|
self.assertEqual(A, utils.get_method_class(A.m))
|
2014-06-25 13:39:27 +04:00
|
|
|
|
|
|
|
def test_method_class_for_module_level_method(self):
|
|
|
|
self.assertIsNone(utils.get_method_class(module_level_method))
|
|
|
|
|
|
|
|
|
|
|
|
class FirstIndexTestCase(test.TestCase):
|
|
|
|
|
|
|
|
def test_list_with_existing_matching_element(self):
|
|
|
|
lst = [1, 3, 5, 7]
|
|
|
|
self.assertEqual(utils.first_index(lst, lambda e: e == 1), 0)
|
|
|
|
self.assertEqual(utils.first_index(lst, lambda e: e == 5), 2)
|
|
|
|
self.assertEqual(utils.first_index(lst, lambda e: e == 7), 3)
|
|
|
|
|
|
|
|
def test_list_with_non_existing_matching_element(self):
|
|
|
|
lst = [1, 3, 5, 7]
|
2014-11-27 17:58:21 +02:00
|
|
|
self.assertIsNone(utils.first_index(lst, lambda e: e == 2))
|
2014-06-25 13:39:27 +04:00
|
|
|
|
|
|
|
|
2014-10-01 03:58:11 +04:00
|
|
|
class EditDistanceTestCase(test.TestCase):
|
|
|
|
|
|
|
|
def test_distance_empty_strings(self):
|
|
|
|
dist = utils.distance("", "")
|
|
|
|
self.assertEqual(0, dist)
|
|
|
|
|
|
|
|
def test_distance_equal_strings(self):
|
|
|
|
dist = utils.distance("abcde", "abcde")
|
|
|
|
self.assertEqual(0, dist)
|
|
|
|
|
|
|
|
def test_distance_replacement(self):
|
|
|
|
dist = utils.distance("abcde", "__cde")
|
|
|
|
self.assertEqual(2, dist)
|
|
|
|
|
|
|
|
def test_distance_insertion(self):
|
|
|
|
dist = utils.distance("abcde", "ab__cde")
|
|
|
|
self.assertEqual(2, dist)
|
|
|
|
|
|
|
|
def test_distance_deletion(self):
|
|
|
|
dist = utils.distance("abcde", "abc")
|
|
|
|
self.assertEqual(2, dist)
|
2014-12-08 13:34:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TenantIteratorTestCase(test.TestCase):
|
|
|
|
|
|
|
|
def test_iterate_per_tenant(self):
|
2015-04-14 09:12:11 -07:00
|
|
|
users = []
|
2014-12-08 13:34:38 +02:00
|
|
|
tenants_count = 2
|
|
|
|
users_per_tenant = 5
|
|
|
|
for tenant_id in range(tenants_count):
|
|
|
|
for user_id in range(users_per_tenant):
|
|
|
|
users.append({"id": str(user_id),
|
|
|
|
"tenant_id": str(tenant_id)})
|
|
|
|
|
|
|
|
expected_result = [
|
|
|
|
({"id": "0", "tenant_id": str(i)}, str(i)) for i in range(
|
|
|
|
tenants_count)]
|
|
|
|
real_result = [i for i in utils.iterate_per_tenants(users)]
|
|
|
|
|
|
|
|
self.assertEqual(expected_result, real_result)
|
2014-06-27 14:53:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RAMIntTestCase(test.TestCase):
|
|
|
|
|
2014-12-25 15:02:44 +08:00
|
|
|
@mock.patch("rally.common.utils.multiprocessing")
|
2015-06-04 19:58:41 +03:00
|
|
|
def test__init__(self, mock_multiprocessing):
|
2014-06-27 14:53:32 +08:00
|
|
|
utils.RAMInt()
|
2015-06-04 19:58:41 +03:00
|
|
|
mock_multiprocessing.Lock.assert_called_once_with()
|
|
|
|
mock_multiprocessing.Value.assert_called_once_with("I", 0)
|
2014-06-27 14:53:32 +08:00
|
|
|
|
2014-12-25 15:02:44 +08:00
|
|
|
@mock.patch("rally.common.utils.multiprocessing")
|
2015-06-04 19:58:41 +03:00
|
|
|
def test__int__(self, mock_multiprocessing):
|
|
|
|
mock_multiprocessing.Value.return_value = mock.Mock(value=42)
|
2014-06-27 14:53:32 +08:00
|
|
|
self.assertEqual(int(utils.RAMInt()), 42)
|
|
|
|
|
2014-12-25 15:02:44 +08:00
|
|
|
@mock.patch("rally.common.utils.multiprocessing")
|
2015-06-04 19:58:41 +03:00
|
|
|
def test__str__(self, mock_multiprocessing):
|
|
|
|
mock_multiprocessing.Value.return_value = mock.Mock(value=42)
|
2014-06-27 14:53:32 +08:00
|
|
|
self.assertEqual(str(utils.RAMInt()), "42")
|
|
|
|
|
2014-12-25 15:02:44 +08:00
|
|
|
@mock.patch("rally.common.utils.multiprocessing")
|
2015-06-04 19:58:41 +03:00
|
|
|
def test__iter__(self, mock_multiprocessing):
|
2014-06-27 14:53:32 +08:00
|
|
|
ram_int = utils.RAMInt()
|
|
|
|
self.assertEqual(iter(ram_int), ram_int)
|
|
|
|
|
2014-12-25 15:02:44 +08:00
|
|
|
@mock.patch("rally.common.utils.multiprocessing")
|
2015-06-04 19:58:41 +03:00
|
|
|
def test__next__(self, mock_multiprocessing):
|
2014-06-27 14:53:32 +08:00
|
|
|
class MemInt(int):
|
|
|
|
THRESHOLD = 5
|
|
|
|
|
|
|
|
def __iadd__(self, i):
|
|
|
|
return MemInt((int(self) + i) % self.THRESHOLD)
|
|
|
|
|
|
|
|
mock_lock = mock.MagicMock()
|
2015-06-04 19:58:41 +03:00
|
|
|
mock_multiprocessing.Lock.return_value = mock_lock
|
|
|
|
mock_multiprocessing.Value.return_value = mock.Mock(value=MemInt(0))
|
2014-06-27 14:53:32 +08:00
|
|
|
|
|
|
|
ram_int = utils.RAMInt()
|
|
|
|
self.assertEqual(int(ram_int), 0)
|
|
|
|
for i in range(MemInt.THRESHOLD - 1):
|
|
|
|
self.assertEqual(ram_int.__next__(), i)
|
|
|
|
self.assertRaises(StopIteration, ram_int.__next__)
|
|
|
|
self.assertEqual(mock_lock.__enter__.mock_calls,
|
|
|
|
[mock.call()] * MemInt.THRESHOLD)
|
|
|
|
self.assertEqual(len(mock_lock.__exit__.mock_calls), MemInt.THRESHOLD)
|
|
|
|
|
2014-12-25 15:02:44 +08:00
|
|
|
@mock.patch("rally.common.utils.RAMInt.__next__",
|
|
|
|
return_value="next_value")
|
|
|
|
@mock.patch("rally.common.utils.multiprocessing")
|
2015-06-04 19:58:41 +03:00
|
|
|
def test_next(self, mock_multiprocessing, mock_ram_int___next__):
|
2014-12-31 08:18:48 +08:00
|
|
|
self.assertEqual(next(utils.RAMInt()), "next_value")
|
2015-06-04 19:58:41 +03:00
|
|
|
mock_ram_int___next__.assert_called_once_with()
|
2014-06-27 14:53:32 +08:00
|
|
|
|
2014-12-25 15:02:44 +08:00
|
|
|
@mock.patch("rally.common.utils.multiprocessing")
|
2015-06-04 19:58:41 +03:00
|
|
|
def test_reset(self, mock_multiprocessing):
|
2014-06-27 14:53:32 +08:00
|
|
|
ram_int = utils.RAMInt()
|
|
|
|
self.assertRaises(TypeError, int, ram_int)
|
|
|
|
ram_int.reset()
|
|
|
|
self.assertEqual(int(ram_int), 0)
|
|
|
|
|
|
|
|
|
|
|
|
class GenerateRandomTestCase(test.TestCase):
|
|
|
|
|
2014-12-25 15:02:44 +08:00
|
|
|
@mock.patch("rally.common.utils.random")
|
2014-06-27 14:53:32 +08:00
|
|
|
def test_generate_random_name(self, mock_random):
|
|
|
|
choice = "foobarspamchoicestring"
|
|
|
|
|
|
|
|
idx = iter(range(100))
|
2014-12-31 08:18:48 +08:00
|
|
|
mock_random.choice.side_effect = lambda choice: choice[next(idx)]
|
2014-12-29 13:11:49 +08:00
|
|
|
self.assertEqual(utils.generate_random_name(),
|
|
|
|
string.ascii_lowercase[:16])
|
2014-06-27 14:53:32 +08:00
|
|
|
|
|
|
|
idx = iter(range(100))
|
2014-12-31 08:18:48 +08:00
|
|
|
mock_random.choice.side_effect = lambda choice: choice[next(idx)]
|
2014-06-27 14:53:32 +08:00
|
|
|
self.assertEqual(utils.generate_random_name(length=10),
|
2014-12-29 13:11:49 +08:00
|
|
|
string.ascii_lowercase[:10])
|
2014-06-27 14:53:32 +08:00
|
|
|
|
|
|
|
idx = iter(range(100))
|
2014-12-31 08:18:48 +08:00
|
|
|
mock_random.choice.side_effect = lambda choice: choice[next(idx)]
|
2014-06-27 14:53:32 +08:00
|
|
|
self.assertEqual(utils.generate_random_name(choice=choice),
|
|
|
|
choice[:16])
|
|
|
|
|
|
|
|
idx = iter(range(100))
|
2014-12-31 08:18:48 +08:00
|
|
|
mock_random.choice.side_effect = lambda choice: choice[next(idx)]
|
2014-06-27 14:53:32 +08:00
|
|
|
self.assertEqual(utils.generate_random_name(choice=choice, length=5),
|
|
|
|
choice[:5])
|
|
|
|
|
|
|
|
idx = iter(range(100))
|
2014-12-31 08:18:48 +08:00
|
|
|
mock_random.choice.side_effect = lambda choice: choice[next(idx)]
|
2014-06-27 14:53:32 +08:00
|
|
|
self.assertEqual(
|
|
|
|
utils.generate_random_name(prefix="foo_", length=10),
|
2014-12-29 13:11:49 +08:00
|
|
|
"foo_" + string.ascii_lowercase[:10])
|
2014-06-27 14:53:32 +08:00
|
|
|
|
|
|
|
idx = iter(range(100))
|
2014-12-31 08:18:48 +08:00
|
|
|
mock_random.choice.side_effect = lambda choice: choice[next(idx)]
|
2014-06-27 14:53:32 +08:00
|
|
|
self.assertEqual(
|
|
|
|
utils.generate_random_name(prefix="foo_",
|
|
|
|
choice=choice, length=10),
|
|
|
|
"foo_" + choice[:10])
|
2015-09-22 09:13:51 -05:00
|
|
|
|
|
|
|
|
2015-10-23 10:21:24 -05:00
|
|
|
@ddt.ddt
|
2015-09-22 09:13:51 -05:00
|
|
|
class RandomNameTestCase(test.TestCase):
|
|
|
|
|
|
|
|
@ddt.data(
|
|
|
|
{},
|
|
|
|
{"task_id": "fake-task"},
|
|
|
|
{"task_id": "fake!task",
|
2015-10-23 10:21:24 -05:00
|
|
|
"expected": "s_rally_blargles_dweebled"},
|
2015-09-22 09:13:51 -05:00
|
|
|
{"fmt": "XXXX-test-XXX-test",
|
|
|
|
"expected": "fake-test-bla-test"})
|
|
|
|
@ddt.unpack
|
|
|
|
@mock.patch("random.choice")
|
|
|
|
def test_generate_random_name(self, mock_choice, task_id="faketask",
|
|
|
|
expected="s_rally_faketask_blargles",
|
|
|
|
fmt="s_rally_XXXXXXXX_XXXXXXXX"):
|
|
|
|
class FakeNameGenerator(utils.RandomNameGeneratorMixin):
|
|
|
|
RESOURCE_NAME_FORMAT = fmt
|
|
|
|
task = {"uuid": task_id}
|
|
|
|
|
|
|
|
generator = FakeNameGenerator()
|
|
|
|
|
|
|
|
mock_choice.side_effect = iter("blarglesdweebled")
|
|
|
|
self.assertEqual(generator.generate_random_name(), expected)
|
|
|
|
|
|
|
|
def test_generate_random_name_bogus_name_format(self):
|
|
|
|
class FakeNameGenerator(utils.RandomNameGeneratorMixin):
|
|
|
|
RESOURCE_NAME_FORMAT = "invalid_XXX_format"
|
|
|
|
task = {"uuid": "fake-task-id"}
|
|
|
|
|
|
|
|
generator = FakeNameGenerator()
|
|
|
|
self.assertRaises(ValueError,
|
|
|
|
generator.generate_random_name)
|
|
|
|
|
|
|
|
@ddt.data(
|
|
|
|
{"good": ("rally_abcdefgh_abcdefgh", "rally_12345678_abcdefgh",
|
|
|
|
"rally_ABCdef12_ABCdef12"),
|
|
|
|
"bad": ("rally_abcd_efgh", "rally_abcd!efg_12345678",
|
|
|
|
"rally_", "rally__", "rally_abcdefgh_",
|
|
|
|
"rally_abcdefghi_12345678", "foo", "foo_abcdefgh_abcdefgh")},
|
|
|
|
{"fmt": "][*_XXX_XXX",
|
|
|
|
"chars": "abc(.*)",
|
|
|
|
"good": ("][*_abc_abc", "][*_abc_((("),
|
|
|
|
"bad": ("rally_ab_cd", "rally_ab!_abc", "rally_", "rally__",
|
|
|
|
"rally_abc_", "rally_abcd_abc", "foo", "foo_abc_abc")},
|
|
|
|
{"fmt": "XXXX-test-XXX-test",
|
|
|
|
"good": ("abcd-test-abc-test",),
|
|
|
|
"bad": ("rally-abcdefgh-abcdefgh", "abc-test-abc-test",
|
|
|
|
"abcd_test_abc_test", "abc-test-abcd-test")})
|
|
|
|
@ddt.unpack
|
|
|
|
def test_name_matches_pattern(self, good=(), bad=(),
|
2015-10-23 10:21:24 -05:00
|
|
|
fmt="rally_XXXXXXXX_XXXXXXXX",
|
2015-09-22 09:13:51 -05:00
|
|
|
chars=string.ascii_letters + string.digits):
|
|
|
|
for name in good:
|
|
|
|
self.assertTrue(
|
|
|
|
utils.name_matches_pattern(name, fmt, chars),
|
|
|
|
"%s unexpectedly didn't match resource_name_format" % name)
|
|
|
|
|
|
|
|
for name in bad:
|
|
|
|
self.assertFalse(
|
|
|
|
utils.name_matches_pattern(name, fmt, chars),
|
|
|
|
"%s unexpectedly matched resource_name_format" % name)
|
|
|
|
|
|
|
|
@mock.patch("rally.common.utils.name_matches_pattern")
|
|
|
|
def test_name_matches_object(self, mock_name_matches_pattern):
|
|
|
|
name = "foo"
|
|
|
|
self.assertEqual(
|
|
|
|
utils.name_matches_object(name,
|
|
|
|
utils.RandomNameGeneratorMixin),
|
|
|
|
mock_name_matches_pattern.return_value)
|
|
|
|
mock_name_matches_pattern.assert_called_once_with(
|
|
|
|
name,
|
|
|
|
utils. RandomNameGeneratorMixin.RESOURCE_NAME_FORMAT,
|
|
|
|
utils.RandomNameGeneratorMixin.RESOURCE_NAME_ALLOWED_CHARACTERS)
|
|
|
|
|
|
|
|
def test_name_matches_pattern_identity(self):
|
|
|
|
generator = utils.RandomNameGeneratorMixin()
|
|
|
|
generator.task = {"uuid": "faketask"}
|
|
|
|
|
|
|
|
self.assertTrue(utils.name_matches_pattern(
|
|
|
|
generator.generate_random_name(),
|
|
|
|
generator.RESOURCE_NAME_FORMAT,
|
|
|
|
generator.RESOURCE_NAME_ALLOWED_CHARACTERS))
|
|
|
|
|
|
|
|
def test_name_matches_object_identity(self):
|
|
|
|
generator = utils.RandomNameGeneratorMixin()
|
|
|
|
generator.task = {"uuid": "faketask"}
|
|
|
|
|
|
|
|
self.assertTrue(utils.name_matches_object(
|
|
|
|
generator.generate_random_name(), generator))
|
|
|
|
self.assertTrue(utils.name_matches_object(
|
|
|
|
generator.generate_random_name(), utils.RandomNameGeneratorMixin))
|
|
|
|
|
|
|
|
def test_consistent_task_id_part(self):
|
|
|
|
class FakeNameGenerator(utils.RandomNameGeneratorMixin):
|
|
|
|
RESOURCE_NAME_FORMAT = "XXXXXXXX_XXXXXXXX"
|
|
|
|
|
|
|
|
generator = FakeNameGenerator()
|
|
|
|
generator.task = {"uuid": "good-task-id"}
|
|
|
|
|
|
|
|
names = [generator.generate_random_name() for i in range(100)]
|
|
|
|
task_id_parts = set([n.split("_")[0] for n in names])
|
|
|
|
self.assertEqual(len(task_id_parts), 1)
|
|
|
|
|
|
|
|
generator.task = {"uuid": "bogus! task! id!"}
|
|
|
|
|
|
|
|
names = [generator.generate_random_name() for i in range(100)]
|
|
|
|
task_id_parts = set([n.split("_")[0] for n in names])
|
|
|
|
self.assertEqual(len(task_id_parts), 1)
|