1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd 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 ##############################################################################
16 from yardstick.cmd.commands import env
17 from yardstick.tests.unit import base
20 class EnvCommandTestCase(base.BaseUnitTestCase):
22 @mock.patch.object(env.EnvCommand, '_start_async_task')
23 @mock.patch.object(env.EnvCommand, '_check_status')
24 def test_do_influxdb(self, check_status_mock, start_async_task_mock):
25 _env = env.EnvCommand()
27 start_async_task_mock.assert_called_once()
28 check_status_mock.assert_called_once()
30 @mock.patch.object(env.EnvCommand, '_start_async_task')
31 @mock.patch.object(env.EnvCommand, '_check_status')
32 def test_do_grafana(self, check_status_mock, start_async_task_mock):
33 _env = env.EnvCommand()
35 start_async_task_mock.assert_called_once()
36 check_status_mock.assert_called_once()
38 @mock.patch.object(env.EnvCommand, '_start_async_task')
39 @mock.patch.object(env.EnvCommand, '_check_status')
40 def test_do_prepare(self, check_status_mock, start_async_task_mock):
41 _env = env.EnvCommand()
43 start_async_task_mock.assert_called_once()
44 check_status_mock.assert_called_once()
46 @mock.patch.object(env.HttpClient, 'post')
47 def test_start_async_task(self, post_mock):
48 data = {'action': 'create_grafana'}
49 env.EnvCommand()._start_async_task(data)
50 post_mock.assert_called_once()
52 @mock.patch.object(env.HttpClient, 'get')
53 @mock.patch.object(env.EnvCommand, '_print_status')
54 def test_check_status(self, mock_print, mock_get):
55 task_id = str(uuid.uuid4())
56 mock_get.return_value = {'status': 2, 'result': 'error'}
58 2, env.EnvCommand()._check_status(task_id, 'hello world'))
59 self.assertEqual(2, mock_print.call_count)
61 @mock.patch.object(sys, 'stdout')
62 @mock.patch.object(os, 'popen')
63 def test_print_status(self, mock_popen, mock_stdout):
64 mock_popen_obj = mock.Mock()
65 mock_popen_obj.read.return_value = ''
66 mock_popen.return_value = mock_popen_obj
67 env.EnvCommand()._print_status('hello', 'word')
68 mock_stdout.write.assert_not_called()
69 mock_stdout.flush.assert_not_called()