Files
group-based-policy/gbpservice/neutron/tests/unit/common.py
Sumit Naiksatam 725c7aa466 Newton sync
* The IP addresses handed by Neutron’s ipam are no longer sequential per the
following commit:
dcb2a931b5

Several UTs were making sequential assignment assumptions and were expecting
specific IP address allocations. These had to be refactored appropriately by
checking if the assigned IP address belongs to the expected CIDR.

* There was a bug in Neutron until stable/mitaka which prevented duplicate SG
rules being added. Since that is fixed in stable/newton overlapping SG rules
can be added within the same tenant, see:
3c1a068c7a
We actually dont want to add overlapping rules in the resource_mapping driver,
hence a check was added to prevent adding of duplicate rules.

* The unit test discovery path is being set to "gbpservice/neutron" in
.testr.conf to avoid running the tests in gbpservice/contrib path which
currently have some NFP related tests. The path can be reverted back to
“gbpservice” once the contrib code has been updated.

* There is a bug in the neutron code which always requires passing the filters
argument (even if empty) to get_sg_rules() call.

* The flavors service plugin needs to be explicitly configured in the UTs:
0e3f4b8335

* The use of unittest has been migrated to using unittest2.

* The default tenant in the Neutron UTs is no longer ‘test-tenant’. Instead the
following constant should be used:
neutron.tests.unit.db.test_db_base_plugin_v2.TEST_TENANT_ID

* The project_id is now being added to the resource by the API layer. The
extension test cases had to be updated to accommodate for this extra
argument.

* Neutron now sends DHCP and Nova notifications for operations on resources
from the ML2 plugin. See the following relevant commits in Neutron:
181bdb374f
a5cd3b65d1
877778ee4c
a4df99ff5d

With the above changes, it is no longer needed for GBP to send DHCP and Nova
notifications (previously being sent from local_api.py). The neutron_resource
and the aim_mapping drivers, which attempt to provide transactional semantics,
still need the queueing functionality on the notification framework, so this is
being preserved. The send_or_queue_notification method from this framework is
also being preserved since the aim_mapping driver makes use of this method to
send specific notifications which are outside the scope of the notifications
that Neutron sends. When the ML2Plus plugin is used for the aim_mapping driver,
Neutron’s registry notification is patched to allow the notification to be
queued. It should be noted that at this point, some notifications cannot be
queued since for some resources the existence checks fail if they are queued
and sent at a later time. In such cases, the notifications are sent
immediately. This logic needs to be revisited.

* The _get_tenant_id_for_create() method was removed in Neutron since the
context object provides the project_id:
5d53dfb8d6

GBP should also follow this approach, however its a big change, mostly in the
UTs. So this patch temporarily adds the _get_tenant_id_for_create() method to
the GBP service plugins.

* The patch for create_floatingip in gbpservice/neutron/extensions/patch.py is
no longer needed and is being removed. Other such methods in the module
cannot be blindly removed, so for now, they have been updated to sync with
their newton version, but should be revisited to explore their removal.

* The ml2_network_segments table was renamed to network segments, and the
allowed_address_pair definition was moved in neutron:
c8fca1c96f
7c0f189309

* DB objects are being detached from the session when the extension attribute
processing happens. However, ml2plus needs the session context in the
extend_dict functions. Hence, a utility function was added in
gbpservice/neutron/plugins/ml2plus/patch_neutron.py to get the currently
active session.

* The following change adds a transaction guard to some operations which
prevent then from being called from within a transaction:
https://review.openstack.org/gitweb?p=openstack/neutron.git;a=commitdiff;h=afe1a834000d33900b8646d308fa26fa807a2ca0

ml2plus however needs to support calling these operations from within a
transaction. Hence the transaction guard is disabled by use of a decorator on
the ml2plus functions.

* Neutron defines a new dns-integration extension and all the DB related DNS
handling was moved out of the DB core plugin:
64f5fc8259

* Retry decorator has been added to ml2plus methods to align with the
following:
acbabaa3db
09c87425fa

* A bug in the aim_mapping log statements was fixed by using vars() to displace
aim resource dictionary attributes.

* A bug was fixed in the test_apic_aim code, where the tests set the expected
value of dns_name to None, but the implementation sets it to ‘’.

* The following changes were made to move things to neutron_lib and have been
refactored in this patch:

** neutron.db.model_base was moved to neutron_lib:
61cc14fd67

** neutron.common.exceptions was moved to neutron_lib, and has been refactored
here.

** The converter and validator functions in neutron.api.v2.attributes were
moved to neutron_lib and has been refactored here.

** Constants like ATTR_NOT_SPECIFIED have been moved from
neutron.api.v2.attributes to neutron_lib and has been refactored here.

Note that the integration tests fail in this patch since the DB schema needs to
be updated to rename the tenant_id column to project_id. This is being done in
the dependent patch, and the integration tests should be validated on that
patch.

Follow up items:

* The following test fails sporadically:
gbpservice.neutron.tests.unit.services.grouppolicy.test_aim_mapping_driver.TestNeutronPortOperation.test_gbp_details_for_allowed_address_pair
* Some hacking directives have been disabled and need to enabled but will
* require significant code refactoring.
* HasId and HasTenant are deprecated, move to HasProject and HasId in
* model_base, see commit:
61cc14fd67 (diff-b923b82d6a7b3c5cd77c32354ffc9f13)
* A couple of UTs are being skipped in:
* gbpservice/neutron/tests/unit/plugins/ml2plus/test_extension_driver_api.py
* and need to updated per the comments in the code.

Change-Id: I887ee6cfca8199710cf5c653b5f57dff86bb035a
2017-03-17 01:35:29 -07:00

570 lines
15 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_utils import uuidutils
_uuid = uuidutils.generate_uuid
def gbp_attributes(func):
def inner(**kwargs):
attrs = func()
attrs.update(kwargs)
project_id = _uuid()
if 'prj' in func.__name__ or 'default' not in func.__name__ and (
'update' not in func.__name__):
attrs.update({'project_id': project_id, 'tenant_id': project_id})
return attrs
return inner
@gbp_attributes
def get_create_policy_target_default_attrs():
return {'name': '', 'description': '', 'policy_target_group_id': None,
'cluster_id': ''}
@gbp_attributes
def get_create_policy_target_attrs():
return {'name': 'ep1', 'policy_target_group_id': _uuid(),
'description': 'test policy_target',
'cluster_id': 'some_cluster_id'}
@gbp_attributes
def get_update_policy_target_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_application_policy_group_default_attrs():
return {'name': '', 'description': '', 'shared': False}
@gbp_attributes
def get_create_application_policy_group_attrs():
return {'name': 'apg1', 'tenant_id': _uuid(),
'description': 'test application_policy_group',
'shared': False}
@gbp_attributes
def get_update_application_policy_group_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_policy_target_group_default_attrs():
return {'name': '', 'description': '', 'l2_policy_id': None,
'application_policy_group_id': None,
'provided_policy_rule_sets': {},
'consumed_policy_rule_sets': {},
'network_service_policy_id': None, 'shared': False,
'service_management': False}
@gbp_attributes
def get_create_policy_target_group_attrs():
return {'name': 'ptg1',
'description': 'test policy_target group',
'l2_policy_id': _uuid(),
'application_policy_group_id': _uuid(),
'provided_policy_rule_sets': {_uuid(): None},
'consumed_policy_rule_sets': {_uuid(): None},
'network_service_policy_id': _uuid(),
'shared': False, 'service_management': False}
@gbp_attributes
def get_update_policy_target_group_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_l2_policy_default_attrs():
return {'name': '', 'description': '', 'shared': False,
'inject_default_route': True}
@gbp_attributes
def get_create_l2_policy_attrs():
return {'name': 'l2p1',
'description': 'test L2 policy', 'l3_policy_id': _uuid(),
'inject_default_route': True, 'shared': False}
@gbp_attributes
def get_update_l2_policy_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_l3_policy_default_attrs():
return {'name': '', 'description': '', 'ip_version': 4,
'ip_pool': '10.0.0.0/8', 'subnet_prefix_length': 24,
'external_segments': {}, 'shared': False}
@gbp_attributes
def get_create_l3_policy_attrs():
return {'name': 'l3p1',
'description': 'test L3 policy', 'ip_version': 6,
'ip_pool': 'fd01:2345:6789::/48',
'external_segments': {_uuid(): ['192.168.0.3']},
'subnet_prefix_length': 64, 'shared': False}
@gbp_attributes
def get_update_l3_policy_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_policy_action_default_attrs():
return {'name': '',
'description': '',
'action_type': 'allow',
'action_value': None,
'shared': False}
@gbp_attributes
def get_create_policy_action_attrs():
return {'name': 'pa1',
'description': 'test policy action',
'action_type': 'redirect',
'action_value': _uuid(),
'shared': False}
@gbp_attributes
def get_update_policy_action_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_policy_classifier_default_attrs():
return {'name': '',
'description': '',
'protocol': None,
'port_range': None,
'direction': None,
'shared': False}
@gbp_attributes
def get_create_policy_classifier_attrs():
return {'name': 'pc1',
'description': 'test policy classifier',
'protocol': 'tcp',
'port_range': '100:200',
'direction': 'in',
'shared': False}
@gbp_attributes
def get_update_policy_classifier_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_policy_rule_default_attrs():
return {'name': '',
'description': '',
'enabled': True,
'policy_actions': [],
'shared': False}
@gbp_attributes
def get_create_policy_rule_attrs():
return {'name': 'pr1',
'description': 'test policy rule',
'enabled': True,
'policy_classifier_id': _uuid(),
'policy_actions': [_uuid()],
'shared': False}
@gbp_attributes
def get_update_policy_rule_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_policy_rule_set_default_attrs():
return {'name': '',
'description': '',
'child_policy_rule_sets': [],
'policy_rules': [],
'shared': False}
@gbp_attributes
def get_create_policy_rule_set_attrs():
return {'name': 'policy_rule_set1',
'description': 'test policy_rule_set',
'child_policy_rule_sets': [_uuid()],
'policy_rules': [_uuid()],
'shared': False}
@gbp_attributes
def get_update_policy_rule_set_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_network_service_policy_default_attrs():
return {'name': '', 'description': '',
'network_service_params': [], 'shared': False}
@gbp_attributes
def get_create_network_service_policy_attrs():
return {'name': 'nsp1',
'shared': False,
'description': 'test Net Svc Policy',
'network_service_params': [{'type': 'ip_single', 'name': 'vip',
'value': 'self_subnet'}]}
@gbp_attributes
def get_update_network_service_policy_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_external_policy_default_attrs():
return {'name': '', 'description': '',
'external_segments': [],
'provided_policy_rule_sets': {},
'consumed_policy_rule_sets': {},
'shared': False}
@gbp_attributes
def get_create_external_policy_attrs():
return {'name': 'ep1',
'description': 'test ep',
'external_segments': [_uuid()],
'provided_policy_rule_sets': {_uuid(): None},
'consumed_policy_rule_sets': {_uuid(): None},
'shared': False}
@gbp_attributes
def get_update_external_policy_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_external_segment_default_attrs():
return {'name': '', 'description': '',
'external_routes': [],
'ip_version': 4,
'cidr': '172.16.0.0/12',
'port_address_translation': False,
'shared': False}
@gbp_attributes
def get_create_external_segment_attrs():
return {'name': 'es1',
'description': 'test ep',
'external_routes': [{'destination': '0.0.0.0/0',
'nexthop': '192.168.0.1'}],
'cidr': '192.168.0.0/24',
'ip_version': 4, 'port_address_translation': True,
'shared': False}
@gbp_attributes
def get_update_external_segment_attrs():
return {'name': 'new_name'}
@gbp_attributes
def get_create_nat_pool_default_attrs():
return {'name': '', 'description': '',
'external_segment_id': None, 'ip_version': 4,
'ip_pool': '172.16.0.0/16',
'shared': False}
@gbp_attributes
def get_create_nat_pool_attrs():
return {'name': 'es1',
'description': 'test ep',
'ip_version': 4,
'ip_pool': '172.16.0.0/16',
'external_segment_id': _uuid(),
'shared': False}
@gbp_attributes
def get_update_nat_pool_attrs():
return {'name': 'new_name'}
# Service Chain
@gbp_attributes
def get_create_service_profile_default_attrs():
return {'name': '', 'description': ''}
@gbp_attributes
def get_create_service_profile_attrs():
return {
'name': 'serviceprofile1',
'service_type': 'FIREWALL',
'description': 'test service profile',
}
@gbp_attributes
def get_update_service_profile_attrs():
return {
'name': 'new_name',
}
@gbp_attributes
def get_create_servicechain_node_default_attrs():
return {
'name': '',
'description': '',
'config': '{}',
'service_type': None,
'shared': False,
}
@gbp_attributes
def get_create_servicechain_node_attrs():
return {
'name': 'servicechain1',
'service_profile_id': _uuid(),
'description': 'test servicechain node',
'config': '{}',
'service_type': None,
'shared': True,
}
@gbp_attributes
def get_update_servicechain_node_attrs():
return {
'name': 'new_name',
'config': 'new_config',
}
@gbp_attributes
def get_create_servicechain_spec_default_attrs():
return {
'name': '',
'description': '',
'nodes': [],
'shared': False,
}
@gbp_attributes
def get_create_servicechain_spec_attrs():
return {
'name': 'servicechainspec1',
'nodes': [_uuid(), _uuid()],
'description': 'test servicechain spec',
'shared': True,
}
@gbp_attributes
def get_update_servicechain_spec_attrs():
return {
'name': 'new_name',
'nodes': [_uuid()]
}
@gbp_attributes
def get_create_servicechain_instance_default_attrs():
return {'name': '', 'description': '', 'config_param_values': "{}"}
@gbp_attributes
def get_create_servicechain_instance_attrs():
return {
'name': 'servicechaininstance1',
'servicechain_specs': [_uuid()],
'provider_ptg_id': _uuid(),
'consumer_ptg_id': _uuid(),
'management_ptg_id': _uuid(),
'classifier_id': _uuid(),
'config_param_values': "{}",
'description': 'test servicechain instance'
}
def get_update_servicechain_instance_attrs():
return {
'name': 'new_name',
'servicechain_specs': [_uuid()],
'classifier_id': _uuid()
}
@gbp_attributes
def get_create_application_policy_group_default_attrs_and_prj_id():
return {'name': '', 'description': '', 'shared': False}
@gbp_attributes
def get_create_policy_target_default_attrs_and_prj_id():
return {'name': '', 'description': '', 'policy_target_group_id': None,
'cluster_id': ''}
@gbp_attributes
def get_create_policy_target_group_default_attrs_and_prj_id():
return {'name': '', 'description': '', 'l2_policy_id': None,
'application_policy_group_id': None,
'provided_policy_rule_sets': {},
'consumed_policy_rule_sets': {},
'network_service_policy_id': None, 'shared': False,
'service_management': False}
@gbp_attributes
def get_create_l2_policy_default_attrs_and_prj_id():
return {'name': '', 'description': '', 'shared': False,
'inject_default_route': True}
@gbp_attributes
def get_create_l3_policy_default_attrs_and_prj_id():
return {'name': '', 'description': '', 'ip_version': 4,
'ip_pool': '10.0.0.0/8', 'subnet_prefix_length': 24,
'external_segments': {}, 'shared': False}
@gbp_attributes
def get_create_policy_action_default_attrs_and_prj_id():
return {'name': '',
'description': '',
'action_type': 'allow',
'action_value': None,
'shared': False}
@gbp_attributes
def get_create_policy_classifier_default_attrs_and_prj_id():
return {'name': '',
'description': '',
'protocol': None,
'port_range': None,
'direction': None,
'shared': False}
@gbp_attributes
def get_create_policy_rule_default_attrs_and_prj_id():
return {'name': '',
'description': '',
'enabled': True,
'policy_actions': [],
'shared': False}
@gbp_attributes
def get_create_policy_rule_set_default_attrs_and_prj_id():
return {'name': '',
'description': '',
'child_policy_rule_sets': [],
'policy_rules': [],
'shared': False}
@gbp_attributes
def get_create_network_service_policy_default_attrs_and_prj_id():
return {'name': '', 'description': '',
'network_service_params': [], 'shared': False}
@gbp_attributes
def get_create_external_policy_default_attrs_and_prj_id():
return {'name': '', 'description': '',
'external_segments': [],
'provided_policy_rule_sets': {},
'consumed_policy_rule_sets': {},
'shared': False}
@gbp_attributes
def get_create_external_segment_default_attrs_and_prj_id():
return {'name': '', 'description': '',
'external_routes': [],
'ip_version': 4,
'cidr': '172.16.0.0/12',
'port_address_translation': False,
'shared': False}
@gbp_attributes
def get_create_nat_pool_default_attrs_and_prj_id():
return {'name': '', 'description': '',
'external_segment_id': None, 'ip_version': 4,
'ip_pool': '172.16.0.0/16',
'shared': False}
# Service Chain
@gbp_attributes
def get_create_service_profile_default_attrs_and_prj_id():
return {'name': '', 'description': ''}
@gbp_attributes
def get_create_servicechain_node_default_attrs_and_prj_id():
return {
'name': '',
'description': '',
'config': '{}',
'service_type': None,
'shared': False,
}
@gbp_attributes
def get_create_servicechain_spec_default_attrs_and_prj_id():
return {
'name': '',
'description': '',
'nodes': [],
'shared': False,
}
@gbp_attributes
def get_create_servicechain_instance_default_attrs_and_prj_id():
return {'name': '', 'description': '', 'config_param_values': "{}"}
def get_resource_plural(resource):
if resource.endswith('y'):
resource_plural = resource.replace('y', 'ies')
else:
resource_plural = resource + 's'
return resource_plural