Merge "Bugfix: Failed executing command: 'free -s 1 -c 10'"
[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 from __future__ import absolute_import
13 import os
14 import mock
15 import unittest
16
17 from yardstick.common import utils
18
19
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.
24
25     def test_itersubclasses(self):
26         class A(object):
27             pass
28
29         class B(A):
30             pass
31
32         class C(A):
33             pass
34
35         class D(C):
36             pass
37
38         self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
39
40
41 class TryAppendModuleTestCase(unittest.TestCase):
42
43     @mock.patch('yardstick.common.utils.importutils')
44     def test_try_append_module_not_in_modules(self, mock_importutils):
45
46         modules = {}
47         name = 'foo'
48         utils.try_append_module(name, modules)
49         mock_importutils.import_module.assert_called_with(name)
50
51     @mock.patch('yardstick.common.utils.importutils')
52     def test_try_append_module_already_in_modules(self, mock_importutils):
53
54         modules = {'foo'}
55         name = 'foo'
56         utils.try_append_module(name, modules)
57         self.assertFalse(mock_importutils.import_module.called)
58
59
60 class ImportModulesFromPackageTestCase(unittest.TestCase):
61
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):
65
66         sep = os.sep
67         mock_walk.return_value = ([
68             ('..' + sep + 'foo', ['bar'], ['__init__.py']),
69             ('..' + sep + 'foo' + sep + 'bar', [], ['baz.txt', 'qux.rst'])
70         ])
71
72         utils.import_modules_from_package('foo.bar')
73         self.assertFalse(mock_append.called)
74
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):
78
79         sep = os.sep
80         mock_walk.return_value = ([
81             ('foo' + sep + '..' + sep + 'bar', [], ['baz.py'])
82         ])
83
84         utils.import_modules_from_package('foo.bar')
85         mock_importutils.import_module.assert_called_with('bar.baz')
86
87
88 class GetParaFromYaml(unittest.TestCase):
89
90     def test_get_para_from_yaml_file_not_exist(self):
91         file_path = '/etc/yardstick/hello.yaml'
92         args = 'hello.world'
93         para = utils.get_para_from_yaml(file_path, args)
94         self.assertIsNone(para)
95
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)
99         args = 'releng.file'
100         self.assertIsNone(utils.get_para_from_yaml(file_path, args))
101
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)
105         args = 'releng.dir'
106         para = '/home/opnfv/repos/releng'
107         self.assertEqual(para, utils.get_para_from_yaml(file_path, args))
108
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)
112         return file_path
113
114
115 def main():
116     unittest.main()
117
118 if __name__ == '__main__':
119     main()