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