def test_delete_pool(self): with nested( self.subnet(), mock.patch.object(self.driver.plugin._core_plugin, 'get_subnet')) as (subnet, mock_get_subnet): mock_get_subnet.return_value = subnet['subnet'] with nested( self.pool(provider=LBAAS_PROVIDER_NAME), mock.patch.object(self.driver.plugin._core_plugin, 'delete_port'), mock.patch.object(self.driver.plugin._core_plugin, 'get_ports'), mock.patch.object(self.driver.plugin, 'get_pools'), mock.patch.object( self.driver.plugin, '_delete_db_pool')) as (pool, mock_delete_port, mock_get_ports, mock_get_pools, mock_delete_db_pool): mock_delete_port.return_value = None mock_get_ports.return_value = [{'id': TESTPOOL_PORT_ID}] mock_get_pools.return_value = [] mock_delete_db_pool.return_value = None #reset the remove_resource() mock self.remove_resource_mock.reset_mock() # execute the method under test. self.driver.delete_pool(self.context, pool['pool']) pool_resource_path = "%s/%s" % ( (netscaler_driver.POOLS_RESOURCE, pool['pool']['id'])) # Assert that delete_resource was called # once with expected params. (self.remove_resource_mock.assert_called_once_with( None, pool_resource_path))
def test_delete_pool_health_monitor(self): with nested( self.subnet(), mock.patch.object(self.driver.plugin._core_plugin, 'get_subnet')) as (subnet, mock_get_subnet): mock_get_subnet.return_value = subnet['subnet'] with self.pool(provider=LBAAS_PROVIDER_NAME) as pool: with nested( self.health_monitor(pool_id=pool['pool']['id']), mock.patch.object( self.driver.plugin, '_delete_db_pool_health_monitor')) as ( health_monitor, mock_delete_db_monitor): mock_delete_db_monitor.return_value = None # reset the remove_resource() mock self.remove_resource_mock.reset_mock() # execute the method under test. self.driver.delete_pool_health_monitor( self.context, health_monitor['health_monitor'], pool['pool']['id']) monitor_resource_path = "%s/%s/%s/%s" % ( netscaler_driver.POOLS_RESOURCE, pool['pool']['id'], netscaler_driver.MONITORS_RESOURCE, health_monitor['health_monitor']['id']) # Assert that delete_resource was called once # with expected params. self.remove_resource_mock.assert_called_once_with( None, monitor_resource_path)
def test_create_member_on_different_subnets(self): with nested(self.subnet(), self.subnet(cidr='20.0.0.0/24'), self.subnet(cidr='30.0.0.0/24')) as (vip_sub, pool_sub, member_sub): with self.pool(provider='radware', subnet_id=pool_sub['subnet']['id']) as pool: with nested( self.port(subnet=vip_sub, fixed_ips=[{ 'ip_address': '10.0.0.2' }]), self.port(subnet=pool_sub, fixed_ips=[{ 'ip_address': '20.0.0.2' }]), self.port(subnet=member_sub, fixed_ips=[{ 'ip_address': '30.0.0.2' }])): with nested( self.member(pool_id=pool['pool']['id'], address='10.0.0.2'), self.member(pool_id=pool['pool']['id'], address='20.0.0.2'), self.member(pool_id=pool['pool']['id'], address='30.0.0.2')) as (member_vip, member_pool, member_out): with self.vip(pool=pool, subnet=vip_sub): calls = [ mock.call( 'POST', '/api/workflow/' + pool['pool']['id'] + '/action/BaseCreate', mock.ANY, driver.TEMPLATE_HEADER) ] self.driver_rest_call_mock.assert_has_calls( calls, any_order=True) mock_calls = self.driver_rest_call_mock.mock_calls params = mock_calls[-2][1][2]['parameters'] member_subnet_array = params['member_subnet_array'] member_mask_array = params['member_mask_array'] member_gw_array = params['member_gw_array'] self.assertEqual( ['10.0.0.0', '255.255.255.255', '30.0.0.0'], member_subnet_array) self.assertEqual([ '255.255.255.0', '255.255.255.255', '255.255.255.0' ], member_mask_array) self.assertEqual([ pool_sub['subnet']['gateway_ip'], '255.255.255.255', pool_sub['subnet']['gateway_ip'] ], member_gw_array)
def test_create_member_on_different_subnets(self): with nested( self.subnet(), self.subnet(cidr='20.0.0.0/24'), self.subnet(cidr='30.0.0.0/24') ) as (vip_sub, pool_sub, member_sub): with self.pool(provider='radware', subnet_id=pool_sub['subnet']['id']) as pool: with nested( self.port(subnet=vip_sub, fixed_ips=[{'ip_address': '10.0.0.2'}]), self.port(subnet=pool_sub, fixed_ips=[{'ip_address': '20.0.0.2'}]), self.port(subnet=member_sub, fixed_ips=[{'ip_address': '30.0.0.2'}]) ): with nested( self.member(pool_id=pool['pool']['id'], address='10.0.0.2'), self.member(pool_id=pool['pool']['id'], address='20.0.0.2'), self.member(pool_id=pool['pool']['id'], address='30.0.0.2') ) as (member_vip, member_pool, member_out): with self.vip(pool=pool, subnet=vip_sub): calls = [ mock.call( 'POST', '/api/workflow/' + pool['pool']['id'] + '/action/BaseCreate', mock.ANY, driver.TEMPLATE_HEADER ) ] self.driver_rest_call_mock.assert_has_calls( calls, any_order=True) mock_calls = self.driver_rest_call_mock.mock_calls params = mock_calls[-2][1][2]['parameters'] member_subnet_array = params['member_subnet_array'] member_mask_array = params['member_mask_array'] member_gw_array = params['member_gw_array'] self.assertEqual(['10.0.0.0', '255.255.255.255', '30.0.0.0'], member_subnet_array) self.assertEqual(['255.255.255.0', '255.255.255.255', '255.255.255.0'], member_mask_array) self.assertEqual( [pool_sub['subnet']['gateway_ip'], '255.255.255.255', pool_sub['subnet']['gateway_ip']], member_gw_array)
def test_build_objects_with_l7(self): with self.subnet(cidr='10.0.0.0/24') as vip_sub: with self.loadbalancer( subnet=vip_sub, vip_address=WF_APPLY_PARAMS['parameters']['vip_address'] ) as lb: lb_id = lb['loadbalancer']['id'] with self.listener( protocol=lb_con.PROTOCOL_HTTP, loadbalancer_id=lb_id) as listener: listener_id = listener['listener']['id'] with nested( self.pool( protocol=lb_con.PROTOCOL_HTTP, listener_id=listener_id), self.pool( protocol=lb_con.PROTOCOL_HTTP, loadbalancer_id=lb_id)) as (def_pool, pol_pool): def_pool_id = def_pool['pool']['id'] pol_pool_id = pol_pool['pool']['id'] with self.l7policy( listener_id, action=lb_con.L7_POLICY_ACTION_REDIRECT_TO_POOL, redirect_pool_id=pol_pool_id) as policy: policy_id = policy['l7policy']['id'] self.driver_rest_call_mock.reset_mock() with nested( self.l7policy_rule( l7policy_id=policy_id, key=u'key1', value=u'val1'), self.l7policy_rule( l7policy_id=policy_id, key=u'key2', value=u'val2'), self.member( pool_id=def_pool_id, subnet=vip_sub, address=u'10.0.1.10')) as ( rule1, rule2, def_m): self.driver_rest_call_mock.reset_mock() rest_call_function_mock.__dict__.update( {'WORKFLOW_MISSING': False}) with self.member( pool_id=pol_pool_id, subnet=vip_sub, address=u'10.0.1.20'): self.compare_apply_call()
def test_update_pool_health_monitor(self): from_hmon = { 'admin_state_up': True, 'tenant_id': TENANT_ID, 'delay': 5, 'max_retries': 5, 'timeout': 5, 'pools': [ {'status': 'PENDING_UPDATE', 'status_description': None, 'pool_id': POOL_ID}], 'type': 'PING', 'id': HEALTHMON_ID} to_hmon = { 'admin_state_up': True, 'tenant_id': TENANT_ID, 'delay': 5, 'max_retries': 10, 'timeout': 5, 'pools': [ {'status': 'ACTIVE', 'status_description': None, 'pool_id': POOL_ID}], 'type': 'PING', 'id': HEALTHMON_ID} pool_mapping = {'edge_id': EDGE_ID, 'edge_pool_id': EDGE_POOL_ID} mon_mapping = {'edge_id': EDGE_ID, 'edge_monitor_id': EDGE_MON_ID} with nested( mock.patch.object(db, 'get_nsxv_edge_pool_mapping'), mock.patch.object(db, 'get_nsxv_edge_monitor_mapping'), mock.patch.object(self.service_plugin._core_plugin.nsx_v, 'update_pool_health_monitor') ) as (mock_get_pool_mapping, mock_get_mon_mapping, mock_upd_pool_hm): mock_get_pool_mapping.return_value = pool_mapping mock_get_mon_mapping.return_value = mon_mapping self.edge_driver.update_pool_health_monitor( self.context, from_hmon, to_hmon, POOL_ID) mock_upd_pool_hm.assert_called_with( self.context, from_hmon, to_hmon, POOL_ID, mon_mapping)
def test_update_vip(self): vip_from = { 'status': 'ACTIVE', 'protocol': 'HTTP', 'description': '', 'address': '10.0.0.8', 'protocol_port': 555, 'port_id': VIP_PORT_ID, 'id': VIP_ID, 'status_description': None, 'name': 'testvip1', 'admin_state_up': True, 'subnet_id': SUBNET_ID, 'tenant_id': TENANT_ID, 'connection_limit': -1, 'pool_id': POOL_ID, 'session_persistence': {'type': 'SOURCE_IP'}} vip_to = { 'status': 'PENDING_UPDATE', 'protocol': 'HTTP', 'description': '', 'address': '10.0.0.8', 'protocol_port': 555, 'port_id': VIP_PORT_ID, 'id': VIP_ID, 'status_description': None, 'name': 'testvip1', 'admin_state_up': True, 'subnet_id': SUBNET_ID, 'tenant_id': TENANT_ID, 'connection_limit': -1, 'pool_id': POOL_ID, 'session_persistence': {'type': 'HTTP_COOKIE'}} pool_mapping = {'edge_id': EDGE_ID, 'edge_pool_id': EDGE_POOL_ID} vip_mapping = {'edge_id': EDGE_ID, 'edge_vse_id': EDGE_VSE_ID, 'edge_app_profile_id': APP_PROFILE_ID} with nested( mock.patch.object(db, 'get_nsxv_edge_pool_mapping'), mock.patch.object(db, 'get_nsxv_edge_vip_mapping'), mock.patch.object(self.service_plugin._core_plugin.nsx_v, 'update_vip') ) as (mock_get_pool_mapping, mock_get_vip_mapping, mock_upd_vip): mock_get_pool_mapping.return_value = pool_mapping mock_get_vip_mapping.return_value = vip_mapping self.edge_driver.update_vip(self.context, vip_from, vip_to) mock_upd_vip.assert_called_with(self.context, vip_from, vip_to, pool_mapping, vip_mapping)
def test_create_vip(self): with nested( self.subnet(), mock.patch.object(self.driver.plugin._core_plugin, 'get_subnet')) as (subnet, mock_get_subnet): mock_get_subnet.return_value = subnet['subnet'] with self.pool(provider=LBAAS_PROVIDER_NAME) as pool: testvip = self._build_testvip_contents(subnet['subnet'], pool['pool']) expectedvip = self._build_expectedvip_contents( testvip, subnet['subnet']) # mock the LBaaS plugin update_status(). self._mock_update_status() # reset the create_resource() mock self.create_resource_mock.reset_mock() # execute the method under test self.driver.create_vip(self.context, testvip) # First, assert that create_resource was called once # with expected params. self.create_resource_mock.assert_called_once_with( None, netscaler_driver.VIPS_RESOURCE, netscaler_driver.VIP_RESOURCE, expectedvip) #Finally, assert that the vip object is now ACTIVE self.mock_update_status_obj.assert_called_once_with( mock.ANY, loadbalancer_db.Vip, expectedvip['id'], constants.ACTIVE)
def test_build_objects_with_tls(self): with self.subnet(cidr='10.0.0.0/24') as vip_sub: with self.loadbalancer(subnet=vip_sub) as lb: lb_id = lb['loadbalancer']['id'] with nested( mock.patch('neutron_lbaas.services.loadbalancer.plugin.' 'cert_parser', autospec=True), mock.patch('neutron_lbaas.services.loadbalancer.plugin.' 'CERT_MANAGER_PLUGIN.CertManager', autospec=True) ) as (cert_parser_mock, cert_manager_mock): cert_mock = mock.Mock(spec=cert_manager.Cert) cert_mock.get_certificate.return_value = 'certificate' cert_mock.get_intermediates.return_value = 'intermediates' cert_mock.get_private_key.return_value = 'private_key' cert_mock.get_private_key_passphrase.return_value = \ 'private_key_passphrase' cert_manager_mock().get_cert.return_value = cert_mock cert_parser_mock.validate_cert.return_value = True with self.listener( protocol=lb_con.PROTOCOL_TERMINATED_HTTPS, loadbalancer_id=lb_id, default_tls_container_ref='def1', sni_container_refs=['sni1', 'sni2']) as listener: with self.pool( protocol=lb_con.PROTOCOL_HTTP, listener_id=listener['listener']['id']) as pool: with self.member(pool_id=pool['pool']['id'], subnet=vip_sub, address='10.0.1.10'): self.compare_apply_call()
def test_create_pool(self): with nested( self.subnet(), mock.patch.object(self.driver.plugin._core_plugin, 'get_subnet'), mock.patch.object(self.driver.plugin._core_plugin, 'get_ports'), mock.patch.object(self.driver.plugin._core_plugin, 'create_port')) as (subnet, mock_get_subnet, mock_get_ports, mock_create_port): mock_get_subnet.return_value = subnet['subnet'] mock_get_ports.return_value = None mock_create_port.return_value = TESTPOOL_SNAT_PORT testpool = self._build_testpool_contents(subnet['subnet']) expectedpool = self._build_expectedpool_contents( testpool, subnet['subnet']) #reset the create_resource() mock self.create_resource_mock.reset_mock() # mock the plugin's update_status() self._mock_update_status() # execute the method under test self.driver.create_pool(self.context, testpool) # First, assert that create_resource was called once # with expected params. (self.create_resource_mock.assert_called_once_with( None, netscaler_driver.POOLS_RESOURCE, netscaler_driver.POOL_RESOURCE, expectedpool)) #Finally, assert that the pool object is now ACTIVE self.mock_update_status_obj.assert_called_once_with( mock.ANY, loadbalancer_db.Pool, expectedpool['id'], constants.ACTIVE)
def test_create_vip_without_connection(self): with nested( self.subnet(), mock.patch.object(self.driver.plugin._core_plugin, 'get_subnet')) as (subnet, mock_get_subnet): mock_get_subnet.return_value = subnet['subnet'] with self.pool(provider=LBAAS_PROVIDER_NAME) as pool: testvip = self._build_testvip_contents(subnet['subnet'], pool['pool']) expectedvip = self._build_expectedvip_contents( testvip, subnet['subnet']) errorcode = ncc_client.NCCException.CONNECTION_ERROR self.create_resource_mock.side_effect = ( ncc_client.NCCException(errorcode)) # mock the plugin's update_status() self._mock_update_status() # reset the create_resource() mock self.create_resource_mock.reset_mock() # execute the method under test. self.driver.create_vip(self.context, testvip) # First, assert that update_resource was called once # with expected params. self.create_resource_mock.assert_called_once_with( None, netscaler_driver.VIPS_RESOURCE, netscaler_driver.VIP_RESOURCE, expectedvip) #Finally, assert that the vip object is in ERROR state self.mock_update_status_obj.assert_called_once_with( mock.ANY, loadbalancer_db.Vip, testvip['id'], constants.ERROR)
def test_plug_not_send_gratuitous_arp(self): self.conf.haproxy.send_gratuitous_arp = 0 test_port = {'id': 'port_id', 'network_id': 'net_id', 'mac_address': 'mac_addr', 'fixed_ips': [{'ip_address': '10.0.0.2', 'subnet': {'cidr': '10.0.0.0/24', 'gateway_ip': '10.0.0.1'}}]} test_address = '10.0.0.2' with nested( mock.patch('neutron.agent.linux.ip_lib.device_exists'), mock.patch('netaddr.IPNetwork'), mock.patch('neutron.agent.linux.ip_lib.IPWrapper'), ) as (dev_exists, ip_net, ip_wrap): self.vif_driver.get_device_name.return_value = 'test_interface' dev_exists.return_value = False ip_net.return_value = ip_net ip_net.prefixlen = 24 self.driver._plug('test_ns', test_port, test_address) cmd = ['route', 'add', 'default', 'gw', '10.0.0.1'] expected = [ self._ip_mock_call('test_ns'), mock.call().netns.execute(cmd, check_exit_code=False)] self.assertEqual(expected, ip_wrap.mock_calls)
def test_plug_no_gw(self): test_port = {'id': 'port_id', 'network_id': 'net_id', 'mac_address': 'mac_addr', 'fixed_ips': [{'ip_address': '10.0.0.2', 'subnet': {'cidr': '10.0.0.0/24'}}]} test_address = '10.0.0.2' with nested( mock.patch('neutron.agent.linux.ip_lib.device_exists'), mock.patch('netaddr.IPNetwork'), mock.patch('neutron.agent.linux.ip_lib.IPWrapper'), ) as (dev_exists, ip_net, ip_wrap): self.vif_driver.get_device_name.return_value = 'test_interface' dev_exists.return_value = False ip_net.return_value = ip_net ip_net.prefixlen = 24 self.driver._plug('test_ns', test_port, test_address) self.rpc_mock.plug_vip_port.assert_called_once_with( test_port['id']) self.assertTrue(dev_exists.called) self.vif_driver.plug.assert_called_once_with('net_id', 'port_id', 'test_interface', 'mac_addr', namespace='test_ns') self.vif_driver.init_l3.assert_called_once_with( 'test_interface', ['10.0.0.2/24'], namespace='test_ns' ) self.assertFalse(ip_wrap.called) dev_exists.return_value = True self.assertRaises(exceptions.PreexistingDeviceFailure, self.driver._plug, 'test_ns', test_port, test_address, False)
def test_create_pool_with_error(self): with nested( self.subnet(), mock.patch.object(self.driver.plugin._core_plugin, 'get_subnet'), mock.patch.object(self.driver.plugin._core_plugin, 'get_ports'), mock.patch.object(self.driver.plugin._core_plugin, 'create_port')) as (subnet, mock_get_subnet, mock_get_ports, mock_create_port): mock_get_subnet.return_value = subnet['subnet'] mock_get_ports.return_value = None mock_create_port.return_value = TESTPOOL_SNAT_PORT errorcode = ncc_client.NCCException.CONNECTION_ERROR self.create_resource_mock.side_effect = ( ncc_client.NCCException(errorcode)) testpool = self._build_testpool_contents(subnet['subnet']) expectedpool = self._build_expectedpool_contents( testpool, subnet['subnet']) # mock the plugin's update_status() self._mock_update_status() #reset the create_resource() mock self.create_resource_mock.reset_mock() # execute the method under test. self.driver.create_pool(self.context, testpool) # Also assert that create_resource was called once # with expected params. (self.create_resource_mock.assert_called_once_with( None, netscaler_driver.POOLS_RESOURCE, netscaler_driver.POOL_RESOURCE, expectedpool)) #Finally, assert that the pool object is in ERROR state self.mock_update_status_obj.assert_called_once_with( mock.ANY, loadbalancer_db.Pool, expectedpool['id'], constants.ERROR)
def test_update_member(self): with nested( self.subnet(), mock.patch.object(self.driver.plugin._core_plugin, 'get_subnet')) as (subnet, mock_get_subnet): mock_get_subnet.return_value = subnet['subnet'] with self.pool(provider=LBAAS_PROVIDER_NAME) as pool: with self.member(pool_id=pool['pool']['id']) as member: updatedmember = (self._build_updated_testmember_contents( member['member'])) expectedmember = ( self._build_updated_expectedmember_contents( updatedmember)) # mock the plugin's update_status() self._mock_update_status() # reset the update_resource() mock self.update_resource_mock.reset_mock() # execute the method under test self.driver.update_member(self.context, member['member'], updatedmember) member_resource_path = "%s/%s" % ( (netscaler_driver.POOLMEMBERS_RESOURCE, member['member']['id'])) # First, assert that update_resource was called once # with expected params. (self.update_resource_mock.assert_called_once_with( None, member_resource_path, netscaler_driver.POOLMEMBER_RESOURCE, expectedmember)) #Finally, assert that the member object is now ACTIVE self.mock_update_status_obj.assert_called_once_with( mock.ANY, loadbalancer_db.Member, member['member']['id'], constants.ACTIVE)
def test_create_pool_health_monitor(self): with nested( self.subnet(), mock.patch.object(self.driver.plugin._core_plugin, 'get_subnet')) as (subnet, mock_get_subnet): mock_get_subnet.return_value = subnet['subnet'] with self.pool(provider=LBAAS_PROVIDER_NAME) as pool: testhealthmonitor = self._build_testhealthmonitor_contents( pool['pool']) expectedhealthmonitor = ( self._build_expectedhealthmonitor_contents( testhealthmonitor)) with mock.patch.object(self.driver.plugin, 'update_pool_health_monitor') as mhm: # reset the create_resource() mock self.create_resource_mock.reset_mock() # execute the method under test. self.driver.create_pool_health_monitor( self.context, testhealthmonitor, pool['pool']['id']) # First, assert that create_resource was called once # with expected params. resource_path = "%s/%s/%s" % ( netscaler_driver.POOLS_RESOURCE, pool['pool']['id'], netscaler_driver.MONITORS_RESOURCE) (self.create_resource_mock.assert_called_once_with( None, resource_path, netscaler_driver.MONITOR_RESOURCE, expectedhealthmonitor)) # Finally, assert that the healthmonitor object is # now ACTIVE. (mhm.assert_called_once_with(mock.ANY, expectedhealthmonitor['id'], pool['pool']['id'], constants.ACTIVE, ""))
def test_build_objects_graph_lb_pool(self): with self.subnet(cidr='10.0.0.0/24') as vip_sub: with self.loadbalancer( subnet=vip_sub, vip_address=WF_APPLY_PARAMS['parameters']['vip_address'] ) as lb: lb_id = lb['loadbalancer']['id'] with self.listener(loadbalancer_id=lb_id) as listener: listener_id = listener['listener']['id'] with self.pool( protocol=lb_con.PROTOCOL_HTTP, listener_id=listener_id) as pool: with nested( self.member(pool_id=pool['pool']['id'], subnet=vip_sub, address='10.0.1.10'), self.member(pool_id=pool['pool']['id'], subnet=vip_sub, address='10.0.1.20')): self.driver_rest_call_mock.reset_mock() rest_call_function_mock.__dict__.update( {'WORKFLOW_MISSING': False}) with self.pool( protocol=lb_con.PROTOCOL_HTTP, loadbalancer_id=lb_id): self.compare_apply_call()
def test_update_pool(self): from_pool = { 'status': 'ACTIVE', 'lb_method': 'ROUND_ROBIN', 'protocol': 'HTTP', 'description': '', 'health_monitors': [], 'members': [], 'status_description': None, 'id': POOL_ID, 'vip_id': None, 'name': 'testpool2', 'admin_state_up': True, 'subnet_id': SUBNET_ID, 'tenant_id': TENANT_ID, 'health_monitors_status': [], 'provider': 'vmwareedge'} to_pool = { 'status': 'PENDING_UPDATE', 'lb_method': 'LEAST_CONNECTIONS', 'protocol': 'HTTP', 'description': '', 'health_monitors': [], 'members': [], 'status_description': None, 'id': POOL_ID, 'vip_id': None, 'name': 'testpool2', 'admin_state_up': True, 'subnet_id': SUBNET_ID, 'tenant_id': TENANT_ID, 'health_monitors_status': [], 'provider': 'vmwareedge'} mapping = {'edge_id': EDGE_ID, 'edge_pool_id': EDGE_POOL_ID} with nested( mock.patch.object(db, 'get_nsxv_edge_pool_mapping'), mock.patch.object(self.service_plugin._core_plugin.nsx_v, 'update_pool') ) as (mock_get_mapping, mock_update_pool): mock_get_mapping.return_value = mapping self.edge_driver.update_pool(self.context, from_pool, to_pool) mock_update_pool.assert_called_with(self.context, from_pool, to_pool, mapping)
def test_delete_pool_non_existing(self): with nested( mock.patch.object(self.driver, 'undeploy_instance'), mock.patch.object(self.driver, 'exists'), ) as (undeploy, exists): exists.return_value = False self.driver.delete_pool({'id': '1'}) self.assertFalse(undeploy.called)
def test_delete_pool_existing(self): with nested( mock.patch.object(self.driver, 'undeploy_instance'), mock.patch.object(self.driver, 'exists'), ) as (undeploy, exists): exists.return_value = True self.driver.delete_pool({'id': '1'}) undeploy.assert_called_once_with('1', delete_namespace=True)
def test_create_member_on_different_subnet_multiple_ports(self): cfg.CONF.set_override("allow_overlapping_ips", 'true') with self.network() as other_net: with nested(self.subnet(), self.subnet(cidr='20.0.0.0/24'), self.subnet(cidr='30.0.0.0/24'), self.subnet(network=other_net, cidr='30.0.0.0/24')) as (vip_sub, pool_sub, member_sub1, member_sub2): with self.pool(provider='radware', subnet_id=pool_sub['subnet']['id']) as pool: with nested( self.port(subnet=member_sub1, fixed_ips=[{ 'ip_address': '30.0.0.2' }]), self.port(subnet=member_sub2, fixed_ips=[{ 'ip_address': '30.0.0.2' }])): with self.member(pool_id=pool['pool']['id'], address='30.0.0.2'): with self.vip(pool=pool, subnet=vip_sub): calls = [ mock.call( 'POST', '/api/workflow/' + pool['pool']['id'] + '/action/BaseCreate', mock.ANY, driver.TEMPLATE_HEADER) ] self.driver_rest_call_mock.assert_has_calls( calls, any_order=True) calls = self.driver_rest_call_mock.mock_calls params = calls[-2][1][2]['parameters'] m_sub_array = params['member_subnet_array'] m_mask_array = params['member_mask_array'] m_gw_array = params['member_gw_array'] self.assertEqual(['30.0.0.2'], m_sub_array) self.assertEqual(['255.255.255.255'], m_mask_array) self.assertEqual( [pool_sub['subnet']['gateway_ip']], m_gw_array)
def test_delete_pool_successful(self): pool = {'id': POOL_ID} with nested( mock.patch.object(self.service_plugin, '_delete_db_pool'), mock.patch.object(db, 'delete_nsxv_edge_pool_mapping') ) as (mock_del_db_pool, mock_del_mapping): self.edge_driver.delete_pool_successful(self.context, pool) mock_del_db_pool.assert_called_with(self.context, POOL_ID) mock_del_mapping.assert_called_with(self.context, POOL_ID)
def test_delete_vip_successful(self): vip = {'pool_id': POOL_ID, 'id': VIP_ID} with nested( mock.patch.object(db, 'delete_nsxv_edge_vip_mapping'), mock.patch.object(self.service_plugin, '_delete_db_vip') ) as (mock_del_vip_mapping, mock_del_vip): self.edge_driver.delete_vip_successful(self.context, vip) mock_del_vip_mapping.assert_called_with(self.context, POOL_ID) mock_del_vip.assert_called_with(self.context, VIP_ID)
def test_create_member_on_different_subnet_multiple_ports(self): cfg.CONF.set_override("allow_overlapping_ips", 'true') with self.network() as other_net: with nested( self.subnet(), self.subnet(cidr='20.0.0.0/24'), self.subnet(cidr='30.0.0.0/24'), self.subnet(network=other_net, cidr='30.0.0.0/24') ) as (vip_sub, pool_sub, member_sub1, member_sub2): with self.pool(provider='radware', subnet_id=pool_sub['subnet']['id']) as pool: with nested( self.port(subnet=member_sub1, fixed_ips=[{'ip_address': '30.0.0.2'}]), self.port(subnet=member_sub2, fixed_ips=[{'ip_address': '30.0.0.2'}])): with self.member(pool_id=pool['pool']['id'], address='30.0.0.2'): with self.vip(pool=pool, subnet=vip_sub): calls = [ mock.call( 'POST', '/api/workflow/' + pool['pool']['id'] + '/action/BaseCreate', mock.ANY, driver.TEMPLATE_HEADER ) ] self.driver_rest_call_mock.assert_has_calls( calls, any_order=True) calls = self.driver_rest_call_mock.mock_calls params = calls[-2][1][2]['parameters'] m_sub_array = params['member_subnet_array'] m_mask_array = params['member_mask_array'] m_gw_array = params['member_gw_array'] self.assertEqual(['30.0.0.2'], m_sub_array) self.assertEqual(['255.255.255.255'], m_mask_array) self.assertEqual( [pool_sub['subnet']['gateway_ip']], m_gw_array)
def test_refresh_device_non_existing(self): with nested( mock.patch.object(self.driver, 'deploy_instance'), mock.patch.object(self.driver, 'exists'), mock.patch.object(self.driver, 'undeploy_instance') ) as (deploy, exists, undeploy): pool_id = 'pool_id1' deploy.return_value = False exists.return_value = False self.driver._refresh_device(pool_id) self.assertFalse(undeploy.called)
def test_refresh_device_not_deployed(self): with nested( mock.patch.object(self.driver, 'deploy_instance'), mock.patch.object(self.driver, 'exists'), mock.patch.object(self.driver, 'undeploy_instance') ) as (deploy, exists, undeploy): pool_id = 'pool_id1' deploy.return_value = False exists.return_value = True self.driver._refresh_device(pool_id) undeploy.assert_called_once_with(pool_id)
def test_refresh_device(self): with nested( mock.patch.object(self.driver, 'deploy_instance'), mock.patch.object(self.driver, 'undeploy_instance') ) as (deploy, undeploy): pool_id = 'pool_id1' self.driver._refresh_device(pool_id) self.rpc_mock.get_logical_device.assert_called_once_with(pool_id) deploy.assert_called_once_with( self.rpc_mock.get_logical_device.return_value) self.assertFalse(undeploy.called)
def test_main(self): logging_str = 'neutron.agent.common.config.setup_logging' with nested( mock.patch(logging_str), mock.patch.object(agent.service, 'launch'), mock.patch('sys.argv'), mock.patch.object(agent.manager, 'LbaasAgentManager'), mock.patch.object(cfg.CONF, 'register_opts') ) as (mock_logging, mock_launch, sys_argv, mgr_cls, ro): agent.main() mock_launch.assert_called_once_with(mock.ANY, mock.ANY)
def test_update(self): with nested( mock.patch.object(self.driver, '_get_state_file_path'), mock.patch.object(self.driver, '_spawn'), mock.patch.object(six.moves.builtins, 'open') ) as (gsp, spawn, mock_open): mock_open.return_value = ['5'] self.driver.update(self.fake_config) mock_open.assert_called_once_with(gsp.return_value, 'r') spawn.assert_called_once_with(self.fake_config, ['-sf', '5'])
def test_create_pool_health_monitor(self): with nested( self.health_monitor(), self.pool(), ) as (hm, pool): pool_id = pool['pool']['id'] ctx = context.get_admin_context() self.plugin_instance.create_pool_health_monitor(ctx, hm, pool_id) # hm now has a ref to the pool with which it is associated hm = self.plugin.get_health_monitor( ctx, hm['health_monitor']['id']) self.mock_api.create_pool_health_monitor.assert_called_once_with( mock.ANY, hm, pool_id, 'host')
def test_create_pool_successful(self): pool = {'id': POOL_ID} with nested( mock.patch.object(db, 'add_nsxv_edge_pool_mapping'), mock.patch.object(self.edge_driver, 'pool_successful') ) as (mock_add_pool, mock_pool_successful): self.edge_driver.create_pool_successful(self.context, pool, EDGE_ID, EDGE_POOL_ID) mock_add_pool.assert_called_with(self.context, POOL_ID, EDGE_ID, EDGE_POOL_ID) mock_pool_successful.assert_called_with(self.context, pool)
def test_create_pool_health_monitor_successful(self): hmon = {'id': HEALTHMON_ID} with nested( mock.patch.object(db, 'add_nsxv_edge_monitor_mapping'), mock.patch.object(self.edge_driver, 'pool_health_monitor_successful') ) as (mock_add_pool_mon_mapping, mock_pool_hmon_successful): self.edge_driver.create_pool_health_monitor_successful( self.context, hmon, POOL_ID, EDGE_ID, EDGE_MON_ID) mock_add_pool_mon_mapping.assert_called_with( self.context, HEALTHMON_ID, EDGE_ID, EDGE_MON_ID) mock_pool_hmon_successful.assert_called_with(self.context, hmon, POOL_ID)
def test_delete_pool_health_monitor(self): with nested(self.pool(), self.health_monitor()) as (pool, hm): pool_id = pool['pool']['id'] ctx = context.get_admin_context() self.plugin_instance.create_pool_health_monitor(ctx, hm, pool_id) # hm now has a ref to the pool with which it is associated hm = self.plugin.get_health_monitor(ctx, hm['health_monitor']['id']) hm['pools'][0]['status'] = 'PENDING_DELETE' self.plugin_instance.delete_pool_health_monitor( ctx, hm['id'], pool_id) self.mock_api.delete_pool_health_monitor.assert_called_once_with( mock.ANY, hm, pool_id, 'host')
def test_create_pool_health_monitor(self): with nested( self.health_monitor(), self.pool(), ) as (hm, pool): pool_id = pool['pool']['id'] ctx = context.get_admin_context() self.plugin_instance.create_pool_health_monitor(ctx, hm, pool_id) # hm now has a ref to the pool with which it is associated hm = self.plugin.get_health_monitor(ctx, hm['health_monitor']['id']) self.mock_api.create_pool_health_monitor.assert_called_once_with( mock.ANY, hm, pool_id, 'host')
def test_main(self): logging_str = 'neutron.agent.common.config.setup_logging' with nested(mock.patch(logging_str), mock.patch.object(agent.service, 'launch'), mock.patch('sys.argv'), mock.patch.object(agent.manager, 'LbaasAgentManager'), mock.patch.object(cfg.CONF, 'register_opts')) as (mock_logging, mock_launch, sys_argv, mgr_cls, ro): agent.main() mock_launch.assert_called_once_with(mock.ANY, mock.ANY)
def test_delete_vip(self): with nested( self.subnet(), mock.patch.object(self.driver.plugin._core_plugin, 'get_subnet')) as (subnet, mock_get_subnet): mock_get_subnet.return_value = subnet['subnet'] with self.pool(provider=LBAAS_PROVIDER_NAME) as pool: with nested( self.vip(pool=pool, subnet=subnet), mock.patch.object( self.driver.plugin, '_delete_db_vip')) as (vip, mock_delete_db_vip): mock_delete_db_vip.return_value = None #reset the remove_resource() mock self.remove_resource_mock.reset_mock() # execute the method under test self.driver.delete_vip(self.context, vip['vip']) vip_resource_path = "%s/%s" % ( (netscaler_driver.VIPS_RESOURCE, vip['vip']['id'])) # Assert that remove_resource() was called once # with expected params. (self.remove_resource_mock.assert_called_once_with( None, vip_resource_path))
def test_build_objects_graph_one_leg(self): with self.subnet(cidr='10.0.0.0/24') as vip_sub: with self.loadbalancer(subnet=vip_sub) as lb: lb_id = lb['loadbalancer']['id'] with self.listener(loadbalancer_id=lb_id) as listener: listener_id = listener['listener']['id'] with self.pool( protocol='HTTP', listener_id=listener_id) as pool: with nested( self.member(pool_id=pool['pool']['id'], subnet=vip_sub, address='10.0.1.10'), self.member(pool_id=pool['pool']['id'], subnet=vip_sub, address='10.0.1.20')): self.compare_apply_call()
def test_create_vip_successful(self): vip = {'pool_id': POOL_ID} with nested( mock.patch.object(db, 'add_nsxv_edge_vip_mapping'), mock.patch.object(self.edge_driver, 'vip_successful') ) as (mock_add_vip_mapping, mock_vip_successful): self.edge_driver.create_vip_successful( self.context, vip, EDGE_ID, APP_PROFILE_ID, EDGE_VSE_ID, EDGE_FW_RULE_ID) mock_add_vip_mapping.assert_called_with( self.context, POOL_ID, EDGE_ID, APP_PROFILE_ID, EDGE_VSE_ID, EDGE_FW_RULE_ID) mock_vip_successful.assert_called_with(self.context, vip)
def test_delete_pool_health_monitor_successful(self): hmon = {'id': HEALTHMON_ID, 'pool_id': POOL_ID} hmon_mapping = {'edge_id': EDGE_ID} with nested( mock.patch.object(db, 'delete_nsxv_edge_monitor_mapping'), mock.patch.object(self.service_plugin, '_delete_db_pool_health_monitor') ) as (mock_del_pool_hmon_mapping, mock_del_db_pool_hmon): self.edge_driver.delete_pool_health_monitor_successful( self.context, hmon, POOL_ID, hmon_mapping) mock_del_pool_hmon_mapping.assert_called_with( self.context, HEALTHMON_ID, EDGE_ID) mock_del_db_pool_hmon.assert_called_with( self.context, HEALTHMON_ID, POOL_ID)
def test_save_config(self): with nested( mock.patch( 'neutron_lbaas.services.loadbalancer.' 'drivers.haproxy.jinja_cfg.render_loadbalancer_obj'), mock.patch('neutron.common.utils.replace_file')) as (r_t, replace): r_t.return_value = 'fake_rendered_template' lb = mock.Mock() jinja_cfg.save_config('test_conf_path', lb, 'test_sock_path', 'nogroup', 'fake_state_path') r_t.assert_called_once_with(lb, 'nogroup', 'test_sock_path', 'fake_state_path') replace.assert_called_once_with('test_conf_path', 'fake_rendered_template')
def test_delete_pool_health_monitor(self): with nested( self.pool(), self.health_monitor() ) as (pool, hm): pool_id = pool['pool']['id'] ctx = context.get_admin_context() self.plugin_instance.create_pool_health_monitor(ctx, hm, pool_id) # hm now has a ref to the pool with which it is associated hm = self.plugin.get_health_monitor( ctx, hm['health_monitor']['id']) hm['pools'][0]['status'] = 'PENDING_DELETE' self.plugin_instance.delete_pool_health_monitor( ctx, hm['id'], pool_id) self.mock_api.delete_pool_health_monitor.assert_called_once_with( mock.ANY, hm, pool_id, 'host')
def test_update_status_health_monitor(self): with nested(self.health_monitor(), self.pool()) as (hm, pool): pool_id = pool['pool']['id'] ctx = context.get_admin_context() self.plugin_instance.create_pool_health_monitor(ctx, hm, pool_id) hm_id = hm['health_monitor']['id'] h = self.plugin_instance.get_pool_health_monitor( ctx, hm_id, pool_id) self.assertEqual('PENDING_CREATE', h['status']) self.callbacks.update_status(ctx, 'health_monitor', { 'monitor_id': hm_id, 'pool_id': pool_id }, 'ACTIVE') h = self.plugin_instance.get_pool_health_monitor( ctx, hm_id, pool_id) self.assertEqual('ACTIVE', h['status'])
def test_remove_orphans(self): with nested( mock.patch.object(self.driver, 'exists'), mock.patch.object(self.driver, 'undeploy_instance'), mock.patch('os.listdir'), mock.patch('os.path.exists') ) as (exists, undeploy, listdir, path_exists): known = ['known1', 'known2'] unknown = ['unknown1', 'unknown2'] listdir.return_value = known + unknown exists.side_effect = lambda x: x == 'unknown2' self.driver.remove_orphans(known) undeploy.assert_called_once_with('unknown2', cleanup_namespace=True)
def test_update_health_monitor_associated_with_pool(self): with nested( self.health_monitor(type='HTTP'), self.pool() ) as (monitor, pool): data = { 'health_monitor': { 'id': monitor['health_monitor']['id'], 'tenant_id': self._tenant_id } } req = self.new_create_request( 'pools', data, fmt=self.fmt, id=pool['pool']['id'], subresource='health_monitors') res = req.get_response(self.ext_api) self.assertEqual(exc.HTTPCreated.code, res.status_int) # hm now has a ref to the pool with which it is associated ctx = context.get_admin_context() hm = self.plugin.get_health_monitor( ctx, monitor['health_monitor']['id']) self.mock_api.create_pool_health_monitor.assert_called_once_with( mock.ANY, hm, pool['pool']['id'], 'host' ) self.mock_api.reset_mock() data = {'health_monitor': {'delay': 20, 'timeout': 20, 'max_retries': 2, 'admin_state_up': False}} updated = hm.copy() updated.update(data['health_monitor']) req = self.new_update_request("health_monitors", data, monitor['health_monitor']['id']) req.get_response(self.ext_api) self.mock_api.update_pool_health_monitor.assert_called_once_with( mock.ANY, hm, updated, pool['pool']['id'], 'host')
def test_save_config(self): with nested( mock.patch('neutron_lbaas.services.loadbalancer.' 'drivers.haproxy.jinja_cfg.render_loadbalancer_obj'), mock.patch('neutron.common.utils.replace_file') ) as (r_t, replace): r_t.return_value = 'fake_rendered_template' lb = mock.Mock() jinja_cfg.save_config('test_conf_path', lb, 'test_sock_path', 'nogroup', 'fake_state_path') r_t.assert_called_once_with(lb, 'nogroup', 'test_sock_path', 'fake_state_path') replace.assert_called_once_with('test_conf_path', 'fake_rendered_template')
def test_delete_vip_failure(self): plugin = self.plugin_instance with self.network() as network: with self.subnet(network=network) as subnet: with self.pool(do_delete=False, provider='radware', subnet_id=subnet['subnet']['id']) as pool: with nested( self.member(pool_id=pool['pool']['id'], do_delete=False), self.member(pool_id=pool['pool']['id'], address='192.168.1.101', do_delete=False), self.health_monitor(do_delete=False), self.vip(pool=pool, subnet=subnet, do_delete=False)) as (mem1, mem2, hm, vip): plugin.create_pool_health_monitor( context.get_admin_context(), hm, pool['pool']['id']) rest_call_function_mock.__dict__.update( {'RESPOND_WITH_ERROR': True}) plugin.delete_vip(context.get_admin_context(), vip['vip']['id']) u_vip = plugin.get_vip(context.get_admin_context(), vip['vip']['id']) u_pool = plugin.get_pool(context.get_admin_context(), pool['pool']['id']) u_mem1 = plugin.get_member(context.get_admin_context(), mem1['member']['id']) u_mem2 = plugin.get_member(context.get_admin_context(), mem2['member']['id']) u_phm = plugin.get_pool_health_monitor( context.get_admin_context(), hm['health_monitor']['id'], pool['pool']['id']) self.assertEqual(constants.ERROR, u_vip['status']) self.assertEqual(constants.ACTIVE, u_pool['status']) self.assertEqual(constants.ACTIVE, u_mem1['status']) self.assertEqual(constants.ACTIVE, u_mem2['status']) self.assertEqual(constants.ACTIVE, u_phm['status'])
def test_spawn(self): with nested( mock.patch.object(namespace_driver.hacfg, 'save_config'), mock.patch.object(self.driver, '_get_state_file_path'), mock.patch('neutron.agent.linux.ip_lib.IPWrapper') ) as (mock_save, gsp, ip_wrap): gsp.side_effect = lambda x, y: y self.driver._spawn(self.fake_config) mock_save.assert_called_once_with('conf', self.fake_config, 'sock', 'test_group') cmd = ['haproxy', '-f', 'conf', '-p', 'pid'] ip_wrap.assert_has_calls([ self._ip_mock_call('qlbaas-pool_id'), mock.call().netns.execute(cmd) ])