Skip to content

Commit

Permalink
tests: HTTP checker
Browse files Browse the repository at this point in the history
Signed-off-by: Romain Beuque <romain.beuque@gmail.com>
  • Loading branch information
rbeuque74 committed May 1, 2018
1 parent 47e798e commit f059fee
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 2 deletions.
4 changes: 2 additions & 2 deletions plugins/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func (cfg *httpConfig) UnmarshalJSON(b []byte) error {
return err
}

defaults.SetDefaults(raw)

cfg.rawHTTPConfig = *raw
cfg.Timeout = time.Duration(raw.RawTimeout) * time.Millisecond
cfg.Warning = time.Duration(raw.RawWarning) * time.Millisecond
cfg.Critical = time.Duration(raw.RawCritical) * time.Millisecond

defaults.SetDefaults(raw)

validate := validator.New()
if err := validate.Struct(cfg); err != nil {
return err
Expand Down
114 changes: 114 additions & 0 deletions plugins/http/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package http

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestHTTPServer(t *testing.T) {
// creating HTTP server
srvcfg := HttpServerTimeout{
StatusCode: 200,
}
shutdown := NewHTTPServer(t, srvcfg)

time.Sleep(20 * time.Millisecond)

// creating HTTP checker
cfg := map[string]interface{}{
"type": "request",
"url": "http://localhost:8080",
"method": "GET",
"warn": 200,
"crit": 400,
"timeout": 450,
"name": "test-1",
}
checker, err := NewHTTPChecker(cfg, nil)
assert.Nilf(t, err, "http checker instantiation failed: %q", err)

ctxRun, cancelFunc1 := context.WithTimeout(context.Background(), time.Second)
defer cancelFunc1()
result, err := checker.Run(ctxRun)
assert.Nilf(t, err, "http error: %q", err)
assert.Equalf(t, "OK", result, "http bad result: %q", result)

ctx, cancelFunc := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancelFunc()
shutdown(ctx)
}

func TestHTTPServerFails(t *testing.T) {
// creating HTTP server
srvcfg := HttpServerTimeout{
StatusCode: 200,
Sleep: time.Millisecond * 80,
}
shutdown := NewHTTPServer(t, srvcfg)

time.Sleep(20 * time.Millisecond)

// creating HTTP checker
cfg := map[string]interface{}{
"type": "request",
"url": "http://localhost:8080",
"method": "GET",
"warn": 40,
"crit": 200,
"timeout": 1000,
"name": "test-1",
}
checker, err := NewHTTPChecker(cfg, nil)
assert.Nilf(t, err, "http checker instantiation failed: %q", err)

ctxRun, cancelFunc1 := context.WithTimeout(context.Background(), time.Second)
defer cancelFunc1()
result, err := checker.Run(ctxRun)
assert.NotNilf(t, err, "http no error but should")
assert.Equal(t, "warning", err.Error())
assert.Equal(t, "", result)

// critical
cfg["warn"] = 10
cfg["crit"] = 50
checker, err = NewHTTPChecker(cfg, nil)
assert.Nilf(t, err, "http checker instantiation failed: %q", err)

ctxRun, cancelFunc1 = context.WithTimeout(context.Background(), time.Second)
defer cancelFunc1()
result, err = checker.Run(ctxRun)
assert.NotNilf(t, err, "http no error but should")
assert.Equal(t, "critical", err.Error())
assert.Equal(t, "", result)

// bad status code
cfg["code"] = 400
checker, err = NewHTTPChecker(cfg, nil)
assert.Nilf(t, err, "http checker instantiation failed: %q", err)

ctxRun, cancelFunc1 = context.WithTimeout(context.Background(), time.Second)
defer cancelFunc1()
result, err = checker.Run(ctxRun)
assert.NotNilf(t, err, "http no error but should")
assert.Equal(t, "invalid status code: 200 instead of 400", err.Error())
assert.Equal(t, "", result)

// conn refused
cfg["url"] = "http://localhost:8081"
checker, err = NewHTTPChecker(cfg, nil)
assert.Nilf(t, err, "http checker instantiation failed: %q", err)

ctxRun, cancelFunc1 = context.WithTimeout(context.Background(), time.Second)
defer cancelFunc1()
result, err = checker.Run(ctxRun)
assert.NotNilf(t, err, "http no error but should")
assert.Containsf(t, err.Error(), "connection refused", "err is not connection refused: %q", err)
assert.Equal(t, "", result)

ctx, cancelFunc := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancelFunc()
shutdown(ctx)
}
39 changes: 39 additions & 0 deletions plugins/http/http_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package http

import (
"context"
"io"
"net/http"
"testing"
"time"
)

type HttpServerTimeout struct {
Sleep time.Duration
StatusCode int
}

func (s HttpServerTimeout) ServeHTTP(respW http.ResponseWriter, req *http.Request) {
if s.Sleep != 0 {
time.Sleep(s.Sleep)
}

if s.StatusCode != 0 {
respW.WriteHeader(s.StatusCode)
}

io.WriteString(respW, "OK\n")
}

func NewHTTPServer(t *testing.T, cfg HttpServerTimeout) func(context.Context) error {
srv := http.Server{
Addr: ":8080",
Handler: cfg,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
t.Fatal(err)
}
}()
return srv.Shutdown
}

0 comments on commit f059fee

Please sign in to comment.