Create a constants.py to manage constant variable consistently
[yardstick.git] / tests / unit / common / test_utils.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 #
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 ##############################################################################
9
10 # Unittest for yardstick.common.utils
11
12 import os
13 import mock
14 import unittest
15
16 from yardstick.common import utils
17
18
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):
24         class A(object):
25             pass
26
27         class B(A):
28             pass
29
30         class C(A):
31             pass
32
33         class D(C):
34             pass
35
36         self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
37
38
39 class TryAppendModuleTestCase(unittest.TestCase):
40
41     @mock.patch('yardstick.common.utils.importutils')
42     def test_try_append_module_not_in_modules(self, mock_importutils):
43
44         modules = {}
45         name = 'foo'
46         utils.try_append_module(name, modules)
47         mock_importutils.import_module.assert_called_with(name)
48
49     @mock.patch('yardstick.common.utils.importutils')
50     def test_try_append_module_already_in_modules(self, mock_importutils):
51
52         modules = {'foo'}
53         name = 'foo'
54         utils.try_append_module(name, modules)
55         self.assertFalse(mock_importutils.import_module.called)
56
57
58 class ImportModulesFromPackageTestCase(unittest.TestCase):
59
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):
63
64         sep = os.sep
65         mock_walk.return_value = ([
66             ('..' + sep + 'foo', ['bar'], ['__init__.py']),
67             ('..' + sep + 'foo' + sep + 'bar', [], ['baz.txt', 'qux.rst'])
68         ])
69
70         utils.import_modules_from_package('foo.bar')
71         self.assertFalse(mock_append.called)
72
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):
76
77         sep = os.sep
78         mock_walk.return_value = ([
79             ('foo' + sep + '..' + sep + 'bar', [], ['baz.py'])
80         ])
81
82         utils.import_modules_from_package('foo.bar')
83         mock_importutils.import_module.assert_called_with('bar.baz')
84
85
86 class GetParaFromYaml(unittest.TestCase):
87
88     def test_get_para_from_yaml_file_not_exist(self):
89         file_path = '/etc/yardstick/hello.yaml'
90         args = 'hello.world'
91         para = utils.get_para_from_yaml(file_path, args)
92         self.assertIsNone(para)
93
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)
97         args = 'releng.file'
98         self.assertIsNone(utils.get_para_from_yaml(file_path, args))
99
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)
103         args = 'releng.dir'
104         para = '/home/opnfv/repos/releng'
105         self.assertEqual(para, utils.get_para_from_yaml(file_path, args))
106
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)
110         return file_path
111
112
113 def main():
114     unittest.main()
115
116 if __name__ == '__main__':
117     main()