Merge "Update: assign static IP to VM for standalone"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / networking / test_netperf_node.py
1 ##############################################################################
2 # Copyright (c) 2015 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
11 # yardstick.benchmark.scenarios.networking.netperf_node.NetperfNode
12
13 from __future__ import absolute_import
14
15 import os
16 import unittest
17
18 import mock
19 from oslo_serialization import jsonutils
20
21 from yardstick.benchmark.scenarios.networking import netperf_node
22 from yardstick.common import exceptions as y_exc
23
24
25 @mock.patch('yardstick.benchmark.scenarios.networking.netperf_node.ssh')
26 class NetperfNodeTestCase(unittest.TestCase):
27
28     def setUp(self):
29         self.ctx = {
30             'host': {
31                 'ip': '192.168.10.10',
32                 'user': 'root',
33                 'password': 'root'
34             },
35             'target': {
36                 'ip': '192.168.10.11',
37                 'user': 'root',
38                 'password': 'root'
39             }
40         }
41
42     def test_netperf_node_successful_setup(self, mock_ssh):
43
44         p = netperf_node.NetperfNode({}, self.ctx)
45         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
46
47         p.setup()
48         self.assertIsNotNone(p.server)
49         self.assertIsNotNone(p.client)
50         self.assertTrue(p.setup_done)
51
52     def test_netperf_node_successful_no_sla(self, mock_ssh):
53
54         options = {}
55         args = {'options': options}
56         result = {}
57
58         p = netperf_node.NetperfNode(args, self.ctx)
59         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
60         p.host = mock_ssh.SSH.from_node()
61
62         sample_output = self._read_sample_output()
63         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
64         expected_result = jsonutils.loads(sample_output)
65         p.run(result)
66         self.assertEqual(result, expected_result)
67
68     def test_netperf_node_successful_sla(self, mock_ssh):
69
70         options = {}
71         args = {
72             'options': options,
73             'sla': {'mean_latency': 100}
74         }
75         result = {}
76
77         p = netperf_node.NetperfNode(args, self.ctx)
78         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
79         p.host = mock_ssh.SSH.from_node()
80
81         sample_output = self._read_sample_output()
82         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
83         expected_result = jsonutils.loads(sample_output)
84         p.run(result)
85         self.assertEqual(result, expected_result)
86
87     def test_netperf_node_unsuccessful_sla(self, mock_ssh):
88
89         options = {}
90         args = {
91             'options': options,
92             'sla': {'mean_latency': 5}
93         }
94         result = {}
95
96         p = netperf_node.NetperfNode(args, self.ctx)
97         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
98         p.host = mock_ssh.SSH.from_node()
99
100         sample_output = self._read_sample_output()
101         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
102         self.assertRaises(y_exc.SLAValidationError, p.run, result)
103
104     def test_netperf_node_unsuccessful_script_error(self, mock_ssh):
105
106         options = {}
107         args = {'options': options}
108         result = {}
109
110         p = netperf_node.NetperfNode(args, self.ctx)
111         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
112         p.host = mock_ssh.SSH.from_node()
113
114         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
115         self.assertRaises(RuntimeError, p.run, result)
116
117     def _read_sample_output(self):
118         curr_path = os.path.dirname(os.path.abspath(__file__))
119         output = os.path.join(curr_path, 'netperf_sample_output.json')
120         with open(output) as f:
121             sample_output = f.read()
122         return sample_output