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