blob: 6febb7962e58357507db21186eac6a0676669a62 [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
Chandan Kumarc125fd12017-11-15 19:41:01 +053019from neutron_lib.services.qos import constants as qos_consts
Itzik Brown1ef813a2016-06-06 12:56:21 +000020from oslo_log import log as logging
Chandan Kumarc125fd12017-11-15 19:41:01 +053021from tempest.common import utils as tutils
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +000022from tempest.lib import decorators
Itzik Brown1ef813a2016-06-06 12:56:21 +000023from tempest.lib import exceptions
Itzik Brown1ef813a2016-06-06 12:56:21 +000024
Chandan Kumar667d3d32017-09-22 12:24:06 +053025from neutron_tempest_plugin.api import base as base_api
Chandan Kumar667d3d32017-09-22 12:24:06 +053026from neutron_tempest_plugin.common import ssh
27from neutron_tempest_plugin.common import utils
28from neutron_tempest_plugin import config
29from neutron_tempest_plugin.scenario import base
30from neutron_tempest_plugin.scenario import constants
31from neutron_tempest_plugin.scenario import exceptions as sc_exceptions
Itzik Brown1ef813a2016-06-06 12:56:21 +000032
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):
Brian Haleyaee61ac2018-10-09 20:00:27 -040052 """Try to initiate a connection to a host using an ip address and a port.
Itzik Brown1ef813a2016-06-06 12:56:21 +000053
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
YAMAMOTO Takashia2cc2e52018-07-31 18:54:02 +090068class QoSTestMixin(object):
Itzik Brown1ef813a2016-06-06 12:56:21 +000069 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
Brian Haley33ef4602018-04-26 14:37:49 -040077 LIMIT_BYTES_SEC = (constants.LIMIT_KILO_BITS_PER_SECOND * 1024 *
78 TOLERANCE_FACTOR / 8.0)
Itzik Brown1ef813a2016-06-06 12:56:21 +000079 FILE_PATH = "/tmp/img"
80
LIU Yulong5ba88ef2017-12-22 10:50:15 +080081 NC_PORT = 1234
82
Itzik Brown1ef813a2016-06-06 12:56:21 +000083 def _create_file_for_bw_tests(self, ssh_client):
84 cmd = ("(dd if=/dev/zero bs=%(bs)d count=%(count)d of=%(file_path)s) "
YAMAMOTO Takashia2cc2e52018-07-31 18:54:02 +090085 % {'bs': QoSTestMixin.BS, 'count': QoSTestMixin.COUNT,
86 'file_path': QoSTestMixin.FILE_PATH})
Itzik Brown1ef813a2016-06-06 12:56:21 +000087 ssh_client.exec_command(cmd)
YAMAMOTO Takashia2cc2e52018-07-31 18:54:02 +090088 cmd = "stat -c %%s %s" % QoSTestMixin.FILE_PATH
Itzik Brown1ef813a2016-06-06 12:56:21 +000089 filesize = ssh_client.exec_command(cmd)
YAMAMOTO Takashia2cc2e52018-07-31 18:54:02 +090090 if int(filesize.strip()) != QoSTestMixin.FILE_SIZE:
Itzik Brown1ef813a2016-06-06 12:56:21 +000091 raise sc_exceptions.FileCreationFailedException(
YAMAMOTO Takashia2cc2e52018-07-31 18:54:02 +090092 file=QoSTestMixin.FILE_PATH)
Itzik Brown1ef813a2016-06-06 12:56:21 +000093
94 def _check_bw(self, ssh_client, host, port):
Itzik Brown1ef813a2016-06-06 12:56:21 +000095 cmd = "killall -q nc"
96 try:
97 ssh_client.exec_command(cmd)
98 except exceptions.SSHExecCommandFailed:
99 pass
100 cmd = ("(nc -ll -p %(port)d < %(file_path)s > /dev/null &)" % {
YAMAMOTO Takashia2cc2e52018-07-31 18:54:02 +0900101 'port': port, 'file_path': QoSTestMixin.FILE_PATH})
Itzik Brown1ef813a2016-06-06 12:56:21 +0000102 ssh_client.exec_command(cmd)
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100103
104 start_time = time.time()
Itzik Brown1ef813a2016-06-06 12:56:21 +0000105 client_socket = _connect_socket(host, port)
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100106 total_bytes_read = 0
Itzik Brown1ef813a2016-06-06 12:56:21 +0000107
YAMAMOTO Takashia2cc2e52018-07-31 18:54:02 +0900108 while total_bytes_read < QoSTestMixin.FILE_SIZE:
109 data = client_socket.recv(QoSTestMixin.BUFFER_SIZE)
Itzik Brown1ef813a2016-06-06 12:56:21 +0000110 total_bytes_read += len(data)
Itzik Brown1ef813a2016-06-06 12:56:21 +0000111
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100112 time_elapsed = time.time() - start_time
113 bytes_per_second = total_bytes_read / time_elapsed
114
LIU Yulong5ba88ef2017-12-22 10:50:15 +0800115 LOG.debug("time_elapsed = %(time_elapsed).16f, "
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100116 "total_bytes_read = %(total_bytes_read)d, "
117 "bytes_per_second = %(bytes_per_second)d",
118 {'time_elapsed': time_elapsed,
119 'total_bytes_read': total_bytes_read,
120 'bytes_per_second': bytes_per_second})
121
YAMAMOTO Takashia2cc2e52018-07-31 18:54:02 +0900122 return bytes_per_second <= QoSTestMixin.LIMIT_BYTES_SEC
Itzik Brown1ef813a2016-06-06 12:56:21 +0000123
LIU Yulong5ba88ef2017-12-22 10:50:15 +0800124 def _create_ssh_client(self):
125 return ssh.Client(self.fip['floating_ip_address'],
126 CONF.validation.image_ssh_user,
127 pkey=self.keypair['private_key'])
128
129 def _test_basic_resources(self):
130 self.setup_network_and_server()
131 self.check_connectivity(self.fip['floating_ip_address'],
132 CONF.validation.image_ssh_user,
133 self.keypair['private_key'])
134 rulesets = [{'protocol': 'tcp',
135 'direction': 'ingress',
136 'port_range_min': self.NC_PORT,
137 'port_range_max': self.NC_PORT,
138 'remote_ip_prefix': '0.0.0.0/0'}]
139 self.create_secgroup_rules(rulesets,
140 self.security_groups[-1]['id'])
141
142 def _create_qos_policy(self):
143 policy = self.os_admin.network_client.create_qos_policy(
144 name='test-policy',
145 description='test-qos-policy',
146 shared=True)
147 return policy['policy']['id']
148
YAMAMOTO Takashia2cc2e52018-07-31 18:54:02 +0900149
150class QoSTest(QoSTestMixin, base.BaseTempestTestCase):
151 @classmethod
152 @tutils.requires_ext(extension="qos", service="network")
153 @base_api.require_qos_rule_type(qos_consts.RULE_TYPE_BANDWIDTH_LIMIT)
154 def resource_setup(cls):
155 super(QoSTest, cls).resource_setup()
156
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000157 @decorators.idempotent_id('1f7ed39b-428f-410a-bd2b-db9f465680df')
Itzik Brown1ef813a2016-06-06 12:56:21 +0000158 def test_qos(self):
159 """This is a basic test that check that a QoS policy with
160
161 a bandwidth limit rule is applied correctly by sending
162 a file from the instance to the test node.
163 Then calculating the bandwidth every ~1 sec by the number of bits
164 received / elapsed time.
165 """
LIU Yulong5ba88ef2017-12-22 10:50:15 +0800166 self._test_basic_resources()
167 policy_id = self._create_qos_policy()
168 ssh_client = self._create_ssh_client()
Brian Haleyf86ac2e2017-06-21 10:43:50 -0400169 self.os_admin.network_client.create_bandwidth_limit_rule(
Itzik Brown1ef813a2016-06-06 12:56:21 +0000170 policy_id, max_kbps=constants.LIMIT_KILO_BITS_PER_SECOND,
171 max_burst_kbps=constants.LIMIT_KILO_BITS_PER_SECOND)
172 port = self.client.list_ports(network_id=self.network['id'],
173 device_id=self.server[
174 'server']['id'])['ports'][0]
Brian Haleyf86ac2e2017-06-21 10:43:50 -0400175 self.os_admin.network_client.update_port(port['id'],
176 qos_policy_id=policy_id)
Itzik Brown1ef813a2016-06-06 12:56:21 +0000177 self._create_file_for_bw_tests(ssh_client)
178 utils.wait_until_true(lambda: self._check_bw(
179 ssh_client,
180 self.fip['floating_ip_address'],
LIU Yulong5ba88ef2017-12-22 10:50:15 +0800181 port=self.NC_PORT),
Itzik Brown1ef813a2016-06-06 12:56:21 +0000182 timeout=120,
183 sleep=1)