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