-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Romain Beuque <romain.beuque@gmail.com>
- Loading branch information
Showing
3 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |