aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk.py
blob: 70cd8ad4025bf73945ccf0c501ce1bc1e7fc4d23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
##############################################################################
# Copyright (c) 2015 ZTE and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import mock
import unittest
import time
import logging

import yardstick.common.utils as utils
from yardstick import ssh
from yardstick.benchmark.scenarios.networking import pktgen_dpdk
from yardstick.common import exceptions as y_exc


logging.disable(logging.CRITICAL)


class PktgenDPDKLatencyTestCase(unittest.TestCase):

    def setUp(self):
        self.context_cfg = {
            'host': {
                'ip': '172.16.0.137',
                'user': 'root',
                'key_filename': 'mykey.key'
            },
            'target': {
                'ip': '172.16.0.138',
                'user': 'root',
                'key_filename': 'mykey.key',
                'ipaddr': '172.16.0.138'
            }
        }
        self.scenario_cfg = {
            'options': {'packetsize': 60}
        }

        self._mock_SSH = mock.patch.object(ssh, 'SSH')
        self.mock_SSH = self._mock_SSH.start()

        self._mock_time_sleep = mock.patch.object(time, 'sleep')
        self.mock_time_sleep = self._mock_time_sleep.start()

        self._mock_utils_get_port_ip = mock.patch.object(utils, 'get_port_ip')
        self.mock_utils_get_port_ip = self._mock_utils_get_port_ip.start()

        self._mock_utils_get_port_mac = mock.patch.object(utils,
                                                          'get_port_mac')
        self.mock_utils_get_port_mac = self._mock_utils_get_port_mac.start()

        self.mock_SSH.from_node().execute.return_value = (0, '', '')

        self.addCleanup(self._stop_mock)

        self.scenario = pktgen_dpdk.PktgenDPDKLatency(self.scenario_cfg,
                                                      self.context_cfg)
        self.scenario.server = self.mock_SSH.from_node()
        self.scenario.client = self.mock_SSH.from_node()

    def _stop_mock(self):
        self._mock_SSH.stop()
        self._mock_time_sleep.stop()
        self._mock_utils_get_port_ip.stop()
        self._mock_utils_get_port_mac.stop()

    def test_setup(self):
        scenario = pktgen_dpdk.PktgenDPDKLatency(self.scenario_cfg,
                                                 self.context_cfg)
        scenario.setup()

        self.assertIsNotNone(scenario.server)
        self.assertIsNotNone(scenario.client)
        self.assertTrue(scenario.setup_done)

    def test_run_get_port_ip_command(self):
        self.scenario.run({})

        self.mock_utils_get_port_ip.assert_has_calls(
            [mock.call(self.scenario.server, 'ens4'),
             mock.call(self.scenario.server, 'ens5')])

    def test_get_port_mac_command(self):
        self.scenario.run({})

        self.mock_utils_get_port_mac.assert_has_calls(
            [mock.call(self.scenario.server, 'ens5'),
             mock.call(self.scenario.server, 'ens4'),
             mock.call(self.scenario.server, 'ens5')])

    def test_run_no_sla(self):
        sample_output = '100\n110\n112\n130\n149\n150\n90\n150\n200\n162\n'
        self.mock_SSH.from_node().execute.return_value = (0, sample_output, '')

        result = {}
        self.scenario.run(result)
        # with python 3 we get float, might be due python division changes
        # AssertionError: {'avg_latency': 132.33333333333334} != {
        # 'avg_latency': 132}
        delta = result['avg_latency'] - 132
        self.assertLessEqual(delta, 1)

    def test_run_sla(self):
        self.scenario_cfg['sla'] = {'max_latency': 100}
        scenario = pktgen_dpdk.PktgenDPDKLatency(self.scenario_cfg,
                                                 self.context_cfg)

        sample_output = '100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n'
        self.mock_SSH.from_node().execute.return_value = (0, sample_output, '')

        result = {}
        scenario.run(result)

        self.assertEqual(result, {"avg_latency": 100})

    def test_run_sla_error(self):
        self.scenario_cfg['sla'] = {'max_latency': 100}
        scenario = pktgen_dpdk.PktgenDPDKLatency(self.scenario_cfg,
                                                 self.context_cfg)

        sample_output = '100\n110\n112\n130\n149\n150\n90\n150\n200\n162\n'
        self.mock_SSH.from_node().execute.return_value = (0, sample_output, '')

        with self.assertRaises(y_exc.SLAValidationError):
            scenario.run({})

    def test_run_last_command_raise_on_error(self):
        self.mock_SSH.from_node().execute.side_effect = y_exc.SSHError

        with self.assertRaises(y_exc.SSHError):
            self.scenario.run({})