b2c5b985906f5a71f0c5725c0657caff8c620f38
[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         result = {}
39
40         mock_ssh.SSH().execute.return_value = (0, '100', '')
41         p.run(args, result)
42         self.assertEqual(result, {'rtt': 100.0})
43
44     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
45     def test_ping_successful_sla(self, mock_ssh):
46
47         p = ping.Ping(self.ctx)
48
49         args = {
50             'options': {'packetsize': 200},
51             'ipaddr': '172.16.0.138',
52             'sla': {'max_rtt': 150}
53             }
54         result = {}
55
56         mock_ssh.SSH().execute.return_value = (0, '100', '')
57         p.run(args, result)
58         self.assertEqual(result, {'rtt': 100.0})
59
60     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
61     def test_ping_unsuccessful_sla(self, mock_ssh):
62
63         p = ping.Ping(self.ctx)
64
65         args = {
66             'options': {'packetsize': 200},
67             'ipaddr': '172.16.0.138',
68             'sla': {'max_rtt': 50}
69             }
70         result = {}
71
72         mock_ssh.SSH().execute.return_value = (0, '100', '')
73         self.assertRaises(AssertionError, p.run, args, result)
74
75     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
76     def test_ping_unsuccessful_script_error(self, mock_ssh):
77
78         p = ping.Ping(self.ctx)
79
80         args = {
81             'options': {'packetsize': 200},
82             'ipaddr': '172.16.0.138',
83             'sla': {'max_rtt': 50}
84             }
85         result = {}
86
87         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
88         self.assertRaises(RuntimeError, p.run, args, result)
89
90
91 def main():
92     unittest.main()
93
94 if __name__ == '__main__':
95     main()