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 | |
| 19 | from oslo_log import log as logging |
| 20 | from tempest.lib.common import ssh |
| 21 | from tempest.lib import exceptions |
| 22 | from tempest import test |
| 23 | |
| 24 | from neutron.common import utils |
Sławek Kapłoński | ff29406 | 2016-12-04 15:00:54 +0000 | [diff] [blame] | 25 | from neutron.services.qos import qos_consts |
| 26 | from neutron.tests.tempest.api import base as base_api |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 27 | from neutron.tests.tempest import config |
| 28 | from neutron.tests.tempest.scenario import base |
| 29 | from neutron.tests.tempest.scenario import constants |
| 30 | from neutron.tests.tempest.scenario import exceptions as sc_exceptions |
| 31 | |
| 32 | CONF = config.CONF |
| 33 | LOG = logging.getLogger(__name__) |
| 34 | |
| 35 | |
| 36 | def _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 Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 41 | 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 | |
| 50 | def _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 | |
| 68 | class 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 Takashi | ca17464 | 2016-07-15 15:01:31 +0900 | [diff] [blame] | 81 | @classmethod |
| 82 | @test.requires_ext(extension="qos", service="network") |
YAMAMOTO Takashi | 3bd3d0f | 2016-12-12 11:14:58 +0900 | [diff] [blame] | 83 | @base_api.require_qos_rule_type(qos_consts.RULE_TYPE_BANDWIDTH_LIMIT) |
YAMAMOTO Takashi | ca17464 | 2016-07-15 15:01:31 +0900 | [diff] [blame] | 84 | def resource_setup(cls): |
| 85 | super(QoSTest, cls).resource_setup() |
| 86 | |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 87 | 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 Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 99 | 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 Ajo | d47e21a | 2017-02-07 16:21:16 +0100 | [diff] [blame^] | 107 | |
| 108 | start_time = time.time() |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 109 | client_socket = _connect_socket(host, port) |
Miguel Angel Ajo | d47e21a | 2017-02-07 16:21:16 +0100 | [diff] [blame^] | 110 | total_bytes_read = 0 |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 111 | |
| 112 | while total_bytes_read < QoSTest.FILE_SIZE: |
Miguel Angel Ajo | d47e21a | 2017-02-07 16:21:16 +0100 | [diff] [blame^] | 113 | data = client_socket.recv(QoSTest.BUFFER_SIZE) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 114 | total_bytes_read += len(data) |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 115 | |
Miguel Angel Ajo | d47e21a | 2017-02-07 16:21:16 +0100 | [diff] [blame^] | 116 | 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 Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 127 | |
| 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 Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 149 | self.create_secgroup_rules(rulesets, |
| 150 | self.security_groups[-1]['id']) |
| 151 | |
Itzik Brown | 1ef813a | 2016-06-06 12:56:21 +0000 | [diff] [blame] | 152 | 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) |