def test_create_pool_conflict(self): lbaas = self.useFixture(k_fix.MockLBaaSClient()).client cls = d_lbaasv2.LBaaSv2Driver m_driver = mock.Mock(spec=d_lbaasv2.LBaaSv2Driver) lb_algorithm = 'ROUND_ROBIN' pool = obj_lbaas.LBaaSPool( name='TEST_NAME', project_id='TEST_PROJECT', protocol='TCP', listener_id='A57B7771-6050-4CA8-A63C-443493EC98AB', loadbalancer_id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C') req = { 'pool': { 'name': pool.name, 'project_id': pool.project_id, 'listener_id': pool.listener_id, 'loadbalancer_id': pool.loadbalancer_id, 'protocol': pool.protocol, 'lb_algorithm': lb_algorithm } } lbaas.create_lbaas_pool.side_effect = n_exc.StateInvalidClient self.assertRaises(n_exc.StateInvalidClient, cls._create_pool, m_driver, pool) lbaas.create_lbaas_pool.assert_called_once_with(req) m_driver._cleanup_bogus_pool.assert_called_once_with( lbaas, pool, lb_algorithm)
def test_create_pool_with_different_lb_algorithm(self): cls = d_lbaasv2.LBaaSv2Driver m_driver = mock.Mock(spec=d_lbaasv2.LBaaSv2Driver) lbaas = self.useFixture(k_fix.MockLBaaSClient()).client lb_algorithm = 'SOURCE_IP_PORT' pool = obj_lbaas.LBaaSPool( name='TEST_NAME', project_id='TEST_PROJECT', protocol='TCP', listener_id='A57B7771-6050-4CA8-A63C-443493EC98AB', loadbalancer_id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C') pool_id = 'D4F35594-27EB-4F4C-930C-31DD40F53B77' req = { 'name': pool.name, 'project_id': pool.project_id, 'listener_id': pool.listener_id, 'loadbalancer_id': pool.loadbalancer_id, 'protocol': pool.protocol, 'lb_algorithm': lb_algorithm} resp = o_pool.Pool(id=pool_id) lbaas.create_pool.return_value = resp CONF.set_override('lb_algorithm', lb_algorithm, group='octavia_defaults') self.addCleanup(CONF.clear_override, 'lb_algorithm', group='octavia_defaults') ret = cls._create_pool(m_driver, pool) lbaas.create_pool.assert_called_once_with(**req) for attr in pool.obj_fields: self.assertEqual(getattr(pool, attr), getattr(ret, attr)) self.assertEqual(pool_id, ret.id)
def test_find_pool_by_listener(self): lbaas = self.useFixture(k_fix.MockLBaaSClient()).client cls = d_lbaasv2.LBaaSv2Driver m_driver = mock.Mock(spec=d_lbaasv2.LBaaSv2Driver) pool = obj_lbaas.LBaaSPool( name='TEST_NAME', project_id='TEST_PROJECT', protocol='TCP', listener_id='A57B7771-6050-4CA8-A63C-443493EC98AB', loadbalancer_id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C') pool_id = 'D4F35594-27EB-4F4C-930C-31DD40F53B77' resp = { 'pools': [{ 'id': pool_id, 'listeners': [{ 'id': pool.listener_id }] }] } lbaas.list_lbaas_pools.return_value = resp ret = cls._find_pool(m_driver, pool) lbaas.list_lbaas_pools.assert_called_once_with( name=pool.name, project_id=pool.project_id, loadbalancer_id=pool.loadbalancer_id, protocol=pool.protocol) for attr in pool.obj_fields: self.assertEqual(getattr(pool, attr), getattr(ret, attr)) self.assertEqual(pool_id, ret.id)
def test_create_pool(self): neutron = self.useFixture(k_fix.MockNeutronClient()).client cls = d_lbaasv2.LBaaSv2Driver m_driver = mock.Mock(spec=d_lbaasv2.LBaaSv2Driver) lb_algorithm = 'ROUND_ROBIN' pool = obj_lbaas.LBaaSPool( name='TEST_NAME', project_id='TEST_PROJECT', protocol='TCP', listener_id='A57B7771-6050-4CA8-A63C-443493EC98AB', loadbalancer_id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C') pool_id = 'D4F35594-27EB-4F4C-930C-31DD40F53B77' req = { 'pool': { 'name': pool.name, 'project_id': pool.project_id, 'tenant_id': pool.project_id, 'listener_id': pool.listener_id, 'loadbalancer_id': pool.loadbalancer_id, 'protocol': pool.protocol, 'lb_algorithm': lb_algorithm } } resp = {'pool': {'id': pool_id}} neutron.create_lbaas_pool.return_value = resp ret = cls._create_pool(m_driver, pool) neutron.create_lbaas_pool.assert_called_once_with(req) for attr in pool.obj_fields: self.assertEqual(getattr(pool, attr), getattr(ret, attr)) self.assertEqual(pool_id, ret.id)
def test_ensure_l7policy(self): cls = d_lbaasv2.LBaaSv2Driver m_driver = mock.Mock(spec=d_lbaasv2.LBaaSv2Driver) expected_resp = mock.sentinel.expected_resp loadbalancer = mock.sentinel.expected_resp route_name = 'ROUTE_NAME' namespace = 'NAMESPACE' listener_id = 'D4F35594-27EB-4F4C-930C-31DD40F53B77' pool = obj_lbaas.LBaaSPool(id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C', project_id='TEST_PROJECT', name='NAME', loadbalancer_id='010101', listener_id='12345', protocol='TCP') m_driver._ensure_provisioned.return_value = expected_resp cls.ensure_l7_policy(m_driver, namespace, route_name, loadbalancer, pool, listener_id) m_driver._ensure_provisioned.assert_called_once_with( loadbalancer, mock.ANY, m_driver._create_l7_policy, m_driver._find_l7_policy) l7policy = m_driver._ensure_provisioned.call_args[0][1] self.assertEqual("%s%s" % (namespace, route_name), l7policy.name) self.assertEqual(listener_id, l7policy.listener_id) self.assertEqual(pool.id, l7policy.redirect_pool_id) self.assertEqual(pool.project_id, l7policy.project_id)
def ensure_pool(self, endpoints, loadbalancer, listener): return obj_lbaas.LBaaSPool(name=listener.name, project_id=loadbalancer.project_id, loadbalancer_id=loadbalancer.id, listener_id=listener.id, protocol=listener.protocol, id=uuidutils.generate_uuid())
def test_ensure_member(self): cls = d_lbaasv2.LBaaSv2Driver m_driver = mock.Mock(spec=d_lbaasv2.LBaaSv2Driver) expected_resp = mock.sentinel.expected_resp loadbalancer = obj_lbaas.LBaaSLoadBalancer( id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C', project_id='TEST_PROJECT') pool = obj_lbaas.LBaaSPool(project_id='TEST_PROJECT', id='D4F35594-27EB-4F4C-930C-31DD40F53B77') subnet_id = 'D3FA400A-F543-4B91-9CD3-047AF0CE42D1' ip = '1.2.3.4' port = 1234 namespace = 'TEST_NAMESPACE' name = 'TEST_NAME' target_ref = {'namespace': namespace, 'name': name} m_driver._ensure_provisioned.return_value = expected_resp resp = cls.ensure_member(m_driver, loadbalancer, pool, subnet_id, ip, port, target_ref['namespace'], target_ref['name']) m_driver._ensure_provisioned.assert_called_once_with( loadbalancer, mock.ANY, m_driver._create_member, m_driver._find_member) member = m_driver._ensure_provisioned.call_args[0][1] self.assertEqual("%s/%s:%s" % (namespace, name, port), member.name) self.assertEqual(pool.project_id, member.project_id) self.assertEqual(pool.id, member.pool_id) self.assertEqual(subnet_id, member.subnet_id) self.assertEqual(ip, str(member.ip)) self.assertEqual(port, member.port) self.assertEqual(expected_resp, resp)
def ensure_pool(self, loadbalancer, listener): return obj_lbaas.LBaaSPool(name=listener.name, project_id=loadbalancer.project_id, loadbalancer_id=loadbalancer.id, listener_id=listener.id, protocol=listener.protocol, id=str(uuid.uuid4()))
def ensure_pool(self, loadbalancer, listener): pool = obj_lbaas.LBaaSPool(name=listener.name, project_id=loadbalancer.project_id, loadbalancer_id=loadbalancer.id, listener_id=listener.id, protocol=listener.protocol) return self._ensure_provisioned(loadbalancer, pool, self._create_pool, self._find_pool)
def ensure_pool_attached_to_lb(self, loadbalancer, namespace, svc_name, protocol): name = self.get_loadbalancer_pool_name(loadbalancer.name, namespace, svc_name) pool = obj_lbaas.LBaaSPool(name=name, project_id=loadbalancer.project_id, loadbalancer_id=loadbalancer.id, listener_id=None, protocol=protocol) return self._ensure_provisioned(loadbalancer, pool, self._create_pool, self._find_pool_by_name)
def _test_get_pool_by_name_found(self, listener_is_empty): lbaas = self.useFixture(k_fix.MockLBaaSClient()).client cls = d_lbaasv2.LBaaSv2Driver m_driver = mock.Mock(spec=d_lbaasv2.LBaaSv2Driver) pool_name = 'KUKU' pool_lb_id = "607226db-27ef-4d41-ae89-f2a800e9c2db" pool_project_id = "e3cd678b11784734bc366148aa37580e" pool_id = "ddb2b28f-89e9-45d3-a329-a359c3e39e4a" pool_protocol = "HTTP" pool_listener_id = "023f2e34-7806-443b-bfae-16c324569a3d" if listener_is_empty: resp_listeners = [] else: resp_listeners = [{"id": pool_listener_id}] listener_id = (resp_listeners[0]['id'] if resp_listeners else None) expected_result = obj_lbaas.LBaaSPool( name=pool_name, project_id=pool_project_id, loadbalancer_id=pool_lb_id, listener_id=listener_id, protocol=pool_protocol, id=pool_id) resp = {"pools": [ { "protocol": pool_protocol, "loadbalancers": [ { "id": pool_lb_id } ], "listeners": resp_listeners, "project_id": pool_project_id, "id": pool_id, "name": pool_name } ]} lbaas.list_lbaas_pools.return_value = resp pool = cls.get_pool_by_name(m_driver, pool_name, pool_project_id) lbaas.list_lbaas_pools.assert_called_once() for attr in expected_result.obj_fields: self.assertEqual(getattr(expected_result, attr), getattr(pool, attr))
def test_find_pool_by_listener_not_found(self): lbaas = self.useFixture(k_fix.MockLBaaSClient()).client cls = d_lbaasv2.LBaaSv2Driver m_driver = mock.Mock(spec=d_lbaasv2.LBaaSv2Driver) pool = obj_lbaas.LBaaSPool( name='TEST_NAME', project_id='TEST_PROJECT', protocol='TCP', listener_id='A57B7771-6050-4CA8-A63C-443493EC98AB', loadbalancer_id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C') resp = [] lbaas.pools.return_value = resp ret = cls._find_pool(m_driver, pool) lbaas.pools.assert_called_once_with( name=pool.name, project_id=pool.project_id, loadbalancer_id=pool.loadbalancer_id, protocol=pool.protocol) self.assertIsNone(ret)
def get_pool_by_name(self, pool_name, project_id): lbaas = clients.get_loadbalancer_client() # NOTE(yboaron): pool_name should be constructed using # get_loadbalancer_pool_name function, which means that pool's name # is unique pools = lbaas.pools(project_id=project_id) for entry in pools: if not entry: continue if entry.name == pool_name: listener_id = (entry.listeners[0].id if entry.listeners else None) return obj_lbaas.LBaaSPool( name=entry.name, project_id=entry.project_id, loadbalancer_id=entry.loadbalancers[0].id, listener_id=listener_id, protocol=entry.protocol, id=entry.id) return None
def get_pool_by_name(self, pool_name, project_id): lbaas = clients.get_loadbalancer_client() # NOTE(yboaron): pool_name should be constructed using # get_loadbalancer_pool_name function, which means that pool's name # is unique pools_list = lbaas.list_lbaas_pools( project_id=project_id) for entry in pools_list['pools']: if not entry: continue if entry['name'] == pool_name: listener_id = (entry['listeners'][0]['id'] if entry['listeners'] else None) return obj_lbaas.LBaaSPool( name=entry['name'], project_id=entry['project_id'], loadbalancer_id=entry['loadbalancers'][0]['id'], listener_id=listener_id, protocol=entry['protocol'], id=entry['id']) return None
def test_create_pool_conflict(self): cls = d_lbaasv2.LBaaSv2Driver m_driver = mock.Mock(spec=d_lbaasv2.LBaaSv2Driver) lb_algorithm = 'ROUND_ROBIN' pool = obj_lbaas.LBaaSPool( name='TEST_NAME', project_id='TEST_PROJECT', protocol='TCP', listener_id='A57B7771-6050-4CA8-A63C-443493EC98AB', loadbalancer_id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C') req = { 'name': pool.name, 'project_id': pool.project_id, 'listener_id': pool.listener_id, 'loadbalancer_id': pool.loadbalancer_id, 'protocol': pool.protocol, 'lb_algorithm': lb_algorithm } m_driver._post_lb_resource.side_effect = n_exc.StateInvalidClient self.assertRaises(n_exc.StateInvalidClient, cls._create_pool, m_driver, pool) m_driver._post_lb_resource.assert_called_once_with(o_pool.Pool, req)
def test_sync_router_pool_valid_pool(self): m_handler = mock.Mock(spec=h_route.OcpRouteHandler) m_handler._l7_router = obj_lbaas.LBaaSLoadBalancer( id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C', project_id='TEST_PROJECT') m_handler._drv_lbaas = mock.Mock(spec=drv_base.LBaaSDriver) ret_pool = obj_lbaas.LBaaSPool( name='TEST_NAME', project_id='TEST_PROJECT', protocol='TCP', listener_id='A57B7771-6050-4CA8-A63C-443493EC98AB', loadbalancer_id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C') m_handler._drv_lbaas.get_pool_by_name.return_value = None m_handler._drv_lbaas.ensure_pool_attached_to_lb.return_value = ret_pool route = { 'metadata': { 'namespace': 'namespace' }, 'spec': { 'host': 'www.test.com', 'path': 'mypath', 'to': { 'name': 'target_service' } } } route_spec = obj_route.RouteSpec(host='www.test.com1', path='mypath', to_service='target_service') route_state = obj_route.RouteState() h_route.OcpRouteHandler._sync_router_pool(m_handler, route, route_spec, route_state) self.assertEqual(route_state.router_pool, ret_pool)
def ensure_pool_attached_to_lb(self, loadbalancer, namespace, svc_name, protocol): return obj_lbaas.LBaaSPool(id=uuidutils.generate_uuid(), loadbalancer_id=loadbalancer.id, project_id=loadbalancer.project_id, protocol=protocol)
def ensure_pool_attached_to_lb(self, loadbalancer, namespace, svc_name, protocol): return obj_lbaas.LBaaSPool(id=str(uuid.uuid4()), loadbalancer_id=loadbalancer.id, project_id=loadbalancer.project_id, protocol=protocol)