Merge "Test case override of traffic profile settings."
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / networking / test_netperf.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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 # Unittest for yardstick.benchmark.scenarios.networking.netperf.Netperf
11
12 from __future__ import absolute_import
13
14 import os
15 import unittest
16
17 import mock
18 from oslo_serialization import jsonutils
19
20 from yardstick.benchmark.scenarios.networking import netperf
21 from yardstick.common import exceptions as y_exc
22
23
24 @mock.patch('yardstick.benchmark.scenarios.networking.netperf.ssh')
25 class NetperfTestCase(unittest.TestCase):
26
27     def setUp(self):
28         self.ctx = {
29             'host': {
30                 'ip': '172.16.0.137',
31                 'user': 'cirros',
32                 'key_filename': 'mykey.key'
33             },
34             'target': {
35                 'ip': '172.16.0.138',
36                 'user': 'cirros',
37                 'key_filename': 'mykey.key',
38                 'ipaddr': '172.16.0.138'
39             }
40         }
41
42     def test_netperf_successful_setup(self, mock_ssh):
43
44         p = netperf.Netperf({}, self.ctx)
45         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
46
47         p.setup()
48         self.assertIsNotNone(p.server)
49         self.assertIsNotNone(p.client)
50         self.assertTrue(p.setup_done)
51
52     def test_netperf_successful_no_sla(self, mock_ssh):
53
54         options = {}
55         args = {'options': options}
56         result = {}
57
58         p = netperf.Netperf(args, self.ctx)
59         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
60         p.host = mock_ssh.SSH.from_node()
61
62         sample_output = self._read_sample_output()
63         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
64         expected_result = jsonutils.loads(sample_output)
65         p.run(result)
66         self.assertEqual(result, expected_result)
67
68     def test_netperf_successful_sla(self, mock_ssh):
69
70         options = {}
71         args = {
72             'options': options,
73             'sla': {'mean_latency': 100}
74         }
75         result = {}
76
77         p = netperf.Netperf(args, self.ctx)
78         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
79         p.host = mock_ssh.SSH.from_node()
80
81         sample_output = self._read_sample_output()
82         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
83         expected_result = jsonutils.loads(sample_output)
84         p.run(result)
85         self.assertEqual(result, expected_result)
86
87     def test_netperf_unsuccessful_sla(self, mock_ssh):
88
89         options = {}
90         args = {
91             'options': options,
92             'sla': {'mean_latency': 5}
93         }
94         result = {}
95
96         p = netperf.Netperf(args, self.ctx)
97         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
98         p.host = mock_ssh.SSH.from_node()
99
100         sample_output = self._read_sample_output()
101         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
102         self.assertRaises(y_exc.SLAValidationError, p.run, result)
103
104     def test_netperf_unsuccessful_script_error(self, mock_ssh):
105
106         options = {}
107         args = {'options': options}
108         result = {}
109
110         p = netperf.Netperf(args, self.ctx)
111         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
112         p.host = mock_ssh.SSH.from_node()
113
114         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
115         self.assertRaises(RuntimeError, p.run, result)
116
117     def _read_sample_output(self):
118         curr_path = os.path.dirname(os.path.abspath(__file__))
119         output = os.path.join(curr_path, 'netperf_sample_output.json')
120         with open(output) as f:
121             sample_output = f.read()
122         return sample_output