Remove main() and __main__ from tests.
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / networking / test_nstat.py
1 ##############################################################################
2 # Copyright (c) 2017 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 import mock
11 import unittest
12
13 from yardstick.benchmark.scenarios.networking import nstat
14
15
16 @mock.patch('yardstick.benchmark.scenarios.networking.nstat.ssh')
17 class NstatTestCase(unittest.TestCase):
18
19     def setUp(self):
20         self.ctx = {
21             "host": {
22                 "ip": "192.168.50.28",
23                 "user": "root",
24                 "key_filename": "mykey.key"
25             }
26         }
27
28     def test_nstat_successful_setup(self, mock_ssh):
29
30         n = nstat.Nstat({}, self.ctx)
31         n.setup()
32
33         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
34         self.assertIsNotNone(n.client)
35         self.assertTrue(n.setup_done)
36
37     def test_nstat_successful_no_sla(self, mock_ssh):
38
39         options = {
40             "duration": 0
41         }
42         args = {
43             "options": options,
44         }
45         n = nstat.Nstat(args, self.ctx)
46         result = {}
47
48         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
49
50         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
51
52         n.run(result)
53         expected_result = {"TcpInErrs": 0, "UdpInDatagrams": 1318,
54                            "Tcp_segment_error_rate": 0.0, "IpInAddrErrors": 2,
55                            "IpInHdrErrors": 0, "IcmpInErrors": 0, "IpErrors": 2,
56                            "TcpInSegs": 36, "IpInReceives": 1837, "IcmpInMsgs": 319,
57                            "IP_datagram_error_rate": 0.001, "Udp_datagram_error_rate": 0.0,
58                            "Icmp_message_error_rate": 0.0, "UdpInErrors": 0}
59         self.assertEqual(result, expected_result)
60
61     def test_nstat_successful_sla(self, mock_ssh):
62
63         options = {
64             "duration": 0
65         }
66         sla = {
67             "IP_datagram_error_rate": 0.1
68         }
69         args = {
70             "options": options,
71             "sla": sla
72         }
73         n = nstat.Nstat(args, self.ctx)
74         result = {}
75
76         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
77
78         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
79
80         n.run(result)
81         expected_result = {"TcpInErrs": 0, "UdpInDatagrams": 1318,
82                            "Tcp_segment_error_rate": 0.0, "IpInAddrErrors": 2,
83                            "IpInHdrErrors": 0, "IcmpInErrors": 0, "IpErrors": 2,
84                            "TcpInSegs": 36, "IpInReceives": 1837, "IcmpInMsgs": 319,
85                            "IP_datagram_error_rate": 0.001, "Udp_datagram_error_rate": 0.0,
86                            "Icmp_message_error_rate": 0.0, "UdpInErrors": 0}
87         self.assertEqual(result, expected_result)
88
89     def test_nstat_unsuccessful_cmd_error(self, mock_ssh):
90
91         options = {
92             "duration": 0
93         }
94         sla = {
95             "IP_datagram_error_rate": 0.1
96         }
97         args = {
98             "options": options,
99             "sla": sla
100         }
101         n = nstat.Nstat(args, self.ctx)
102         result = {}
103
104         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
105         self.assertRaises(RuntimeError, n.run, result)