Exemple #1
0
func (s *ApiSuite) TestFrontendCRUD(c *C) {
	b, err := engine.NewHTTPBackend("b1", engine.HTTPBackendSettings{})
	c.Assert(err, IsNil)

	c.Assert(s.client.UpsertBackend(*b), IsNil)

	f, err := engine.NewHTTPFrontend(s.ng.GetRegistry().GetRouter(), "f1", b.Id, `Path("/")`, engine.HTTPFrontendSettings{})
	c.Assert(err, IsNil)
	fk := engine.FrontendKey{Id: f.Id}

	c.Assert(s.client.UpsertFrontend(*f, 0), IsNil)

	fs, err := s.client.GetFrontends()
	c.Assert(err, IsNil)
	c.Assert(fs[0], DeepEquals, *f)

	out, err := s.client.GetFrontend(fk)
	c.Assert(err, IsNil)
	c.Assert(out, DeepEquals, f)

	settings := f.HTTPSettings()
	settings.Hostname = `localhost`
	f.Settings = settings
	f.Route = `Path("/v2")`

	c.Assert(s.client.UpsertFrontend(*f, 0), IsNil)

	c.Assert(s.client.DeleteFrontend(fk), IsNil)

	out, err = s.client.GetFrontend(fk)
	c.Assert(err, FitsTypeOf, &engine.NotFoundError{})
}
Exemple #2
0
func (s *ApiSuite) TestMiddlewareCRUD(c *C) {
	b, err := engine.NewHTTPBackend("b1", engine.HTTPBackendSettings{})
	c.Assert(err, IsNil)

	c.Assert(s.client.UpsertBackend(*b), IsNil)

	f, err := engine.NewHTTPFrontend(s.ng.GetRegistry().GetRouter(), "f1", b.Id, `Path("/")`, engine.HTTPFrontendSettings{})
	c.Assert(err, IsNil)
	fk := engine.FrontendKey{Id: f.Id}

	c.Assert(s.client.UpsertFrontend(*f, 0), IsNil)

	cl := s.makeConnLimit("c1", 10, "client.ip", 2, f)
	c.Assert(s.client.UpsertMiddleware(fk, cl, 0), IsNil)

	ms, err := s.client.GetMiddlewares(fk)
	c.Assert(err, IsNil)
	c.Assert(ms[0], DeepEquals, cl)

	cl = s.makeConnLimit("c1", 10, "client.ip", 3, f)
	c.Assert(s.client.UpsertMiddleware(fk, cl, 0), IsNil)

	mk := engine.MiddlewareKey{Id: cl.Id, FrontendKey: fk}
	v, err := s.client.GetMiddleware(mk)
	c.Assert(err, IsNil)
	c.Assert(v, DeepEquals, &cl)

	c.Assert(s.client.DeleteMiddleware(mk), IsNil)

	_, err = s.client.GetMiddleware(mk)
	c.Assert(err, FitsTypeOf, &engine.NotFoundError{})

}
Exemple #3
0
func (s *ApiSuite) TestBackendCRUD(c *C) {
	b, err := engine.NewHTTPBackend("b1", engine.HTTPBackendSettings{})
	c.Assert(err, IsNil)

	c.Assert(s.client.UpsertBackend(*b), IsNil)

	bs, _ := s.ng.GetBackends()
	c.Assert(len(bs), Equals, 1)
	c.Assert(bs[0], DeepEquals, *b)

	bs, err = s.client.GetBackends()
	c.Assert(bs, NotNil)
	c.Assert(err, IsNil)
	c.Assert(bs[0], DeepEquals, *b)

	bk := engine.BackendKey{Id: b.Id}
	out, err := s.client.GetBackend(bk)
	c.Assert(err, IsNil)
	c.Assert(out, DeepEquals, b)

	settings := b.HTTPSettings()
	settings.Timeouts.Read = "1s"
	b.Settings = settings
	c.Assert(s.client.UpsertBackend(*b), IsNil)

	out, err = s.client.GetBackend(bk)
	c.Assert(err, IsNil)
	c.Assert(out, DeepEquals, b)

	err = s.client.DeleteBackend(bk)
	c.Assert(err, IsNil)

	out, err = s.client.GetBackend(bk)
	c.Assert(err, FitsTypeOf, &engine.NotFoundError{})
}
Exemple #4
0
func MakeBackend() engine.Backend {
	b, err := engine.NewHTTPBackend(UID("backend"), engine.HTTPBackendSettings{})
	if err != nil {
		panic(err)
	}
	return *b
}
Exemple #5
0
func (cmd *Command) upsertBackendAction(c *cli.Context) error {
	settings, err := getBackendSettings(c)
	if err != nil {
		return err
	}
	b, err := engine.NewHTTPBackend(c.String("id"), settings)
	if err != nil {
		return err
	}
	cmd.printResult("%s upserted", b, cmd.client.UpsertBackend(*b))
	return nil
}
Exemple #6
0
func (s *ApiSuite) TestServerCRUD(c *C) {
	b, err := engine.NewHTTPBackend("b1", engine.HTTPBackendSettings{})
	c.Assert(err, IsNil)

	c.Assert(s.client.UpsertBackend(*b), IsNil)

	srv1 := engine.Server{Id: "srv1", URL: "http://localhost:5000"}
	srv2 := engine.Server{Id: "srv2", URL: "http://localhost:6000"}

	bk := engine.BackendKey{Id: b.Id}
	c.Assert(s.client.UpsertServer(bk, srv1, 0), IsNil)
	c.Assert(s.client.UpsertServer(bk, srv2, 0), IsNil)

	srvs, _ := s.ng.GetServers(bk)
	c.Assert(len(srvs), Equals, 2)
	c.Assert(srvs[0], DeepEquals, srv1)
	c.Assert(srvs[1], DeepEquals, srv2)

	srvs, err = s.client.GetServers(bk)
	c.Assert(srvs, NotNil)
	c.Assert(len(srvs), Equals, 2)
	c.Assert(srvs[0], DeepEquals, srv1)
	c.Assert(srvs[1], DeepEquals, srv2)

	sk := engine.ServerKey{Id: srv1.Id, BackendKey: bk}
	out, err := s.client.GetServer(sk)
	c.Assert(err, IsNil)
	c.Assert(out, DeepEquals, &srv1)

	srv1.URL = "http://localhost:5001"
	c.Assert(s.client.UpsertServer(bk, srv1, 0), IsNil)

	out, err = s.client.GetServer(sk)
	c.Assert(err, IsNil)
	c.Assert(out, DeepEquals, &srv1)

	err = s.client.DeleteServer(sk)
	c.Assert(err, IsNil)

	out, err = s.client.GetServer(sk)
	c.Assert(err, FitsTypeOf, &engine.NotFoundError{})
}
Exemple #7
0
func (r *vulcandRouter) AddBackend(name string) error {
	backendName := r.backendName(name)
	frontendName := r.frontendName(r.frontendHostname(name))
	backendKey := engine.BackendKey{Id: backendName}
	frontendKey := engine.FrontendKey{Id: frontendName}
	if found, _ := r.client.GetBackend(backendKey); found != nil {
		return router.ErrBackendExists
	}
	if found, _ := r.client.GetFrontend(frontendKey); found != nil {
		return router.ErrBackendExists
	}
	backend, err := engine.NewHTTPBackend(
		backendName,
		engine.HTTPBackendSettings{},
	)
	if err != nil {
		return &router.RouterError{Err: err, Op: "add-backend"}
	}
	err = r.client.UpsertBackend(*backend)
	if err != nil {
		return err
	}
	frontend, err := engine.NewHTTPFrontend(
		route.NewMux(),
		frontendName,
		backend.Id,
		fmt.Sprintf(`Host(%q)`, r.frontendHostname(name)),
		engine.HTTPFrontendSettings{},
	)
	if err != nil {
		return &router.RouterError{Err: err, Op: "add-backend"}
	}
	err = r.client.UpsertFrontend(*frontend, engine.NoTTL)
	if err != nil {
		r.client.DeleteBackend(backendKey)
		return &router.RouterError{Err: err, Op: "add-backend"}
	}
	return router.Store(name, name, routerName)
}