Merge changes from topic 'bug/yardstick-864'
[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 from __future__ import absolute_import
15 import mock
16 import unittest
17
18 from yardstick.benchmark.scenarios.networking import ping6
19
20
21 class PingTestCase(unittest.TestCase):
22
23     def setUp(self):
24         self.ctx = {
25             'nodes': {
26                 'host1': {
27                     'ip': '172.16.0.137',
28                     'user': 'cirros',
29                     'role': "Controller",
30                     'key_filename': "mykey.key",
31                     'password': "root"
32                 },
33                 'host2': {
34                     "ip": "172.16.0.138",
35                     "key_filename": "/root/.ssh/id_rsa",
36                     "role": "Compute",
37                     "name": "node3.IPV6",
38                     "user": "root"
39                 },
40             }
41         }
42
43     def test_get_controller_node(self):
44         args = {
45             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
46             'sla': {'max_rtt': 50}
47         }
48         p = ping6.Ping6(args, self.ctx)
49         controller_node = p._get_controller_node(['host1', 'host2'])
50         self.assertEqual(controller_node, 'host1')
51
52     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
53     def test_ping_successful_setup(self, mock_ssh):
54         args = {
55             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
56             'sla': {'max_rtt': 50}
57         }
58         p = ping6.Ping6(args, self.ctx)
59         mock_ssh.SSH.from_node().execute.return_value = (0, '0', '')
60         p.setup()
61
62         self.assertEqual(p.setup_done, True)
63
64     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
65     def test_ping_successful_no_sla(self, mock_ssh):
66         args = {
67             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
68
69         }
70         result = {}
71
72         p = ping6.Ping6(args, self.ctx)
73         p.client = mock_ssh.SSH.from_node()
74         mock_ssh.SSH.from_node().execute.side_effect = [(0, 'host1', ''), (0, 100, '')]
75         p.run(result)
76         self.assertEqual(result, {'rtt': 100.0})
77
78     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
79     def test_ping_successful_sla(self, mock_ssh):
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.from_node()
88         mock_ssh.SSH.from_node().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         args = {
95             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
96             'sla': {'max_rtt': 50}
97         }
98         result = {}
99
100         p = ping6.Ping6(args, self.ctx)
101         p.client = mock_ssh.SSH.from_node()
102         mock_ssh.SSH.from_node().execute.side_effect = [(0, 'host1', ''), (0, 100, '')]
103         self.assertRaises(AssertionError, p.run, result)
104
105     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
106     def test_ping_unsuccessful_script_error(self, mock_ssh):
107
108         args = {
109             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
110             'sla': {'max_rtt': 150}
111         }
112         result = {}
113
114         p = ping6.Ping6(args, self.ctx)
115         p.client = mock_ssh.SSH.from_node()
116         mock_ssh.SSH.from_node().execute.side_effect = [
117             (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()