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