def test_create_load_balancer_failed_on_lb_create(self): def _raise_exception1(*args): raise exception.LoadBalancerNotFoundByName( load_balancer_name=self.create_kwargs['name']) def _raise_exception2(*args): raise Exception() self.mox.StubOutWithMock(db, 'load_balancer_create') self.mox.StubOutWithMock(db, 'load_balancer_get_by_name') self.mox.StubOutWithMock(utils, 'get_all_domain_names') self.mox.StubOutWithMock(utils, 'str_uuid') self.mox.StubOutWithMock(utils, 'gen_dns_prefix') db.load_balancer_get_by_name( self.ctxt, self.create_kwargs['name']).WithSideEffects( _raise_exception1).AndReturn(None) utils.get_all_domain_names().AndReturn(list()) utils.str_uuid().AndReturn(self.uuid) utils.gen_dns_prefix().AndReturn(self.dns_prefix) db.load_balancer_create( self.ctxt, self.lb).WithSideEffects( _raise_exception2).AndReturn(None) self.mox.ReplayAll() self.assertRaises(exception.CreateLoadBalancerFailed, http.create_load_balancer, self.ctxt, **self.create_kwargs) self.mox.VerifyAll()
def test_create_load_balancer(self): def _raise_exception(*args): raise exception.LoadBalancerNotFoundByName( load_balancer_name=self.create_kwargs['name']) self.mox.StubOutWithMock(utils, 'get_all_domain_names') self.mox.StubOutWithMock(utils, 'str_uuid') self.mox.StubOutWithMock(utils, 'gen_dns_prefix') self.mox.StubOutWithMock(db, 'load_balancer_get_by_name') self.mox.StubOutWithMock(db, 'load_balancer_create') self.mox.StubOutWithMock(db, 'load_balancer_config_create') self.mox.StubOutWithMock(db, 'load_balancer_domain_create') self.mox.StubOutWithMock( db, 'load_balancer_instance_association_create') db.load_balancer_get_by_name( self.ctxt, self.create_kwargs['name']).WithSideEffects( _raise_exception).AndReturn(None) utils.get_all_domain_names().AndReturn(list()) utils.str_uuid().AndReturn(self.uuid) utils.gen_dns_prefix().AndReturn(self.dns_prefix) db.load_balancer_create(self.ctxt, self.lb).AndReturn(self.lb_ref) db.load_balancer_config_create( self.ctxt, self.config).AndReturn(self.config_ref) for index, domain in enumerate(self.http_server_names): domain_values = { 'load_balancer_id': self.load_balancer_id, 'name': domain, } self.tmp = copy.deepcopy(domain_values) self.tmp['id'] = index + 1 domain_ref = models.LoadBalancerDomain() domain_ref.update(self.tmp) db.load_balancer_domain_create( self.ctxt, domain_values).AndReturn(domain_ref) for uuid in self.create_kwargs['instance_uuids']: association_values = { 'load_balancer_id': self.load_balancer_id, 'instance_uuid': uuid, } association_ref = models.LoadBalancerInstanceAssociation() association_ref.update(association_values) db.load_balancer_instance_association_create( self.ctxt, association_values).AndReturn(association_ref) self.mox.ReplayAll() r = http.create_load_balancer(self.ctxt, **self.create_kwargs) self.mox.VerifyAll() self.assertEqual(r, {'data': {'uuid': self.uuid}})
def test_create_load_balancer(self): def _raise_exception(*args): raise exception.LoadBalancerNotFoundByName( load_balancer_name=self.create_kwargs['name']) self.mox.StubOutWithMock(utils, 'str_uuid') self.mox.StubOutWithMock(utils, 'gen_dns_prefix') self.mox.StubOutWithMock(utils, 'allocate_listen_port') self.mox.StubOutWithMock(db, 'load_balancer_get_by_name') self.mox.StubOutWithMock(db, 'load_balancer_create') self.mox.StubOutWithMock(db, 'load_balancer_config_create') self.mox.StubOutWithMock(db, 'load_balancer_domain_create') self.mox.StubOutWithMock( db, 'load_balancer_instance_association_create') db.load_balancer_get_by_name( self.ctxt, self.create_kwargs['name']).WithSideEffects( _raise_exception).AndReturn(None) utils.str_uuid().AndReturn(self.uuid) utils.gen_dns_prefix().AndReturn(self.dns_prefix) utils.allocate_listen_port().AndReturn(self.listen_port) db.load_balancer_create( self.ctxt, self.lb_values).AndReturn(self.lb_ref) db.load_balancer_config_create( self.ctxt, self.config_values).AndReturn(self.config_ref) for uuid in self.create_kwargs['instance_uuids']: association_values = { 'load_balancer_id': self.load_balancer_id, 'instance_uuid': uuid, } association_ref = models.LoadBalancerInstanceAssociation() association_ref.update(association_values) db.load_balancer_instance_association_create( self.ctxt, association_values).AndReturn(association_ref) self.mox.ReplayAll() r = tcp.create_load_balancer(self.ctxt, **self.create_kwargs) self.mox.VerifyAll() self.assertEqual(r, {'data': {'uuid': self.uuid}})
def setUp(self): super(DBApiTestCase, self).setUp() engine = get_engine() self.connection = engine.connect() self.configs = dict() self.user_id = 'fake-user-0' self.tenant_id = 'fake-project-0' self.load_balancer_id = utils.str_uuid() self.lb = { 'id': self.load_balancer_id, 'user_id': self.user_id, 'tenant_id': self.tenant_id, 'free': False, 'name': 'test-lb-1', 'state': 'creating', 'protocol': 'proto-1', 'dns_prefix': 'abcdefg', 'listen_port': 80, 'instance_port': 80, } self.configs['lb'] = self.lb self.config = { 'load_balancer_id': self.load_balancer_id, 'balancing_method': 'round_robin', 'health_check_timeout_ms': 5, 'health_check_interval_ms': 500, 'health_check_target_path': '/', 'health_check_healthy_threshold': 5, 'health_check_unhealthy_threshold': 3, } self.configs['config'] = self.config self.domain = { 'load_balancer_id': self.load_balancer_id, 'name': "www.abc.com", } self.configs['domains'] = [self.domain] self.association = { 'load_balancer_id': self.load_balancer_id, 'instance_uuid': 'inst-0', 'instance_ip': '192.168.1.1', } self.configs['associations'] = [self.association] self.context = context.get_context(self.user_id, self.tenant_id)
def checker_routine(*args, **kwargs): LOG.info('nozzle checker starting...') broadcast = kwargs['broadcast'] states = [state.CREATING, state.UPDATING, state.DELETING] while True: eventlet.sleep(6) msg_type = 'lb' msg_uuid = utils.str_uuid() try: ctxt = context.get_admin_context() all_load_balancers = db.load_balancer_get_all(ctxt) transient_load_balancers = filter(lambda x: x.state in states, all_load_balancers) for load_balancer_ref in transient_load_balancers: try: result = dict() message = dict() if load_balancer_ref.state == state.CREATING: message['cmd'] = 'create_lb' result = api.format_msg_to_worker(load_balancer_ref) elif load_balancer_ref.state == state.UPDATING: message['cmd'] = 'update_lb' result = api.format_msg_to_worker(load_balancer_ref) elif load_balancer_ref.state == state.DELETING: message['cmd'] = 'delete_lb' result['user_id'] = load_balancer_ref['user_id'] result['tenant_id'] = load_balancer_ref['project_id'] result['uuid'] = load_balancer_ref['uuid'] result['protocol'] = load_balancer_ref['protocol'] message['args'] = result request_msg = jsonutils.dumps(message) LOG.debug(">>>>>>> worker: %s" % request_msg) broadcast.send_multipart([msg_type, msg_uuid, request_msg]) except Exception as exp: LOG.exception(str(exp)) continue except Exception as exp: LOG.exception(str(exp)) continue
except Exception, exp: raise exception.CreateLoadBalancerFailed(msg=str(exp)) else: raise exception.CreateLoadBalancerFailed(msg='already exists!') # create load balancer config_ref = None load_balancer_ref = None associated_instances = [] try: load_balancer_values = { 'name': kwargs['name'], 'user_id': kwargs['user_id'], 'project_id': kwargs['tenant_id'], 'uuid': utils.str_uuid(), 'free': kwargs['free'], 'protocol': kwargs['protocol'], 'state': state.CREATING, 'dns_prefix': utils.gen_dns_prefix(), 'listen_port': utils.allocate_listen_port(), 'instance_port': kwargs['instance_port'], } load_balancer_ref = db.load_balancer_create(context, load_balancer_values) config_values = { 'load_balancer_id': load_balancer_ref.id, 'balancing_method': config['balancing_method'], 'health_check_timeout_ms': config['health_check_timeout_ms'], 'health_check_interval_ms': config['health_check_interval_ms'],