def setUpClass(cls): """Run class setup for running LBaaSv2 service tests.""" super(LBAASv2Test, cls).setUpClass() cls.keystone_client = ObjectRetrierWraps( openstack_utils.get_keystone_session_client(cls.keystone_session)) if (openstack_utils.get_os_release() >= openstack_utils.get_os_release('focal_wallaby')): # add role to admin user for the duration of the test grant_role_current_user(cls.keystone_client, cls.keystone_session, LBAAS_ADMIN_ROLE) cls.neutron_client = ObjectRetrierWraps( openstack_utils.get_neutron_session_client(cls.keystone_session)) cls.octavia_client = ObjectRetrierWraps( openstack_utils.get_octavia_session_client(cls.keystone_session)) cls.RESOURCE_PREFIX = 'zaza-octavia' # NOTE(fnordahl): in the event of a test failure we do not want to run # tear down code as it will make debugging a problem virtually # impossible. To alleviate each test method will set the # `run_tearDown` instance variable at the end which will let us run # tear down only when there were no failure. cls.run_tearDown = False # List of load balancers created by this test cls.loadbalancers = [] # List of floating IPs created by this test cls.fips = []
def test_create_loadbalancer(self): """Create load balancer.""" keystone_session = openstack_utils.get_overcloud_keystone_session() neutron_client = openstack_utils.get_neutron_session_client( keystone_session) resp = neutron_client.list_networks(name='private') subnet_id = resp['networks'][0]['subnets'][0] octavia_client = openstack_utils.get_octavia_session_client( keystone_session) result = octavia_client.load_balancer_create( json={ 'loadbalancer': { 'description': 'Created by Zaza', 'admin_state_up': True, 'vip_subnet_id': subnet_id, 'name': 'zaza-lb-0', }}) lb_id = result['loadbalancer']['id'] @tenacity.retry(wait=tenacity.wait_fixed(1), reraise=True, stop=tenacity.stop_after_delay(900)) def wait_for_loadbalancer(octavia_client, load_balancer_id): resp = octavia_client.load_balancer_show(load_balancer_id) if resp['provisioning_status'] != 'ACTIVE': raise Exception('load balancer has not reached expected ' 'status: {}'.format(resp)) return resp logging.info('Awaiting loadbalancer to reach provisioning_status ' '"ACTIVE"') resp = wait_for_loadbalancer(octavia_client, lb_id) logging.info(resp)
def setup_for_attempt_operation(self, ip): """Create a loadbalancer. This is necessary so that the attempt is to show the load-balancer and this is an operator that the policy can stop. Unfortunately, octavia, whilst it has a policy for just listing load-balancers, unfortunately, it doesn't work; whereas showing the load-balancer can be stopped. NB this only works if the setup phase of the octavia tests have been completed. :param ip: the ip of for keystone. :type ip: str """ logging.info("Setting up loadbalancer.") auth = openstack_utils.get_overcloud_auth(address=ip) sess = openstack_utils.get_keystone_session(auth) octavia_client = openstack_utils.get_octavia_session_client(sess) neutron_client = openstack_utils.get_neutron_session_client(sess) if openstack_utils.dvr_enabled(): network_name = 'private_lb_fip_network' else: network_name = 'private' resp = neutron_client.list_networks(name=network_name) vip_subnet_id = resp['networks'][0]['subnets'][0] res = octavia_client.load_balancer_create( json={ 'loadbalancer': { 'description': 'Created by Zaza', 'admin_state_up': True, 'vip_subnet_id': vip_subnet_id, 'name': 'zaza-lb-0', }}) self.lb_id = res['loadbalancer']['id'] # now wait for it to get to the active state @tenacity.retry(wait=tenacity.wait_fixed(1), reraise=True, stop=tenacity.stop_after_delay(900)) def wait_for_lb_resource(client, resource_id): resp = client.load_balancer_show(resource_id) logging.info(resp['provisioning_status']) assert resp['provisioning_status'] == 'ACTIVE', ( 'load balancer resource has not reached ' 'expected provisioning status: {}' .format(resp)) return resp logging.info('Awaiting loadbalancer to reach provisioning_status ' '"ACTIVE"') resp = wait_for_lb_resource(octavia_client, self.lb_id) logging.info(resp) logging.info("Setup loadbalancer complete.")
def get_client_and_attempt_operation(self, ip): """Attempt to show the loadbalancer as a policyd override. This operation should pass normally, and fail when the rule has been overriden (see the `rule` class variable. :param ip: the IP address to get the session against. :type ip: str :raises: PolicydOperationFailedException if operation fails. """ octavia_client = openstack_utils.get_octavia_session_client( self.get_keystone_session_admin_user(ip)) try: octavia_client.load_balancer_show(self.lb_id) except octaviaclient.OctaviaClientException: raise PolicydOperationFailedException()
def get_client_and_attempt_operation(self, ip): """Attempt to list available provider drivers. This operation should pass normally, and fail when the rule has been overriden (see the `rule` class variable. :param ip: the IP address to get the session against. :type ip: str :raises: PolicydOperationFailedException if operation fails. """ octavia_client = openstack_utils.get_octavia_session_client( self.get_keystone_session_admin_user(ip)) try: octavia_client.provider_list() self.run_resource_cleanup = True except (octaviaclient.OctaviaClientException, keystoneauth1.exceptions.http.Forbidden): raise PolicydOperationFailedException()
def setUpClass(cls): """Run class setup for running LBaaSv2 service tests.""" super(LBAASv2Test, cls).setUpClass() cls.keystone_session = openstack_utils.get_overcloud_keystone_session() cls.neutron_client = openstack_utils.get_neutron_session_client( cls.keystone_session) cls.octavia_client = openstack_utils.get_octavia_session_client( cls.keystone_session) # NOTE(fnordahl): in the event of a test failure we do not want to run # tear down code as it will make debugging a problem virtually # impossible. To alleviate each test method will set the # `run_tearDown` instance variable at the end which will let us run # tear down only when there were no failure. cls.run_tearDown = False # List of load balancers created by this test cls.loadbalancers = [] # LIst of floating IPs created by this test cls.fips = []
def cleanup_for_attempt_operation(self, ip): """Remove the loadbalancer. :param ip: the ip of for keystone. :type ip: str """ logging.info("Deleting loadbalancer {}.".format(self.lb_id)) auth = openstack_utils.get_overcloud_auth(address=ip) sess = openstack_utils.get_keystone_session(auth) octavia_client = openstack_utils.get_octavia_session_client(sess) octavia_client.load_balancer_delete(self.lb_id) logging.info("Deleting loadbalancer in progress ...") @tenacity.retry(wait=tenacity.wait_fixed(1), reraise=True, stop=tenacity.stop_after_delay(900)) def wait_til_deleted(client, lb_id): lb_list = client.load_balancer_list() ids = [lb['id'] for lb in lb_list['loadbalancers']] assert lb_id not in ids, 'load balancer still deleting' wait_til_deleted(octavia_client, self.lb_id) logging.info("Deleted loadbalancer.")
def test_create_loadbalancer(self): """Create load balancer.""" keystone_session = openstack_utils.get_overcloud_keystone_session() neutron_client = openstack_utils.get_neutron_session_client( keystone_session) nova_client = openstack_utils.get_nova_session_client(keystone_session) # Get IP of the prepared payload instances payload_ips = [] for server in nova_client.servers.list(): payload_ips.append(server.networks['private'][0]) self.assertTrue(len(payload_ips) > 0) resp = neutron_client.list_networks(name='private') subnet_id = resp['networks'][0]['subnets'][0] if openstack_utils.dvr_enabled(): resp = neutron_client.list_networks(name='private_lb_fip_network') vip_subnet_id = resp['networks'][0]['subnets'][0] else: vip_subnet_id = subnet_id octavia_client = openstack_utils.get_octavia_session_client( keystone_session) for provider in self.get_lb_providers(octavia_client).keys(): logging.info( 'Creating loadbalancer with provider {}'.format(provider)) lb = self._create_lb_resources(octavia_client, provider, vip_subnet_id, subnet_id, payload_ips) lb_fp = openstack_utils.create_floating_ip( neutron_client, 'ext_net', port={'id': lb['vip_port_id']}) snippet = 'This is the default welcome page' assert snippet in self._get_payload(lb_fp['floating_ip_address']) logging.info('Found "{}" in page retrieved through load balancer ' ' (provider="{}") at "http://{}/"'.format( snippet, provider, lb_fp['floating_ip_address']))
def test_create_loadbalancer(self): """Create load balancer.""" keystone_session = openstack_utils.get_overcloud_keystone_session() neutron_client = openstack_utils.get_neutron_session_client( keystone_session) nova_client = openstack_utils.get_nova_session_client(keystone_session) # Get IP of the prepared payload instances payload_ips = [] for server in nova_client.servers.list(): payload_ips.append(server.networks['private'][0]) self.assertTrue(len(payload_ips) > 0) resp = neutron_client.list_networks(name='private') subnet_id = resp['networks'][0]['subnets'][0] if openstack_utils.dvr_enabled(): resp = neutron_client.list_networks(name='private_lb_fip_network') vip_subnet_id = resp['networks'][0]['subnets'][0] else: vip_subnet_id = subnet_id octavia_client = openstack_utils.get_octavia_session_client( keystone_session) result = octavia_client.load_balancer_create( json={ 'loadbalancer': { 'description': 'Created by Zaza', 'admin_state_up': True, 'vip_subnet_id': vip_subnet_id, 'name': 'zaza-lb-0', } }) lb_id = result['loadbalancer']['id'] lb_vip_port_id = result['loadbalancer']['vip_port_id'] @tenacity.retry(wait=tenacity.wait_fixed(1), reraise=True, stop=tenacity.stop_after_delay(900)) def wait_for_lb_resource(octavia_show_func, resource_id, operating_status=None): resp = octavia_show_func(resource_id) logging.info(resp['provisioning_status']) assert resp['provisioning_status'] == 'ACTIVE', ( 'load balancer resource has not reached ' 'expected provisioning status: {}'.format(resp)) if operating_status: logging.info(resp['operating_status']) assert resp['operating_status'] == operating_status, ( 'load balancer resource has not reached ' 'expected operating status: {}'.format(resp)) return resp logging.info('Awaiting loadbalancer to reach provisioning_status ' '"ACTIVE"') resp = wait_for_lb_resource(octavia_client.load_balancer_show, lb_id) logging.info(resp) result = octavia_client.listener_create( json={ 'listener': { 'loadbalancer_id': lb_id, 'name': 'listener1', 'protocol': 'HTTP', 'protocol_port': 80 }, }) listener_id = result['listener']['id'] logging.info('Awaiting listener to reach provisioning_status ' '"ACTIVE"') resp = wait_for_lb_resource(octavia_client.listener_show, listener_id) logging.info(resp) result = octavia_client.pool_create( json={ 'pool': { 'listener_id': listener_id, 'name': 'pool1', 'lb_algorithm': 'ROUND_ROBIN', 'protocol': 'HTTP', }, }) pool_id = result['pool']['id'] logging.info('Awaiting pool to reach provisioning_status ' '"ACTIVE"') resp = wait_for_lb_resource(octavia_client.pool_show, pool_id) logging.info(resp) result = octavia_client.health_monitor_create( json={ 'healthmonitor': { 'pool_id': pool_id, 'delay': 5, 'max_retries': 4, 'timeout': 10, 'type': 'HTTP', 'url_path': '/', }, }) healthmonitor_id = result['healthmonitor']['id'] logging.info('Awaiting healthmonitor to reach provisioning_status ' '"ACTIVE"') resp = wait_for_lb_resource(octavia_client.health_monitor_show, healthmonitor_id) logging.info(resp) for ip in payload_ips: result = octavia_client.member_create(pool_id=pool_id, json={ 'member': { 'subnet_id': subnet_id, 'address': ip, 'protocol_port': 80, }, }) member_id = result['member']['id'] logging.info('Awaiting member to reach provisioning_status ' '"ACTIVE"') resp = wait_for_lb_resource(lambda x: octavia_client.member_show( pool_id=pool_id, member_id=x), member_id, operating_status='ONLINE') logging.info(resp) lb_fp = openstack_utils.create_floating_ip(neutron_client, 'ext_net', port={'id': lb_vip_port_id}) @tenacity.retry(wait=tenacity.wait_fixed(1), reraise=True, stop=tenacity.stop_after_delay(900)) def get_payload(): return subprocess.check_output([ 'wget', '-O', '-', 'http://{}/'.format( lb_fp['floating_ip_address']) ], universal_newlines=True) snippet = 'This is the default welcome page' assert snippet in get_payload() logging.info('Found "{}" in page retrieved through load balancer at ' '"http://{}/"'.format(snippet, lb_fp['floating_ip_address']))