Merge "fix SSH object examples to use correct context manager form"
[yardstick.git] / tests / unit / benchmark / scenarios / networking / test_vsperf.py
1 #!/usr/bin/env python
2
3 # Copyright 2016 Intel Corporation.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #   http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Unittest for yardstick.benchmark.scenarios.networking.vsperf.Vsperf
18
19 import mock
20 import unittest
21 import os
22 import subprocess
23
24 from yardstick.benchmark.scenarios.networking import vsperf
25
26
27 @mock.patch('yardstick.benchmark.scenarios.networking.vsperf.subprocess')
28 @mock.patch('yardstick.benchmark.scenarios.networking.vsperf.ssh')
29 @mock.patch("__builtin__.open", return_value=None)
30 class VsperfTestCase(unittest.TestCase):
31
32     def setUp(self):
33         self.ctx = {
34             "host": {
35                 "ip": "10.229.47.137",
36                 "user": "ubuntu",
37                 "password": "ubuntu",
38             },
39         }
40         self.args = {
41             'options': {
42                 'testname': 'p2p_rfc2544_continuous',
43                 'traffic_type': 'continuous',
44                 'frame_size': '64',
45                 'bidirectional': 'True',
46                 'iload': 100,
47                 'trafficgen_port1': 'eth1',
48                 'trafficgen_port2': 'eth3',
49                 'external_bridge': 'br-ex',
50                 'conf_file': 'vsperf-yardstick.conf',
51                 'setup_script': 'setup_yardstick.sh',
52                 'test_params': 'TRAFFICGEN_DURATION=30;',
53             },
54             'sla': {
55                 'metrics': 'throughput_rx_fps',
56                 'throughput_rx_fps': 500000,
57                 'action': 'monitor',
58             }
59         }
60
61     def test_vsperf_setup(self, mock_open, mock_ssh, mock_subprocess):
62         p = vsperf.Vsperf(self.args, self.ctx)
63         mock_ssh.SSH().execute.return_value = (0, '', '')
64         mock_subprocess.call().execute.return_value = None
65
66         p.setup()
67         self.assertIsNotNone(p.client)
68         self.assertEqual(p.setup_done, True)
69
70     def test_vsperf_teardown(self, mock_open, mock_ssh, mock_subprocess):
71         p = vsperf.Vsperf(self.args, self.ctx)
72
73         # setup() specific mocks
74         mock_ssh.SSH().execute.return_value = (0, '', '')
75         mock_subprocess.call().execute.return_value = None
76
77         p.setup()
78         self.assertIsNotNone(p.client)
79         self.assertEqual(p.setup_done, True)
80
81         p.teardown()
82         self.assertEqual(p.setup_done, False)
83
84     def test_vsperf_run_ok(self, mock_open, mock_ssh, mock_subprocess):
85         p = vsperf.Vsperf(self.args, self.ctx)
86
87         # setup() specific mocks
88         mock_ssh.SSH().execute.return_value = (0, '', '')
89         mock_subprocess.call().execute.return_value = None
90
91         # run() specific mocks
92         mock_ssh.SSH().execute.return_value = (0, '', '')
93         mock_ssh.SSH().execute.return_value = (0, 'throughput_rx_fps\r\n14797660.000\r\n', '')
94
95         result = {}
96         p.run(result)
97
98         self.assertEqual(result['throughput_rx_fps'], '14797660.000')
99
100     def test_vsperf_run_falied_vsperf_execution(self, mock_open, mock_ssh, mock_subprocess):
101         p = vsperf.Vsperf(self.args, self.ctx)
102
103         # setup() specific mocks
104         mock_ssh.SSH().execute.return_value = (0, '', '')
105         mock_subprocess.call().execute.return_value = None
106
107         # run() specific mocks
108         mock_ssh.SSH().execute.return_value = (1, '', '')
109
110         result = {}
111         self.assertRaises(RuntimeError, p.run, result)
112
113     def test_vsperf_run_falied_csv_report(self, mock_open, mock_ssh, mock_subprocess):
114         p = vsperf.Vsperf(self.args, self.ctx)
115
116         # setup() specific mocks
117         mock_ssh.SSH().execute.return_value = (0, '', '')
118         mock_subprocess.call().execute.return_value = None
119
120         # run() specific mocks
121         mock_ssh.SSH().execute.return_value = (0, '', '')
122         mock_ssh.SSH().execute.return_value = (1, '', '')
123
124         result = {}
125         self.assertRaises(RuntimeError, p.run, result)
126
127
128 def main():
129     unittest.main()
130
131 if __name__ == '__main__':
132     main()