1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 # Unittest for yardstick.common.utils
12 from __future__ import absolute_import
17 from yardstick.common import utils
20 class IterSubclassesTestCase(unittest.TestCase):
21 # Disclaimer: this class is a modified copy from
22 # rally/tests/unit/common/plugin/test_discover.py
23 # Copyright 2015: Mirantis Inc.
25 def test_itersubclasses(self):
38 self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
41 class TryAppendModuleTestCase(unittest.TestCase):
43 @mock.patch('yardstick.common.utils.importutils')
44 def test_try_append_module_not_in_modules(self, mock_importutils):
48 utils.try_append_module(name, modules)
49 mock_importutils.import_module.assert_called_with(name)
51 @mock.patch('yardstick.common.utils.importutils')
52 def test_try_append_module_already_in_modules(self, mock_importutils):
56 utils.try_append_module(name, modules)
57 self.assertFalse(mock_importutils.import_module.called)
60 class ImportModulesFromPackageTestCase(unittest.TestCase):
62 @mock.patch('yardstick.common.utils.os.walk')
63 @mock.patch('yardstick.common.utils.try_append_module')
64 def test_import_modules_from_package_no_mod(self, mock_append, mock_walk):
67 mock_walk.return_value = ([
68 ('..' + sep + 'foo', ['bar'], ['__init__.py']),
69 ('..' + sep + 'foo' + sep + 'bar', [], ['baz.txt', 'qux.rst'])
72 utils.import_modules_from_package('foo.bar')
73 self.assertFalse(mock_append.called)
75 @mock.patch('yardstick.common.utils.os.walk')
76 @mock.patch('yardstick.common.utils.importutils')
77 def test_import_modules_from_package(self, mock_importutils, mock_walk):
80 mock_walk.return_value = ([
81 ('foo' + sep + '..' + sep + 'bar', [], ['baz.py'])
84 utils.import_modules_from_package('foo.bar')
85 mock_importutils.import_module.assert_called_with('bar.baz')
88 class GetParaFromYaml(unittest.TestCase):
90 def test_get_para_from_yaml_file_not_exist(self):
91 file_path = '/etc/yardstick/hello.yaml'
93 para = utils.get_para_from_yaml(file_path, args)
94 self.assertIsNone(para)
96 def test_get_para_from_yaml_para_not_found(self):
97 file_path = 'config_sample.yaml'
98 file_path = self._get_file_abspath(file_path)
100 self.assertIsNone(utils.get_para_from_yaml(file_path, args))
102 def test_get_para_from_yaml_para_exists(self):
103 file_path = 'config_sample.yaml'
104 file_path = self._get_file_abspath(file_path)
106 para = '/home/opnfv/repos/releng'
107 self.assertEqual(para, utils.get_para_from_yaml(file_path, args))
109 def _get_file_abspath(self, filename):
110 curr_path = os.path.dirname(os.path.abspath(__file__))
111 file_path = os.path.join(curr_path, filename)
118 if __name__ == '__main__':