0b8fba2688d20d4de83493187c1a56c9c9d0b535
[yardstick.git] / tests / unit / benchmark / scenarios / networking / test_ping6.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 ping6
18
19
20 class PingTestCase(unittest.TestCase):
21
22     def setUp(self):
23         self.ctx = {
24             'nodes':{
25             'host1': {
26                 'ip': '172.16.0.137',
27                 'user': 'cirros',
28                 'role': "Controller",
29                 'key_filename': "mykey.key",
30                 'password': "root"
31                 },
32             'host2': {
33                 "ip": "172.16.0.138",
34                 "key_filename": "/root/.ssh/id_rsa",
35                 "role": "Compute",
36                 "name": "node3.IPV6",
37                 "user": "root"
38                 },
39             }
40         }
41
42     def test_get_controller_node(self):
43         args = {
44             'options': {'host': 'host1','packetsize': 200, 'ping_count': 5},
45             'sla': {'max_rtt': 50}
46         }
47         p = ping6.Ping6(args, self.ctx)
48         controller_node = p._get_controller_node(['host1','host2'])
49         self.assertEqual(controller_node, 'host1')
50
51     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
52     def test_ping_successful_setup(self, mock_ssh):
53         args = {
54             'options': {'host': 'host1','packetsize': 200, 'ping_count': 5},
55             'sla': {'max_rtt': 50}
56         }
57         p = ping6.Ping6(args, self.ctx)
58         mock_ssh.SSH().execute.return_value = (0, '0', '')
59         p.setup()
60
61         self.assertEqual(p.setup_done, True)
62
63     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
64     def test_ping_successful_no_sla(self, mock_ssh):
65         args = {
66             'options': {'host': 'host1','packetsize': 200, 'ping_count': 5},
67
68         }
69         result = {}
70
71         p = ping6.Ping6(args, self.ctx)
72         p.client = mock_ssh.SSH()
73         mock_ssh.SSH().execute.side_effect = [(0, 'host1', ''),(0, 100, '')]
74         p.run(result)
75         self.assertEqual(result, {'rtt': 100.0})
76
77     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
78     def test_ping_successful_sla(self, mock_ssh):
79
80         args = {
81             'options': {'host': 'host1','packetsize': 200, 'ping_count': 5},
82             'sla': {'max_rtt': 150}
83         }
84         result = {}
85
86         p = ping6.Ping6(args, self.ctx)
87         p.client = mock_ssh.SSH()
88         mock_ssh.SSH().execute.side_effect = [(0, 'host1', ''),(0, 100, '')]
89         p.run(result)
90         self.assertEqual(result, {'rtt': 100.0})
91
92     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
93     def test_ping_unsuccessful_sla(self, mock_ssh):
94
95         args = {
96             'options': {'host': 'host1','packetsize': 200, 'ping_count': 5},
97             'sla': {'max_rtt': 50}
98         }
99         result = {}
100
101         p = ping6.Ping6(args, self.ctx)
102         p.client = mock_ssh.SSH()
103         mock_ssh.SSH().execute.side_effect = [(0, 'host1', ''),(0, 100, '')]
104         self.assertRaises(AssertionError, p.run, result)
105
106     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
107     def test_ping_unsuccessful_script_error(self, mock_ssh):
108
109         args = {
110             'options': {'host': 'host1','packetsize': 200, 'ping_count': 5},
111             'sla': {'max_rtt': 150}
112         }
113         result = {}
114
115         p = ping6.Ping6(args, self.ctx)
116         p.client = mock_ssh.SSH()
117         mock_ssh.SSH().execute.side_effect = [(0, 'host1', ''),(1, '', 'FOOBAR')]
118         self.assertRaises(RuntimeError, p.run, result)
119
120
121 def main():
122     unittest.main()
123
124 if __name__ == '__main__':
125     main()