| 1 |
# Copyright 2016 |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import netaddr |
| 16 |
|
| 17 |
from tempest.common import utils |
| 18 |
from tempest import config |
| 19 |
from tempest.lib.common.utils import data_utils |
| 20 |
from tempest.lib.common.utils import test_utils |
| 21 |
from tempest.lib import decorators |
| 22 |
from tempest.lib import exceptions as lib_exc |
| 23 |
|
| 24 |
from neutron_tempest_plugin.fwaas.api import fwaas_v2_base as v2_base |
| 25 |
|
| 26 |
|
| 27 |
CONF = config.CONF |
| 28 |
DEFAULT_FWG = 'default' |
| 29 |
|
| 30 |
|
| 31 |
class FWaaSv2ExtensionTestJSON(v2_base.BaseFWaaSTest): |
| 32 |
|
| 33 |
"""List of tests |
| 34 |
|
| 35 |
Tests the following operations in the Neutron API using the REST client |
| 36 |
for Neutron: |
| 37 |
|
| 38 |
List firewall rules |
| 39 |
Create firewall rule |
| 40 |
Update firewall rule |
| 41 |
Delete firewall rule |
| 42 |
Show firewall rule |
| 43 |
List firewall policies |
| 44 |
Create firewall policy |
| 45 |
Update firewall policy |
| 46 |
Insert firewall rule to policy |
| 47 |
Remove firewall rule from policy |
| 48 |
Insert firewall rule after/before rule in policy |
| 49 |
Update firewall policy audited attribute |
| 50 |
Delete firewall policy |
| 51 |
Show firewall policy |
| 52 |
List firewall group |
| 53 |
Create firewall group |
| 54 |
Update firewall group |
| 55 |
Delete firewall group |
| 56 |
Show firewall group |
| 57 |
""" |
| 58 |
|
| 59 |
@classmethod |
| 60 |
def resource_setup(cls): |
| 61 |
super(FWaaSv2ExtensionTestJSON, cls).resource_setup() |
| 62 |
if not utils.is_extension_enabled('fwaas_v2', 'network'): |
| 63 |
msg = "FWaaS v2 Extension not enabled." |
| 64 |
raise cls.skipException(msg) |
| 65 |
|
| 66 |
def setUp(self): |
| 67 |
super(FWaaSv2ExtensionTestJSON, self).setUp() |
| 68 |
self.fw_rule_1 = self.create_firewall_rule(action="allow", |
| 69 |
protocol="tcp") |
| 70 |
self.fw_rule_2 = self.create_firewall_rule(action="deny", |
| 71 |
protocol="udp") |
| 72 |
self.fw_policy_1 = self.create_firewall_policy( |
| 73 |
firewall_rules=[self.fw_rule_1['id']]) |
| 74 |
self.fw_policy_2 = self.create_firewall_policy( |
| 75 |
firewall_rules=[self.fw_rule_2['id']]) |
| 76 |
|
| 77 |
def _create_router_interfaces(self): |
| 78 |
network_1 = self.create_network() |
| 79 |
network_2 = self.create_network() |
| 80 |
|
| 81 |
cidr = netaddr.IPNetwork(CONF.network.project_network_cidr) |
| 82 |
mask_bits = CONF.network.project_network_mask_bits |
| 83 |
|
| 84 |
subnet_cidr_1 = list(cidr.subnet(mask_bits))[-1] |
| 85 |
subnet_cidr_2 = list(cidr.subnet(mask_bits))[-2] |
| 86 |
subnet_1 = self.create_subnet(network_1, cidr=subnet_cidr_1, |
| 87 |
mask_bits=mask_bits) |
| 88 |
subnet_2 = self.create_subnet(network_2, cidr=subnet_cidr_2, |
| 89 |
mask_bits=mask_bits) |
| 90 |
|
| 91 |
router = self.create_router( |
| 92 |
data_utils.rand_name('router-'), |
| 93 |
admin_state_up=True) |
| 94 |
self.addCleanup(self._try_delete_router, router) |
| 95 |
|
| 96 |
intf_1 = self.routers_client.add_router_interface(router['id'], |
| 97 |
subnet_id=subnet_1['id']) |
| 98 |
intf_2 = self.routers_client.add_router_interface(router['id'], |
| 99 |
subnet_id=subnet_2['id']) |
| 100 |
|
| 101 |
return intf_1, intf_2 |
| 102 |
|
| 103 |
def _try_delete_router(self, router): |
| 104 |
# delete router, if it exists |
| 105 |
try: |
| 106 |
self.delete_router(router) |
| 107 |
# if router is not found, this means it was deleted in the test |
| 108 |
except lib_exc.NotFound: |
| 109 |
pass |
| 110 |
|
| 111 |
def _try_delete_policy(self, policy_id): |
| 112 |
# delete policy, if it exists |
| 113 |
try: |
| 114 |
self.firewall_policies_client.delete_firewall_policy(policy_id) |
| 115 |
# if policy is not found, this means it was deleted in the test |
| 116 |
except lib_exc.NotFound: |
| 117 |
pass |
| 118 |
|
| 119 |
def _try_delete_rule(self, rule_id): |
| 120 |
# delete rule, if it exists |
| 121 |
try: |
| 122 |
self.firewall_rules_client.delete_firewall_rule(rule_id) |
| 123 |
# if rule is not found, this means it was deleted in the test |
| 124 |
except lib_exc.NotFound: |
| 125 |
pass |
| 126 |
|
| 127 |
def _try_delete_firewall_group(self, fwg_id): |
| 128 |
# delete firewall group, if it exists |
| 129 |
try: |
| 130 |
self.firewall_groups_client.delete_firewall_group(fwg_id) |
| 131 |
# if firewall group is not found, this means it was deleted in the test |
| 132 |
except lib_exc.NotFound: |
| 133 |
pass |
| 134 |
|
| 135 |
self.firewall_groups_client.wait_for_resource_deletion(fwg_id) |
| 136 |
|
| 137 |
def _wait_until_ready(self, fwg_id): |
| 138 |
target_states = ('ACTIVE', 'CREATED') |
| 139 |
|
| 140 |
def _wait(): |
| 141 |
firewall_group = self.firewall_groups_client.show_firewall_group( |
| 142 |
fwg_id) |
| 143 |
firewall_group = firewall_group['firewall_group'] |
| 144 |
return firewall_group['status'] in target_states |
| 145 |
|
| 146 |
if not test_utils.call_until_true(_wait, CONF.network.build_timeout, |
| 147 |
CONF.network.build_interval): |
| 148 |
m = ("Timed out waiting for firewall_group %s to reach %s " |
| 149 |
"state(s)" % |
| 150 |
(fwg_id, target_states)) |
| 151 |
raise lib_exc.TimeoutException(m) |
| 152 |
|
| 153 |
def _wait_until_deleted(self, fwg_id): |
| 154 |
def _wait(): |
| 155 |
try: |
| 156 |
fwg = self.firewall_groups_client.show_firewall_group(fwg_id) |
| 157 |
except lib_exc.NotFound: |
| 158 |
return True |
| 159 |
|
| 160 |
fwg_status = fwg['firewall_group']['status'] |
| 161 |
if fwg_status == 'ERROR': |
| 162 |
raise lib_exc.DeleteErrorException(resource_id=fwg_id) |
| 163 |
|
| 164 |
if not test_utils.call_until_true(_wait, CONF.network.build_timeout, |
| 165 |
CONF.network.build_interval): |
| 166 |
m = ("Timed out waiting for firewall_group %s deleted" % fwg_id) |
| 167 |
raise lib_exc.TimeoutException(m) |
| 168 |
|
| 169 |
@decorators.idempotent_id('ddccfa87-4af7-48a6-9e50-0bd0ad1348cb') |
| 170 |
def test_list_firewall_rules(self): |
| 171 |
# List firewall rules |
| 172 |
fw_rules = self.firewall_rules_client.list_firewall_rules() |
| 173 |
fw_rules = fw_rules['firewall_rules'] |
| 174 |
self.assertIn((self.fw_rule_1['id'], |
| 175 |
self.fw_rule_1['name'], |
| 176 |
self.fw_rule_1['action'], |
| 177 |
self.fw_rule_1['protocol'], |
| 178 |
self.fw_rule_1['ip_version'], |
| 179 |
self.fw_rule_1['enabled']), |
| 180 |
[(m['id'], |
| 181 |
m['name'], |
| 182 |
m['action'], |
| 183 |
m['protocol'], |
| 184 |
m['ip_version'], |
| 185 |
m['enabled']) for m in fw_rules]) |
| 186 |
|
| 187 |
@decorators.idempotent_id('ffc009fa-cd17-4029-8025-c4b81a7dd923') |
| 188 |
def test_create_update_delete_firewall_rule(self): |
| 189 |
# Create firewall rule |
| 190 |
body = self.firewall_rules_client.create_firewall_rule( |
| 191 |
name=data_utils.rand_name("fw-rule"), |
| 192 |
action="allow", |
| 193 |
protocol="tcp") |
| 194 |
fw_rule_id = body['firewall_rule']['id'] |
| 195 |
self.addCleanup(self._try_delete_rule, fw_rule_id) |
| 196 |
|
| 197 |
# Update firewall rule |
| 198 |
body = self.firewall_rules_client.update_firewall_rule(fw_rule_id, |
| 199 |
action="deny") |
| 200 |
self.assertEqual("deny", body["firewall_rule"]['action']) |
| 201 |
|
| 202 |
# Delete firewall rule |
| 203 |
self.firewall_rules_client.delete_firewall_rule(fw_rule_id) |
| 204 |
# Confirm deletion |
| 205 |
fw_rules = self.firewall_rules_client.list_firewall_rules() |
| 206 |
self.assertNotIn(fw_rule_id, |
| 207 |
[m['id'] for m in fw_rules['firewall_rules']]) |
| 208 |
|
| 209 |
@decorators.idempotent_id('76b07afc-444e-4bb9-abec-9b8c5f994dcd') |
| 210 |
def test_show_firewall_rule(self): |
| 211 |
# show a created firewall rule |
| 212 |
fw_rule = self.firewall_rules_client.show_firewall_rule( |
| 213 |
self.fw_rule_1['id']) |
| 214 |
for key, value in fw_rule['firewall_rule'].items(): |
| 215 |
if key != 'firewall_policy_id': |
| 216 |
self.assertEqual(self.fw_rule_1[key], value) |
| 217 |
# This check is placed because we cannot modify policy during |
| 218 |
# Create/Update of Firewall Rule but we can see the association |
| 219 |
# of a Firewall Rule with the policies it belongs to. |
| 220 |
|
| 221 |
@decorators.idempotent_id('f6b83902-746f-4e74-9403-2ec9899583a3') |
| 222 |
def test_list_firewall_policies(self): |
| 223 |
fw_policies = self.firewall_policies_client.list_firewall_policies() |
| 224 |
fw_policies = fw_policies['firewall_policies'] |
| 225 |
self.assertIn((self.fw_policy_1['id'], |
| 226 |
self.fw_policy_1['name'], |
| 227 |
self.fw_policy_1['firewall_rules']), |
| 228 |
[(m['id'], |
| 229 |
m['name'], |
| 230 |
m['firewall_rules']) for m in fw_policies]) |
| 231 |
|
| 232 |
@decorators.idempotent_id('6ef9bd02-7349-4d61-8d1f-80479f64d904') |
| 233 |
def test_create_update_delete_firewall_policy(self): |
| 234 |
# Create firewall policy |
| 235 |
body = self.firewall_policies_client.create_firewall_policy( |
| 236 |
name=data_utils.rand_name("fw-policy")) |
| 237 |
fw_policy_id = body['firewall_policy']['id'] |
| 238 |
self.addCleanup(self._try_delete_policy, fw_policy_id) |
| 239 |
|
| 240 |
# Update firewall policy |
| 241 |
body = self.firewall_policies_client.update_firewall_policy( |
| 242 |
fw_policy_id, |
| 243 |
name="updated_policy") |
| 244 |
updated_fw_policy = body["firewall_policy"] |
| 245 |
self.assertEqual("updated_policy", updated_fw_policy['name']) |
| 246 |
|
| 247 |
# Delete firewall policy |
| 248 |
self.firewall_policies_client.delete_firewall_policy(fw_policy_id) |
| 249 |
# Confirm deletion |
| 250 |
fw_policies = self.firewall_policies_client.list_firewall_policies() |
| 251 |
fw_policies = fw_policies['firewall_policies'] |
| 252 |
self.assertNotIn(fw_policy_id, [m['id'] for m in fw_policies]) |
| 253 |
|
| 254 |
@decorators.idempotent_id('164381de-61f4-483f-9a5a-48105b8e70e2') |
| 255 |
def test_show_firewall_policy(self): |
| 256 |
# show a created firewall policy |
| 257 |
fw_policy = self.firewall_policies_client.show_firewall_policy( |
| 258 |
self.fw_policy_1['id']) |
| 259 |
fw_policy = fw_policy['firewall_policy'] |
| 260 |
for key, value in fw_policy.items(): |
| 261 |
self.assertEqual(self.fw_policy_1[key], value) |
| 262 |
|
| 263 |
@decorators.idempotent_id('48dfcd75-3924-479d-bb65-b3ed33397663') |
| 264 |
def test_create_show_delete_firewall_group(self): |
| 265 |
# create router and add interfaces |
| 266 |
intf_1, intf_2 = self._create_router_interfaces() |
| 267 |
|
| 268 |
# Create firewall_group |
| 269 |
body = self.firewall_groups_client.create_firewall_group( |
| 270 |
name=data_utils.rand_name("firewall_group"), |
| 271 |
ingress_firewall_policy_id=self.fw_policy_1['id'], |
| 272 |
egress_firewall_policy_id=self.fw_policy_2['id'], |
| 273 |
ports=[intf_1['port_id'], intf_2['port_id']]) |
| 274 |
created_firewall_group = body['firewall_group'] |
| 275 |
fwg_id = created_firewall_group['id'] |
| 276 |
|
| 277 |
# Wait for the firewall resource to become ready |
| 278 |
self._wait_until_ready(fwg_id) |
| 279 |
|
| 280 |
# show created firewall_group |
| 281 |
firewall_group = self.firewall_groups_client.show_firewall_group( |
| 282 |
fwg_id) |
| 283 |
fwg = firewall_group['firewall_group'] |
| 284 |
|
| 285 |
for key, value in fwg.items(): |
| 286 |
if key == 'status': |
| 287 |
continue |
| 288 |
self.assertEqual(created_firewall_group[key], value) |
| 289 |
|
| 290 |
# list firewall_groups |
| 291 |
firewall_groups = self.firewall_groups_client.list_firewall_groups() |
| 292 |
fwgs = firewall_groups['firewall_groups'] |
| 293 |
self.assertIn((created_firewall_group['id'], |
| 294 |
created_firewall_group['name'], |
| 295 |
created_firewall_group['ingress_firewall_policy_id'], |
| 296 |
created_firewall_group['egress_firewall_policy_id']), |
| 297 |
[(m['id'], |
| 298 |
m['name'], |
| 299 |
m['ingress_firewall_policy_id'], |
| 300 |
m['egress_firewall_policy_id']) for m in fwgs]) |
| 301 |
|
| 302 |
# Disassociate all port with this firewall group |
| 303 |
self.firewall_groups_client.update_firewall_group(fwg_id, ports=[]) |
| 304 |
# Delete firewall_group |
| 305 |
self.firewall_groups_client.delete_firewall_group(fwg_id) |
| 306 |
|
| 307 |
# Wait for the firewall group to be deleted |
| 308 |
self._wait_until_deleted(fwg_id) |
| 309 |
|
| 310 |
# Confirm deletion |
| 311 |
firewall_groups = self.firewall_groups_client.list_firewall_groups() |
| 312 |
fwgs = firewall_groups['firewall_groups'] |
| 313 |
self.assertNotIn(fwg_id, [m['id'] for m in fwgs]) |
| 314 |
|
| 315 |
@decorators.idempotent_id('e021baab-d4f7-4bad-b382-bde4946e0e0b') |
| 316 |
def test_update_firewall_group(self): |
| 317 |
# create router and add interfaces |
| 318 |
intf_1, intf_2 = self._create_router_interfaces() |
| 319 |
|
| 320 |
# Create firewall_group |
| 321 |
body = self.firewall_groups_client.create_firewall_group( |
| 322 |
name=data_utils.rand_name("firewall_group"), |
| 323 |
ingress_firewall_policy_id=self.fw_policy_1['id'], |
| 324 |
egress_firewall_policy_id=self.fw_policy_2['id'], |
| 325 |
ports=[intf_1['port_id']]) |
| 326 |
created_firewall_group = body['firewall_group'] |
| 327 |
fwg_id = created_firewall_group['id'] |
| 328 |
self.addCleanup(self._try_delete_firewall_group, fwg_id) |
| 329 |
|
| 330 |
# Wait for the firewall resource to become ready |
| 331 |
self._wait_until_ready(fwg_id) |
| 332 |
|
| 333 |
# Update firewall group |
| 334 |
body = self.firewall_groups_client.update_firewall_group( |
| 335 |
fwg_id, |
| 336 |
ports=[intf_2['port_id']]) |
| 337 |
updated_fwg = body["firewall_group"] |
| 338 |
self.assertEqual([intf_2['port_id']], updated_fwg['ports']) |
| 339 |
# Wait for the firewall resource to become ready |
| 340 |
self._wait_until_ready(fwg_id) |
| 341 |
# Disassociate all ports with this firewall group |
| 342 |
self.firewall_groups_client.update_firewall_group(fwg_id, ports=[]) |
| 343 |
# Delete firewall_group |
| 344 |
self.firewall_groups_client.delete_firewall_group(fwg_id) |
| 345 |
|
| 346 |
@decorators.idempotent_id('a1f524d8-5336-4769-aa7b-0830bb4df6c8') |
| 347 |
def test_error_on_create_firewall_group_name_default(self): |
| 348 |
try: |
| 349 |
# Create firewall_group with name == reserved default fwg |
| 350 |
self.firewall_groups_client.create_firewall_group( |
| 351 |
name=DEFAULT_FWG) |
| 352 |
except lib_exc.Conflict: |
| 353 |
pass |
| 354 |
|
| 355 |
@decorators.idempotent_id('fd24db16-c8cb-4cb4-ba60-b0cd18a66b83') |
| 356 |
def test_default_fwg_created_on_list_firewall_groups(self): |
| 357 |
fw_groups = self.firewall_groups_client.list_firewall_groups() |
| 358 |
fw_groups = fw_groups['firewall_groups'] |
| 359 |
self.assertIn(DEFAULT_FWG, |
| 360 |
[g['name'] for g in fw_groups]) |