blob: 70b962f5366dbf44f2de17925ebb0ba8c3b97afe [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
Jakub Libosvar4fb7ba52017-02-22 10:51:35 -050023import testtools
Itzik Brown1ef813a2016-06-06 12:56:21 +000024
25from neutron.common import utils
Sławek Kapłońskiff294062016-12-04 15:00:54 +000026from neutron.services.qos import qos_consts
27from neutron.tests.tempest.api import base as base_api
Itzik Brown1ef813a2016-06-06 12:56:21 +000028from neutron.tests.tempest import config
29from neutron.tests.tempest.scenario import base
30from neutron.tests.tempest.scenario import constants
31from neutron.tests.tempest.scenario import exceptions as sc_exceptions
32
33CONF = config.CONF
34LOG = logging.getLogger(__name__)
35
36
37def _try_connect(host_ip, port):
38 try:
39 client_socket = socket.socket(socket.AF_INET,
40 socket.SOCK_STREAM)
41 client_socket.connect((host_ip, port))
Itzik Brown1ef813a2016-06-06 12:56:21 +000042 return client_socket
43 except socket.error as serr:
44 if serr.errno == errno.ECONNREFUSED:
45 raise sc_exceptions.SocketConnectionRefused(host=host_ip,
46 port=port)
47 else:
48 raise
49
50
51def _connect_socket(host, port):
52 """Try to initiate a connection to a host using an ip address
53 and a port.
54
55 Trying couple of times until a timeout is reached in case the listening
56 host is not ready yet.
57 """
58
59 start = time.time()
60 while True:
61 try:
62 return _try_connect(host, port)
63 except sc_exceptions.SocketConnectionRefused:
64 if time.time() - start > constants.SOCKET_CONNECT_TIMEOUT:
65 raise sc_exceptions.ConnectionTimeoutException(host=host,
66 port=port)
67
68
69class QoSTest(base.BaseTempestTestCase):
70 credentials = ['primary', 'admin']
71 force_tenant_isolation = False
72
73 BUFFER_SIZE = 1024 * 1024
74 TOLERANCE_FACTOR = 1.5
75 BS = 512
76 COUNT = BUFFER_SIZE / BS
77 FILE_SIZE = BS * COUNT
78 LIMIT_BYTES_SEC = (constants.LIMIT_KILO_BITS_PER_SECOND * 1024
79 * TOLERANCE_FACTOR / 8.0)
80 FILE_PATH = "/tmp/img"
81
YAMAMOTO Takashica174642016-07-15 15:01:31 +090082 @classmethod
83 @test.requires_ext(extension="qos", service="network")
YAMAMOTO Takashi3bd3d0f2016-12-12 11:14:58 +090084 @base_api.require_qos_rule_type(qos_consts.RULE_TYPE_BANDWIDTH_LIMIT)
Jakub Libosvar4fb7ba52017-02-22 10:51:35 -050085 @testtools.skip('bug/1662109')
YAMAMOTO Takashica174642016-07-15 15:01:31 +090086 def resource_setup(cls):
87 super(QoSTest, cls).resource_setup()
88
Itzik Brown1ef813a2016-06-06 12:56:21 +000089 def _create_file_for_bw_tests(self, ssh_client):
90 cmd = ("(dd if=/dev/zero bs=%(bs)d count=%(count)d of=%(file_path)s) "
91 % {'bs': QoSTest.BS, 'count': QoSTest.COUNT,
92 'file_path': QoSTest.FILE_PATH})
93 ssh_client.exec_command(cmd)
94 cmd = "stat -c %%s %s" % QoSTest.FILE_PATH
95 filesize = ssh_client.exec_command(cmd)
96 if int(filesize.strip()) != QoSTest.FILE_SIZE:
97 raise sc_exceptions.FileCreationFailedException(
98 file=QoSTest.FILE_PATH)
99
100 def _check_bw(self, ssh_client, host, port):
Itzik Brown1ef813a2016-06-06 12:56:21 +0000101 cmd = "killall -q nc"
102 try:
103 ssh_client.exec_command(cmd)
104 except exceptions.SSHExecCommandFailed:
105 pass
106 cmd = ("(nc -ll -p %(port)d < %(file_path)s > /dev/null &)" % {
107 'port': port, 'file_path': QoSTest.FILE_PATH})
108 ssh_client.exec_command(cmd)
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100109
110 start_time = time.time()
Itzik Brown1ef813a2016-06-06 12:56:21 +0000111 client_socket = _connect_socket(host, port)
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100112 total_bytes_read = 0
Itzik Brown1ef813a2016-06-06 12:56:21 +0000113
114 while total_bytes_read < QoSTest.FILE_SIZE:
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100115 data = client_socket.recv(QoSTest.BUFFER_SIZE)
Itzik Brown1ef813a2016-06-06 12:56:21 +0000116 total_bytes_read += len(data)
Itzik Brown1ef813a2016-06-06 12:56:21 +0000117
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100118 time_elapsed = time.time() - start_time
119 bytes_per_second = total_bytes_read / time_elapsed
120
121 LOG.debug("time_elapsed = %(time_elapsed)d, "
122 "total_bytes_read = %(total_bytes_read)d, "
123 "bytes_per_second = %(bytes_per_second)d",
124 {'time_elapsed': time_elapsed,
125 'total_bytes_read': total_bytes_read,
126 'bytes_per_second': bytes_per_second})
127
128 return bytes_per_second <= QoSTest.LIMIT_BYTES_SEC
Itzik Brown1ef813a2016-06-06 12:56:21 +0000129
130 @test.idempotent_id('1f7ed39b-428f-410a-bd2b-db9f465680df')
131 def test_qos(self):
132 """This is a basic test that check that a QoS policy with
133
134 a bandwidth limit rule is applied correctly by sending
135 a file from the instance to the test node.
136 Then calculating the bandwidth every ~1 sec by the number of bits
137 received / elapsed time.
138 """
139
140 NC_PORT = 1234
141
142 self.setup_network_and_server()
143 self.check_connectivity(self.fip['floating_ip_address'],
144 CONF.validation.image_ssh_user,
145 self.keypair['private_key'])
146 rulesets = [{'protocol': 'tcp',
147 'direction': 'ingress',
148 'port_range_min': NC_PORT,
149 'port_range_max': NC_PORT,
150 'remote_ip_prefix': '0.0.0.0/0'}]
Itzik Brownbac51dc2016-10-31 12:25:04 +0000151 self.create_secgroup_rules(rulesets,
152 self.security_groups[-1]['id'])
153
Itzik Brown1ef813a2016-06-06 12:56:21 +0000154 ssh_client = ssh.Client(self.fip['floating_ip_address'],
155 CONF.validation.image_ssh_user,
156 pkey=self.keypair['private_key'])
157 policy = self.admin_manager.network_client.create_qos_policy(
158 name='test-policy',
159 description='test-qos-policy',
160 shared=True)
161 policy_id = policy['policy']['id']
162 self.admin_manager.network_client.create_bandwidth_limit_rule(
163 policy_id, max_kbps=constants.LIMIT_KILO_BITS_PER_SECOND,
164 max_burst_kbps=constants.LIMIT_KILO_BITS_PER_SECOND)
165 port = self.client.list_ports(network_id=self.network['id'],
166 device_id=self.server[
167 'server']['id'])['ports'][0]
168 self.admin_manager.network_client.update_port(port['id'],
169 qos_policy_id=policy_id)
170 self._create_file_for_bw_tests(ssh_client)
171 utils.wait_until_true(lambda: self._check_bw(
172 ssh_client,
173 self.fip['floating_ip_address'],
174 port=NC_PORT),
175 timeout=120,
176 sleep=1)