示例#1
0
func (s *ExternalSuite) TestSwapWithDifferentRouterKinds(c *check.C) {
	config.Set("hipache:redis-server", "127.0.0.1:6379")
	config.Set("hipache:redis-db", 5)
	backend1 := "bb1"
	backend2 := "bb2"
	r1, err := router.Get("fake")
	c.Assert(err, check.IsNil)
	r2, err := router.Get("hipache")
	c.Assert(err, check.IsNil)
	err = r1.AddBackend(backend1)
	c.Assert(err, check.IsNil)
	defer r1.RemoveBackend(backend1)
	addr1, _ := url.Parse("http://127.0.0.1")
	err = r1.AddRoute(backend1, addr1)
	c.Assert(err, check.IsNil)
	defer r1.RemoveRoute(backend1, addr1)
	err = r2.AddBackend(backend2)
	c.Assert(err, check.IsNil)
	defer r2.RemoveBackend(backend2)
	addr2, _ := url.Parse("http://10.10.10.10")
	err = r2.AddRoute(backend2, addr2)
	c.Assert(err, check.IsNil)
	defer r2.RemoveRoute(backend2, addr2)
	err = router.Swap(r1, backend1, backend2, false)
	c.Assert(err, check.ErrorMatches, `swap is only allowed between routers of the same kind. "bb1" uses "fake", "bb2" uses "hipache"`)
	err = router.Swap(r2, backend1, backend2, false)
	c.Assert(err, check.ErrorMatches, `swap is only allowed between routers of the same kind. "bb1" uses "fake", "bb2" uses "hipache"`)
}
示例#2
0
func (s *S) TestShouldBeRegisteredAllowingPrefixes(c *check.C) {
	config.Set("routers:inst1:type", "vulcand")
	config.Set("routers:inst1:api-url", "http://localhost:1")
	config.Set("routers:inst1:domain", "inst1.example.com")
	config.Set("routers:inst2:type", "vulcand")
	config.Set("routers:inst2:api-url", "http://localhost:2")
	config.Set("routers:inst2:domain", "inst2.example.com")
	defer config.Unset("routers:inst1:type")
	defer config.Unset("routers:inst1:api-url")
	defer config.Unset("routers:inst1:domain")
	defer config.Unset("routers:inst2:type")
	defer config.Unset("routers:inst2:api-url")
	defer config.Unset("routers:inst2:domain")
	got1, err := router.Get("inst1")
	c.Assert(err, check.IsNil)
	got2, err := router.Get("inst2")
	c.Assert(err, check.IsNil)
	r1, ok := got1.(*vulcandRouter)
	c.Assert(ok, check.Equals, true)
	c.Assert(r1.client.Addr, check.Equals, "http://localhost:1")
	c.Assert(r1.domain, check.Equals, "inst1.example.com")
	c.Assert(r1.prefix, check.Equals, "routers:inst1")
	r2, ok := got2.(*vulcandRouter)
	c.Assert(ok, check.Equals, true)
	c.Assert(r2.client.Addr, check.Equals, "http://localhost:2")
	c.Assert(r2.domain, check.Equals, "inst2.example.com")
	c.Assert(r2.prefix, check.Equals, "routers:inst2")
}
示例#3
0
func (s *S) TestShouldBeRegistered(c *check.C) {
	r, err := router.Get("fake")
	c.Assert(err, check.IsNil)
	c.Assert(r, check.FitsTypeOf, &fakeRouter{})
	r, err = router.Get("fake-hc")
	c.Assert(err, check.IsNil)
	c.Assert(r, check.FitsTypeOf, &hcRouter{})
}
示例#4
0
文件: app.go 项目: tsuru/tsuru
func (app *App) Router() (router.Router, error) {
	routerName, err := app.GetRouter()
	if err != nil {
		return nil, err
	}
	return router.Get(routerName)
}
示例#5
0
func Example() {
	router, err := router.Get("hipache")
	if err != nil {
		panic(err)
	}
	err = router.AddBackend("myapp")
	if err != nil {
		panic(err)
	}
	url, err := url.Parse("http://10.10.10.10:8080")
	if err != nil {
		panic(err)
	}
	err = router.AddRoute("myapp", url)
	if err != nil {
		panic(err)
	}
	addr, _ := router.Addr("myapp")
	fmt.Println("Please access:", addr)
	err = router.RemoveRoute("myapp", url)
	if err != nil {
		panic(err)
	}
	err = router.RemoveBackend("myapp")
	if err != nil {
		panic(err)
	}
}
示例#6
0
func (s *S) TestShouldBeRegisteredAllowingPrefixes(c *check.C) {
	config.Set("routers:inst1:type", "hipache")
	config.Set("routers:inst2:type", "hipache")
	defer config.Unset("routers:inst1:type")
	defer config.Unset("routers:inst2:type")
	got1, err := router.Get("inst1")
	c.Assert(err, check.IsNil)
	got2, err := router.Get("inst2")
	c.Assert(err, check.IsNil)
	r1, ok := got1.(*hipacheRouter)
	c.Assert(ok, check.Equals, true)
	c.Assert(r1.prefix, check.Equals, "routers:inst1")
	r2, ok := got2.(*hipacheRouter)
	c.Assert(ok, check.Equals, true)
	c.Assert(r2.prefix, check.Equals, "routers:inst2")
}
示例#7
0
func getRouterForApp(app provision.App) (router.Router, error) {
	routerName, err := app.GetRouter()
	if err != nil {
		return nil, err
	}
	return router.Get(routerName)
}
示例#8
0
文件: plan.go 项目: tsuru/tsuru
func (plan *Plan) Save() error {
	if plan.Name == "" {
		return PlanValidationError{"name"}
	}
	if plan.CpuShare < 2 {
		return ErrLimitOfCpuShare
	}
	if plan.Memory > 0 && plan.Memory < 4194304 {
		return ErrLimitOfMemory
	}
	if plan.Router != "" {
		_, err := router.Get(plan.Router)
		if err != nil {
			return PlanValidationError{fmt.Sprintf("router error: %v", err)}
		}
	}
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	defer conn.Close()
	if plan.Default {
		_, err = conn.Plans().UpdateAll(bson.M{"default": true}, bson.M{"$unset": bson.M{"default": false}})
		if err != nil {
			return err
		}
	}
	err = conn.Plans().Insert(plan)
	if err != nil && strings.Contains(err.Error(), "duplicate key") {
		return ErrPlanAlreadyExists
	}
	return err
}
示例#9
0
func (s *ExternalSuite) TestSwap(c *check.C) {
	backend1 := "b1"
	backend2 := "b2"
	r, err := router.Get("fake")
	c.Assert(err, check.IsNil)
	r.AddBackend(backend1)
	addr1, _ := url.Parse("http://127.0.0.1")
	r.AddRoute(backend1, addr1)
	r.AddBackend(backend2)
	addr2, _ := url.Parse("http://10.10.10.10")
	r.AddRoute(backend2, addr2)
	err = router.Swap(r, backend1, backend2)
	c.Assert(err, check.IsNil)
	routes1, err := r.Routes(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(routes1, check.DeepEquals, []*url.URL{addr1})
	routes2, err := r.Routes(backend2)
	c.Assert(err, check.IsNil)
	c.Assert(routes2, check.DeepEquals, []*url.URL{addr2})
	name1, err := router.Retrieve(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(name1, check.Equals, backend2)
	name2, err := router.Retrieve(backend2)
	c.Assert(err, check.IsNil)
	c.Assert(name2, check.Equals, backend1)
}
示例#10
0
文件: plan.go 项目: RichardKnop/tsuru
func (plan *Plan) Save() error {
	if plan.Name == "" {
		return PlanValidationError{"name"}
	}
	if plan.CpuShare == 0 {
		return PlanValidationError{"cpushare"}
	}
	if plan.Router != "" {
		_, err := router.Get(plan.Router)
		if err != nil {
			return PlanValidationError{"router"}
		}
	}
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	defer conn.Close()
	if plan.Default {
		_, err := conn.Plans().UpdateAll(bson.M{"default": true}, bson.M{"$unset": bson.M{"default": false}})
		if err != nil {
			return err
		}
	}
	err = conn.Plans().Insert(plan)
	if err != nil && strings.Contains(err.Error(), "duplicate key") {
		return ErrPlanAlreadyExists
	}
	return err
}
示例#11
0
func (s *S) TestSetCName(c *check.C) {
	vRouter, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	err = vRouter.AddBackend("myapp")
	c.Assert(err, check.IsNil)
	u1, _ := url.Parse("http://1.1.1.1:111")
	u2, _ := url.Parse("http://2.2.2.2:222")
	err = vRouter.AddRoute("myapp", u1)
	c.Assert(err, check.IsNil)
	err = vRouter.AddRoute("myapp", u2)
	c.Assert(err, check.IsNil)
	err = vRouter.SetCName("myapp.cname.example.com", "myapp")
	c.Assert(err, check.IsNil)
	appFrontend, err := s.engine.GetFrontend(engine.FrontendKey{
		Id: "tsuru_myapp.vulcand.example.com",
	})
	c.Assert(err, check.IsNil)
	cnameFrontend, err := s.engine.GetFrontend(engine.FrontendKey{
		Id: "tsuru_myapp.cname.example.com",
	})
	c.Assert(err, check.IsNil)
	c.Assert(cnameFrontend.BackendId, check.DeepEquals, appFrontend.BackendId)
	c.Assert(cnameFrontend.Route, check.Equals, `Host("myapp.cname.example.com")`)
	c.Assert(cnameFrontend.Type, check.Equals, "http")
}
示例#12
0
func (s *S) TestAddBackendRollbackOnError(c *check.C) {
	s.vulcandServer.Close()
	scrollApp := scroll.NewApp()
	var postRequestCount int
	conditionalHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			postRequestCount++
			if postRequestCount > 1 {
				w.WriteHeader(http.StatusInternalServerError)
				return
			}
		}
		scrollApp.GetHandler().ServeHTTP(w, r)
	})
	api.InitProxyController(s.engine, &supervisor.Supervisor{}, scrollApp)
	s.vulcandServer = httptest.NewServer(conditionalHandler)
	config.Set("routers:vulcand:api-url", s.vulcandServer.URL)
	vRouter, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	err = vRouter.AddBackend("myapp")
	c.Assert(err, check.NotNil)
	backends, err := s.engine.GetBackends()
	c.Assert(err, check.IsNil)
	c.Assert(backends, check.HasLen, 0)
	frontends, err := s.engine.GetFrontends()
	c.Assert(err, check.IsNil)
	c.Assert(frontends, check.HasLen, 0)
}
示例#13
0
func getRouter() (router.Router, error) {
	r, err := config.GetString("docker:router")
	if err != nil {
		return nil, err
	}
	return router.Get(r)
}
示例#14
0
func (s *S) TestHealthCheck(c *check.C) {
	got, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	hcRouter, ok := got.(router.HealthChecker)
	c.Assert(ok, check.Equals, true)
	c.Assert(hcRouter.HealthCheck(), check.IsNil)
}
示例#15
0
func (s *S) TestShouldBeRegistered(c *check.C) {
	got, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	r, ok := got.(*vulcandRouter)
	c.Assert(ok, check.Equals, true)
	c.Assert(r.client.Addr, check.Equals, s.vulcandServer.URL)
	c.Assert(r.domain, check.Equals, "vulcand.example.com")
}
示例#16
0
func (s *S) TestShouldBeRegisteredAsPlanb(c *check.C) {
	config.Set("routers:myplanb:type", "planb")
	defer config.Unset("routers:myplanb:type")
	r, err := router.Get("myplanb")
	c.Assert(err, check.IsNil)
	_, ok := r.(*hipacheRouter)
	c.Assert(ok, check.Equals, true)
}
示例#17
0
func (s *S) TestAddBackendDuplicate(c *check.C) {
	vRouter, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	err = vRouter.AddBackend("myapp")
	c.Assert(err, check.IsNil)
	err = vRouter.AddBackend("myapp")
	c.Assert(err, check.ErrorMatches, router.ErrBackendExists.Error())
}
示例#18
0
func (s *S) TestHealthCheckFailure(c *check.C) {
	s.vulcandServer.Close()
	got, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	hcRouter, ok := got.(router.HealthChecker)
	c.Assert(ok, check.Equals, true)
	c.Assert(hcRouter.HealthCheck(), check.ErrorMatches, ".* connection refused")
}
示例#19
0
func (s *S) TestAddr(c *check.C) {
	vRouter, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	err = vRouter.AddBackend("myapp")
	c.Assert(err, check.IsNil)
	addr, err := vRouter.Addr("myapp")
	c.Assert(err, check.IsNil)
	c.Assert(addr, check.Equals, "myapp.vulcand.example.com")
}
示例#20
0
func (s *S) TestUnsetCNameNotExist(c *check.C) {
	vRouter, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	frontends, err := s.engine.GetFrontends()
	c.Assert(err, check.IsNil)
	c.Assert(frontends, check.HasLen, 0)
	err = vRouter.UnsetCName("myapp.cname.example.com", "myapp")
	c.Assert(err, check.Equals, router.ErrCNameNotFound)
}
示例#21
0
func (s *S) TestStartupMessage(c *check.C) {
	got, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	mRouter, ok := got.(router.MessageRouter)
	c.Assert(ok, check.Equals, true)
	message, err := mRouter.StartupMessage()
	c.Assert(err, check.IsNil)
	c.Assert(message, check.Equals,
		fmt.Sprintf(`vulcand router "vulcand.example.com" with API at "%s"`, s.vulcandServer.URL),
	)
}
示例#22
0
func (s *S) TestAddRouteDuplicate(c *check.C) {
	vRouter, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	err = vRouter.AddBackend("myapp")
	c.Assert(err, check.IsNil)
	u1, _ := url.Parse("http://1.1.1.1:111")
	err = vRouter.AddRoute("myapp", u1)
	c.Assert(err, check.IsNil)
	err = vRouter.AddRoute("myapp", u1)
	c.Assert(err, check.ErrorMatches, router.ErrRouteExists.Error())
}
示例#23
0
func (s *S) TestRemoveRouteNotExist(c *check.C) {
	vRouter, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	err = vRouter.AddBackend("myapp")
	c.Assert(err, check.IsNil)
	servers, err := s.engine.GetServers(engine.BackendKey{Id: "tsuru_myapp"})
	c.Assert(err, check.IsNil)
	c.Assert(servers, check.HasLen, 0)
	u1, _ := url.Parse("http://1.1.1.1:111")
	err = vRouter.RemoveRoute("myapp", u1)
	c.Assert(err, check.ErrorMatches, router.ErrRouteNotFound.Error())
}
示例#24
0
func (s *S) TestRemoveBackendNotExist(c *check.C) {
	vRouter, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	frontends, err := s.engine.GetFrontends()
	c.Assert(err, check.IsNil)
	c.Assert(frontends, check.HasLen, 0)
	backends, err := s.engine.GetBackends()
	c.Assert(err, check.IsNil)
	c.Assert(backends, check.HasLen, 0)
	err = vRouter.RemoveBackend("myapp")
	c.Assert(err, check.Equals, router.ErrBackendNotFound)
}
示例#25
0
func (s *S) TestShouldBeRegisteredAllowingPrefixes(c *check.C) {
	config.Set("routers:inst1:api-url", "url1")
	config.Set("routers:inst1:username", "username1")
	config.Set("routers:inst1:password", "pass1")
	config.Set("routers:inst1:domain", "domain1")
	config.Set("routers:inst2:api-url", "url2")
	config.Set("routers:inst2:username", "username2")
	config.Set("routers:inst2:password", "pass2")
	config.Set("routers:inst2:domain", "domain2")
	config.Set("routers:inst1:type", "galeb")
	config.Set("routers:inst2:type", "galeb")
	defer config.Unset("routers:inst1:type")
	defer config.Unset("routers:inst2:type")
	defer config.Unset("routers:inst1:api-url")
	defer config.Unset("routers:inst1:username")
	defer config.Unset("routers:inst1:password")
	defer config.Unset("routers:inst1:domain")
	defer config.Unset("routers:inst2:api-url")
	defer config.Unset("routers:inst2:username")
	defer config.Unset("routers:inst2:password")
	defer config.Unset("routers:inst2:domain")
	got1, err := router.Get("inst1")
	c.Assert(err, check.IsNil)
	got2, err := router.Get("inst2")
	c.Assert(err, check.IsNil)
	r1, ok := got1.(*galebRouter)
	c.Assert(ok, check.Equals, true)
	c.Assert(r1.prefix, check.Equals, "routers:inst1")
	c.Assert(r1.client.ApiUrl, check.Equals, "url1")
	c.Assert(r1.client.Username, check.Equals, "username1")
	c.Assert(r1.client.Password, check.Equals, "pass1")
	c.Assert(r1.domain, check.Equals, "domain1")
	r2, ok := got2.(*galebRouter)
	c.Assert(ok, check.Equals, true)
	c.Assert(r2.prefix, check.Equals, "routers:inst2")
	c.Assert(r2.client.ApiUrl, check.Equals, "url2")
	c.Assert(r2.client.Username, check.Equals, "username2")
	c.Assert(r2.client.Password, check.Equals, "pass2")
	c.Assert(r2.domain, check.Equals, "domain2")
}
示例#26
0
文件: app.go 项目: 4eek/tsuru
func (app *App) RebuildRoutes() (*RebuildRoutesResult, error) {
	routerName, err := app.GetRouter()
	if err != nil {
		return nil, err
	}
	r, err := router.Get(routerName)
	if err != nil {
		return nil, err
	}
	err = r.AddBackend(app.Name)
	if err != nil && err != router.ErrBackendExists {
		return nil, err
	}
	for _, cname := range app.CName {
		err := r.SetCName(cname, app.Name)
		if err != nil && err != router.ErrCNameExists {
			return nil, err
		}
	}
	oldRoutes, err := r.Routes(app.GetName())
	if err != nil {
		return nil, err
	}
	expectedMap := make(map[string]*url.URL)
	for _, unit := range app.Units() {
		expectedMap[unit.Address.String()] = unit.Address
	}
	var toRemove []*url.URL
	for _, url := range oldRoutes {
		if _, isPresent := expectedMap[url.String()]; isPresent {
			delete(expectedMap, url.String())
		} else {
			toRemove = append(toRemove, url)
		}
	}
	var result RebuildRoutesResult
	for _, toAddUrl := range expectedMap {
		err := r.AddRoute(app.GetName(), toAddUrl)
		if err != nil {
			return nil, err
		}
		result.Added = append(result.Added, toAddUrl.String())
	}
	for _, toRemoveUrl := range toRemove {
		err := r.RemoveRoute(app.GetName(), toRemoveUrl)
		if err != nil {
			return nil, err
		}
		result.Removed = append(result.Removed, toRemoveUrl.String())
	}
	return &result, nil
}
示例#27
0
func (s *ExternalSuite) TestSwapWithDifferentRouterKinds(c *check.C) {
	backend1 := "bb1"
	backend2 := "bb2"
	r1, err := router.Get("fake")
	c.Assert(err, check.IsNil)
	r2, err := router.Get("hipache")
	c.Assert(err, check.IsNil)
	err = r1.AddBackend(backend1)
	c.Assert(err, check.IsNil)
	addr1, _ := url.Parse("http://127.0.0.1")
	err = r1.AddRoute(backend1, addr1)
	c.Assert(err, check.IsNil)
	err = r2.AddBackend(backend2)
	c.Assert(err, check.IsNil)
	addr2, _ := url.Parse("http://10.10.10.10")
	err = r2.AddRoute(backend2, addr2)
	c.Assert(err, check.IsNil)
	err = router.Swap(r1, backend1, backend2)
	c.Assert(err, check.ErrorMatches, `swap is only allowed between routers of the same kind. "bb1" uses "fake", "bb2" uses "hipache"`)
	err = router.Swap(r2, backend1, backend2)
	c.Assert(err, check.ErrorMatches, `swap is only allowed between routers of the same kind. "bb1" uses "fake", "bb2" uses "hipache"`)
}
示例#28
0
func (s *S) TestHealthCheckFailure(c *check.C) {
	s.vulcandServer.Close()
	err := tsurutest.WaitCondition(time.Second, func() bool {
		_, err := http.Get(s.vulcandServer.URL)
		return err != nil
	})
	c.Assert(err, check.IsNil)
	got, err := router.Get("vulcand")
	c.Assert(err, check.IsNil)
	hcRouter, ok := got.(router.HealthChecker)
	c.Assert(ok, check.Equals, true)
	c.Assert(hcRouter.HealthCheck(), check.ErrorMatches, ".* connection refused")
}
示例#29
0
func (s *ExternalSuite) TestSwapCnameOnly(c *check.C) {
	backend1 := "bx1"
	backend2 := "bx2"
	r, err := router.Get("fake")
	c.Assert(err, check.IsNil)
	cnameRouter, ok := r.(router.CNameRouter)
	c.Assert(ok, check.Equals, true)
	err = r.AddBackend(backend1)
	c.Assert(err, check.IsNil)
	addr1, err := url.Parse("http://127.0.0.1")
	c.Assert(err, check.IsNil)
	r.AddRoute(backend1, addr1)
	err = cnameRouter.SetCName("cname.com", backend1)
	c.Assert(err, check.IsNil)
	err = r.AddBackend(backend2)
	c.Assert(err, check.IsNil)
	addr2, err := url.Parse("http://10.10.10.10")
	c.Assert(err, check.IsNil)
	r.AddRoute(backend2, addr2)
	err = router.Swap(r, backend1, backend2, true)
	c.Assert(err, check.IsNil)
	routes1, err := r.Routes(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(routes1, check.DeepEquals, []*url.URL{addr1})
	routes2, err := r.Routes(backend2)
	c.Assert(err, check.IsNil)
	c.Assert(routes2, check.DeepEquals, []*url.URL{addr2})
	name1, err := router.Retrieve(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(name1, check.Equals, backend1)
	name2, err := router.Retrieve(backend2)
	c.Assert(err, check.IsNil)
	c.Assert(name2, check.Equals, backend2)
	cnames, err := cnameRouter.CNames(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(cnames, check.HasLen, 0)
	expected := []*url.URL{{Host: "cname.com"}}
	cnames, err = cnameRouter.CNames(backend2)
	c.Assert(err, check.IsNil)
	c.Assert(expected, check.DeepEquals, cnames)
	err = router.Swap(r, backend1, backend2, true)
	c.Assert(err, check.IsNil)
	cnames, err = cnameRouter.CNames(backend1)
	c.Assert(err, check.IsNil)
	c.Assert(expected, check.DeepEquals, cnames)
	cnames, err = cnameRouter.CNames(backend2)
	c.Assert(err, check.IsNil)
	c.Assert(cnames, check.HasLen, 0)
}
示例#30
0
func init() {
	base := &S{}
	suite := &routertest.RouterSuite{
		SetUpSuiteFunc:   base.SetUpSuite,
		TearDownTestFunc: base.TearDownTest,
	}
	suite.SetUpTestFunc = func(c *check.C) {
		config.Set("database:name", "router_generic_vulcand_tests")
		base.SetUpTest(c)
		r, err := router.Get("vulcand")
		c.Assert(err, check.IsNil)
		suite.Router = r
	}
	check.Suite(suite)
}