예제 #1
0
func (s *HTTPSuite) TestAddRemoveBackends(c *C) {
	svcCfg := client.ServiceConfig{
		Name: "VHostTest",
		Addr: "127.0.0.1:9000",
	}

	err := Registry.AddService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	for _, srv := range s.backendServers {
		cfg := client.BackendConfig{
			Addr: srv.addr,
			Name: srv.addr,
		}
		svcCfg.Backends = append(svcCfg.Backends, cfg)
	}

	err = Registry.UpdateService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	cfg := Registry.Config()
	if !svcCfg.DeepEqual(cfg.Services[0]) {
		c.Errorf("we should have 1 service, we have %d", len(cfg.Services))
		c.Errorf("we should have 4 backends, we have %d", len(cfg.Services[0].Backends))
	}

	svcCfg.Backends = svcCfg.Backends[:3]
	err = Registry.UpdateService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	cfg = Registry.Config()
	if !svcCfg.DeepEqual(cfg.Services[0]) {
		c.Errorf("we should have 1 service, we have %d", len(cfg.Services))
		c.Errorf("we should have 3 backends, we have %d", len(cfg.Services[0].Backends))
	}

}
예제 #2
0
// Add multiple services under the same VirtualHost
// Each proxy request should round-robin through the two of them
func (s *HTTPSuite) TestMultiServiceVHost(c *C) {
	svcCfgOne := client.ServiceConfig{
		Name:         "VHostTest",
		Addr:         "127.0.0.1:9000",
		VirtualHosts: []string{"test-vhost"},
	}

	svcCfgTwo := client.ServiceConfig{
		Name:         "VHostTest2",
		Addr:         "127.0.0.1:9001",
		VirtualHosts: []string{"test-vhost-2"},
	}

	var backends []client.BackendConfig
	for _, srv := range s.backendServers {
		cfg := client.BackendConfig{
			Addr: srv.addr,
			Name: srv.addr,
		}
		backends = append(backends, cfg)
	}

	svcCfgOne.Backends = backends
	svcCfgTwo.Backends = backends

	err := Registry.AddService(svcCfgOne)
	if err != nil {
		c.Fatal(err)
	}

	err = Registry.AddService(svcCfgTwo)
	if err != nil {
		c.Fatal(err)
	}

	for _, srv := range s.backendServers {
		checkHTTP("http://"+s.httpAddr+"/addr", "test-vhost", srv.addr, 200, c)
		checkHTTP("http://"+s.httpAddr+"/addr", "test-vhost-2", srv.addr, 200, c)
	}
}
예제 #3
0
// Make HTTP calls over the TCP proxy for comparison to ReverseProxy
func BenchmarkTCPProxy(b *testing.B) {
	setupBench(b)
	defer tearDownBench(b)

	svcCfg := client.ServiceConfig{
		Name:         "VHostTest",
		Addr:         "127.0.0.1:9000",
		VirtualHosts: []string{"test-vhost"},
	}

	for _, srv := range benchBackends {
		cfg := client.BackendConfig{
			Addr: srv.addr,
			Name: srv.addr,
		}
		svcCfg.Backends = append(svcCfg.Backends, cfg)
	}

	err := Registry.AddService(svcCfg)
	if err != nil {
		b.Fatal(err)
	}

	req, err := http.NewRequest("GET", "http://127.0.0.1:9000/addr", nil)
	if err != nil {
		b.Fatal(err)
	}

	req.Host = "test-vhost"

	http.DefaultTransport.(*http.Transport).DisableKeepAlives = true
	runtime.GC()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			b.Fatal("Error during GET:", err)
		}
		body, err := ioutil.ReadAll(resp.Body)
		resp.Body.Close()
		if err != nil {
			b.Fatal("Error during Read:", err)
		}
		if len(body) < 7 {
			b.Fatalf("Error in Response: %s", body)
		}
	}

	runtime.GC()
}
예제 #4
0
func (s *HTTPSuite) TestErrorPage(c *C) {
	svcCfg := client.ServiceConfig{
		Name:         "VHostTest",
		Addr:         "127.0.0.1:9000",
		VirtualHosts: []string{"test-vhost"},
	}

	okServer := s.backendServers[0]
	errServer := s.backendServers[1]

	// Add one backend to service requests
	cfg := client.BackendConfig{
		Addr: okServer.addr,
		Name: okServer.addr,
	}
	svcCfg.Backends = append(svcCfg.Backends, cfg)

	// use another backend to provide the error page
	svcCfg.ErrorPages = map[string][]int{
		"http://" + errServer.addr + "/error": []int{400, 503},
	}

	err := Registry.AddService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	// check that the normal response comes from srv1
	checkHTTP("http://"+s.httpAddr+"/addr", "test-vhost", okServer.addr, 200, c)
	// verify that an unregistered error doesn't give the cached page
	checkHTTP("http://"+s.httpAddr+"/error?code=504", "test-vhost", okServer.addr, 504, c)
	// now see if the registered error comes from srv2
	checkHTTP("http://"+s.httpAddr+"/error?code=503", "test-vhost", errServer.addr, 503, c)

	// now check that we got the header cached in the error page as well
	req, err := http.NewRequest("GET", "http://"+s.httpAddr+"/error?code=503", nil)
	if err != nil {
		c.Fatal(err)
	}

	req.Host = "test-vhost"
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		c.Fatal(err)
	}

	c.Assert(resp.StatusCode, Equals, 503)
	c.Assert(resp.Header.Get("Last-Modified"), Equals, errServer.addr)
}
예제 #5
0
func (s *HTTPSuite) TestAddRemoveVHosts(c *C) {
	svcCfg := client.ServiceConfig{
		Name:         "VHostTest",
		Addr:         "127.0.0.1:9000",
		VirtualHosts: []string{"test-vhost"},
	}

	for _, srv := range s.backendServers {
		cfg := client.BackendConfig{
			Addr: srv.addr,
			Name: srv.addr,
		}
		svcCfg.Backends = append(svcCfg.Backends, cfg)
	}

	err := Registry.AddService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	// now update the service with another vhost
	svcCfg.VirtualHosts = append(svcCfg.VirtualHosts, "test-vhost-2")
	err = Registry.UpdateService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	if Registry.VHostsLen() != 2 {
		c.Fatal("missing new vhost")
	}

	// remove the first vhost
	svcCfg.VirtualHosts = []string{"test-vhost-2"}
	err = Registry.UpdateService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	if Registry.VHostsLen() != 1 {
		c.Fatal("extra vhost:", Registry.VHostsLen())
	}

	// check responses from this new vhost
	for _, srv := range s.backendServers {
		checkHTTP("http://"+s.httpAddr+"/addr", "test-vhost-2", srv.addr, 200, c)
	}
}
예제 #6
0
func (s *HTTPSuite) TestRouter(c *C) {
	svcCfg := client.ServiceConfig{
		Name:         "VHostTest",
		Addr:         "127.0.0.1:9000",
		VirtualHosts: []string{"test-vhost"},
	}

	for _, srv := range s.backendServers {
		cfg := client.BackendConfig{
			Addr: srv.addr,
			Name: srv.addr,
		}
		svcCfg.Backends = append(svcCfg.Backends, cfg)
	}

	err := Registry.AddService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	for _, srv := range s.backendServers {
		checkHTTP("http://"+s.httpAddr+"/addr", "test-vhost", srv.addr, 200, c)
	}
}
예제 #7
0
func (s *HTTPSuite) TestHTTPAddRemoveBackends(c *C) {
	svcCfg := client.ServiceConfig{
		Name: "VHostTest",
		Addr: "127.0.0.1:9000",
	}

	err := Registry.AddService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	for _, srv := range s.backendServers {
		cfg := client.BackendConfig{
			Addr: srv.addr,
			Name: srv.addr,
		}
		svcCfg.Backends = append(svcCfg.Backends, cfg)
	}

	req, _ := http.NewRequest("PUT", s.httpSvr.URL+"/VHostTest", bytes.NewReader(svcCfg.Marshal()))
	_, err = http.DefaultClient.Do(req)
	if err != nil {
		c.Fatal(err)
	}

	cfg := Registry.Config()
	if !svcCfg.DeepEqual(cfg.Services[0]) {
		c.Errorf("we should have 1 service, we have %d", len(cfg.Services))
		c.Errorf("we should have 4 backends, we have %d", len(cfg.Services[0].Backends))
	}

	// remove a backend from the config and submit it again
	svcCfg.Backends = svcCfg.Backends[:3]
	err = Registry.UpdateService(svcCfg)
	if err != nil {
		c.Fatal(err)
	}

	req, _ = http.NewRequest("PUT", s.httpSvr.URL+"/VHostTest", bytes.NewReader(svcCfg.Marshal()))
	_, err = http.DefaultClient.Do(req)
	if err != nil {
		c.Fatal(err)
	}

	// now check the config via what's returned from the http server
	resp, err := http.Get(s.httpSvr.URL + "/_config")
	if err != nil {
		c.Fatal(err)
	}
	defer resp.Body.Close()

	cfg = client.Config{}
	body, _ := ioutil.ReadAll(resp.Body)
	err = json.Unmarshal(body, &cfg)
	if err != nil {
		c.Fatal(err)
	}

	if !svcCfg.DeepEqual(cfg.Services[0]) {
		c.Errorf("we should have 1 service, we have %d", len(cfg.Services))
		c.Errorf("we should have 3 backends, we have %d", len(cfg.Services[0].Backends))
	}
}