Implementation of yardstick-tc007 in Yardstick
[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': {
25                 'ip': '172.16.0.137',
26                 'user': 'cirros',
27                 'key_filename': "mykey.key"
28             },
29             "target": {
30                 "ipaddr": "10.229.17.105",
31             }
32         }
33
34     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
35     def test_ping_successful_no_sla(self, mock_ssh):
36
37         args = {
38             'options': {'packetsize': 200},
39             }
40         result = {}
41
42         p = ping.Ping(args, self.ctx)
43
44         mock_ssh.SSH().execute.return_value = (0, '100', '')
45         p.run(result)
46         self.assertEqual(result, {'rtt': 100.0})
47
48     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
49     def test_ping_successful_sla(self, mock_ssh):
50
51         args = {
52             'options': {'packetsize': 200},
53             'sla': {'max_rtt': 150}
54             }
55         result = {}
56
57         p = ping.Ping(args, self.ctx)
58
59         mock_ssh.SSH().execute.return_value = (0, '100', '')
60         p.run(result)
61         self.assertEqual(result, {'rtt': 100.0})
62
63     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
64     def test_ping_unsuccessful_sla(self, mock_ssh):
65
66         args = {
67             'options': {'packetsize': 200},
68             'sla': {'max_rtt': 50}
69         }
70         result = {}
71
72         p = ping.Ping(args, self.ctx)
73
74         mock_ssh.SSH().execute.return_value = (0, '100', '')
75         self.assertRaises(AssertionError, p.run, result)
76
77     @mock.patch('yardstick.benchmark.scenarios.networking.ping.ssh')
78     def test_ping_unsuccessful_script_error(self, mock_ssh):
79
80         args = {
81             'options': {'packetsize': 200},
82             'sla': {'max_rtt': 50}
83         }
84         result = {}
85
86         p = ping.Ping(args, self.ctx)
87
88         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
89         self.assertRaises(RuntimeError, p.run, result)
90
91
92 def main():
93     unittest.main()
94
95 if __name__ == '__main__':
96     main()