Merge changes from topic 'bug/yardstick-864'
[yardstick.git] / tests / unit / benchmark / scenarios / networking / test_nstat.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Unittest for yardstick.benchmark.scenarios.networking.nstat.Nstat
13
14 from __future__ import absolute_import
15
16 import unittest
17
18 import mock
19
20 from yardstick.benchmark.scenarios.networking import nstat
21
22
23 @mock.patch('yardstick.benchmark.scenarios.networking.nstat.ssh')
24 class NstatTestCase(unittest.TestCase):
25
26     def setUp(self):
27         self.ctx = {
28             "host": {
29                 "ip": "192.168.50.28",
30                 "user": "root",
31                 "key_filename": "mykey.key"
32             }
33         }
34
35     def test_nstat_successful_setup(self, mock_ssh):
36
37         n = nstat.Nstat({}, self.ctx)
38         n.setup()
39
40         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
41         self.assertIsNotNone(n.client)
42         self.assertEqual(n.setup_done, True)
43
44     def test_nstat_successful_no_sla(self, mock_ssh):
45
46         options = {
47             "duration": 0
48         }
49         args = {
50             "options": options,
51         }
52         n = nstat.Nstat(args, self.ctx)
53         result = {}
54
55         sample_output = '#kernel\nIpInReceives                    1837               0.0\nIpInHdrErrors                   0                  0.0\nIpInAddrErrors                  2                  0.0\nIcmpInMsgs                      319                  0.0\nIcmpInErrors                    0                0.0\nTcpInSegs                       36               0.0\nTcpInErrs                       0                  0.0\nUdpInDatagrams                  1318                  0.0\nUdpInErrors                     0                  0.0\n'  # pylint: disable=line-too-long
56
57         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
58
59         n.run(result)
60         expected_result = {"TcpInErrs": 0, "UdpInDatagrams": 1318,
61                            "Tcp_segment_error_rate": 0.0, "IpInAddrErrors": 2,
62                            "IpInHdrErrors": 0, "IcmpInErrors": 0, "IpErrors": 2,
63                            "TcpInSegs": 36, "IpInReceives": 1837, "IcmpInMsgs": 319,
64                            "IP_datagram_error_rate": 0.001, "Udp_datagram_error_rate": 0.0,
65                            "Icmp_message_error_rate": 0.0, "UdpInErrors": 0}
66         self.assertEqual(result, expected_result)
67
68     def test_nstat_successful_sla(self, mock_ssh):
69
70         options = {
71             "duration": 0
72         }
73         sla = {
74             "IP_datagram_error_rate": 0.1
75         }
76         args = {
77             "options": options,
78             "sla": sla
79         }
80         n = nstat.Nstat(args, self.ctx)
81         result = {}
82
83         sample_output = '#kernel\nIpInReceives                    1837               0.0\nIpInHdrErrors                   0                  0.0\nIpInAddrErrors                  2                  0.0\nIcmpInMsgs                      319                  0.0\nIcmpInErrors                    0                0.0\nTcpInSegs                       36               0.0\nTcpInErrs                       0                  0.0\nUdpInDatagrams                  1318                  0.0\nUdpInErrors                     0                  0.0\n'  # pylint: disable=line-too-long
84
85         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
86
87         n.run(result)
88         expected_result = {"TcpInErrs": 0, "UdpInDatagrams": 1318,
89                            "Tcp_segment_error_rate": 0.0, "IpInAddrErrors": 2,
90                            "IpInHdrErrors": 0, "IcmpInErrors": 0, "IpErrors": 2,
91                            "TcpInSegs": 36, "IpInReceives": 1837, "IcmpInMsgs": 319,
92                            "IP_datagram_error_rate": 0.001, "Udp_datagram_error_rate": 0.0,
93                            "Icmp_message_error_rate": 0.0, "UdpInErrors": 0}
94         self.assertEqual(result, expected_result)
95
96     def test_nstat_unsuccessful_cmd_error(self, mock_ssh):
97
98         options = {
99             "duration": 0
100         }
101         sla = {
102             "IP_datagram_error_rate": 0.1
103         }
104         args = {
105             "options": options,
106             "sla": sla
107         }
108         n = nstat.Nstat(args, self.ctx)
109         result = {}
110
111         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
112         self.assertRaises(RuntimeError, n.run, result)
113
114
115 def main():
116     unittest.main()
117
118
119 if __name__ == '__main__':
120     main()