13c3ed44abd42459d7102c29e167d3717c2c19aa
[yardstick.git] / yardstick / tests / unit / test_cmd / commands / test_env.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd 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 from __future__ import absolute_import
10 import unittest
11 import mock
12 import uuid
13
14 from yardstick.cmd.commands.env import EnvCommand
15
16
17 class EnvCommandTestCase(unittest.TestCase):
18
19     @mock.patch('yardstick.cmd.commands.env.EnvCommand._start_async_task')
20     @mock.patch('yardstick.cmd.commands.env.EnvCommand._check_status')
21     def test_do_influxdb(self, check_status_mock, start_async_task_mock):
22         env = EnvCommand()
23         env.do_influxdb({})
24         self.assertTrue(start_async_task_mock.called)
25         self.assertTrue(check_status_mock.called)
26
27     @mock.patch('yardstick.cmd.commands.env.EnvCommand._start_async_task')
28     @mock.patch('yardstick.cmd.commands.env.EnvCommand._check_status')
29     def test_do_grafana(self, check_status_mock, start_async_task_mock):
30         env = EnvCommand()
31         env.do_grafana({})
32         self.assertTrue(start_async_task_mock.called)
33         self.assertTrue(check_status_mock.called)
34
35     @mock.patch('yardstick.cmd.commands.env.EnvCommand._start_async_task')
36     @mock.patch('yardstick.cmd.commands.env.EnvCommand._check_status')
37     def test_do_prepare(self, check_status_mock, start_async_task_mock):
38         env = EnvCommand()
39         env.do_prepare({})
40         self.assertTrue(start_async_task_mock.called)
41         self.assertTrue(check_status_mock.called)
42
43     @mock.patch('yardstick.cmd.commands.env.HttpClient.post')
44     def test_start_async_task(self, post_mock):
45         data = {'action': 'create_grafana'}
46         EnvCommand()._start_async_task(data)
47         self.assertTrue(post_mock.called)
48
49     @mock.patch('yardstick.cmd.commands.env.HttpClient.get')
50     @mock.patch('yardstick.cmd.commands.env.EnvCommand._print_status')
51     def test_check_status(self, print_mock, get_mock):
52         # pylint: disable=unused-argument
53         # NOTE(ralonsoh): the pylint exception must be removed. The mocked
54         # command call must be tested.
55         task_id = str(uuid.uuid4())
56         get_mock.return_value = {'status': 2, 'result': 'error'}
57         status = EnvCommand()._check_status(task_id, 'hello world')
58         self.assertEqual(status, 2)
59
60     def test_print_status(self):
61         try:
62             EnvCommand()._print_status('hello', 'word')
63         except Exception as e:  # pylint: disable=broad-except
64             # NOTE(ralonsoh): try to reduce the scope of this exception.
65             self.assertIsInstance(e, IndexError)
66
67
68 def main():
69     unittest.main()
70
71
72 if __name__ == '__main__':
73     main()