blob: 1ecc212ae595ac8e215cfced9a9dbec0aef78d5c [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
Doug Hellmann583ce2c2015-03-11 14:55:46 +000016from oslo_log import log as logging
Matthew Treinish01472ff2015-02-20 17:26:52 -050017from tempest_lib.common.utils import data_utils
18
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000019from tempest import clients
Matthew Treinish6c072292014-01-29 19:15:52 +000020from tempest import config
Yair Fried4d7efa62013-11-17 17:12:29 +020021from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090022from tempest import test
Yair Fried4d7efa62013-11-17 17:12:29 +020023
Matthew Treinish6c072292014-01-29 19:15:52 +000024CONF = config.CONF
25
Yair Fried4d7efa62013-11-17 17:12:29 +020026LOG = logging.getLogger(__name__)
27
28
Andrea Frittoli4971fc82014-09-25 10:22:20 +010029class TestSecurityGroupsBasicOps(manager.NetworkScenarioTest):
Yair Fried4d7efa62013-11-17 17:12:29 +020030
31 """
32 This test suite assumes that Nova has been configured to
33 boot VM's with Neutron-managed networking, and attempts to
34 verify cross tenant connectivity as follows
35
36 ssh:
37 in order to overcome "ip namespace", each tenant has an "access point"
38 VM with floating-ip open to incoming ssh connection allowing network
39 commands (ping/ssh) to be executed from within the
40 tenant-network-namespace
41 Tempest host performs key-based authentication to the ssh server via
42 floating IP address
43
44 connectivity test is done by pinging destination server via source server
45 ssh connection.
46 success - ping returns
47 failure - ping_timeout reached
48
49 setup:
Yair Friedbf2e2c42014-01-28 12:06:38 +020050 for primary tenant:
Yair Fried4d7efa62013-11-17 17:12:29 +020051 1. create a network&subnet
52 2. create a router (if public router isn't configured)
53 3. connect tenant network to public network via router
54 4. create an access point:
55 a. a security group open to incoming ssh connection
56 b. a VM with a floating ip
57 5. create a general empty security group (same as "default", but
58 without rules allowing in-tenant traffic)
Yair Fried4d7efa62013-11-17 17:12:29 +020059
60 tests:
61 1. _verify_network_details
62 2. _verify_mac_addr: for each access point verify that
63 (subnet, fix_ip, mac address) are as defined in the port list
64 3. _test_in_tenant_block: test that in-tenant traffic is disabled
65 without rules allowing it
66 4. _test_in_tenant_allow: test that in-tenant traffic is enabled
67 once an appropriate rule has been created
68 5. _test_cross_tenant_block: test that cross-tenant traffic is disabled
69 without a rule allowing it on destination tenant
70 6. _test_cross_tenant_allow:
71 * test that cross-tenant traffic is enabled once an appropriate
72 rule has been created on destination tenant.
73 * test that reverse traffic is still blocked
Fei Long Wang50131ee2015-02-02 16:58:24 +130074 * test than reverse traffic is enabled once an appropriate rule has
Yair Fried4d7efa62013-11-17 17:12:29 +020075 been created on source tenant
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -080076 7._test_port_update_new_security_group:
77 * test that traffic is blocked with default security group
78 * test that traffic is enabled after updating port with new security
79 group having appropriate rule
prdsilva8b733ad2014-12-09 02:54:49 -080080 8. _test_multiple_security_groups: test multiple security groups can be
81 associated with the vm
Yair Fried4d7efa62013-11-17 17:12:29 +020082
83 assumptions:
Yair Friedbf2e2c42014-01-28 12:06:38 +020084 1. alt_tenant/user existed and is different from primary_tenant/user
Yair Fried4d7efa62013-11-17 17:12:29 +020085 2. Public network is defined and reachable from the Tempest host
86 3. Public router can either be:
87 * defined, in which case all tenants networks can connect directly
88 to it, and cross tenant check will be done on the private IP of the
89 destination tenant
90 or
Fei Long Wang50131ee2015-02-02 16:58:24 +130091 * not defined (empty string), in which case each tenant will have
Yair Fried4d7efa62013-11-17 17:12:29 +020092 its own router connected to the public network
93 """
94
Joe Gordon28788b42015-02-25 12:42:37 -080095 class TenantProperties(object):
Yair Friedbf2e2c42014-01-28 12:06:38 +020096 """
Yair Fried4d7efa62013-11-17 17:12:29 +020097 helper class to save tenant details
98 id
99 credentials
100 network
101 subnet
102 security groups
103 servers
104 access point
Yair Friedbf2e2c42014-01-28 12:06:38 +0200105 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200106
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000107 def __init__(self, credentials):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300108 self.manager = clients.Manager(credentials)
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000109 # Credentials from manager are filled with both names and IDs
110 self.creds = self.manager.credentials
Yair Fried4d7efa62013-11-17 17:12:29 +0200111 self.network = None
112 self.subnet = None
113 self.router = None
114 self.security_groups = {}
115 self.servers = list()
116
Yair Friedbf2e2c42014-01-28 12:06:38 +0200117 def set_network(self, network, subnet, router):
Yair Fried4d7efa62013-11-17 17:12:29 +0200118 self.network = network
119 self.subnet = subnet
120 self.router = router
121
Yair Fried4d7efa62013-11-17 17:12:29 +0200122 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000123 def skip_checks(cls):
124 super(TestSecurityGroupsBasicOps, cls).skip_checks()
Yair Frieddb6c9e92014-08-06 08:53:13 +0300125 if CONF.baremetal.driver_enabled:
126 msg = ('Not currently supported by baremetal.')
Yair Frieddb6c9e92014-08-06 08:53:13 +0300127 raise cls.skipException(msg)
Itzik Brown06952672015-03-29 12:38:58 +0300128 if CONF.network.port_vnic_type in ['direct', 'macvtap']:
129 msg = ('Not currently supported when using vnic_type'
130 ' direct or macvtap')
131 raise cls.skipException(msg)
Matthew Treinish6c072292014-01-29 19:15:52 +0000132 if not (CONF.network.tenant_networks_reachable or
133 CONF.network.public_network_id):
Yair Fried4d7efa62013-11-17 17:12:29 +0200134 msg = ('Either tenant_networks_reachable must be "true", or '
135 'public_network_id must be defined.')
Yair Fried4d7efa62013-11-17 17:12:29 +0200136 raise cls.skipException(msg)
137
138 @classmethod
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000139 def setup_credentials(cls):
Yair Fried764610a2014-04-07 12:17:05 +0300140 # Create no network resources for these tests.
141 cls.set_network_resources()
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000142 super(TestSecurityGroupsBasicOps, cls).setup_credentials()
Yair Fried4d7efa62013-11-17 17:12:29 +0200143 # TODO(mnewby) Consider looking up entities as needed instead
144 # of storing them as collections on the class.
Yair Fried79b0a912014-10-20 11:15:37 +0300145
146 # get credentials for secondary tenant
147 cls.alt_creds = cls.isolated_creds.get_alt_creds()
148 cls.alt_manager = clients.Manager(cls.alt_creds)
149 # Credentials from the manager are filled with both IDs and Names
150 cls.alt_creds = cls.alt_manager.credentials
151
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000152 @classmethod
153 def resource_setup(cls):
154 super(TestSecurityGroupsBasicOps, cls).resource_setup()
Yair Fried4d7efa62013-11-17 17:12:29 +0200155 cls.floating_ips = {}
156 cls.tenants = {}
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000157 creds = cls.credentials()
158 cls.primary_tenant = cls.TenantProperties(creds)
159 cls.alt_tenant = cls.TenantProperties(cls.alt_creds)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200160 for tenant in [cls.primary_tenant, cls.alt_tenant]:
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000161 cls.tenants[tenant.creds.tenant_id] = tenant
Emily Hugenbruch5e2d2a22015-02-25 21:35:45 +0000162
Yair Friedbf2e2c42014-01-28 12:06:38 +0200163 cls.floating_ip_access = not CONF.network.public_router_id
Yair Fried4d7efa62013-11-17 17:12:29 +0200164
Yair Friedbf2e2c42014-01-28 12:06:38 +0200165 def cleanup_wrapper(self, resource):
166 self.cleanup_resource(resource, self.__class__.__name__)
167
168 def setUp(self):
169 super(TestSecurityGroupsBasicOps, self).setUp()
170 self._deploy_tenant(self.primary_tenant)
171 self._verify_network_details(self.primary_tenant)
172 self._verify_mac_addr(self.primary_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200173
Yair Frieddb6c9e92014-08-06 08:53:13 +0300174 def _create_tenant_keypairs(self, tenant):
175 keypair = self.create_keypair(tenant.manager.keypairs_client)
176 tenant.keypair = keypair
Yair Fried4d7efa62013-11-17 17:12:29 +0200177
178 def _create_tenant_security_groups(self, tenant):
Yair Fried4d7efa62013-11-17 17:12:29 +0200179 access_sg = self._create_empty_security_group(
180 namestart='secgroup_access-',
Yair Frieddb6c9e92014-08-06 08:53:13 +0300181 tenant_id=tenant.creds.tenant_id,
182 client=tenant.manager.network_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200183 )
Yair Friedbf2e2c42014-01-28 12:06:38 +0200184
Yair Fried4d7efa62013-11-17 17:12:29 +0200185 # don't use default secgroup since it allows in-tenant traffic
186 def_sg = self._create_empty_security_group(
187 namestart='secgroup_general-',
Yair Frieddb6c9e92014-08-06 08:53:13 +0300188 tenant_id=tenant.creds.tenant_id,
189 client=tenant.manager.network_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200190 )
191 tenant.security_groups.update(access=access_sg, default=def_sg)
192 ssh_rule = dict(
193 protocol='tcp',
194 port_range_min=22,
195 port_range_max=22,
196 direction='ingress',
197 )
Yair Frieddb6c9e92014-08-06 08:53:13 +0300198 self._create_security_group_rule(secgroup=access_sg,
199 client=tenant.manager.network_client,
200 **ssh_rule)
Yair Fried4d7efa62013-11-17 17:12:29 +0200201
202 def _verify_network_details(self, tenant):
203 # Checks that we see the newly created network/subnet/router via
204 # checking the result of list_[networks,routers,subnets]
205 # Check that (router, subnet) couple exist in port_list
206 seen_nets = self._list_networks()
207 seen_names = [n['name'] for n in seen_nets]
208 seen_ids = [n['id'] for n in seen_nets]
209
210 self.assertIn(tenant.network.name, seen_names)
211 self.assertIn(tenant.network.id, seen_ids)
212
213 seen_subnets = [(n['id'], n['cidr'], n['network_id'])
214 for n in self._list_subnets()]
215 mysubnet = (tenant.subnet.id, tenant.subnet.cidr, tenant.network.id)
216 self.assertIn(mysubnet, seen_subnets)
217
218 seen_routers = self._list_routers()
219 seen_router_ids = [n['id'] for n in seen_routers]
220 seen_router_names = [n['name'] for n in seen_routers]
221
222 self.assertIn(tenant.router.name, seen_router_names)
223 self.assertIn(tenant.router.id, seen_router_ids)
224
225 myport = (tenant.router.id, tenant.subnet.id)
226 router_ports = [(i['device_id'], i['fixed_ips'][0]['subnet_id']) for i
Yair Frieddb6c9e92014-08-06 08:53:13 +0300227 in self._list_ports()
armando-migliacciobcfbbeb2014-08-11 18:33:47 -0700228 if self._is_router_port(i)]
Yair Fried4d7efa62013-11-17 17:12:29 +0200229
230 self.assertIn(myport, router_ports)
231
armando-migliacciobcfbbeb2014-08-11 18:33:47 -0700232 def _is_router_port(self, port):
233 """Return True if port is a router interface."""
234 # NOTE(armando-migliaccio): match device owner for both centralized
235 # and distributed routers; 'device_owner' is "" by default.
236 return port['device_owner'].startswith('network:router_interface')
237
Yair Fried4d7efa62013-11-17 17:12:29 +0200238 def _create_server(self, name, tenant, security_groups=None):
239 """
240 creates a server and assigns to security group
241 """
242 self._set_compute_context(tenant)
243 if security_groups is None:
Yair Frieddb6c9e92014-08-06 08:53:13 +0300244 security_groups = [tenant.security_groups['default']]
Ken'ichi Ohmichi1b3461e2014-12-02 03:41:07 +0000245 security_groups_names = [{'name': s['name']} for s in security_groups]
Yair Fried4d7efa62013-11-17 17:12:29 +0200246 create_kwargs = {
Dirk Mueller8cf79722014-09-12 17:37:15 +0200247 'networks': [
248 {'uuid': tenant.network.id},
Yair Fried4d7efa62013-11-17 17:12:29 +0200249 ],
Yair Frieddb6c9e92014-08-06 08:53:13 +0300250 'key_name': tenant.keypair['name'],
Ken'ichi Ohmichi1b3461e2014-12-02 03:41:07 +0000251 'security_groups': security_groups_names,
Itzik Brown2ca01cd2014-12-08 12:58:20 +0200252 'tenant_id': tenant.creds.tenant_id,
253 'network_client': tenant.manager.network_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200254 }
Claudiu Belufaa98912014-09-01 16:50:28 +0300255 server = self.create_server(name=name, create_kwargs=create_kwargs)
256 self.assertEqual(
257 sorted([s['name'] for s in security_groups]),
258 sorted([s['name'] for s in server['security_groups']]))
259 return server
Yair Fried4d7efa62013-11-17 17:12:29 +0200260
261 def _create_tenant_servers(self, tenant, num=1):
262 for i in range(num):
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000263 name = 'server-{tenant}-gen-{num}'.format(
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000264 tenant=tenant.creds.tenant_name,
Yair Fried4d7efa62013-11-17 17:12:29 +0200265 num=i
266 )
267 name = data_utils.rand_name(name)
268 server = self._create_server(name, tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200269 tenant.servers.append(server)
270
271 def _set_access_point(self, tenant):
272 """
273 creates a server in a secgroup with rule allowing external ssh
274 in order to access tenant internal network
275 workaround ip namespace
276 """
Yair Frieddb6c9e92014-08-06 08:53:13 +0300277 secgroups = tenant.security_groups.values()
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000278 name = 'server-{tenant}-access_point'.format(
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000279 tenant=tenant.creds.tenant_name)
Yair Fried4d7efa62013-11-17 17:12:29 +0200280 name = data_utils.rand_name(name)
281 server = self._create_server(name, tenant,
282 security_groups=secgroups)
Yair Fried4d7efa62013-11-17 17:12:29 +0200283 tenant.access_point = server
Yair Frieddb6c9e92014-08-06 08:53:13 +0300284 self._assign_floating_ips(tenant, server)
Yair Fried4d7efa62013-11-17 17:12:29 +0200285
Yair Frieddb6c9e92014-08-06 08:53:13 +0300286 def _assign_floating_ips(self, tenant, server):
Matthew Treinish6c072292014-01-29 19:15:52 +0000287 public_network_id = CONF.network.public_network_id
Yair Friedae0e73d2014-11-24 11:56:26 +0200288 floating_ip = self.create_floating_ip(
Yair Frieddb6c9e92014-08-06 08:53:13 +0300289 server, public_network_id,
290 client=tenant.manager.network_client)
291 self.floating_ips.setdefault(server['id'], floating_ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200292
293 def _create_tenant_network(self, tenant):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300294 network, subnet, router = self.create_networks(
295 client=tenant.manager.network_client)
Yair Friedbf2e2c42014-01-28 12:06:38 +0200296 tenant.set_network(network, subnet, router)
Yair Fried4d7efa62013-11-17 17:12:29 +0200297
298 def _set_compute_context(self, tenant):
Yair Frieddb6c9e92014-08-06 08:53:13 +0300299 self.servers_client = tenant.manager.servers_client
300 return self.servers_client
Yair Fried4d7efa62013-11-17 17:12:29 +0200301
302 def _deploy_tenant(self, tenant_or_id):
303 """
304 creates:
305 network
306 subnet
307 router (if public not defined)
308 access security group
309 access-point server
Yair Fried4d7efa62013-11-17 17:12:29 +0200310 """
311 if not isinstance(tenant_or_id, self.TenantProperties):
312 tenant = self.tenants[tenant_or_id]
Yair Fried4d7efa62013-11-17 17:12:29 +0200313 else:
314 tenant = tenant_or_id
Yair Fried4d7efa62013-11-17 17:12:29 +0200315 self._set_compute_context(tenant)
Yair Frieddb6c9e92014-08-06 08:53:13 +0300316 self._create_tenant_keypairs(tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200317 self._create_tenant_network(tenant)
318 self._create_tenant_security_groups(tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200319 self._set_access_point(tenant)
320
321 def _get_server_ip(self, server, floating=False):
Yair Friedbf2e2c42014-01-28 12:06:38 +0200322 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200323 returns the ip (floating/internal) of a server
Yair Friedbf2e2c42014-01-28 12:06:38 +0200324 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200325 if floating:
Yair Frieddb6c9e92014-08-06 08:53:13 +0300326 server_ip = self.floating_ips[server['id']].floating_ip_address
Yair Fried4d7efa62013-11-17 17:12:29 +0200327 else:
armando-migliacciod03f2642014-02-21 19:55:50 -0800328 server_ip = None
Yair Frieddb6c9e92014-08-06 08:53:13 +0300329 network_name = self.tenants[server['tenant_id']].network.name
330 if network_name in server['addresses']:
331 server_ip = server['addresses'][network_name][0]['addr']
armando-migliacciod03f2642014-02-21 19:55:50 -0800332 return server_ip
Yair Fried4d7efa62013-11-17 17:12:29 +0200333
334 def _connect_to_access_point(self, tenant):
335 """
336 create ssh connection to tenant access point
337 """
338 access_point_ssh = \
Yair Frieddb6c9e92014-08-06 08:53:13 +0300339 self.floating_ips[tenant.access_point['id']].floating_ip_address
340 private_key = tenant.keypair['private_key']
Yair Fried4d7efa62013-11-17 17:12:29 +0200341 access_point_ssh = self._ssh_to_server(access_point_ssh,
342 private_key=private_key)
343 return access_point_ssh
344
Yair Fried4d7efa62013-11-17 17:12:29 +0200345 def _check_connectivity(self, access_point, ip, should_succeed=True):
346 if should_succeed:
347 msg = "Timed out waiting for %s to become reachable" % ip
348 else:
Yair Fried4d7efa62013-11-17 17:12:29 +0200349 msg = "%s is reachable" % ip
Matthew Treinish53483132014-12-09 18:50:06 -0500350 self.assertTrue(self._check_remote_connectivity(access_point, ip,
351 should_succeed), msg)
Yair Fried4d7efa62013-11-17 17:12:29 +0200352
353 def _test_in_tenant_block(self, tenant):
354 access_point_ssh = self._connect_to_access_point(tenant)
355 for server in tenant.servers:
356 self._check_connectivity(access_point=access_point_ssh,
357 ip=self._get_server_ip(server),
358 should_succeed=False)
359
360 def _test_in_tenant_allow(self, tenant):
361 ruleset = dict(
362 protocol='icmp',
363 remote_group_id=tenant.security_groups['default'].id,
364 direction='ingress'
365 )
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000366 self._create_security_group_rule(
Yair Fried4d7efa62013-11-17 17:12:29 +0200367 secgroup=tenant.security_groups['default'],
368 **ruleset
369 )
370 access_point_ssh = self._connect_to_access_point(tenant)
371 for server in tenant.servers:
372 self._check_connectivity(access_point=access_point_ssh,
373 ip=self._get_server_ip(server))
Yair Fried4d7efa62013-11-17 17:12:29 +0200374
375 def _test_cross_tenant_block(self, source_tenant, dest_tenant):
Yair Friedbf2e2c42014-01-28 12:06:38 +0200376 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200377 if public router isn't defined, then dest_tenant access is via
378 floating-ip
Yair Friedbf2e2c42014-01-28 12:06:38 +0200379 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200380 access_point_ssh = self._connect_to_access_point(source_tenant)
381 ip = self._get_server_ip(dest_tenant.access_point,
382 floating=self.floating_ip_access)
383 self._check_connectivity(access_point=access_point_ssh, ip=ip,
384 should_succeed=False)
385
386 def _test_cross_tenant_allow(self, source_tenant, dest_tenant):
Yair Friedbf2e2c42014-01-28 12:06:38 +0200387 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200388 check for each direction:
389 creating rule for tenant incoming traffic enables only 1way traffic
Yair Friedbf2e2c42014-01-28 12:06:38 +0200390 """
Yair Fried4d7efa62013-11-17 17:12:29 +0200391 ruleset = dict(
392 protocol='icmp',
393 direction='ingress'
394 )
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000395 self._create_security_group_rule(
Yair Fried4d7efa62013-11-17 17:12:29 +0200396 secgroup=dest_tenant.security_groups['default'],
Yair Frieddb6c9e92014-08-06 08:53:13 +0300397 client=dest_tenant.manager.network_client,
Yair Fried4d7efa62013-11-17 17:12:29 +0200398 **ruleset
399 )
Yair Friedbf2e2c42014-01-28 12:06:38 +0200400 access_point_ssh = self._connect_to_access_point(source_tenant)
401 ip = self._get_server_ip(dest_tenant.access_point,
402 floating=self.floating_ip_access)
403 self._check_connectivity(access_point_ssh, ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200404
Yair Friedbf2e2c42014-01-28 12:06:38 +0200405 # test that reverse traffic is still blocked
406 self._test_cross_tenant_block(dest_tenant, source_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200407
Yair Friedbf2e2c42014-01-28 12:06:38 +0200408 # allow reverse traffic and check
Matthew Treinishb7144eb2013-12-13 22:57:35 +0000409 self._create_security_group_rule(
Yair Friedbf2e2c42014-01-28 12:06:38 +0200410 secgroup=source_tenant.security_groups['default'],
Yair Frieddb6c9e92014-08-06 08:53:13 +0300411 client=source_tenant.manager.network_client,
Yair Friedbf2e2c42014-01-28 12:06:38 +0200412 **ruleset
413 )
Yair Fried4d7efa62013-11-17 17:12:29 +0200414
Yair Friedbf2e2c42014-01-28 12:06:38 +0200415 access_point_ssh_2 = self._connect_to_access_point(dest_tenant)
416 ip = self._get_server_ip(source_tenant.access_point,
417 floating=self.floating_ip_access)
418 self._check_connectivity(access_point_ssh_2, ip)
Yair Fried4d7efa62013-11-17 17:12:29 +0200419
420 def _verify_mac_addr(self, tenant):
421 """
422 verify that VM (tenant's access point) has the same ip,mac as listed in
423 port list
424 """
425 access_point_ssh = self._connect_to_access_point(tenant)
426 mac_addr = access_point_ssh.get_mac_address()
427 mac_addr = mac_addr.strip().lower()
Henry Gessau78ab4b02014-03-31 15:10:13 -0400428 # Get the fixed_ips and mac_address fields of all ports. Select
429 # only those two columns to reduce the size of the response.
Yair Frieddb6c9e92014-08-06 08:53:13 +0300430 port_list = self._list_ports(fields=['fixed_ips', 'mac_address'])
Yair Fried4d7efa62013-11-17 17:12:29 +0200431 port_detail_list = [
432 (port['fixed_ips'][0]['subnet_id'],
433 port['fixed_ips'][0]['ip_address'],
Henry Gessau78ab4b02014-03-31 15:10:13 -0400434 port['mac_address'].lower())
435 for port in port_list if port['fixed_ips']
Yair Fried4d7efa62013-11-17 17:12:29 +0200436 ]
437 server_ip = self._get_server_ip(tenant.access_point)
438 subnet_id = tenant.subnet.id
439 self.assertIn((subnet_id, server_ip, mac_addr), port_detail_list)
440
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900441 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800442 @test.idempotent_id('e79f879e-debb-440c-a7e4-efeda05b6848')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900443 @test.services('compute', 'network')
Yair Fried4d7efa62013-11-17 17:12:29 +0200444 def test_cross_tenant_traffic(self):
Yair Fried79b0a912014-10-20 11:15:37 +0300445 if not self.isolated_creds.is_multi_tenant():
446 raise self.skipException("No secondary tenant defined")
Nachi Ueno26b4c972014-01-17 06:15:13 -0800447 try:
Yair Friedbf2e2c42014-01-28 12:06:38 +0200448 # deploy new tenant
449 self._deploy_tenant(self.alt_tenant)
450 self._verify_network_details(self.alt_tenant)
451 self._verify_mac_addr(self.alt_tenant)
Yair Fried4d7efa62013-11-17 17:12:29 +0200452
Nachi Ueno26b4c972014-01-17 06:15:13 -0800453 # cross tenant check
Yair Friedbf2e2c42014-01-28 12:06:38 +0200454 source_tenant = self.primary_tenant
Nachi Ueno26b4c972014-01-17 06:15:13 -0800455 dest_tenant = self.alt_tenant
456 self._test_cross_tenant_block(source_tenant, dest_tenant)
457 self._test_cross_tenant_allow(source_tenant, dest_tenant)
458 except Exception:
Yair Friedbf2e2c42014-01-28 12:06:38 +0200459 for tenant in self.tenants.values():
460 self._log_console_output(servers=tenant.servers)
461 raise
462
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900463 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800464 @test.idempotent_id('63163892-bbf6-4249-aa12-d5ea1f8f421b')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900465 @test.services('compute', 'network')
Yair Friedbf2e2c42014-01-28 12:06:38 +0200466 def test_in_tenant_traffic(self):
467 try:
468 self._create_tenant_servers(self.primary_tenant, num=1)
469
470 # in-tenant check
471 self._test_in_tenant_block(self.primary_tenant)
472 self._test_in_tenant_allow(self.primary_tenant)
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800473 except Exception:
474 for tenant in self.tenants.values():
475 self._log_console_output(servers=tenant.servers)
476 raise
Yair Friedbf2e2c42014-01-28 12:06:38 +0200477
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800478 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800479 @test.idempotent_id('f4d556d7-1526-42ad-bafb-6bebf48568f6')
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800480 @test.services('compute', 'network')
481 def test_port_update_new_security_group(self):
482 """
483 This test verifies the traffic after updating the vm port with new
Fei Long Wang50131ee2015-02-02 16:58:24 +1300484 security group having appropriate rule.
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800485 """
486 new_tenant = self.primary_tenant
487
488 # Create empty security group and add icmp rule in it
489 new_sg = self._create_empty_security_group(
490 namestart='secgroup_new-',
491 tenant_id=new_tenant.creds.tenant_id,
492 client=new_tenant.manager.network_client)
493 icmp_rule = dict(
494 protocol='icmp',
495 direction='ingress',
496 )
497 self._create_security_group_rule(
498 secgroup=new_sg,
499 client=new_tenant.manager.network_client,
500 **icmp_rule)
501 new_tenant.security_groups.update(new_sg=new_sg)
502
503 # Create server with default security group
Ken'ichi Ohmichi6ded8df2015-03-23 02:00:19 +0000504 name = 'server-{tenant}-gen-1'.format(
Rajkumar Thiyagarajand9e964a2014-12-17 01:55:52 -0800505 tenant=new_tenant.creds.tenant_name
506 )
507 name = data_utils.rand_name(name)
508 server = self._create_server(name, new_tenant)
509
510 # Check connectivity failure with default security group
511 try:
512 access_point_ssh = self._connect_to_access_point(new_tenant)
513 self._check_connectivity(access_point=access_point_ssh,
514 ip=self._get_server_ip(server),
515 should_succeed=False)
516 server_id = server['id']
517 port_id = self._list_ports(device_id=server_id)[0]['id']
518
519 # update port with new security group and check connectivity
520 self.network_client.update_port(port_id, security_groups=[
521 new_tenant.security_groups['new_sg'].id])
522 self._check_connectivity(
523 access_point=access_point_ssh,
524 ip=self._get_server_ip(server))
Yair Friedbf2e2c42014-01-28 12:06:38 +0200525 except Exception:
526 for tenant in self.tenants.values():
527 self._log_console_output(servers=tenant.servers)
Nachi Ueno26b4c972014-01-17 06:15:13 -0800528 raise
prdsilva8b733ad2014-12-09 02:54:49 -0800529
530 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800531 @test.idempotent_id('d2f77418-fcc4-439d-b935-72eca704e293')
prdsilva8b733ad2014-12-09 02:54:49 -0800532 @test.services('compute', 'network')
533 def test_multiple_security_groups(self):
534 """
535 This test verifies multiple security groups and checks that rules
536 provided in the both the groups is applied onto VM
537 """
538 tenant = self.primary_tenant
539 ip = self._get_server_ip(tenant.access_point,
540 floating=self.floating_ip_access)
541 ssh_login = CONF.compute.image_ssh_user
542 private_key = tenant.keypair['private_key']
543 self.check_vm_connectivity(ip,
544 should_connect=False)
545 ruleset = dict(
546 protocol='icmp',
547 direction='ingress'
548 )
549 self._create_security_group_rule(
550 secgroup=tenant.security_groups['default'],
551 **ruleset
552 )
553 """
554 Vm now has 2 security groups one with ssh rule(
555 already added in setUp() method),and other with icmp rule
556 (added in the above step).The check_vm_connectivity tests
557 -that vm ping test is successful
558 -ssh to vm is successful
559 """
560 self.check_vm_connectivity(ip,
561 username=ssh_login,
562 private_key=private_key,
563 should_connect=True)