Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 1 | # 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. |
| 15 | import errno |
| 16 | import socket |
| 17 | import time |
| 18 | |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 19 | from neutron_lib.services.qos import constants as qos_consts |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 20 | from oslo_log import log as logging |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 21 | from tempest.common import utils as tutils |
Sławek Kapłoński | c0caa2e | 2017-02-25 10:11:32 +0000 | [diff] [blame] | 22 | from tempest.lib import decorators |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 23 | |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 24 | from neutron_tempest_plugin.api import base as base_api |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 25 | from neutron_tempest_plugin.common import ssh |
| 26 | from neutron_tempest_plugin.common import utils |
| 27 | from neutron_tempest_plugin import config |
| 28 | from neutron_tempest_plugin.scenario import base |
| 29 | from neutron_tempest_plugin.scenario import constants |
| 30 | from neutron_tempest_plugin.scenario import exceptions as sc_exceptions |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 31 | |
| 32 | CONF = config.CONF |
| 33 | LOG = logging.getLogger(__name__) |
| 34 | |
| 35 | |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 36 | def _try_connect(host_ip, port, socket_timeout): |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 37 | try: |
| 38 | client_socket = socket.socket(socket.AF_INET, |
| 39 | socket.SOCK_STREAM) |
| 40 | client_socket.connect((host_ip, port)) |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 41 | client_socket.settimeout(socket_timeout) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 42 | 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 | |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 51 | def _connect_socket(host, port, socket_timeout): |
Brian Haley | aee61ac | 2018-10-09 20:00:27 -0400 | [diff] [blame] | 52 | """Try to initiate a connection to a host using an ip address and a port. |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 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: |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 61 | return _try_connect(host, port, socket_timeout) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 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 Takashi | a2cc2e5 | 2018-07-31 18:54:02 +0900 | [diff] [blame] | 68 | class QoSTestMixin(object): |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 69 | credentials = ['primary', 'admin'] |
| 70 | force_tenant_isolation = False |
| 71 | |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 72 | FILE_SIZE = 1024 * 1024 |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 73 | TOLERANCE_FACTOR = 1.5 |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 74 | BUFFER_SIZE = 512 |
| 75 | COUNT = FILE_SIZE / BUFFER_SIZE |
Brian Haley | 33ef460 | 2018-04-26 14:37:49 -0400 | [diff] [blame] | 76 | LIMIT_BYTES_SEC = (constants.LIMIT_KILO_BITS_PER_SECOND * 1024 * |
| 77 | TOLERANCE_FACTOR / 8.0) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 78 | FILE_PATH = "/tmp/img" |
| 79 | |
LIU Yulong | 5ba88ef | 2017-12-22 10:50:15 +0800 | [diff] [blame] | 80 | NC_PORT = 1234 |
Arkady Shtempler | 3e1d8f1 | 2018-08-19 10:36:24 +0300 | [diff] [blame] | 81 | FILE_DOWNLOAD_TIMEOUT = 120 |
LIU Yulong | 5ba88ef | 2017-12-22 10:50:15 +0800 | [diff] [blame] | 82 | |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 83 | 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) " |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 85 | % {'bs': self.BUFFER_SIZE, 'count': self.COUNT, |
| 86 | 'file_path': self.FILE_PATH}) |
Rodolfo Alonso Hernandez | aa65dfb | 2019-09-18 11:30:04 +0000 | [diff] [blame] | 87 | ssh_client.exec_command(cmd, timeout=5) |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 88 | cmd = "stat -c %%s %s" % self.FILE_PATH |
Rodolfo Alonso Hernandez | aa65dfb | 2019-09-18 11:30:04 +0000 | [diff] [blame] | 89 | filesize = ssh_client.exec_command(cmd, timeout=5) |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 90 | if int(filesize.strip()) != self.FILE_SIZE: |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 91 | raise sc_exceptions.FileCreationFailedException( |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 92 | file=self.FILE_PATH) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 93 | |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 94 | def _check_bw(self, ssh_client, host, port, expected_bw=LIMIT_BYTES_SEC): |
Maciej Józefczyk | 328edc8 | 2019-09-16 14:05:48 +0000 | [diff] [blame^] | 95 | utils.kill_nc_process(ssh_client) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 96 | cmd = ("(nc -ll -p %(port)d < %(file_path)s > /dev/null &)" % { |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 97 | 'port': port, 'file_path': self.FILE_PATH}) |
Rodolfo Alonso Hernandez | aa65dfb | 2019-09-18 11:30:04 +0000 | [diff] [blame] | 98 | ssh_client.exec_command(cmd, timeout=5) |
Miguel Angel Ajo | d47e21a | 2017-02-07 16:21:16 +0100 | [diff] [blame] | 99 | |
Arkady Shtempler | 3e1d8f1 | 2018-08-19 10:36:24 +0300 | [diff] [blame] | 100 | # Open TCP socket to remote VM and download big file |
Miguel Angel Ajo | d47e21a | 2017-02-07 16:21:16 +0100 | [diff] [blame] | 101 | start_time = time.time() |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 102 | socket_timeout = self.FILE_SIZE * self.TOLERANCE_FACTOR / expected_bw |
| 103 | client_socket = _connect_socket(host, port, socket_timeout) |
Miguel Angel Ajo | d47e21a | 2017-02-07 16:21:16 +0100 | [diff] [blame] | 104 | total_bytes_read = 0 |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 105 | try: |
| 106 | while total_bytes_read < self.FILE_SIZE: |
| 107 | data = client_socket.recv(self.BUFFER_SIZE) |
| 108 | total_bytes_read += len(data) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 109 | |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 110 | # Calculate and return actual BW + logging result |
| 111 | time_elapsed = time.time() - start_time |
| 112 | bytes_per_second = total_bytes_read / time_elapsed |
Miguel Angel Ajo | d47e21a | 2017-02-07 16:21:16 +0100 | [diff] [blame] | 113 | |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 114 | LOG.debug("time_elapsed = %(time_elapsed).16f, " |
| 115 | "total_bytes_read = %(total_bytes_read)d, " |
| 116 | "bytes_per_second = %(bytes_per_second)d", |
| 117 | {'time_elapsed': time_elapsed, |
| 118 | 'total_bytes_read': total_bytes_read, |
| 119 | 'bytes_per_second': bytes_per_second}) |
| 120 | return bytes_per_second <= expected_bw |
| 121 | except socket.timeout: |
| 122 | LOG.warning('Socket timeout while reading the remote file, bytes ' |
| 123 | 'read: %s', total_bytes_read) |
Maciej Józefczyk | 328edc8 | 2019-09-16 14:05:48 +0000 | [diff] [blame^] | 124 | utils.kill_nc_process(ssh_client) |
Rodolfo Alonso Hernandez | fa5ebc4 | 2019-07-26 17:02:40 +0000 | [diff] [blame] | 125 | return False |
| 126 | finally: |
| 127 | client_socket.close() |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 128 | |
LIU Yulong | 5ba88ef | 2017-12-22 10:50:15 +0800 | [diff] [blame] | 129 | def _create_ssh_client(self): |
| 130 | return ssh.Client(self.fip['floating_ip_address'], |
| 131 | CONF.validation.image_ssh_user, |
| 132 | pkey=self.keypair['private_key']) |
| 133 | |
| 134 | def _test_basic_resources(self): |
| 135 | self.setup_network_and_server() |
| 136 | self.check_connectivity(self.fip['floating_ip_address'], |
| 137 | CONF.validation.image_ssh_user, |
| 138 | self.keypair['private_key']) |
| 139 | rulesets = [{'protocol': 'tcp', |
| 140 | 'direction': 'ingress', |
| 141 | 'port_range_min': self.NC_PORT, |
| 142 | 'port_range_max': self.NC_PORT, |
| 143 | 'remote_ip_prefix': '0.0.0.0/0'}] |
| 144 | self.create_secgroup_rules(rulesets, |
| 145 | self.security_groups[-1]['id']) |
| 146 | |
| 147 | def _create_qos_policy(self): |
| 148 | policy = self.os_admin.network_client.create_qos_policy( |
| 149 | name='test-policy', |
| 150 | description='test-qos-policy', |
| 151 | shared=True) |
| 152 | return policy['policy']['id'] |
| 153 | |
YAMAMOTO Takashi | a2cc2e5 | 2018-07-31 18:54:02 +0900 | [diff] [blame] | 154 | |
| 155 | class QoSTest(QoSTestMixin, base.BaseTempestTestCase): |
| 156 | @classmethod |
| 157 | @tutils.requires_ext(extension="qos", service="network") |
| 158 | @base_api.require_qos_rule_type(qos_consts.RULE_TYPE_BANDWIDTH_LIMIT) |
| 159 | def resource_setup(cls): |
| 160 | super(QoSTest, cls).resource_setup() |
| 161 | |
Arkady Shtempler | 3e1d8f1 | 2018-08-19 10:36:24 +0300 | [diff] [blame] | 162 | @decorators.idempotent_id('00682a0c-b72e-11e8-b81e-8c16450ea513') |
| 163 | def test_qos_basic_and_update(self): |
| 164 | """This test covers both: |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 165 | |
Arkady Shtempler | 3e1d8f1 | 2018-08-19 10:36:24 +0300 | [diff] [blame] | 166 | 1) Basic QoS functionality |
| 167 | This is a basic test that check that a QoS policy with |
| 168 | a bandwidth limit rule is applied correctly by sending |
| 169 | a file from the instance to the test node. |
| 170 | Then calculating the bandwidth every ~1 sec by the number of bits |
| 171 | received / elapsed time. |
| 172 | |
| 173 | 2) Update QoS policy |
| 174 | Administrator has the ability to update existing QoS policy, |
| 175 | this test is planned to verify that: |
| 176 | - actual BW is affected as expected after updating QoS policy. |
| 177 | Test scenario: |
| 178 | 1) Associating QoS Policy with "Original_bandwidth" |
| 179 | to the test node |
| 180 | 2) BW validation - by downloading file on test node. |
| 181 | ("Original_bandwidth" is expected) |
| 182 | 3) Updating existing QoS Policy to a new BW value |
| 183 | "Updated_bandwidth" |
| 184 | 4) BW validation - by downloading file on test node. |
| 185 | ("Updated_bandwidth" is expected) |
| 186 | Note: |
| 187 | There are two options to associate QoS policy to VM: |
| 188 | "Neutron Port" or "Network", in this test |
| 189 | both options are covered. |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 190 | """ |
Arkady Shtempler | 3e1d8f1 | 2018-08-19 10:36:24 +0300 | [diff] [blame] | 191 | |
| 192 | # Setup resources |
LIU Yulong | 5ba88ef | 2017-12-22 10:50:15 +0800 | [diff] [blame] | 193 | self._test_basic_resources() |
LIU Yulong | 5ba88ef | 2017-12-22 10:50:15 +0800 | [diff] [blame] | 194 | ssh_client = self._create_ssh_client() |
Arkady Shtempler | 3e1d8f1 | 2018-08-19 10:36:24 +0300 | [diff] [blame] | 195 | |
| 196 | # Create QoS policy |
| 197 | bw_limit_policy_id = self._create_qos_policy() |
| 198 | |
| 199 | # As admin user create QoS rule |
| 200 | rule_id = self.os_admin.network_client.create_bandwidth_limit_rule( |
| 201 | policy_id=bw_limit_policy_id, |
| 202 | max_kbps=constants.LIMIT_KILO_BITS_PER_SECOND, |
| 203 | max_burst_kbps=constants.LIMIT_KILO_BITS_PER_SECOND)[ |
| 204 | 'bandwidth_limit_rule']['id'] |
| 205 | |
| 206 | # Associate QoS to the network |
| 207 | self.os_admin.network_client.update_network( |
| 208 | self.network['id'], qos_policy_id=bw_limit_policy_id) |
| 209 | |
| 210 | # Create file on VM |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 211 | self._create_file_for_bw_tests(ssh_client) |
Arkady Shtempler | 3e1d8f1 | 2018-08-19 10:36:24 +0300 | [diff] [blame] | 212 | |
| 213 | # Basic test, Check that actual BW while downloading file |
| 214 | # is as expected (Original BW) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 215 | utils.wait_until_true(lambda: self._check_bw( |
| 216 | ssh_client, |
| 217 | self.fip['floating_ip_address'], |
LIU Yulong | 5ba88ef | 2017-12-22 10:50:15 +0800 | [diff] [blame] | 218 | port=self.NC_PORT), |
Arkady Shtempler | 3e1d8f1 | 2018-08-19 10:36:24 +0300 | [diff] [blame] | 219 | timeout=self.FILE_DOWNLOAD_TIMEOUT, |
| 220 | sleep=1) |
| 221 | |
| 222 | # As admin user update QoS rule |
| 223 | self.os_admin.network_client.update_bandwidth_limit_rule( |
| 224 | bw_limit_policy_id, |
| 225 | rule_id, |
| 226 | max_kbps=constants.LIMIT_KILO_BITS_PER_SECOND * 2, |
| 227 | max_burst_kbps=constants.LIMIT_KILO_BITS_PER_SECOND * 2) |
| 228 | |
| 229 | # Check that actual BW while downloading file |
| 230 | # is as expected (Update BW) |
| 231 | utils.wait_until_true(lambda: self._check_bw( |
| 232 | ssh_client, |
| 233 | self.fip['floating_ip_address'], |
| 234 | port=self.NC_PORT, |
| 235 | expected_bw=QoSTest.LIMIT_BYTES_SEC * 2), |
| 236 | timeout=self.FILE_DOWNLOAD_TIMEOUT, |
| 237 | sleep=1) |
| 238 | |
| 239 | # Create a new QoS policy |
| 240 | bw_limit_policy_id_new = self._create_qos_policy() |
| 241 | |
| 242 | # As admin user create a new QoS rule |
| 243 | rule_id_new = self.os_admin.network_client.create_bandwidth_limit_rule( |
| 244 | policy_id=bw_limit_policy_id_new, |
| 245 | max_kbps=constants.LIMIT_KILO_BITS_PER_SECOND, |
| 246 | max_burst_kbps=constants.LIMIT_KILO_BITS_PER_SECOND)[ |
| 247 | 'bandwidth_limit_rule']['id'] |
| 248 | |
| 249 | # Associate a new QoS policy to Neutron port |
| 250 | self.os_admin.network_client.update_port( |
| 251 | self.port['id'], qos_policy_id=bw_limit_policy_id_new) |
| 252 | |
| 253 | # Check that actual BW while downloading file |
| 254 | # is as expected (Original BW) |
| 255 | utils.wait_until_true(lambda: self._check_bw( |
| 256 | ssh_client, |
| 257 | self.fip['floating_ip_address'], |
| 258 | port=self.NC_PORT), |
| 259 | timeout=self.FILE_DOWNLOAD_TIMEOUT, |
| 260 | sleep=1) |
| 261 | |
| 262 | # As admin user update QoS rule |
| 263 | self.os_admin.network_client.update_bandwidth_limit_rule( |
| 264 | bw_limit_policy_id_new, |
| 265 | rule_id_new, |
| 266 | max_kbps=constants.LIMIT_KILO_BITS_PER_SECOND * 3, |
| 267 | max_burst_kbps=constants.LIMIT_KILO_BITS_PER_SECOND * 3) |
| 268 | |
| 269 | # Check that actual BW while downloading file |
| 270 | # is as expected (Update BW) |
| 271 | utils.wait_until_true(lambda: self._check_bw( |
| 272 | ssh_client, |
| 273 | self.fip['floating_ip_address'], |
| 274 | port=self.NC_PORT, expected_bw=QoSTest.LIMIT_BYTES_SEC * 3), |
| 275 | timeout=self.FILE_DOWNLOAD_TIMEOUT, |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 276 | sleep=1) |