Merge "Add send socket commands function"
[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 from yardstick.common import exceptions as y_exc
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.from_node().execute.return_value = (0, '0', '')
59         p.setup()
60
61         self.assertTrue(p.setup_done)
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.from_node()
73         mock_ssh.SSH.from_node().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         args = {
80             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
81             'sla': {'max_rtt': 150}
82         }
83         result = {}
84
85         p = ping6.Ping6(args, self.ctx)
86         p.client = mock_ssh.SSH.from_node()
87         mock_ssh.SSH.from_node().execute.side_effect = [(0, 'host1', ''), (0, 100, '')]
88         p.run(result)
89         self.assertEqual(result, {'rtt': 100.0})
90
91     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
92     def test_ping_unsuccessful_sla(self, mock_ssh):
93         args = {
94             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
95             'sla': {'max_rtt': 50}
96         }
97         result = {}
98
99         p = ping6.Ping6(args, self.ctx)
100         p.client = mock_ssh.SSH.from_node()
101         mock_ssh.SSH.from_node().execute.side_effect = [(0, 'host1', ''), (0, 100, '')]
102         self.assertRaises(y_exc.SLAValidationError, p.run, result)
103
104     @mock.patch('yardstick.benchmark.scenarios.networking.ping6.ssh')
105     def test_ping_unsuccessful_script_error(self, mock_ssh):
106
107         args = {
108             'options': {'host': 'host1', 'packetsize': 200, 'ping_count': 5},
109             'sla': {'max_rtt': 150}
110         }
111         result = {}
112
113         p = ping6.Ping6(args, self.ctx)
114         p.client = mock_ssh.SSH.from_node()
115         mock_ssh.SSH.from_node().execute.side_effect = [
116             (0, 'host1', ''), (1, '', 'FOOBAR')]
117         self.assertRaises(RuntimeError, p.run, result)