Merge "Add Flags class to base.Context"
[yardstick.git] / yardstick / 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 from __future__ import absolute_import
20 try:
21     from unittest import mock
22 except ImportError:
23     import mock
24 import unittest
25
26 from yardstick.benchmark.scenarios.networking import vsperf
27
28
29 @mock.patch('yardstick.benchmark.scenarios.networking.vsperf.subprocess')
30 @mock.patch('yardstick.benchmark.scenarios.networking.vsperf.ssh')
31 class VsperfTestCase(unittest.TestCase):
32
33     def setUp(self):
34         self.ctx = {
35             "host": {
36                 "ip": "10.229.47.137",
37                 "user": "ubuntu",
38                 "password": "ubuntu",
39             },
40         }
41         self.args = {
42             'options': {
43                 'testname': 'p2p_rfc2544_continuous',
44                 'traffic_type': 'continuous',
45                 'frame_size': '64',
46                 'bidirectional': 'True',
47                 'iload': 100,
48                 'trafficgen_port1': 'eth1',
49                 'trafficgen_port2': 'eth3',
50                 'external_bridge': 'br-ex',
51                 'conf_file': 'vsperf-yardstick.conf',
52                 'setup_script': 'setup_yardstick.sh',
53                 'test_params': 'TRAFFICGEN_DURATION=30;',
54             },
55             'sla': {
56                 'metrics': 'throughput_rx_fps',
57                 'throughput_rx_fps': 500000,
58                 'action': 'monitor',
59             }
60         }
61
62     def test_vsperf_setup(self, mock_ssh, mock_subprocess):
63         p = vsperf.Vsperf(self.args, self.ctx)
64         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
65         mock_subprocess.call().execute.return_value = None
66
67         p.setup()
68         self.assertIsNotNone(p.client)
69         self.assertTrue(p.setup_done)
70
71     def test_vsperf_teardown(self, mock_ssh, mock_subprocess):
72         p = vsperf.Vsperf(self.args, self.ctx)
73
74         # setup() specific mocks
75         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
76         mock_subprocess.call().execute.return_value = None
77
78         p.setup()
79         self.assertIsNotNone(p.client)
80         self.assertTrue(p.setup_done)
81
82         p.teardown()
83         self.assertFalse(p.setup_done)
84
85     def test_vsperf_run_ok(self, mock_ssh, mock_subprocess):
86         p = vsperf.Vsperf(self.args, self.ctx)
87
88         # setup() specific mocks
89         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
90         mock_subprocess.call().execute.return_value = None
91
92         # run() specific mocks
93         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
94         mock_ssh.SSH.from_node().execute.return_value = (
95             0, 'throughput_rx_fps\r\n14797660.000\r\n', '')
96
97         result = {}
98         p.run(result)
99
100         self.assertEqual(result['throughput_rx_fps'], '14797660.000')
101
102     def test_vsperf_run_falied_vsperf_execution(self, mock_ssh,
103                                                 mock_subprocess):
104         p = vsperf.Vsperf(self.args, self.ctx)
105
106         # setup() specific mocks
107         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
108         mock_subprocess.call().execute.return_value = None
109
110         # run() specific mocks
111         mock_ssh.SSH.from_node().execute.return_value = (1, '', '')
112
113         result = {}
114         self.assertRaises(RuntimeError, p.run, result)
115
116     def test_vsperf_run_falied_csv_report(self, mock_ssh, mock_subprocess):
117         p = vsperf.Vsperf(self.args, self.ctx)
118
119         # setup() specific mocks
120         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
121         mock_subprocess.call().execute.return_value = None
122
123         # run() specific mocks
124         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
125         mock_ssh.SSH.from_node().execute.return_value = (1, '', '')
126
127         result = {}
128         self.assertRaises(RuntimeError, p.run, result)
129
130
131 def main():
132     unittest.main()
133
134
135 if __name__ == '__main__':
136     main()