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