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
16 from yardstick.common import utils
19 class IterSubclassesTestCase(unittest.TestCase):
20 # Disclaimer: this class is a modified copy from
21 # rally/tests/unit/common/plugin/test_discover.py
22 # Copyright 2015: Mirantis Inc.
23 def test_itersubclasses(self):
36 self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
39 class TryAppendModuleTestCase(unittest.TestCase):
41 @mock.patch('yardstick.common.utils.importutils')
42 def test_try_append_module_not_in_modules(self, mock_importutils):
46 utils.try_append_module(name, modules)
47 mock_importutils.import_module.assert_called_with(name)
49 @mock.patch('yardstick.common.utils.importutils')
50 def test_try_append_module_already_in_modules(self, mock_importutils):
54 utils.try_append_module(name, modules)
55 self.assertFalse(mock_importutils.import_module.called)
58 class ImportModulesFromPackageTestCase(unittest.TestCase):
60 @mock.patch('yardstick.common.utils.os.walk')
61 @mock.patch('yardstick.common.utils.try_append_module')
62 def test_import_modules_from_package_no_mod(self, mock_append, mock_walk):
65 mock_walk.return_value = ([
66 ('..' + sep + 'foo', ['bar'], ['__init__.py']),
67 ('..' + sep + 'foo' + sep + 'bar', [], ['baz.txt', 'qux.rst'])
70 utils.import_modules_from_package('foo.bar')
71 self.assertFalse(mock_append.called)
73 @mock.patch('yardstick.common.utils.os.walk')
74 @mock.patch('yardstick.common.utils.importutils')
75 def test_import_modules_from_package(self, mock_importutils, mock_walk):
78 mock_walk.return_value = ([
79 ('foo' + sep + '..' + sep + 'bar', [], ['baz.py'])
82 utils.import_modules_from_package('foo.bar')
83 mock_importutils.import_module.assert_called_with('bar.baz')
86 class GetParaFromYaml(unittest.TestCase):
88 def test_get_para_from_yaml_file_not_exist(self):
89 file_path = '/etc/yardstick/hello.yaml'
91 para = utils.get_para_from_yaml(file_path, args)
92 self.assertIsNone(para)
94 def test_get_para_from_yaml_para_not_found(self):
95 file_path = 'config_sample.yaml'
96 file_path = self._get_file_abspath(file_path)
98 self.assertIsNone(utils.get_para_from_yaml(file_path, args))
100 def test_get_para_from_yaml_para_exists(self):
101 file_path = 'config_sample.yaml'
102 file_path = self._get_file_abspath(file_path)
104 para = '/home/opnfv/repos/releng'
105 self.assertEqual(para, utils.get_para_from_yaml(file_path, args))
107 def _get_file_abspath(self, filename):
108 curr_path = os.path.dirname(os.path.abspath(__file__))
109 file_path = os.path.join(curr_path, filename)
116 if __name__ == '__main__':