def test_get_pool_monitor_no_monitor(self, basic_service): adapter = ServiceModelAdapter(mock.MagicMock()) pools = basic_service.get('pools', [None]) assert not adapter._get_pool_monitor(pools[0], basic_service) pools[0]['healthmonitor_id'] = str(uuid.uuid4()) assert not adapter._get_pool_monitor(pools[0], basic_service)
def __init__(self, driver, f5_global_routed_mode): self.conf = driver.conf self.driver = driver self.f5_global_routed_mode = f5_global_routed_mode self.vlan_binding = None self.fdb_connector = None self.interface_mapping = {} self.tagging_mapping = {} self.system_helper = SystemHelper() self.network_helper = NetworkHelper() self.service_adapter = ServiceModelAdapter(self.conf) if not f5_global_routed_mode: self.fdb_connector = FDBConnectorML2(self.conf) if self.conf.vlan_binding_driver: try: self.vlan_binding = importutils.import_object( self.conf.vlan_binding_driver, self.conf, self) except ImportError: LOG.error('Failed to import VLAN binding driver: %s' % self.conf.vlan_binding_driver) raise # map format is phynet:interface:tagged for maps in self.conf.f5_external_physical_mappings: intmap = maps.split(':') net_key = str(intmap[0]).strip() if len(intmap) > 3: net_key = net_key + ':' + str(intmap[3]).strip() self.interface_mapping[net_key] = str(intmap[1]).strip() self.tagging_mapping[net_key] = str(intmap[2]).strip() LOG.debug('physical_network %s = interface %s, tagged %s' % (net_key, intmap[1], intmap[2]))
def test_get_vip_default_pool_pending_create(self, basic_service): adapter = ServiceModelAdapter(mock.MagicMock()) pools = basic_service.get('pools', [None]) pools[0]['provisioning_status'] = "PENDING_CREATE" default_pool = adapter.get_vip_default_pool(basic_service) assert default_pool == pools[0]
def test_get_pool_monitor(self, basic_service_with_monitor): basic_service = basic_service_with_monitor adapter = ServiceModelAdapter(mock.MagicMock()) pools = basic_service.get('pools', [None]) monitor = adapter._get_pool_monitor(pools[0], basic_service) assert monitor == basic_service.get('healthmonitors')[0]
def test_add_session_persistence_app_cookie_persist( self, basic_service_no_persist): adapter = ServiceModelAdapter(mock.MagicMock()) listener = basic_service_no_persist['listener'] pool = basic_service_no_persist['pools'][0] pool['session_persistence'] = dict(type="APP_COOKIE") vip_name = 'vip_name' persist_name = "app_cookie_{}".format(vip_name) vip = dict(name=vip_name) adapter._add_profiles_session_persistence(listener, pool, vip) expected = dict( name=vip_name, ipProtocol='tcp', profiles=['/Common/http', '/Common/oneconnect'], fallbackPersistence='', persist=[dict(name=persist_name)]) assert vip == expected pool['lb_algorithm'] = 'SOURCE_IP' vip = dict(name=vip_name) adapter._add_profiles_session_persistence(listener, pool, vip) expected = dict( name=vip_name, ipProtocol='tcp', profiles=['/Common/http', '/Common/oneconnect'], fallbackPersistence='/Common/source_addr', persist=[dict(name=persist_name)]) assert vip == expected
def test_vs_http_profiles(self, service): adapter = ServiceModelAdapter(mock.MagicMock()) # should have http and oneconnect but not fastL4 vs = adapter.get_virtual(service) assert '/Common/http' in vs['profiles'] assert '/Common/oneconnect' in vs['profiles'] assert '/Common/fastL4' not in vs['profiles']
def test_pool_no_members_least_conns(self, pool_member_service): '''No members, lb method should be 'least-connections-member'.''' adapter = ServiceModelAdapter(mock.MagicMock()) srvc = copy.deepcopy(pool_member_service) del srvc['members'] pool = adapter.get_pool(srvc) assert pool['loadBalancingMode'] == 'least-connections-member'
def test_add_vlan_and_snat_no_snat(self, basic_service): adapter = ServiceModelAdapter(mock.MagicMock()) listener = basic_service['listener'] vip = dict() adapter._add_vlan_and_snat(listener, vip) expected = dict(vlansDisabled=True, vlans=[]) assert vip == expected
def test_pool_member_with_weight_deleted(self, pool_member_service): '''If lb method does not exist, no change is made.''' adapter = ServiceModelAdapter(mock.MagicMock()) srvc = copy.deepcopy(pool_member_service) del srvc['pool']['lb_algorithm'] pool = adapter.get_pool(srvc) assert 'loadBalancingMode' not in pool
def test_vs_terminated_https_profiles(self, service): adapter = ServiceModelAdapter(mock.MagicMock()) # should have http and oneconnect but not fastL4 service['listener']['protocol'] = 'TERMINATED_HTTPS' vs = adapter.get_virtual(service) assert '/Common/http' in vs['profiles'] assert '/Common/oneconnect' in vs['profiles'] assert '/Common/fastL4' not in vs['profiles']
def test_get_l7policy(self, basic_l7service): adapter = ServiceModelAdapter(mock.MagicMock()) vip = dict(policies=list()) listener = basic_l7service['listener'] policies = list() adapter._apply_l7_and_esd_policies(listener, policies, vip) assert vip == dict(policies=list())
def test_pool_no_members_source_ip(self, pool_member_service): '''No members, lb method should be 'least-connections-node'.''' adapter = ServiceModelAdapter(mock.MagicMock()) srvc = copy.deepcopy(pool_member_service) del srvc['members'] srvc['pool']['lb_algorithm'] = 'SOURCE_IP' pool = adapter.get_pool(srvc) assert pool['loadBalancingMode'] == 'least-connections-node'
def test_pool_no_members_round_robin(self, pool_member_service): '''No members, lb method should be 'round-robin'.''' adapter = ServiceModelAdapter(mock.MagicMock()) srvc = copy.deepcopy(pool_member_service) del srvc['members'] srvc['pool']['lb_algorithm'] = 'ROUND_ROBIN' pool = adapter.get_pool(srvc) assert pool['loadBalancingMode'] == 'round-robin'
def test_vs_https_profiles(self, service): adapter = ServiceModelAdapter(mock.MagicMock()) adapter.esd = Mock() adapter.esd.get_esd.return_value = None # should have http and oneconnect but not fastL4 service['listener']['protocol'] = 'HTTPS' vs = adapter.get_virtual(service) assert '/Common/fastL4' in vs['profiles']
def test_pool_member_weight_least_conns(self, pool_member_service): '''lb method changes if member has weight and lb method least conns. The pool's lb method should be set to 'ratio-least-connections-member'. ''' adapter = ServiceModelAdapter(mock.MagicMock()) pool = adapter.get_pool(pool_member_service) assert pool['loadBalancingMode'] == 'ratio-least-connections-member'
def test_add_vlan_and_snat_automap(self, basic_service): adapter = ServiceModelAdapter(mock.MagicMock()) listener = basic_service['listener'] listener['use_snat'] = True vip = dict() adapter._add_vlan_and_snat(listener, vip) expected = dict(sourceAddressTranslation=dict(type='automap'), vlansDisabled=True, vlans=[]) assert vip == expected
def test_vs_tcp_profiles(self, service): adapter = ServiceModelAdapter(mock.MagicMock()) service['listener']['protocol'] = 'TCP' vs = adapter.get_virtual(service) # should have fastL4 but not http and oneconnect assert '/Common/http' not in vs['profiles'] assert '/Common/oneconnect' not in vs['profiles'] assert '/Common/fastL4' in vs['profiles']
def test_add_vlan_and_snat_automap(self, basic_service): adapter = ServiceModelAdapter(mock.MagicMock()) listener = basic_service['listener'] listener['use_snat'] = True vip = dict() adapter._add_vlan_and_snat(listener, vip) expected = dict( sourceAddressTranslation=dict(type='automap'), vlansDisabled=True, vlans=[]) assert vip == expected
def test_apply_empty_esd(adapter): adapter = ServiceModelAdapter(mock.MagicMock()) esd = dict() vip = dict() adapter._apply_esd(vip, esd) assert "persist" not in vip assert "fallbackPersistence" not in vip assert "profiles" not in vip assert "rules" not in vip assert "policies" not in vip
def test_apply_l4_esd_fallback_persist_profile_nopersist(adapter): adapter = ServiceModelAdapter(mock.MagicMock()) esd = dict(lbaas_fallback_persist="hash") vip = dict(profiles=[]) adapter._apply_fastl4_esd(vip, esd) assert "policies" not in vip assert "persist" not in vip assert "fallbackPersistence" not in vip
def test_pool_member_weight_round_robin(self, pool_member_service): '''lb method changes if member has weight and lb method round robin The pool's lb method should be set to 'ratio-member'. ''' adapter = ServiceModelAdapter(mock.MagicMock()) srvc = copy.deepcopy(pool_member_service) srvc['pool']['lb_algorithm'] = 'ROUND_ROBIN' pool = adapter.get_pool(srvc) assert pool['loadBalancingMode'] == 'ratio-member'
def test_pool_member_weight_source_ip(self, pool_member_service): '''lb method changes if member has weight and lb method source ip The pool's lb method should be set to 'least-connections-node' ''' adapter = ServiceModelAdapter(mock.MagicMock()) srvc = copy.deepcopy(pool_member_service) srvc['pool']['lb_algorithm'] = 'SOURCE_IP' pool = adapter.get_pool(srvc) assert pool['loadBalancingMode'] == 'least-connections-node'
def test_get_listener_policies(self, basic_l7service): adapter = ServiceModelAdapter(mock.MagicMock()) adapter.esd = Mock() adapter.esd.get_esd.return_value = None policies = adapter.get_listener_policies(basic_l7service) policy = policies[0] assert len(policy['rules']) == len(policy['l7policy_rules']) for rule in policy['l7policy_rules']: assert rule.get('provisioning_status', "") == "ACTIVE"
def test_pool_member_weight_bad_lb_method(self, pool_member_service): '''If lb method is bad and member has weight, lb is changed The pool's lb method should be set to 'ratio-member' ''' adapter = ServiceModelAdapter(mock.MagicMock()) srvc = copy.deepcopy(pool_member_service) srvc['pool']['lb_algorithm'] = 'ROUND_ROCK' pool = adapter.get_pool(srvc) assert pool['loadBalancingMode'] == 'ratio-member'
def test_vs_https_profiles(self, service): adapter = ServiceModelAdapter(mock.MagicMock()) adapter.esd = Mock() adapter.esd.get_esd.return_value = None # should have http and oneconnect but not fastL4 service['listener']['protocol'] = 'HTTPS' vs = adapter.get_virtual(service) assert '/Common/http' in vs['profiles'] assert '/Common/oneconnect' in vs['profiles'] assert '/Common/fastL4' not in vs['profiles']
def test_apply_l4_esd_persist_profile_collision(adapter): adapter = ServiceModelAdapter(mock.MagicMock()) esd = dict(lbaas_persist="hash") vip = dict(profiles=[], persist=[dict(name='sourceip')]) adapter._apply_fastl4_esd(vip, esd) assert "fallbackPersistence" not in vip assert "policies" not in vip assert vip['persist'] == [dict(name="hash")] assert vip['profiles'] == ["/Common/http", "/Common/fastL4"]
def test_add_session_peristence_tcp_no_pool( self, basic_http_service_no_pool): adapter = ServiceModelAdapter(mock.MagicMock()) listener = basic_http_service_no_pool['listener'] listener['protocol'] = 'TCP' vip = dict() adapter._add_profiles_session_persistence(listener, None, vip) expected = dict(ipProtocol='tcp', profiles=['/Common/fastL4'], fallbackPersistence='', persist=[]) assert vip == expected
def test_apply_l4_esd_irules_empty(adapter): adapter = ServiceModelAdapter(mock.MagicMock()) esd = dict(lbaas_irule=[]) vip = dict(profiles=[]) adapter._apply_fastl4_esd(vip, esd) assert "policies" not in vip assert "persist" not in vip assert "fallbackPersistence" not in vip assert not vip['rules']
def test_apply_l4_esd_policy(adapter): adapter = ServiceModelAdapter(mock.MagicMock()) esd = dict(lbaas_policy=["demo_policy"]) vip = dict(profiles=[]) adapter._apply_fastl4_esd(vip, esd) assert "persist" not in vip assert "fallbackPersistence" not in vip assert vip['rules'] == [] assert vip['policies'] == [dict(name='demo_policy', partition="Common")]
def test_apply_l4_esd_http_profile(adapter): adapter = ServiceModelAdapter(mock.MagicMock()) esd = dict(lbaas_http_profile="http_profile") vip = dict(profiles=[]) adapter._apply_fastl4_esd(vip, esd) assert "policies" not in vip assert "persist" not in vip assert "fallbackPersistence" not in vip assert vip['profiles'] == ["/Common/http_profile", "/Common/fastL4"]
def test_apply_esd_persist_profile_collision(adapter): adapter = ServiceModelAdapter(mock.MagicMock()) esd = dict(lbaas_persist="hash") vip = dict(profiles=[], persist=[dict(name='sourceip')]) adapter._apply_esd(vip, esd) assert "fallbackPersistence" not in vip assert "policies" not in vip assert vip['persist'] == [dict(name="hash")] assert vip['profiles'] == [dict( name="tcp", partition="Common", context="all")]
def test_apply_esd_persist_profile(adapter): adapter = ServiceModelAdapter(mock.MagicMock()) esd = dict(lbaas_persist="hash") vip = dict(profiles=[]) adapter._apply_esd(vip, esd) assert "fallbackPersistence" not in vip assert "policies" not in vip assert vip['persist'] == [dict(name="hash")] assert vip['profiles'] == [ dict(name="tcp", partition="Common", context="all")]
def test_get_vlan(self, basic_service): adapter = ServiceModelAdapter(mock.MagicMock()) vip = dict(vlans=[], vlansDisabled=True) bigip = Mock() # Test case where network is not assured bigip.assured_networks = {} adapter.get_vlan(vip, bigip, "net_UUID") expected = dict(vlans=[], vlansDisabled=True) assert vip == expected # Test case where network is assured. vip = dict(vlans=[], vlansDisabled=True) bigip.assured_networks = {'net_UUID': 'tunnel-vxlan-100'} adapter.get_vlan(vip, bigip, "net_UUID") expected = dict(vlans=['tunnel-vxlan-100'], vlansEnabled=True) assert vip == expected # Test case where network is not assured, but in common networks vip = dict(vlans=[], vlansDisabled=True) bigip.assured_networks = {} adapter.conf.common_network_ids = {'net_UUID': 'vlan-100'} adapter.get_vlan(vip, bigip, "net_UUID") expected = dict(vlans=['vlan-100'], vlansEnabled=True) assert vip == expected
def prep_service(self, service, bigips): """Prepares for LBaaS service request by creating partition. Creates partition, if not Common, and sets partition name on service loadbalancer. :param service: service object for request. :param bigips: Array of BigIP class instances to delete partition. """ # create partition if not Common self.create_partition(service, bigips) # set partition name on loadbalancer object ServiceModelAdapter.set_partition(service)
def test_get_listener_policies_pending_delete(self, basic_l7service): adapter = ServiceModelAdapter(mock.MagicMock()) adapter.esd = Mock() adapter.esd.get_esd.return_value = None service_rules = basic_l7service['l7policy_rules'] service_rules[2]['provisioning_status'] = "PENDING_DELETE" policies = adapter.get_listener_policies(basic_l7service) policy = policies[0] assert len(policy['rules']) == 5 assert len(policy['l7policy_rules']) == 4 for rule in policy['l7policy_rules']: assert rule.get('provisioning_status', "") == "ACTIVE"
def test_get_listener_policies_2_policies(self, basic_l7service_2policies): adapter = ServiceModelAdapter(mock.MagicMock()) adapter.esd = Mock() adapter.esd.get_esd.return_value = None l7service = basic_l7service_2policies listener = l7service.get('listener', None) policies = adapter.get_listener_policies(l7service) assert len(policies) == 2 for policy in policies: assert len(policy['rules']) == 2 assert len(policy['l7policy_rules']) == 2 assert policy['listener_id'] == listener['id']