blob: 2c3a23857510aa54b9a0f9d42dc99ebec7bff06b [file] [log] [blame]
Yair Fried4d7efa62013-11-17 17:12:29 +02001# Copyright 2013 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
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000016from tempest import clients
Yair Fried4d7efa62013-11-17 17:12:29 +020017from tempest.common.utils import data_utils
Matthew Treinish6c072292014-01-29 19:15:52 +000018from tempest import config
Yair Fried4d7efa62013-11-17 17:12:29 +020019from tempest.openstack.common import log as logging
20from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090021from tempest import test
Yair Fried4d7efa62013-11-17 17:12:29 +020022
Matthew Treinish6c072292014-01-29 19:15:52 +000023CONF = config.CONF
24
Yair Fried4d7efa62013-11-17 17:12:29 +020025LOG = logging.getLogger(__name__)
26
27
Andrea Frittoli4971fc82014-09-25 10:22:20 +010028class TestSecurityGroupsBasicOps(manager.NetworkScenarioTest):
Yair Fried4d7efa62013-11-17 17:12:29 +020029
30 """
31 This test suite assumes that Nova has been configured to
32 boot VM's with Neutron-managed networking, and attempts to
33 verify cross tenant connectivity as follows
34
35 ssh:
36 in order to overcome "ip namespace", each tenant has an "access point"
37 VM with floating-ip open to incoming ssh connection allowing network
38 commands (ping/ssh) to be executed from within the
39 tenant-network-namespace
40 Tempest host performs key-based authentication to the ssh server via
41 floating IP address
42
43 connectivity test is done by pinging destination server via source server
44 ssh connection.
45 success - ping returns
46 failure - ping_timeout reached
47
48 setup:
Yair Friedbf2e2c42014-01-28 12:06:38 +020049 for primary tenant:
Yair Fried4d7efa62013-11-17 17:12:29 +020050 1. create a network&subnet
51 2. create a router (if public router isn't configured)
52 3. connect tenant network to public network via router
53 4. create an access point:
54 a. a security group open to incoming ssh connection
55 b. a VM with a floating ip
56 5. create a general empty security group (same as "default", but
57 without rules allowing in-tenant traffic)
Yair Fried4d7efa62013-11-17 17:12:29 +020058
59 tests:
60 1. _verify_network_details
61 2. _verify_mac_addr: for each access point verify that
62 (subnet, fix_ip, mac address) are as defined in the port list
63 3. _test_in_tenant_block: test that in-tenant traffic is disabled
64 without rules allowing it
65 4. _test_in_tenant_allow: test that in-tenant traffic is enabled
66 once an appropriate rule has been created
67 5. _test_cross_tenant_block: test that cross-tenant traffic is disabled
68 without a rule allowing it on destination tenant
69 6. _test_cross_tenant_allow:
70 * test that cross-tenant traffic is enabled once an appropriate
71 rule has been created on destination tenant.
72 * test that reverse traffic is still blocked
73 * test than revesre traffic is enabled once an appropriate rule has
74 been created on source tenant
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -080075 7._test_port_update_new_security_group:
76 * test that traffic is blocked with default security group
77 * test that traffic is enabled after updating port with new security
78 group having appropriate rule
prdsilva8b733ad2014-12-09 02:54:49 -080079 8. _test_multiple_security_groups: test multiple security groups can be
80 associated with the vm
Yair Fried4d7efa62013-11-17 17:12:29 +020081
82 assumptions:
Yair Friedbf2e2c42014-01-28 12:06:38 +020083 1. alt_tenant/user existed and is different from primary_tenant/user
Yair Fried4d7efa62013-11-17 17:12:29 +020084 2. Public network is defined and reachable from the Tempest host
85 3. Public router can either be:
86 * defined, in which case all tenants networks can connect directly
87 to it, and cross tenant check will be done on the private IP of the
88 destination tenant
89 or
90 * not defined (empty string), in which case each tanant will have
91 its own router connected to the public network
92 """
93
94 class TenantProperties():
Yair Friedbf2e2c42014-01-28 12:06:38 +020095 """
Yair Fried4d7efa62013-11-17 17:12:29 +020096 helper class to save tenant details
97 id
98 credentials
99 network
100 subnet
101 security groups
102 servers
103 access point
Yair Friedbf2e2c42014-01-28 12:06:38 +0200104 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200105
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000106 def __init__(self, credentials):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300107 self.manager = clients.Manager(credentials)
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000108 # Credentials from manager are filled with both names and IDs
109 self.creds = self.manager.credentials
Yair Fried4d7efa62013-11-17 17:12:29 +0200110 self.network = None
111 self.subnet = None
112 self.router = None
113 self.security_groups = {}
114 self.servers = list()
115
Yair Friedbf2e2c42014-01-28 12:06:38 +0200116 def set_network(self, network, subnet, router):
Yair Fried4d7efa62013-11-17 17:12:29 +0200117 self.network = network
118 self.subnet = subnet
119 self.router = router
120
Yair Fried4d7efa62013-11-17 17:12:29 +0200121 @classmethod
122 def check_preconditions(cls):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300123 if CONF.baremetal.driver_enabled:
124 msg = ('Not currently supported by baremetal.')
Yair Frieddb6c9e92014-08-06 08:53:13 +0300125 raise cls.skipException(msg)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200126 super(TestSecurityGroupsBasicOps, cls).check_preconditions()
Matthew Treinish6c072292014-01-29 19:15:52 +0000127 if not (CONF.network.tenant_networks_reachable or
128 CONF.network.public_network_id):
Yair Fried4d7efa62013-11-17 17:12:29 +0200129 msg = ('Either tenant_networks_reachable must be "true", or '
130 'public_network_id must be defined.')
Yair Fried4d7efa62013-11-17 17:12:29 +0200131 raise cls.skipException(msg)
132
133 @classmethod
Andrea Frittoliac20b5e2014-09-15 13:31:14 +0100134 def resource_setup(cls):
Yair Fried764610a2014-04-07 12:17:05 +0300135 # Create no network resources for these tests.
136 cls.set_network_resources()
Andrea Frittoliac20b5e2014-09-15 13:31:14 +0100137 super(TestSecurityGroupsBasicOps, cls).resource_setup()
Yair Fried4d7efa62013-11-17 17:12:29 +0200138 # TODO(mnewby) Consider looking up entities as needed instead
139 # of storing them as collections on the class.
Yair Fried79b0a912014-10-20 11:15:37 +0300140
141 # get credentials for secondary tenant
142 cls.alt_creds = cls.isolated_creds.get_alt_creds()
143 cls.alt_manager = clients.Manager(cls.alt_creds)
144 # Credentials from the manager are filled with both IDs and Names
145 cls.alt_creds = cls.alt_manager.credentials
146
Yair Fried4d7efa62013-11-17 17:12:29 +0200147 cls.floating_ips = {}
148 cls.tenants = {}
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000149 creds = cls.credentials()
150 cls.primary_tenant = cls.TenantProperties(creds)
151 cls.alt_tenant = cls.TenantProperties(cls.alt_creds)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200152 for tenant in [cls.primary_tenant, cls.alt_tenant]:
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000153 cls.tenants[tenant.creds.tenant_id] = tenant
Yair Friedbf2e2c42014-01-28 12:06:38 +0200154 cls.floating_ip_access = not CONF.network.public_router_id
Yair Fried4d7efa62013-11-17 17:12:29 +0200155
Yair Friedbf2e2c42014-01-28 12:06:38 +0200156 def cleanup_wrapper(self, resource):
157 self.cleanup_resource(resource, self.__class__.__name__)
158
159 def setUp(self):
160 super(TestSecurityGroupsBasicOps, self).setUp()
161 self._deploy_tenant(self.primary_tenant)
162 self._verify_network_details(self.primary_tenant)
163 self._verify_mac_addr(self.primary_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200164
Yair Frieddb6c9e92014-08-06 08:53:13 +0300165 def _create_tenant_keypairs(self, tenant):
166 keypair = self.create_keypair(tenant.manager.keypairs_client)
167 tenant.keypair = keypair
Yair Fried4d7efa62013-11-17 17:12:29 +0200168
169 def _create_tenant_security_groups(self, tenant):
Yair Fried4d7efa62013-11-17 17:12:29 +0200170 access_sg = self._create_empty_security_group(
171 namestart='secgroup_access-',
Yair Frieddb6c9e92014-08-06 08:53:13 +0300172 tenant_id=tenant.creds.tenant_id,
173 client=tenant.manager.network_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200174 )
Yair Friedbf2e2c42014-01-28 12:06:38 +0200175
Yair Fried4d7efa62013-11-17 17:12:29 +0200176 # don't use default secgroup since it allows in-tenant traffic
177 def_sg = self._create_empty_security_group(
178 namestart='secgroup_general-',
Yair Frieddb6c9e92014-08-06 08:53:13 +0300179 tenant_id=tenant.creds.tenant_id,
180 client=tenant.manager.network_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200181 )
182 tenant.security_groups.update(access=access_sg, default=def_sg)
183 ssh_rule = dict(
184 protocol='tcp',
185 port_range_min=22,
186 port_range_max=22,
187 direction='ingress',
188 )
Yair Frieddb6c9e92014-08-06 08:53:13 +0300189 self._create_security_group_rule(secgroup=access_sg,
190 client=tenant.manager.network_client,
191 **ssh_rule)
Yair Fried4d7efa62013-11-17 17:12:29 +0200192
193 def _verify_network_details(self, tenant):
194 # Checks that we see the newly created network/subnet/router via
195 # checking the result of list_[networks,routers,subnets]
196 # Check that (router, subnet) couple exist in port_list
197 seen_nets = self._list_networks()
198 seen_names = [n['name'] for n in seen_nets]
199 seen_ids = [n['id'] for n in seen_nets]
200
201 self.assertIn(tenant.network.name, seen_names)
202 self.assertIn(tenant.network.id, seen_ids)
203
204 seen_subnets = [(n['id'], n['cidr'], n['network_id'])
205 for n in self._list_subnets()]
206 mysubnet = (tenant.subnet.id, tenant.subnet.cidr, tenant.network.id)
207 self.assertIn(mysubnet, seen_subnets)
208
209 seen_routers = self._list_routers()
210 seen_router_ids = [n['id'] for n in seen_routers]
211 seen_router_names = [n['name'] for n in seen_routers]
212
213 self.assertIn(tenant.router.name, seen_router_names)
214 self.assertIn(tenant.router.id, seen_router_ids)
215
216 myport = (tenant.router.id, tenant.subnet.id)
217 router_ports = [(i['device_id'], i['fixed_ips'][0]['subnet_id']) for i
Yair Frieddb6c9e92014-08-06 08:53:13 +0300218 in self._list_ports()
armando-migliacciobcfbbeb2014-08-11 18:33:47 -0700219 if self._is_router_port(i)]
Yair Fried4d7efa62013-11-17 17:12:29 +0200220
221 self.assertIn(myport, router_ports)
222
armando-migliacciobcfbbeb2014-08-11 18:33:47 -0700223 def _is_router_port(self, port):
224 """Return True if port is a router interface."""
225 # NOTE(armando-migliaccio): match device owner for both centralized
226 # and distributed routers; 'device_owner' is "" by default.
227 return port['device_owner'].startswith('network:router_interface')
228
Yair Fried4d7efa62013-11-17 17:12:29 +0200229 def _create_server(self, name, tenant, security_groups=None):
230 """
231 creates a server and assigns to security group
232 """
233 self._set_compute_context(tenant)
234 if security_groups is None:
Yair Frieddb6c9e92014-08-06 08:53:13 +0300235 security_groups = [tenant.security_groups['default']]
Ken'ichi Ohmichi1b3461e2014-12-02 03:41:07 +0000236 security_groups_names = [{'name': s['name']} for s in security_groups]
Yair Fried4d7efa62013-11-17 17:12:29 +0200237 create_kwargs = {
Dirk Mueller8cf79722014-09-12 17:37:15 +0200238 'networks': [
239 {'uuid': tenant.network.id},
Yair Fried4d7efa62013-11-17 17:12:29 +0200240 ],
Yair Frieddb6c9e92014-08-06 08:53:13 +0300241 'key_name': tenant.keypair['name'],
Ken'ichi Ohmichi1b3461e2014-12-02 03:41:07 +0000242 'security_groups': security_groups_names,
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000243 'tenant_id': tenant.creds.tenant_id
Yair Fried4d7efa62013-11-17 17:12:29 +0200244 }
Claudiu Belufaa98912014-09-01 16:50:28 +0300245 server = self.create_server(name=name, create_kwargs=create_kwargs)
246 self.assertEqual(
247 sorted([s['name'] for s in security_groups]),
248 sorted([s['name'] for s in server['security_groups']]))
249 return server
Yair Fried4d7efa62013-11-17 17:12:29 +0200250
251 def _create_tenant_servers(self, tenant, num=1):
252 for i in range(num):
253 name = 'server-{tenant}-gen-{num}-'.format(
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000254 tenant=tenant.creds.tenant_name,
Yair Fried4d7efa62013-11-17 17:12:29 +0200255 num=i
256 )
257 name = data_utils.rand_name(name)
258 server = self._create_server(name, tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200259 tenant.servers.append(server)
260
261 def _set_access_point(self, tenant):
262 """
263 creates a server in a secgroup with rule allowing external ssh
264 in order to access tenant internal network
265 workaround ip namespace
266 """
Yair Frieddb6c9e92014-08-06 08:53:13 +0300267 secgroups = tenant.security_groups.values()
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000268 name = 'server-{tenant}-access_point-'.format(
269 tenant=tenant.creds.tenant_name)
Yair Fried4d7efa62013-11-17 17:12:29 +0200270 name = data_utils.rand_name(name)
271 server = self._create_server(name, tenant,
272 security_groups=secgroups)
Yair Fried4d7efa62013-11-17 17:12:29 +0200273 tenant.access_point = server
Yair Frieddb6c9e92014-08-06 08:53:13 +0300274 self._assign_floating_ips(tenant, server)
Yair Fried4d7efa62013-11-17 17:12:29 +0200275
Yair Frieddb6c9e92014-08-06 08:53:13 +0300276 def _assign_floating_ips(self, tenant, server):
Matthew Treinish6c072292014-01-29 19:15:52 +0000277 public_network_id = CONF.network.public_network_id
Yair Friedae0e73d2014-11-24 11:56:26 +0200278 floating_ip = self.create_floating_ip(
Yair Frieddb6c9e92014-08-06 08:53:13 +0300279 server, public_network_id,
280 client=tenant.manager.network_client)
281 self.floating_ips.setdefault(server['id'], floating_ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200282
283 def _create_tenant_network(self, tenant):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300284 network, subnet, router = self.create_networks(
285 client=tenant.manager.network_client)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200286 tenant.set_network(network, subnet, router)
Yair Fried4d7efa62013-11-17 17:12:29 +0200287
288 def _set_compute_context(self, tenant):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300289 self.servers_client = tenant.manager.servers_client
290 return self.servers_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200291
292 def _deploy_tenant(self, tenant_or_id):
293 """
294 creates:
295 network
296 subnet
297 router (if public not defined)
298 access security group
299 access-point server
Yair Fried4d7efa62013-11-17 17:12:29 +0200300 """
301 if not isinstance(tenant_or_id, self.TenantProperties):
302 tenant = self.tenants[tenant_or_id]
Yair Fried4d7efa62013-11-17 17:12:29 +0200303 else:
304 tenant = tenant_or_id
Yair Fried4d7efa62013-11-17 17:12:29 +0200305 self._set_compute_context(tenant)
Yair Frieddb6c9e92014-08-06 08:53:13 +0300306 self._create_tenant_keypairs(tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200307 self._create_tenant_network(tenant)
308 self._create_tenant_security_groups(tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200309 self._set_access_point(tenant)
310
311 def _get_server_ip(self, server, floating=False):
Yair Friedbf2e2c42014-01-28 12:06:38 +0200312 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200313 returns the ip (floating/internal) of a server
Yair Friedbf2e2c42014-01-28 12:06:38 +0200314 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200315 if floating:
Yair Frieddb6c9e92014-08-06 08:53:13 +0300316 server_ip = self.floating_ips[server['id']].floating_ip_address
Yair Fried4d7efa62013-11-17 17:12:29 +0200317 else:
armando-migliacciod03f2642014-02-21 19:55:50 -0800318 server_ip = None
Yair Frieddb6c9e92014-08-06 08:53:13 +0300319 network_name = self.tenants[server['tenant_id']].network.name
320 if network_name in server['addresses']:
321 server_ip = server['addresses'][network_name][0]['addr']
armando-migliacciod03f2642014-02-21 19:55:50 -0800322 return server_ip
Yair Fried4d7efa62013-11-17 17:12:29 +0200323
324 def _connect_to_access_point(self, tenant):
325 """
326 create ssh connection to tenant access point
327 """
328 access_point_ssh = \
Yair Frieddb6c9e92014-08-06 08:53:13 +0300329 self.floating_ips[tenant.access_point['id']].floating_ip_address
330 private_key = tenant.keypair['private_key']
Yair Fried4d7efa62013-11-17 17:12:29 +0200331 access_point_ssh = self._ssh_to_server(access_point_ssh,
332 private_key=private_key)
333 return access_point_ssh
334
Yair Fried4d7efa62013-11-17 17:12:29 +0200335 def _check_connectivity(self, access_point, ip, should_succeed=True):
336 if should_succeed:
337 msg = "Timed out waiting for %s to become reachable" % ip
338 else:
Yair Fried4d7efa62013-11-17 17:12:29 +0200339 msg = "%s is reachable" % ip
Matthew Treinish53483132014-12-09 18:50:06 -0500340 self.assertTrue(self._check_remote_connectivity(access_point, ip,
341 should_succeed), msg)
Yair Fried4d7efa62013-11-17 17:12:29 +0200342
343 def _test_in_tenant_block(self, tenant):
344 access_point_ssh = self._connect_to_access_point(tenant)
345 for server in tenant.servers:
346 self._check_connectivity(access_point=access_point_ssh,
347 ip=self._get_server_ip(server),
348 should_succeed=False)
349
350 def _test_in_tenant_allow(self, tenant):
351 ruleset = dict(
352 protocol='icmp',
353 remote_group_id=tenant.security_groups['default'].id,
354 direction='ingress'
355 )
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000356 self._create_security_group_rule(
Yair Fried4d7efa62013-11-17 17:12:29 +0200357 secgroup=tenant.security_groups['default'],
358 **ruleset
359 )
360 access_point_ssh = self._connect_to_access_point(tenant)
361 for server in tenant.servers:
362 self._check_connectivity(access_point=access_point_ssh,
363 ip=self._get_server_ip(server))
Yair Fried4d7efa62013-11-17 17:12:29 +0200364
365 def _test_cross_tenant_block(self, source_tenant, dest_tenant):
Yair Friedbf2e2c42014-01-28 12:06:38 +0200366 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200367 if public router isn't defined, then dest_tenant access is via
368 floating-ip
Yair Friedbf2e2c42014-01-28 12:06:38 +0200369 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200370 access_point_ssh = self._connect_to_access_point(source_tenant)
371 ip = self._get_server_ip(dest_tenant.access_point,
372 floating=self.floating_ip_access)
373 self._check_connectivity(access_point=access_point_ssh, ip=ip,
374 should_succeed=False)
375
376 def _test_cross_tenant_allow(self, source_tenant, dest_tenant):
Yair Friedbf2e2c42014-01-28 12:06:38 +0200377 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200378 check for each direction:
379 creating rule for tenant incoming traffic enables only 1way traffic
Yair Friedbf2e2c42014-01-28 12:06:38 +0200380 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200381 ruleset = dict(
382 protocol='icmp',
383 direction='ingress'
384 )
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000385 self._create_security_group_rule(
Yair Fried4d7efa62013-11-17 17:12:29 +0200386 secgroup=dest_tenant.security_groups['default'],
Yair Frieddb6c9e92014-08-06 08:53:13 +0300387 client=dest_tenant.manager.network_client,
Yair Fried4d7efa62013-11-17 17:12:29 +0200388 **ruleset
389 )
Yair Friedbf2e2c42014-01-28 12:06:38 +0200390 access_point_ssh = self._connect_to_access_point(source_tenant)
391 ip = self._get_server_ip(dest_tenant.access_point,
392 floating=self.floating_ip_access)
393 self._check_connectivity(access_point_ssh, ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200394
Yair Friedbf2e2c42014-01-28 12:06:38 +0200395 # test that reverse traffic is still blocked
396 self._test_cross_tenant_block(dest_tenant, source_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200397
Yair Friedbf2e2c42014-01-28 12:06:38 +0200398 # allow reverse traffic and check
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000399 self._create_security_group_rule(
Yair Friedbf2e2c42014-01-28 12:06:38 +0200400 secgroup=source_tenant.security_groups['default'],
Yair Frieddb6c9e92014-08-06 08:53:13 +0300401 client=source_tenant.manager.network_client,
Yair Friedbf2e2c42014-01-28 12:06:38 +0200402 **ruleset
403 )
Yair Fried4d7efa62013-11-17 17:12:29 +0200404
Yair Friedbf2e2c42014-01-28 12:06:38 +0200405 access_point_ssh_2 = self._connect_to_access_point(dest_tenant)
406 ip = self._get_server_ip(source_tenant.access_point,
407 floating=self.floating_ip_access)
408 self._check_connectivity(access_point_ssh_2, ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200409
410 def _verify_mac_addr(self, tenant):
411 """
412 verify that VM (tenant's access point) has the same ip,mac as listed in
413 port list
414 """
415 access_point_ssh = self._connect_to_access_point(tenant)
416 mac_addr = access_point_ssh.get_mac_address()
417 mac_addr = mac_addr.strip().lower()
Henry Gessau78ab4b02014-03-31 15:10:13 -0400418 # Get the fixed_ips and mac_address fields of all ports. Select
419 # only those two columns to reduce the size of the response.
Yair Frieddb6c9e92014-08-06 08:53:13 +0300420 port_list = self._list_ports(fields=['fixed_ips', 'mac_address'])
Yair Fried4d7efa62013-11-17 17:12:29 +0200421 port_detail_list = [
422 (port['fixed_ips'][0]['subnet_id'],
423 port['fixed_ips'][0]['ip_address'],
Henry Gessau78ab4b02014-03-31 15:10:13 -0400424 port['mac_address'].lower())
425 for port in port_list if port['fixed_ips']
Yair Fried4d7efa62013-11-17 17:12:29 +0200426 ]
427 server_ip = self._get_server_ip(tenant.access_point)
428 subnet_id = tenant.subnet.id
429 self.assertIn((subnet_id, server_ip, mac_addr), port_detail_list)
430
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900431 @test.attr(type='smoke')
432 @test.services('compute', 'network')
Yair Fried4d7efa62013-11-17 17:12:29 +0200433 def test_cross_tenant_traffic(self):
Yair Fried79b0a912014-10-20 11:15:37 +0300434 if not self.isolated_creds.is_multi_tenant():
435 raise self.skipException("No secondary tenant defined")
Nachi Ueno26b4c972014-01-17 06:15:13 -0800436 try:
Yair Friedbf2e2c42014-01-28 12:06:38 +0200437 # deploy new tenant
438 self._deploy_tenant(self.alt_tenant)
439 self._verify_network_details(self.alt_tenant)
440 self._verify_mac_addr(self.alt_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200441
Nachi Ueno26b4c972014-01-17 06:15:13 -0800442 # cross tenant check
Yair Friedbf2e2c42014-01-28 12:06:38 +0200443 source_tenant = self.primary_tenant
Nachi Ueno26b4c972014-01-17 06:15:13 -0800444 dest_tenant = self.alt_tenant
445 self._test_cross_tenant_block(source_tenant, dest_tenant)
446 self._test_cross_tenant_allow(source_tenant, dest_tenant)
447 except Exception:
Yair Friedbf2e2c42014-01-28 12:06:38 +0200448 for tenant in self.tenants.values():
449 self._log_console_output(servers=tenant.servers)
450 raise
451
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900452 @test.attr(type='smoke')
453 @test.services('compute', 'network')
Yair Friedbf2e2c42014-01-28 12:06:38 +0200454 def test_in_tenant_traffic(self):
455 try:
456 self._create_tenant_servers(self.primary_tenant, num=1)
457
458 # in-tenant check
459 self._test_in_tenant_block(self.primary_tenant)
460 self._test_in_tenant_allow(self.primary_tenant)
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800461 except Exception:
462 for tenant in self.tenants.values():
463 self._log_console_output(servers=tenant.servers)
464 raise
Yair Friedbf2e2c42014-01-28 12:06:38 +0200465
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800466 @test.attr(type='smoke')
467 @test.services('compute', 'network')
468 def test_port_update_new_security_group(self):
469 """
470 This test verifies the traffic after updating the vm port with new
471 security group having appropiate rule.
472 """
473 new_tenant = self.primary_tenant
474
475 # Create empty security group and add icmp rule in it
476 new_sg = self._create_empty_security_group(
477 namestart='secgroup_new-',
478 tenant_id=new_tenant.creds.tenant_id,
479 client=new_tenant.manager.network_client)
480 icmp_rule = dict(
481 protocol='icmp',
482 direction='ingress',
483 )
484 self._create_security_group_rule(
485 secgroup=new_sg,
486 client=new_tenant.manager.network_client,
487 **icmp_rule)
488 new_tenant.security_groups.update(new_sg=new_sg)
489
490 # Create server with default security group
491 name = 'server-{tenant}-gen-1-'.format(
492 tenant=new_tenant.creds.tenant_name
493 )
494 name = data_utils.rand_name(name)
495 server = self._create_server(name, new_tenant)
496
497 # Check connectivity failure with default security group
498 try:
499 access_point_ssh = self._connect_to_access_point(new_tenant)
500 self._check_connectivity(access_point=access_point_ssh,
501 ip=self._get_server_ip(server),
502 should_succeed=False)
503 server_id = server['id']
504 port_id = self._list_ports(device_id=server_id)[0]['id']
505
506 # update port with new security group and check connectivity
507 self.network_client.update_port(port_id, security_groups=[
508 new_tenant.security_groups['new_sg'].id])
509 self._check_connectivity(
510 access_point=access_point_ssh,
511 ip=self._get_server_ip(server))
Yair Friedbf2e2c42014-01-28 12:06:38 +0200512 except Exception:
513 for tenant in self.tenants.values():
514 self._log_console_output(servers=tenant.servers)
Nachi Ueno26b4c972014-01-17 06:15:13 -0800515 raise
prdsilva8b733ad2014-12-09 02:54:49 -0800516
517 @test.attr(type='smoke')
518 @test.services('compute', 'network')
519 def test_multiple_security_groups(self):
520 """
521 This test verifies multiple security groups and checks that rules
522 provided in the both the groups is applied onto VM
523 """
524 tenant = self.primary_tenant
525 ip = self._get_server_ip(tenant.access_point,
526 floating=self.floating_ip_access)
527 ssh_login = CONF.compute.image_ssh_user
528 private_key = tenant.keypair['private_key']
529 self.check_vm_connectivity(ip,
530 should_connect=False)
531 ruleset = dict(
532 protocol='icmp',
533 direction='ingress'
534 )
535 self._create_security_group_rule(
536 secgroup=tenant.security_groups['default'],
537 **ruleset
538 )
539 """
540 Vm now has 2 security groups one with ssh rule(
541 already added in setUp() method),and other with icmp rule
542 (added in the above step).The check_vm_connectivity tests
543 -that vm ping test is successful
544 -ssh to vm is successful
545 """
546 self.check_vm_connectivity(ip,
547 username=ssh_login,
548 private_key=private_key,
549 should_connect=True)