blob: b5584382d1bd7d2294b8030391e12e9026d23b87 [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
21from tempest.lib import exceptions
22from tempest import test
23
24from neutron.common import utils
Sławek Kapłońskiff294062016-12-04 15:00:54 +000025from neutron.services.qos import qos_consts
26from neutron.tests.tempest.api import base as base_api
Itzik Brown1ef813a2016-06-06 12:56:21 +000027from neutron.tests.tempest import config
28from neutron.tests.tempest.scenario import base
29from neutron.tests.tempest.scenario import constants
30from neutron.tests.tempest.scenario import exceptions as sc_exceptions
31
32CONF = config.CONF
33LOG = logging.getLogger(__name__)
34
35
36def _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))
41 client_socket.setblocking(0)
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
51def _connect_socket(host, port):
52 """Try to initiate a connection to a host using an ip address
53 and a port.
54
55 Trying couple of times until a timeout is reached in case the listening
56 host is not ready yet.
57 """
58
59 start = time.time()
60 while True:
61 try:
62 return _try_connect(host, port)
63 except sc_exceptions.SocketConnectionRefused:
64 if time.time() - start > constants.SOCKET_CONNECT_TIMEOUT:
65 raise sc_exceptions.ConnectionTimeoutException(host=host,
66 port=port)
67
68
69class QoSTest(base.BaseTempestTestCase):
70 credentials = ['primary', 'admin']
71 force_tenant_isolation = False
72
73 BUFFER_SIZE = 1024 * 1024
74 TOLERANCE_FACTOR = 1.5
75 BS = 512
76 COUNT = BUFFER_SIZE / BS
77 FILE_SIZE = BS * COUNT
78 LIMIT_BYTES_SEC = (constants.LIMIT_KILO_BITS_PER_SECOND * 1024
79 * TOLERANCE_FACTOR / 8.0)
80 FILE_PATH = "/tmp/img"
81
YAMAMOTO Takashica174642016-07-15 15:01:31 +090082 @classmethod
83 @test.requires_ext(extension="qos", service="network")
YAMAMOTO Takashi3bd3d0f2016-12-12 11:14:58 +090084 @base_api.require_qos_rule_type(qos_consts.RULE_TYPE_BANDWIDTH_LIMIT)
YAMAMOTO Takashica174642016-07-15 15:01:31 +090085 def resource_setup(cls):
86 super(QoSTest, cls).resource_setup()
87
Itzik Brown1ef813a2016-06-06 12:56:21 +000088 def _create_file_for_bw_tests(self, ssh_client):
89 cmd = ("(dd if=/dev/zero bs=%(bs)d count=%(count)d of=%(file_path)s) "
90 % {'bs': QoSTest.BS, 'count': QoSTest.COUNT,
91 'file_path': QoSTest.FILE_PATH})
92 ssh_client.exec_command(cmd)
93 cmd = "stat -c %%s %s" % QoSTest.FILE_PATH
94 filesize = ssh_client.exec_command(cmd)
95 if int(filesize.strip()) != QoSTest.FILE_SIZE:
96 raise sc_exceptions.FileCreationFailedException(
97 file=QoSTest.FILE_PATH)
98
99 def _check_bw(self, ssh_client, host, port):
100 total_bytes_read = 0
101 cycle_start_time = time.time()
102 cycle_data_read = 0
103
104 cmd = "killall -q nc"
105 try:
106 ssh_client.exec_command(cmd)
107 except exceptions.SSHExecCommandFailed:
108 pass
109 cmd = ("(nc -ll -p %(port)d < %(file_path)s > /dev/null &)" % {
110 'port': port, 'file_path': QoSTest.FILE_PATH})
111 ssh_client.exec_command(cmd)
112 client_socket = _connect_socket(host, port)
113
114 while total_bytes_read < QoSTest.FILE_SIZE:
115 try:
116 data = client_socket.recv(QoSTest.BUFFER_SIZE)
117 except socket.error as e:
118 if e.args[0] in [errno.EAGAIN, errno.EWOULDBLOCK]:
119 continue
120 else:
121 raise
122 total_bytes_read += len(data)
123 cycle_data_read += len(data)
124 time_elapsed = time.time() - cycle_start_time
125 should_check = (time_elapsed >= 5 or
126 total_bytes_read == QoSTest.FILE_SIZE)
127 if should_check:
128 LOG.debug("time_elapsed = %(time_elapsed)d,"
129 "total_bytes_read = %(bytes_read)d,"
130 "cycle_data_read = %(cycle_data)d",
131 {"time_elapsed": time_elapsed,
132 "bytes_read": total_bytes_read,
133 "cycle_data": cycle_data_read})
134
135 if cycle_data_read / time_elapsed > QoSTest.LIMIT_BYTES_SEC:
136 # Limit reached
137 return False
138 else:
139 cycle_start_time = time.time()
140 cycle_data_read = 0
141 return True
142
143 @test.idempotent_id('1f7ed39b-428f-410a-bd2b-db9f465680df')
144 def test_qos(self):
145 """This is a basic test that check that a QoS policy with
146
147 a bandwidth limit rule is applied correctly by sending
148 a file from the instance to the test node.
149 Then calculating the bandwidth every ~1 sec by the number of bits
150 received / elapsed time.
151 """
152
153 NC_PORT = 1234
154
155 self.setup_network_and_server()
156 self.check_connectivity(self.fip['floating_ip_address'],
157 CONF.validation.image_ssh_user,
158 self.keypair['private_key'])
159 rulesets = [{'protocol': 'tcp',
160 'direction': 'ingress',
161 'port_range_min': NC_PORT,
162 'port_range_max': NC_PORT,
163 'remote_ip_prefix': '0.0.0.0/0'}]
Itzik Brownbac51dc2016-10-31 12:25:04 +0000164 self.create_secgroup_rules(rulesets,
165 self.security_groups[-1]['id'])
166
Itzik Brown1ef813a2016-06-06 12:56:21 +0000167 ssh_client = ssh.Client(self.fip['floating_ip_address'],
168 CONF.validation.image_ssh_user,
169 pkey=self.keypair['private_key'])
170 policy = self.admin_manager.network_client.create_qos_policy(
171 name='test-policy',
172 description='test-qos-policy',
173 shared=True)
174 policy_id = policy['policy']['id']
175 self.admin_manager.network_client.create_bandwidth_limit_rule(
176 policy_id, max_kbps=constants.LIMIT_KILO_BITS_PER_SECOND,
177 max_burst_kbps=constants.LIMIT_KILO_BITS_PER_SECOND)
178 port = self.client.list_ports(network_id=self.network['id'],
179 device_id=self.server[
180 'server']['id'])['ports'][0]
181 self.admin_manager.network_client.update_port(port['id'],
182 qos_policy_id=policy_id)
183 self._create_file_for_bw_tests(ssh_client)
184 utils.wait_until_true(lambda: self._check_bw(
185 ssh_client,
186 self.fip['floating_ip_address'],
187 port=NC_PORT),
188 timeout=120,
189 sleep=1)