示例#1
0
func (s *S) TestRebuildRoutesAfterSwap(c *check.C) {
	a1 := app.App{Name: "my-test-app-1", TeamOwner: s.team.Name}
	err := app.CreateApp(&a1, s.user)
	c.Assert(err, check.IsNil)
	a2 := app.App{Name: "my-test-app-2", TeamOwner: s.team.Name}
	err = app.CreateApp(&a2, s.user)
	c.Assert(err, check.IsNil)
	err = provisiontest.ProvisionerInstance.AddUnits(&a1, 3, "web", nil)
	c.Assert(err, check.IsNil)
	err = provisiontest.ProvisionerInstance.AddUnits(&a2, 2, "web", nil)
	c.Assert(err, check.IsNil)
	units1, err := a1.Units()
	c.Assert(err, check.IsNil)
	units2, err := a2.Units()
	c.Assert(err, check.IsNil)
	routertest.FakeRouter.AddRoute(a1.Name, &url.URL{Scheme: "http", Host: "invalid:1234"})
	routertest.FakeRouter.RemoveRoute(a2.Name, units2[0].Address)
	err = routertest.FakeRouter.Swap(a1.Name, a2.Name, false)
	c.Assert(err, check.IsNil)
	changes1, err := rebuild.RebuildRoutes(&a1)
	c.Assert(err, check.IsNil)
	changes2, err := rebuild.RebuildRoutes(&a2)
	c.Assert(err, check.IsNil)
	c.Assert(changes1.Added, check.IsNil)
	c.Assert(changes1.Removed, check.DeepEquals, []string{"http://invalid:1234"})
	c.Assert(changes2.Added, check.DeepEquals, []string{units2[0].Address.String()})
	c.Assert(changes2.Removed, check.IsNil)
	routes1, err := routertest.FakeRouter.Routes(a1.Name)
	c.Assert(err, check.IsNil)
	routes2, err := routertest.FakeRouter.Routes(a2.Name)
	c.Assert(err, check.IsNil)
	sort.Sort(URLList(routes1))
	sort.Sort(URLList(routes2))
	c.Assert(routes1, check.DeepEquals, []*url.URL{
		units1[0].Address,
		units1[1].Address,
		units1[2].Address,
	})
	c.Assert(routes2, check.DeepEquals, []*url.URL{
		units2[0].Address,
		units2[1].Address,
	})
	c.Assert(routertest.FakeRouter.HasRoute(a1.Name, units2[0].Address.String()), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasRoute(a1.Name, units2[1].Address.String()), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasRoute(a2.Name, units1[0].Address.String()), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasRoute(a2.Name, units1[1].Address.String()), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasRoute(a2.Name, units1[2].Address.String()), check.Equals, true)
}
示例#2
0
文件: app.go 项目: tsuru/tsuru
// title: rebuild routes
// path: /apps/{app}/routes
// method: POST
// produce: application/json
// responses:
//   200: Ok
//   401: Unauthorized
//   404: App not found
func appRebuildRoutes(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
	a, err := getAppFromContext(r.URL.Query().Get(":app"), r)
	if err != nil {
		return err
	}
	allowed := permission.Check(t, permission.PermAppAdminRoutes,
		contextsForApp(&a)...,
	)
	if !allowed {
		return permission.ErrUnauthorized
	}
	evt, err := event.New(&event.Opts{
		Target:     appTarget(a.Name),
		Kind:       permission.PermAppAdminRoutes,
		Owner:      t,
		CustomData: event.FormToCustomData(r.Form),
		Allowed:    event.Allowed(permission.PermAppReadEvents, contextsForApp(&a)...),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	w.Header().Set("Content-Type", "application/json")
	result, err := rebuild.RebuildRoutes(&a)
	if err != nil {
		return err
	}
	return json.NewEncoder(w).Encode(&result)
}
示例#3
0
func (s *S) TestRebuildRoutesTCPRoutes(c *check.C) {
	a := app.App{Name: "my-test-app", TeamOwner: s.team.Name}
	err := app.CreateApp(&a, s.user)
	c.Assert(err, check.IsNil)
	err = provisiontest.ProvisionerInstance.AddUnits(&a, 3, "web", nil)
	c.Assert(err, check.IsNil)
	units, err := a.Units()
	c.Assert(err, check.IsNil)
	for _, u := range units {
		routertest.FakeRouter.RemoveRoute(a.Name, u.Address)
		routertest.FakeRouter.AddRoute(a.Name, &url.URL{Scheme: "tcp", Host: u.Address.Host})
	}
	changes, err := rebuild.RebuildRoutes(&a)
	c.Assert(err, check.IsNil)
	c.Assert(changes.Added, check.IsNil)
	c.Assert(changes.Removed, check.IsNil)
	routes, err := routertest.FakeRouter.Routes(a.Name)
	c.Assert(err, check.IsNil)
	c.Assert(routes, check.HasLen, 3)
	c.Assert(routertest.FakeRouter.HasRoute(a.Name, units[0].Address.Host), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasRoute(a.Name, units[1].Address.Host), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasRoute(a.Name, units[2].Address.Host), check.Equals, true)
	app, err := app.GetByName(a.Name)
	c.Assert(err, check.IsNil)
	addr, err := routertest.FakeRouter.Addr(app.Name)
	c.Assert(err, check.IsNil)
	c.Assert(app.Ip, check.Equals, addr)
}
示例#4
0
func (s *S) TestRebuildRoutes(c *check.C) {
	a := app.App{Name: "my-test-app", TeamOwner: s.team.Name}
	err := app.CreateApp(&a, s.user)
	c.Assert(err, check.IsNil)
	err = provisiontest.ProvisionerInstance.AddUnits(&a, 3, "web", nil)
	c.Assert(err, check.IsNil)
	units, err := a.Units()
	c.Assert(err, check.IsNil)
	routertest.FakeRouter.RemoveRoute(a.Name, units[2].Address)
	routertest.FakeRouter.AddRoute(a.Name, &url.URL{Scheme: "http", Host: "invalid:1234"})
	changes, err := rebuild.RebuildRoutes(&a)
	c.Assert(err, check.IsNil)
	c.Assert(changes.Added, check.DeepEquals, []string{units[2].Address.String()})
	c.Assert(changes.Removed, check.DeepEquals, []string{"http://invalid:1234"})
	routes, err := routertest.FakeRouter.Routes(a.Name)
	c.Assert(err, check.IsNil)
	c.Assert(routes, check.HasLen, 3)
	c.Assert(routertest.FakeRouter.HasRoute(a.Name, units[0].Address.String()), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasRoute(a.Name, units[1].Address.String()), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasRoute(a.Name, units[2].Address.String()), check.Equals, true)
	app, err := app.GetByName(a.Name)
	c.Assert(err, check.IsNil)
	addr, err := routertest.FakeRouter.Addr(app.Name)
	c.Assert(err, check.IsNil)
	c.Assert(app.Ip, check.Equals, addr)
}
示例#5
0
func (s *S) TestRebuildRoutesBetweenRouters(c *check.C) {
	a := app.App{Name: "my-test-app", TeamOwner: s.team.Name}
	err := app.CreateApp(&a, s.user)
	c.Assert(err, check.IsNil)
	err = provisiontest.ProvisionerInstance.AddUnits(&a, 1, "web", nil)
	c.Assert(err, check.IsNil)
	oldIp := a.Ip
	a.Plan = app.Plan{Router: "fake-hc"}
	_, err = rebuild.RebuildRoutes(&a)
	c.Assert(err, check.IsNil)
	c.Assert(a.Ip, check.Not(check.Equals), oldIp)
	na, err := app.GetByName(a.Name)
	c.Assert(err, check.IsNil)
	c.Assert(na.Ip, check.Equals, a.Ip)
}
示例#6
0
func (s *S) TestRebuildRoutesRecreatesCnames(c *check.C) {
	a := app.App{Name: "my-test-app", TeamOwner: s.team.Name}
	err := app.CreateApp(&a, s.user)
	c.Assert(err, check.IsNil)
	err = provisiontest.ProvisionerInstance.AddUnits(&a, 1, "web", nil)
	c.Assert(err, check.IsNil)
	units, err := a.Units()
	c.Assert(err, check.IsNil)
	err = a.AddCName("my.cname.com")
	c.Assert(err, check.IsNil)
	c.Assert(routertest.FakeRouter.HasCName("my.cname.com"), check.Equals, true)
	err = routertest.FakeRouter.UnsetCName("my.cname.com", a.Name)
	c.Assert(err, check.IsNil)
	c.Assert(routertest.FakeRouter.HasCName("my.cname.com"), check.Equals, false)
	changes, err := rebuild.RebuildRoutes(&a)
	c.Assert(err, check.IsNil)
	c.Assert(changes, check.DeepEquals, &rebuild.RebuildRoutesResult{})
	routes, err := routertest.FakeRouter.Routes(a.Name)
	c.Assert(err, check.IsNil)
	c.Assert(routes, check.HasLen, 1)
	c.Assert(routertest.FakeRouter.HasRoute(a.Name, units[0].Address.String()), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasCName("my.cname.com"), check.Equals, true)
}
示例#7
0
func (s *S) TestRebuildRoutesRecreatesBackend(c *check.C) {
	a := app.App{Name: "my-test-app", TeamOwner: s.team.Name}
	err := app.CreateApp(&a, s.user)
	c.Assert(err, check.IsNil)
	err = provisiontest.ProvisionerInstance.AddUnits(&a, 3, "web", nil)
	c.Assert(err, check.IsNil)
	units, err := a.Units()
	c.Assert(err, check.IsNil)
	routertest.FakeRouter.RemoveBackend(a.Name)
	changes, err := rebuild.RebuildRoutes(&a)
	c.Assert(err, check.IsNil)
	sort.Strings(changes.Added)
	c.Assert(changes.Added, check.DeepEquals, []string{
		units[0].Address.String(),
		units[1].Address.String(),
		units[2].Address.String(),
	})
	routes, err := routertest.FakeRouter.Routes(a.Name)
	c.Assert(err, check.IsNil)
	c.Assert(routes, check.HasLen, 3)
	c.Assert(routertest.FakeRouter.HasRoute(a.Name, units[0].Address.String()), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasRoute(a.Name, units[1].Address.String()), check.Equals, true)
	c.Assert(routertest.FakeRouter.HasRoute(a.Name, units[2].Address.String()), check.Equals, true)
}
示例#8
0
文件: actions.go 项目: tsuru/tsuru
		}
		oldPlan, ok := ctx.Params[1].(*Plan)
		if !ok {
			return nil, errors.New("second parameter must be a *Plan")
		}
		newRouter, err := app.GetRouter()
		if err != nil {
			return nil, err
		}
		oldRouter, err := oldPlan.getRouter()
		if err != nil {
			return nil, err
		}
		result := changePlanPipelineResult{oldPlan: oldPlan, app: app, oldIp: app.Ip}
		if newRouter != oldRouter {
			_, err = rebuild.RebuildRoutes(app)
			if err != nil {
				return nil, err
			}
			result.changedRouter = true
		}
		return &result, nil
	},
	Backward: func(ctx action.BWContext) {
		result := ctx.FWResult.(*changePlanPipelineResult)
		defer func() {
			result.app.Plan = *result.oldPlan
		}()
		if result.changedRouter {
			app := result.app
			app.Ip = result.oldIp