Merge "Fix report generation"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / networking / test_ping6.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 # Unittest for yardstick.benchmark.scenarios.networking.ping.Ping
11
12 from __future__ import absolute_import
13 import mock
14 import unittest
15
16 from yardstick.benchmark.scenarios.networking import ping6
17
18
19 class PingTestCase(unittest.TestCase):
20
21     def setUp(self):
22         self.ctx = {
23             'nodes': {
24                 'host1': {
25                     'ip': '172.16.0.137',
26                     'user': 'cirros',
27                     'role': "Controller",
28                     'key_filename': "mykey.key",
29                     'password': "root"
30                 },
31                 'host2': {
32                     "ip": "172.16.0.138",
33                     "key_filename": "/root/.ssh/id_rsa",
34                     "role": "Compute",
35                     "name": "node3.IPV6",
36                     "user": "root"
37                 },
38             }
39         }
40
41     def test_get_controller_node(self):
42         args = {
43             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
44             'sla': {'max_rtt': 50}
45         }
46         p = ping6.Ping6(args, self.ctx)
47         controller_node = p._get_controller_node(['host1', 'host2'])
48         self.assertEqual(controller_node, 'host1')
49
50     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
51     def test_ping_successful_setup(self, mock_ssh):
52         args = {
53             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
54             'sla': {'max_rtt': 50}
55         }
56         p = ping6.Ping6(args, self.ctx)
57         mock_ssh.SSH.from_node().execute.return_value = (0, '0', '')
58         p.setup()
59
60         self.assertTrue(p.setup_done)
61
62     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
63     def test_ping_successful_no_sla(self, mock_ssh):
64         args = {
65             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
66
67         }
68         result = {}
69
70         p = ping6.Ping6(args, self.ctx)
71         p.client = mock_ssh.SSH.from_node()
72         mock_ssh.SSH.from_node().execute.side_effect = [(0, 'host1', ''), (0, 100, '')]
73         p.run(result)
74         self.assertEqual(result, {'rtt': 100.0})
75
76     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
77     def test_ping_successful_sla(self, mock_ssh):
78         args = {
79             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
80             'sla': {'max_rtt': 150}
81         }
82         result = {}
83
84         p = ping6.Ping6(args, self.ctx)
85         p.client = mock_ssh.SSH.from_node()
86         mock_ssh.SSH.from_node().execute.side_effect = [(0, 'host1', ''), (0, 100, '')]
87         p.run(result)
88         self.assertEqual(result, {'rtt': 100.0})
89
90     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
91     def test_ping_unsuccessful_sla(self, mock_ssh):
92         args = {
93             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
94             'sla': {'max_rtt': 50}
95         }
96         result = {}
97
98         p = ping6.Ping6(args, self.ctx)
99         p.client = mock_ssh.SSH.from_node()
100         mock_ssh.SSH.from_node().execute.side_effect = [(0, 'host1', ''), (0, 100, '')]
101         self.assertRaises(AssertionError, p.run, result)
102
103     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
104     def test_ping_unsuccessful_script_error(self, mock_ssh):
105
106         args = {
107             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
108             'sla': {'max_rtt': 150}
109         }
110         result = {}
111
112         p = ping6.Ping6(args, self.ctx)
113         p.client = mock_ssh.SSH.from_node()
114         mock_ssh.SSH.from_node().execute.side_effect = [
115             (0, 'host1', ''), (1, '', 'FOOBAR')]
116         self.assertRaises(RuntimeError, p.run, result)