2015-08-19 00:19:46 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2015-08-20 00:57:18 +00:00
|
|
|
"net"
|
2015-08-19 00:19:46 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/configuration"
|
2015-08-19 21:24:31 +00:00
|
|
|
"github.com/docker/distribution/context"
|
2015-08-19 00:19:46 +00:00
|
|
|
"github.com/docker/distribution/health"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileHealthCheck(t *testing.T) {
|
|
|
|
interval := time.Second
|
|
|
|
|
|
|
|
tmpfile, err := ioutil.TempFile(os.TempDir(), "healthcheck")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create temporary file: %v", err)
|
|
|
|
}
|
|
|
|
defer tmpfile.Close()
|
|
|
|
|
2015-08-20 20:56:36 +00:00
|
|
|
config := &configuration.Configuration{
|
2015-08-19 00:19:46 +00:00
|
|
|
Storage: configuration.Storage{
|
|
|
|
"inmemory": configuration.Parameters{},
|
|
|
|
},
|
|
|
|
Health: configuration.Health{
|
|
|
|
FileCheckers: []configuration.FileChecker{
|
|
|
|
{
|
|
|
|
Interval: interval,
|
|
|
|
File: tmpfile.Name(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
app := NewApp(ctx, config)
|
2015-08-19 22:11:10 +00:00
|
|
|
healthRegistry := health.NewRegistry()
|
|
|
|
app.RegisterHealthChecks(healthRegistry)
|
2015-08-19 00:19:46 +00:00
|
|
|
|
|
|
|
// Wait for health check to happen
|
|
|
|
<-time.After(2 * interval)
|
|
|
|
|
2015-08-19 22:11:10 +00:00
|
|
|
status := healthRegistry.CheckStatus()
|
|
|
|
if len(status) != 1 {
|
|
|
|
t.Fatal("expected 1 item in health check results")
|
2015-08-19 00:19:46 +00:00
|
|
|
}
|
2015-08-19 22:11:10 +00:00
|
|
|
if status[tmpfile.Name()] != "file exists" {
|
2015-08-19 00:19:46 +00:00
|
|
|
t.Fatal(`did not get "file exists" result for health check`)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
<-time.After(2 * interval)
|
2015-08-19 22:11:10 +00:00
|
|
|
if len(healthRegistry.CheckStatus()) != 0 {
|
|
|
|
t.Fatal("expected 0 items in health check results")
|
2015-08-19 00:19:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 00:57:18 +00:00
|
|
|
func TestTCPHealthCheck(t *testing.T) {
|
|
|
|
interval := time.Second
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create listener: %v", err)
|
|
|
|
}
|
|
|
|
addrStr := ln.Addr().String()
|
|
|
|
|
|
|
|
// Start accepting
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
// listener was closed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-08-20 20:56:36 +00:00
|
|
|
config := &configuration.Configuration{
|
2015-08-20 00:57:18 +00:00
|
|
|
Storage: configuration.Storage{
|
|
|
|
"inmemory": configuration.Parameters{},
|
|
|
|
},
|
|
|
|
Health: configuration.Health{
|
|
|
|
TCPCheckers: []configuration.TCPChecker{
|
|
|
|
{
|
|
|
|
Interval: interval,
|
|
|
|
Addr: addrStr,
|
|
|
|
Timeout: 500 * time.Millisecond,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
app := NewApp(ctx, config)
|
|
|
|
healthRegistry := health.NewRegistry()
|
|
|
|
app.RegisterHealthChecks(healthRegistry)
|
|
|
|
|
|
|
|
// Wait for health check to happen
|
|
|
|
<-time.After(2 * interval)
|
|
|
|
|
|
|
|
if len(healthRegistry.CheckStatus()) != 0 {
|
|
|
|
t.Fatal("expected 0 items in health check results")
|
|
|
|
}
|
|
|
|
|
|
|
|
ln.Close()
|
|
|
|
<-time.After(2 * interval)
|
|
|
|
|
|
|
|
// Health check should now fail
|
|
|
|
status := healthRegistry.CheckStatus()
|
|
|
|
if len(status) != 1 {
|
|
|
|
t.Fatal("expected 1 item in health check results")
|
|
|
|
}
|
|
|
|
if status[addrStr] != "connection to "+addrStr+" failed" {
|
|
|
|
t.Fatal(`did not get "connection failed" result for health check`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 00:19:46 +00:00
|
|
|
func TestHTTPHealthCheck(t *testing.T) {
|
|
|
|
interval := time.Second
|
|
|
|
threshold := 3
|
|
|
|
|
|
|
|
stopFailing := make(chan struct{})
|
|
|
|
|
|
|
|
checkedServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "HEAD" {
|
|
|
|
t.Fatalf("expected HEAD request, got %s", r.Method)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-stopFailing:
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2015-08-20 20:56:36 +00:00
|
|
|
config := &configuration.Configuration{
|
2015-08-19 00:19:46 +00:00
|
|
|
Storage: configuration.Storage{
|
|
|
|
"inmemory": configuration.Parameters{},
|
|
|
|
},
|
|
|
|
Health: configuration.Health{
|
|
|
|
HTTPCheckers: []configuration.HTTPChecker{
|
|
|
|
{
|
|
|
|
Interval: interval,
|
|
|
|
URI: checkedServer.URL,
|
|
|
|
Threshold: threshold,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
app := NewApp(ctx, config)
|
2015-08-19 22:11:10 +00:00
|
|
|
healthRegistry := health.NewRegistry()
|
|
|
|
app.RegisterHealthChecks(healthRegistry)
|
2015-08-19 00:19:46 +00:00
|
|
|
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
<-time.After(interval)
|
|
|
|
|
2015-08-19 22:11:10 +00:00
|
|
|
status := healthRegistry.CheckStatus()
|
2015-08-19 00:19:46 +00:00
|
|
|
|
|
|
|
if i < threshold-1 {
|
|
|
|
// definitely shouldn't have hit the threshold yet
|
2015-08-19 22:11:10 +00:00
|
|
|
if len(status) != 0 {
|
|
|
|
t.Fatal("expected 1 item in health check results")
|
2015-08-19 00:19:46 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i < threshold+1 {
|
|
|
|
// right on the threshold - don't expect a failure yet
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-19 22:11:10 +00:00
|
|
|
if len(status) != 1 {
|
|
|
|
t.Fatal("expected 1 item in health check results")
|
2015-08-19 00:19:46 +00:00
|
|
|
}
|
2015-08-19 22:11:10 +00:00
|
|
|
if status[checkedServer.URL] != "downstream service returned unexpected status: 500" {
|
2015-08-19 00:19:46 +00:00
|
|
|
t.Fatal("did not get expected result for health check")
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal HTTP handler to start returning 200
|
|
|
|
close(stopFailing)
|
|
|
|
|
|
|
|
<-time.After(2 * interval)
|
2015-08-19 22:11:10 +00:00
|
|
|
|
|
|
|
if len(healthRegistry.CheckStatus()) != 0 {
|
|
|
|
t.Fatal("expected 0 items in health check results")
|
2015-08-19 00:19:46 +00:00
|
|
|
}
|
|
|
|
}
|