2013-08-30 13:35:43 +03:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
"""Test for Rally utils."""
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-10-01 17:16:44 +04:00
|
|
|
import datetime
|
2013-08-30 13:35:43 +03:00
|
|
|
import sys
|
|
|
|
|
2013-09-29 03:11:10 +04:00
|
|
|
from rally import exceptions
|
2013-08-30 13:35:43 +03:00
|
|
|
from rally import test
|
|
|
|
from rally import utils
|
|
|
|
|
|
|
|
|
2013-09-29 03:11:10 +04:00
|
|
|
class ImmutableMixinTestCase(test.NoDBTestCase):
|
|
|
|
|
|
|
|
def test_without_base_values(self):
|
|
|
|
im = utils.ImmutableMixin()
|
|
|
|
self.assertRaises(exceptions.ImmutableException,
|
|
|
|
im.__setattr__, 'test', 'test')
|
|
|
|
|
|
|
|
def test_with_base_values(self):
|
|
|
|
|
|
|
|
class A(utils.ImmutableMixin):
|
|
|
|
def __init__(self, test):
|
|
|
|
self.test = test
|
|
|
|
super(A, self).__init__()
|
|
|
|
|
|
|
|
a = A('test')
|
|
|
|
self.assertRaises(exceptions.ImmutableException,
|
|
|
|
a.__setattr__, 'abc', 'test')
|
|
|
|
self.assertEqual(a.test, 'test')
|
|
|
|
|
|
|
|
|
|
|
|
class EnumMixinTestCase(test.NoDBTestCase):
|
|
|
|
|
|
|
|
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"]))
|
|
|
|
|
|
|
|
|
2013-08-30 13:35:43 +03:00
|
|
|
class StdIOCaptureTestCase(test.NoDBTestCase):
|
|
|
|
|
|
|
|
def test_stdout_capture(self):
|
|
|
|
stdout = sys.stdout
|
|
|
|
messages = ['abcdef', 'defgaga']
|
|
|
|
with utils.StdOutCapture() as out:
|
|
|
|
for msg in messages:
|
|
|
|
print(msg)
|
|
|
|
|
|
|
|
self.assertEqual(out.getvalue().rstrip('\n').split('\n'), messages)
|
|
|
|
self.assertEqual(stdout, sys.stdout)
|
|
|
|
|
|
|
|
def test_stderr_capture(self):
|
|
|
|
stderr = sys.stderr
|
|
|
|
messages = ['abcdef', 'defgaga']
|
|
|
|
with utils.StdErrCapture() as err:
|
|
|
|
for msg in messages:
|
|
|
|
print(msg, file=sys.stderr)
|
|
|
|
|
|
|
|
self.assertEqual(err.getvalue().rstrip('\n').split('\n'), messages)
|
|
|
|
self.assertEqual(stderr, sys.stderr)
|
2013-09-01 02:10:18 +04:00
|
|
|
|
|
|
|
|
|
|
|
class IterSubclassesTestCase(test.NoDBTestCase):
|
|
|
|
|
|
|
|
def test_itersubclasses(self):
|
|
|
|
class A(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class B(A):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class C(A):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class D(C):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
|
2013-09-07 11:42:07 +04:00
|
|
|
|
|
|
|
|
|
|
|
class ImportModulesTestCase(test.NoDBTestCase):
|
|
|
|
def test_try_append_module_into_sys_modules(self):
|
|
|
|
modules = {}
|
|
|
|
utils.try_append_module('rally.version', modules)
|
|
|
|
self.assertTrue('rally.version' in modules)
|
2013-09-24 12:54:25 +03:00
|
|
|
|
|
|
|
def test_try_append_broken_module(self):
|
|
|
|
modules = {}
|
|
|
|
self.assertRaises(ImportError,
|
|
|
|
utils.try_append_module,
|
|
|
|
'tests.fixtures.import.broken',
|
|
|
|
modules)
|
|
|
|
|
|
|
|
def test_import_modules_from_package(self):
|
|
|
|
utils.import_modules_from_package('tests.fixtures.import.package')
|
|
|
|
self.assertTrue('tests.fixtures.import.package.a' in sys.modules)
|
|
|
|
self.assertTrue('tests.fixtures.import.package.b' in sys.modules)
|
2013-10-01 17:16:44 +04:00
|
|
|
|
|
|
|
|
|
|
|
class SyncExecuteTestCase(test.NoDBTestCase):
|
|
|
|
|
|
|
|
def test_sync_execute(self):
|
|
|
|
|
|
|
|
def fake_factory():
|
|
|
|
return object()
|
|
|
|
|
|
|
|
def fake_checker_based_on_time(obj):
|
|
|
|
return datetime.datetime.now().second % 5 == 0
|
|
|
|
|
|
|
|
def fake_checker_always_false(obj):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def fake_updater(obj):
|
|
|
|
return obj
|
|
|
|
|
2013-10-03 16:21:25 +04:00
|
|
|
utils.sync_execute(fake_factory, [], {}, fake_checker_based_on_time,
|
2013-10-01 17:16:44 +04:00
|
|
|
fake_updater)
|
|
|
|
self.assertRaises(exceptions.TimeoutException, utils.sync_execute,
|
2013-10-03 16:21:25 +04:00
|
|
|
fake_factory, [], {}, fake_checker_always_false,
|
2013-10-01 17:16:44 +04:00
|
|
|
fake_updater, 3)
|