Merge "Add IxNextgen API for settings IP priority"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / networking / test_pktgen_dpdk.py
1 ##############################################################################
2 # Copyright (c) 2015 ZTE 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 import mock
11 import unittest
12 import time
13 import logging
14
15 import yardstick.common.utils as utils
16 from yardstick import ssh
17 from yardstick.benchmark.scenarios.networking import pktgen_dpdk
18 from yardstick.common import exceptions as y_exc
19
20
21 logging.disable(logging.CRITICAL)
22
23
24 class PktgenDPDKLatencyTestCase(unittest.TestCase):
25
26     def setUp(self):
27         self.context_cfg = {
28             'host': {
29                 'ip': '172.16.0.137',
30                 'user': 'root',
31                 'key_filename': 'mykey.key'
32             },
33             'target': {
34                 'ip': '172.16.0.138',
35                 'user': 'root',
36                 'key_filename': 'mykey.key',
37                 'ipaddr': '172.16.0.138'
38             }
39         }
40         self.scenario_cfg = {
41             'options': {'packetsize': 60}
42         }
43
44         self._mock_SSH = mock.patch.object(ssh, 'SSH')
45         self.mock_SSH = self._mock_SSH.start()
46
47         self._mock_time_sleep = mock.patch.object(time, 'sleep')
48         self.mock_time_sleep = self._mock_time_sleep.start()
49
50         self._mock_utils_get_port_ip = mock.patch.object(utils, 'get_port_ip')
51         self.mock_utils_get_port_ip = self._mock_utils_get_port_ip.start()
52
53         self._mock_utils_get_port_mac = mock.patch.object(utils,
54                                                           'get_port_mac')
55         self.mock_utils_get_port_mac = self._mock_utils_get_port_mac.start()
56
57         self.mock_SSH.from_node().execute.return_value = (0, '', '')
58
59         self.addCleanup(self._stop_mock)
60
61         self.scenario = pktgen_dpdk.PktgenDPDKLatency(self.scenario_cfg,
62                                                       self.context_cfg)
63         self.scenario.server = self.mock_SSH.from_node()
64         self.scenario.client = self.mock_SSH.from_node()
65
66     def _stop_mock(self):
67         self._mock_SSH.stop()
68         self._mock_time_sleep.stop()
69         self._mock_utils_get_port_ip.stop()
70         self._mock_utils_get_port_mac.stop()
71
72     def test_setup(self):
73         scenario = pktgen_dpdk.PktgenDPDKLatency(self.scenario_cfg,
74                                                  self.context_cfg)
75         scenario.setup()
76
77         self.assertIsNotNone(scenario.server)
78         self.assertIsNotNone(scenario.client)
79         self.assertTrue(scenario.setup_done)
80
81     def test_run_get_port_ip_command(self):
82         self.scenario.run({})
83
84         self.mock_utils_get_port_ip.assert_has_calls(
85             [mock.call(self.scenario.server, 'ens4'),
86              mock.call(self.scenario.server, 'ens5')])
87
88     def test_get_port_mac_command(self):
89         self.scenario.run({})
90
91         self.mock_utils_get_port_mac.assert_has_calls(
92             [mock.call(self.scenario.server, 'ens5'),
93              mock.call(self.scenario.server, 'ens4'),
94              mock.call(self.scenario.server, 'ens5')])
95
96     def test_run_no_sla(self):
97         sample_output = '100\n110\n112\n130\n149\n150\n90\n150\n200\n162\n'
98         self.mock_SSH.from_node().execute.return_value = (0, sample_output, '')
99
100         result = {}
101         self.scenario.run(result)
102         # with python 3 we get float, might be due python division changes
103         # AssertionError: {'avg_latency': 132.33333333333334} != {
104         # 'avg_latency': 132}
105         delta = result['avg_latency'] - 132
106         self.assertLessEqual(delta, 1)
107
108     def test_run_sla(self):
109         self.scenario_cfg['sla'] = {'max_latency': 100}
110         scenario = pktgen_dpdk.PktgenDPDKLatency(self.scenario_cfg,
111                                                  self.context_cfg)
112
113         sample_output = '100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n'
114         self.mock_SSH.from_node().execute.return_value = (0, sample_output, '')
115
116         result = {}
117         scenario.run(result)
118
119         self.assertEqual(result, {"avg_latency": 100})
120
121     def test_run_sla_error(self):
122         self.scenario_cfg['sla'] = {'max_latency': 100}
123         scenario = pktgen_dpdk.PktgenDPDKLatency(self.scenario_cfg,
124                                                  self.context_cfg)
125
126         sample_output = '100\n110\n112\n130\n149\n150\n90\n150\n200\n162\n'
127         self.mock_SSH.from_node().execute.return_value = (0, sample_output, '')
128
129         with self.assertRaises(y_exc.SLAValidationError):
130             scenario.run({})
131
132     def test_run_last_command_raise_on_error(self):
133         self.mock_SSH.from_node().execute.side_effect = y_exc.SSHError
134
135         with self.assertRaises(y_exc.SSHError):
136             self.scenario.run({})