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