94f52738cf652109ec35df4f0fa971304080a950
[yardstick.git] / tests / unit / benchmark / scenarios / compute / test_plugintest.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Unittest for yardstick.benchmark.scenarios.compute.plugintest.PluginTest
13
14 import mock
15 import json
16 import unittest
17 import os
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().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().execute.return_value = (0, sample_output, '')
52         s.run(self.result)
53         expected_result = json.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().execute.return_value = (1, '', 'FOOBAR')
60         self.assertRaises(RuntimeError, s.run, self.result)