Exemple #1
0
func TestSighupHandler(t *testing.T) {
	ranHandler := make(chan bool, 1)
	handler := func(d daemon.Daemon) {
		ranHandler <- true
	}
	conf, _ := config.New("example.yaml")
	d, _ := daemon.New(conf)
	setupSighupHandler(d, handler)
	// Need to sleep here so that the goroutine has time to set up the signal listener, otherwise
	// the signal gets missed
	time.Sleep(1 * time.Second)
	syscall.Kill(syscall.Getpid(), syscall.SIGHUP)

	// Give the syscall 1 second to be handled, should be more than enough time
	timeout := make(chan bool, 1)
	go func() {
		time.Sleep(time.Duration(1 * time.Second))
		timeout <- true
	}()
	select {
	case <-ranHandler:
	case <-timeout:
		t.Fatal("Didn't run handler")
	}

	// Try calling again and make sure it still happens
	syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
	select {
	case <-ranHandler:
	case <-timeout:
		t.Fatal("Didn't run handler second time")
	}
}
Exemple #2
0
func TestHealthCheck(t *testing.T) {
	// Set up the daemon config to proxy to the local server.
	conf, err := config.New("../example.yaml")
	if err != nil {
		t.Fatalf("Couldn't load daemon config: %s", err.Error())
	}
	conf.Proxy.Host = "http://localhost:8000"
	conf.Proxy.Listen = ":6634"
	conf.HealthCheck.Port = "60002"
	conf.HealthCheck.Enabled = true

	err = setUpDaemonWithLocalServer(conf)
	if err != nil {
		t.Fatalf("Test daemon setup failed: %s", err.Error())
	}

	localProxyURL := "http://localhost" + conf.Proxy.Listen

	// Test a route that should be proxied to 404.
	testProxyRequest(t, localProxyURL+"/helloworld", http.StatusNotFound, "404")

	// Test a route that should be proxied to a valid response.
	testProxyRequest(t, localProxyURL+"/healthyroute", http.StatusOK, "healthy")

	healthCheckURL := fmt.Sprintf("http://localhost:%s/health/check", conf.HealthCheck.Port)

	// Test the health check.
	testProxyRequest(t, healthCheckURL, http.StatusOK, "")
}
Exemple #3
0
func TestDaemonWithNoHealthCheck(t *testing.T) {
	// Set up the daemon config to proxy to the local server.
	conf, err := config.New("../example.yaml")
	if err != nil {
		t.Fatalf("Couldn't load daemon config: %s", err.Error())
	}
	conf.Proxy.Host = "http://localhost:8001"
	conf.Proxy.Listen = ":6635"
	conf.HealthCheck.Port = "60003"
	conf.HealthCheck.Enabled = false

	err = setUpDaemonWithLocalServer(conf)
	if err != nil {
		t.Fatalf("Test daemon setup failed: %s", err.Error())
	}

	// Because so many servers are starting, sleep for a second to make sure
	// they start.
	time.Sleep(time.Second)

	localProxyURL := "http://localhost" + conf.Proxy.Listen

	// Test a route that should be proxied to 404.
	testProxyRequest(t, localProxyURL+"/helloworld", http.StatusNotFound, "404")

	// Test a route that should be proxied to a valid response.
	testProxyRequest(t, localProxyURL+"/healthyroute", http.StatusOK, "healthy")
}
Exemple #4
0
func TestConfigReload(t *testing.T) {
	conf, err := config.New("../example.yaml")
	if err != nil {
		t.Fatal("Error loading config: " + err.Error())
	}
	d := daemon{}
	d.LoadConfig(conf)
	if d.handler == nil {
		t.Fatal("Didn't assign handler")
	}
}
Exemple #5
0
// sighupHandler is the default HUP signal handler. It is defined separately so tests can
// overwrite it with their own handler.
func sighupHandler(d daemon.Daemon) {
	conf, err := config.New(*configfile)
	if err != nil {
		log.Println("RELOAD_CONFIG_FILE_FAILED: " + err.Error())
		return
	}
	err = d.LoadConfig(conf)
	if err != nil {
		log.Println("RELOAD_CONFIG_FILE_FAILED: " + err.Error())
		return
	}
	log.Println("Reloaded config file")
}
Exemple #6
0
// ratelimiter is initialized properly based on config
func TestNew(t *testing.T) {

	config, err := config.New("../example.yaml")
	if err != nil {
		t.Error("could not load example configuration")
	}

	rater, err := New(config)
	ratelimiter := rater.(*rateLimiter)
	if err != nil {
		t.Errorf("Error while instantiating ratelimiter: %s", err.Error())
	}
	if len(ratelimiter.limits) != len(config.Limits) {
		t.Error("expected number of limits in configuration to match instantiated limits")
	}
}
Exemple #7
0
// adds different kinds of requests and checks limit Status
// focusses on single bucket adds
func TestSimpleAdd(t *testing.T) {
	config, err := config.New("../example.yaml")
	if err != nil {
		t.Error("could not load example configuration")
	}
	ratelimiter, err := New(config)

	request := common.Request{
		"path": "/special/resources/123",
		"headers": http.Header{
			"Authorization":   []string{"Bearer 12345"},
			"X-Forwarded-For": []string{"IP1", "IP2"},
		},
		"remoteaddr": "127.0.0.1",
	}
	if err = checkLastStatusForRequests(
		ratelimiter, request, 5, []Status{
			Status{Remaining: 195, Name: "bearer-special"}}); err != nil {
		t.Error(err)
	}

	request = common.Request{
		"path": "/resources/123",
		"headers": http.Header{
			"Authorization": []string{"Basic 12345"},
		},
	}

	if err = checkLastStatusForRequests(
		ratelimiter, request, 1, []Status{
			Status{Remaining: 195, Name: "basic-simple"}}); err != nil {
		t.Error(err)
	}

	if status, err := returnLastAddStatus(ratelimiter, request, 200); err == nil {
		t.Fatal("expected error")
	} else if err != leakybucket.ErrorFull {
		t.Fatalf("expected ErrorFull, received %#v", err)
	} else if len(status) != 1 {
		t.Fatalf("expected one status, found %d", len(status))
	} else if status[0].Remaining != 0 {
		t.Fatalf("expected 0 remaining, found %d", status[0].Remaining)
	} else if status[0].Name != "basic-simple" {
		t.Fatalf("expected 'basic-simple' limit, found '%s'", status[0].Name)
	}
}
Exemple #8
0
func TestFailedReload(t *testing.T) {
	conf, err := config.New("../example.yaml")
	if err != nil {
		t.Fatal("Error loading config: " + err.Error())
	}
	daemon, err := New(conf)
	if err != nil {
		t.Fatal("Error creating new daemon: " + err.Error())
	}
	conf2 := config.Config{}
	err = daemon.LoadConfig(conf2)
	if err == nil {
		t.Fatal("Should have errored on empty configuration")
	}

	conf.Proxy.Listen = ":1000"
	err = daemon.LoadConfig(conf)
	if err == nil {
		t.Fatalf("Should have errored on changed listen port")
	}
}
Exemple #9
0
func main() {

	flag.Parse()

	conf, err := config.New(*configfile)
	if err != nil {
		log.Fatalf("LOAD_CONFIG_FAILED: %s", err.Error())
	}

	sphinxd, err := daemon.New(conf)
	if err != nil {
		log.Fatal(err)
	}

	setupSighupHandler(sphinxd, sighupHandler)
	if *validate {
		print("configuration parsed and Sphinx loaded fine. not starting dameon.")
		return
	}

	sphinxd.Start()
}
Exemple #10
0
func setUpHTTPLimiter(b *testing.B) {
	config, err := config.New("example.yaml")
	if err != nil {
		b.Fatalf("LOAD_CONFIG_FAILED: %s", err.Error())
	}
	rateLimiter, err := ratelimiter.New(config)
	if err != nil {
		b.Fatalf("SPHINX_INIT_FAILED: %s", err.Error())
	}

	// if configuration says that use http
	if config.Proxy.Handler != "http" {
		b.Fatalf("sphinx only supports the http handler")
	}

	// ignore the url in the config and use localhost
	target, _ := url.Parse(host)
	proxy := httputil.NewSingleHostReverseProxy(target)
	httpLimiter := handlers.NewHTTPLimiter(rateLimiter, proxy, false)

	go http.ListenAndServe(":8082", httpLimiter)
}