blob: 8b8e90a22a6b7d16e467cb4de464813730dc0737 [file] [log] [blame]
Itzik Brown1ef813a2016-06-06 12:56:21 +00001# Copyright 2016 Red Hat, Inc.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15import errno
16import socket
17import time
18
19from oslo_log import log as logging
20from tempest.lib.common import ssh
21from tempest.lib import exceptions
22from tempest import test
23
24from neutron.common import utils
Sławek Kapłońskiff294062016-12-04 15:00:54 +000025from neutron.services.qos import qos_consts
26from neutron.tests.tempest.api import base as base_api
Itzik Brown1ef813a2016-06-06 12:56:21 +000027from neutron.tests.tempest import config
28from neutron.tests.tempest.scenario import base
29from neutron.tests.tempest.scenario import constants
30from neutron.tests.tempest.scenario import exceptions as sc_exceptions
31
32CONF = config.CONF
33LOG = logging.getLogger(__name__)
34
35
36def _try_connect(host_ip, port):
37 try:
38 client_socket = socket.socket(socket.AF_INET,
39 socket.SOCK_STREAM)
40 client_socket.connect((host_ip, port))
Itzik Brown1ef813a2016-06-06 12:56:21 +000041 return client_socket
42 except socket.error as serr:
43 if serr.errno == errno.ECONNREFUSED:
44 raise sc_exceptions.SocketConnectionRefused(host=host_ip,
45 port=port)
46 else:
47 raise
48
49
50def _connect_socket(host, port):
51 """Try to initiate a connection to a host using an ip address
52 and a port.
53
54 Trying couple of times until a timeout is reached in case the listening
55 host is not ready yet.
56 """
57
58 start = time.time()
59 while True:
60 try:
61 return _try_connect(host, port)
62 except sc_exceptions.SocketConnectionRefused:
63 if time.time() - start > constants.SOCKET_CONNECT_TIMEOUT:
64 raise sc_exceptions.ConnectionTimeoutException(host=host,
65 port=port)
66
67
68class QoSTest(base.BaseTempestTestCase):
69 credentials = ['primary', 'admin']
70 force_tenant_isolation = False
71
72 BUFFER_SIZE = 1024 * 1024
73 TOLERANCE_FACTOR = 1.5
74 BS = 512
75 COUNT = BUFFER_SIZE / BS
76 FILE_SIZE = BS * COUNT
77 LIMIT_BYTES_SEC = (constants.LIMIT_KILO_BITS_PER_SECOND * 1024
78 * TOLERANCE_FACTOR / 8.0)
79 FILE_PATH = "/tmp/img"
80
YAMAMOTO Takashica174642016-07-15 15:01:31 +090081 @classmethod
82 @test.requires_ext(extension="qos", service="network")
YAMAMOTO Takashi3bd3d0f2016-12-12 11:14:58 +090083 @base_api.require_qos_rule_type(qos_consts.RULE_TYPE_BANDWIDTH_LIMIT)
YAMAMOTO Takashica174642016-07-15 15:01:31 +090084 def resource_setup(cls):
85 super(QoSTest, cls).resource_setup()
86
Itzik Brown1ef813a2016-06-06 12:56:21 +000087 def _create_file_for_bw_tests(self, ssh_client):
88 cmd = ("(dd if=/dev/zero bs=%(bs)d count=%(count)d of=%(file_path)s) "
89 % {'bs': QoSTest.BS, 'count': QoSTest.COUNT,
90 'file_path': QoSTest.FILE_PATH})
91 ssh_client.exec_command(cmd)
92 cmd = "stat -c %%s %s" % QoSTest.FILE_PATH
93 filesize = ssh_client.exec_command(cmd)
94 if int(filesize.strip()) != QoSTest.FILE_SIZE:
95 raise sc_exceptions.FileCreationFailedException(
96 file=QoSTest.FILE_PATH)
97
98 def _check_bw(self, ssh_client, host, port):
Itzik Brown1ef813a2016-06-06 12:56:21 +000099 cmd = "killall -q nc"
100 try:
101 ssh_client.exec_command(cmd)
102 except exceptions.SSHExecCommandFailed:
103 pass
104 cmd = ("(nc -ll -p %(port)d < %(file_path)s > /dev/null &)" % {
105 'port': port, 'file_path': QoSTest.FILE_PATH})
106 ssh_client.exec_command(cmd)
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100107
108 start_time = time.time()
Itzik Brown1ef813a2016-06-06 12:56:21 +0000109 client_socket = _connect_socket(host, port)
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100110 total_bytes_read = 0
Itzik Brown1ef813a2016-06-06 12:56:21 +0000111
112 while total_bytes_read < QoSTest.FILE_SIZE:
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100113 data = client_socket.recv(QoSTest.BUFFER_SIZE)
Itzik Brown1ef813a2016-06-06 12:56:21 +0000114 total_bytes_read += len(data)
Itzik Brown1ef813a2016-06-06 12:56:21 +0000115
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100116 time_elapsed = time.time() - start_time
117 bytes_per_second = total_bytes_read / time_elapsed
118
119 LOG.debug("time_elapsed = %(time_elapsed)d, "
120 "total_bytes_read = %(total_bytes_read)d, "
121 "bytes_per_second = %(bytes_per_second)d",
122 {'time_elapsed': time_elapsed,
123 'total_bytes_read': total_bytes_read,
124 'bytes_per_second': bytes_per_second})
125
126 return bytes_per_second <= QoSTest.LIMIT_BYTES_SEC
Itzik Brown1ef813a2016-06-06 12:56:21 +0000127
128 @test.idempotent_id('1f7ed39b-428f-410a-bd2b-db9f465680df')
129 def test_qos(self):
130 """This is a basic test that check that a QoS policy with
131
132 a bandwidth limit rule is applied correctly by sending
133 a file from the instance to the test node.
134 Then calculating the bandwidth every ~1 sec by the number of bits
135 received / elapsed time.
136 """
137
138 NC_PORT = 1234
139
140 self.setup_network_and_server()
141 self.check_connectivity(self.fip['floating_ip_address'],
142 CONF.validation.image_ssh_user,
143 self.keypair['private_key'])
144 rulesets = [{'protocol': 'tcp',
145 'direction': 'ingress',
146 'port_range_min': NC_PORT,
147 'port_range_max': NC_PORT,
148 'remote_ip_prefix': '0.0.0.0/0'}]
Itzik Brownbac51dc2016-10-31 12:25:04 +0000149 self.create_secgroup_rules(rulesets,
150 self.security_groups[-1]['id'])
151
Itzik Brown1ef813a2016-06-06 12:56:21 +0000152 ssh_client = ssh.Client(self.fip['floating_ip_address'],
153 CONF.validation.image_ssh_user,
154 pkey=self.keypair['private_key'])
155 policy = self.admin_manager.network_client.create_qos_policy(
156 name='test-policy',
157 description='test-qos-policy',
158 shared=True)
159 policy_id = policy['policy']['id']
160 self.admin_manager.network_client.create_bandwidth_limit_rule(
161 policy_id, max_kbps=constants.LIMIT_KILO_BITS_PER_SECOND,
162 max_burst_kbps=constants.LIMIT_KILO_BITS_PER_SECOND)
163 port = self.client.list_ports(network_id=self.network['id'],
164 device_id=self.server[
165 'server']['id'])['ports'][0]
166 self.admin_manager.network_client.update_port(port['id'],
167 qos_policy_id=policy_id)
168 self._create_file_for_bw_tests(ssh_client)
169 utils.wait_until_true(lambda: self._check_bw(
170 ssh_client,
171 self.fip['floating_ip_address'],
172 port=NC_PORT),
173 timeout=120,
174 sleep=1)