add unit test for ping
[yardstick.git] / tests / unit / benchmark / scenarios / networking / test_ping.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB 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.ping.Ping
13
14 import mock
15 import unittest
16
17 from yardstick.benchmark.scenarios.networking import ping
18
19
20 class PingTestCase(unittest.TestCase):
21
22     def setUp(self):
23         self.ctx = {
24             'host': '172.16.0.137',
25             'user': 'cirros',
26             'key_filename': "mykey.key"
27             }
28
29     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
30     def test_ping_successful_no_sla(self, mock_ssh):
31
32         p = ping.Ping(self.ctx)
33
34         args = {
35             'options': {'packetsize': 200},
36             'ipaddr': '172.16.0.138'
37             }
38
39         mock_ssh.SSH().execute.return_value = (0, '100', '')
40         result = p.run(args)
41         self.assertEqual(result, float(mock_ssh.SSH().execute.return_value[1]))
42
43     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
44     def test_ping_successful_sla(self, mock_ssh):
45
46         p = ping.Ping(self.ctx)
47
48         args = {
49             'options': {'packetsize': 200},
50             'ipaddr': '172.16.0.138',
51             'sla': {'max_rtt': 150}
52             }
53
54         mock_ssh.SSH().execute.return_value = (0, '100', '')
55         result = p.run(args)
56         self.assertEqual(result, float(mock_ssh.SSH().execute.return_value[1]))
57
58     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
59     def test_ping_unsuccessful_sla(self, mock_ssh):
60
61         p = ping.Ping(self.ctx)
62
63         args = {
64             'options': {'packetsize': 200},
65             'ipaddr': '172.16.0.138',
66             'sla': {'max_rtt': 50}
67             }
68
69         mock_ssh.SSH().execute.return_value = (0, '100', '')
70         self.assertRaises(AssertionError, p.run, args)
71
72     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
73     def test_ping_unsuccessful_script_error(self, mock_ssh):
74
75         p = ping.Ping(self.ctx)
76
77         args = {
78             'options': {'packetsize': 200},
79             'ipaddr': '172.16.0.138',
80             'sla': {'max_rtt': 50}
81             }
82
83         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
84         self.assertRaises(RuntimeError, p.run, args)
85
86
87 def main():
88     unittest.main()
89
90 if __name__ == '__main__':
91     main()