blob: b79faf8e12c0f73b3d0b740325fa636267f1d566 [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
19from oslo_log import log as logging
20from tempest.lib.common import ssh
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +000021from tempest.lib import decorators
Itzik Brown1ef813a2016-06-06 12:56:21 +000022from tempest.lib import exceptions
23from tempest import test
Jakub Libosvar4fb7ba52017-02-22 10:51:35 -050024import testtools
Itzik Brown1ef813a2016-06-06 12:56:21 +000025
26from neutron.common import utils
Sławek Kapłońskiff294062016-12-04 15:00:54 +000027from neutron.services.qos import qos_consts
28from neutron.tests.tempest.api import base as base_api
Itzik Brown1ef813a2016-06-06 12:56:21 +000029from neutron.tests.tempest import config
30from neutron.tests.tempest.scenario import base
31from neutron.tests.tempest.scenario import constants
32from neutron.tests.tempest.scenario import exceptions as sc_exceptions
33
34CONF = config.CONF
35LOG = logging.getLogger(__name__)
36
37
38def _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 Brown1ef813a2016-06-06 12:56:21 +000043 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
52def _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
70class 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 Takashica174642016-07-15 15:01:31 +090083 @classmethod
84 @test.requires_ext(extension="qos", service="network")
YAMAMOTO Takashi3bd3d0f2016-12-12 11:14:58 +090085 @base_api.require_qos_rule_type(qos_consts.RULE_TYPE_BANDWIDTH_LIMIT)
Jakub Libosvar4fb7ba52017-02-22 10:51:35 -050086 @testtools.skip('bug/1662109')
YAMAMOTO Takashica174642016-07-15 15:01:31 +090087 def resource_setup(cls):
88 super(QoSTest, cls).resource_setup()
89
Itzik Brown1ef813a2016-06-06 12:56:21 +000090 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 Brown1ef813a2016-06-06 12:56:21 +0000102 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 Ajod47e21a2017-02-07 16:21:16 +0100110
111 start_time = time.time()
Itzik Brown1ef813a2016-06-06 12:56:21 +0000112 client_socket = _connect_socket(host, port)
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100113 total_bytes_read = 0
Itzik Brown1ef813a2016-06-06 12:56:21 +0000114
115 while total_bytes_read < QoSTest.FILE_SIZE:
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100116 data = client_socket.recv(QoSTest.BUFFER_SIZE)
Itzik Brown1ef813a2016-06-06 12:56:21 +0000117 total_bytes_read += len(data)
Itzik Brown1ef813a2016-06-06 12:56:21 +0000118
Miguel Angel Ajod47e21a2017-02-07 16:21:16 +0100119 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 Brown1ef813a2016-06-06 12:56:21 +0000130
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000131 @decorators.idempotent_id('1f7ed39b-428f-410a-bd2b-db9f465680df')
Itzik Brown1ef813a2016-06-06 12:56:21 +0000132 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 Brownbac51dc2016-10-31 12:25:04 +0000152 self.create_secgroup_rules(rulesets,
153 self.security_groups[-1]['id'])
154
Itzik Brown1ef813a2016-06-06 12:56:21 +0000155 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)