Remove main() and __main__ from tests.
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / compute / test_plugintest.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
10 # Unittest for yardstick.benchmark.scenarios.compute.plugintest.PluginTest
11
12 from __future__ import absolute_import
13
14 import unittest
15
16 import mock
17 from oslo_serialization import jsonutils
18
19 from yardstick.benchmark.scenarios.compute import plugintest
20
21
22 @mock.patch('yardstick.benchmark.scenarios.compute.plugintest.ssh')
23 class PluginTestTestCase(unittest.TestCase):
24
25     def setUp(self):
26         self.ctx = {
27             'nodes': {
28                 'host1': {
29                     'ip': '172.16.0.137',
30                     'user': 'cirros',
31                     'key_filename': "mykey.key",
32                     'password': "root"
33                 },
34             }
35         }
36
37         self.result = {}
38
39     def test_sample_successful_setup(self, mock_ssh):
40         s = plugintest.PluginTest({}, self.ctx)
41         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
42
43         s.setup()
44         self.assertIsNotNone(s.client)
45         self.assertTrue(s.setup_done)
46
47     def test_sample_successful(self, mock_ssh):
48         s = plugintest.PluginTest({}, self.ctx)
49
50         sample_output = '{"Test Output": "Hello world!"}'
51         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
52         s.run(self.result)
53         expected_result = jsonutils.loads(sample_output)
54         self.assertEqual(self.result, expected_result)
55
56     def test_sample_unsuccessful_script_error(self, mock_ssh):
57         s = plugintest.PluginTest({}, self.ctx)
58
59         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
60         self.assertRaises(RuntimeError, s.run, self.result)