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