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_update_load_balancer_http_servers(self): kwargs = copy.deepcopy(self.delete_kwargs) new_http_servers = ['www.abc.com', 'www.123.com'] kwargs['http_server_names'] = new_http_servers self.mox.StubOutWithMock(utils, 'get_all_domain_names') self.mox.StubOutWithMock(db, 'load_balancer_get_by_uuid') self.mox.StubOutWithMock(db, 'load_balancer_domain_create') self.mox.StubOutWithMock(db, 'load_balancer_domain_destroy') self.mox.StubOutWithMock(db, 'load_balancer_update_state') load_balancer_ref = self.lb_ref for index, domain in enumerate(self.http_server_names): domain_values = { 'id': index + 1, 'load_balancer_id': load_balancer_ref.id, 'name': domain, } domain_ref = models.LoadBalancerDomain() domain_ref.update(domain_values) load_balancer_ref.domains.append(domain_ref) db.load_balancer_get_by_uuid( self.ctxt, self.uuid).AndReturn(load_balancer_ref) utils.get_all_domain_names().AndReturn(list()) old_http_servers = map(lambda x: x['name'], load_balancer_ref.domains) need_deleted_domains = filter(lambda x: x not in new_http_servers, old_http_servers) need_created_domains = filter(lambda x: x not in old_http_servers, new_http_servers) for domain in load_balancer_ref.domains: if domain.name in need_deleted_domains: db.load_balancer_domain_destroy( self.ctxt, domain.id).AndReturn(None) for domain in need_created_domains: domain_values = { 'load_balancer_id': load_balancer_ref.id, 'name': domain, } db.load_balancer_domain_create( self.ctxt, domain_values).AndReturn(None) db.load_balancer_update_state( self.ctxt, self.uuid, state.UPDATING).AndReturn(None) self.mox.ReplayAll() r = http.update_load_balancer_http_servers(self.ctxt, **kwargs) self.mox.VerifyAll() self.assertRaises(r, None)
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}})
kwargs['free'] = False except Exception, e: raise exception.CreateLoadBalancerFailed(msg='unknown error!') # check if load balancer already exists try: db.load_balancer_get_by_name(context, kwargs['name']) except exception.LoadBalancerNotFoundByName: pass except Exception, exp: raise exception.CreateLoadBalancerFailed(msg=str(exp)) else: raise exception.CreateLoadBalancerFailed(msg='already exist!') # check if any domain to be created already exists all_domain_names = utils.get_all_domain_names() for name in kwargs['http_server_names']: if name in all_domain_names: exp = exception.LoadBalancerDomainExists(domain_name=name) raise exception.CreateLoadBalancerFailed(msg=str(exp)) # create load balancer config_ref = None load_balancer_ref = None inserted_domains = [] associated_instances = [] try: load_balancer_values = { 'name': kwargs['name'], 'user_id': kwargs['user_id'],