feat: stable version
Co-authored-by: Dmitry Fedotov <dmitry@uint32.ru> Co-committed-by: Dmitry Fedotov <dmitry@uint32.ru>
This commit is contained in:
127
checks_test.go
Normal file
127
checks_test.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package watchdog_test
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.uint32.ru/tiny/watchdog"
|
||||
)
|
||||
|
||||
func TestGetHTTPSuccess(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("OK"))
|
||||
}))
|
||||
|
||||
addr := srv.URL
|
||||
|
||||
fn, err := watchdog.GetHTTP(addr, time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
status, err := fn(t.Context())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if status != watchdog.StatusOK {
|
||||
t.Fatalf("incorrect status %s, expected %s", status, watchdog.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHTTPError(t *testing.T) {
|
||||
addr := "https://127.0.0.1:42014"
|
||||
fn, err := watchdog.GetHTTP(addr, time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
status, err := fn(t.Context())
|
||||
if status != watchdog.StatusDown || err == nil {
|
||||
t.Errorf("incorrect status for unavalable host")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeadHTTPSuccess(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
addr := srv.URL
|
||||
|
||||
fn, err := watchdog.HeadHTTP(addr, time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
status, err := fn(t.Context())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if status != watchdog.StatusOK {
|
||||
t.Fatalf("incorrect status %s, expected %s", status, watchdog.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeadHTTPError(t *testing.T) {
|
||||
addr := "https://127.0.0.1:42014"
|
||||
fn, err := watchdog.HeadHTTP(addr, time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
status, err := fn(t.Context())
|
||||
if status != watchdog.StatusDown || err == nil {
|
||||
t.Errorf("incorrect status for unavalable host")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDialTCPSuccess(t *testing.T) {
|
||||
addr := "127.0.0.1:42013"
|
||||
lis, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
conn, err := lis.Accept()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
<-t.Context().Done()
|
||||
conn.Close()
|
||||
}()
|
||||
|
||||
fn, err := watchdog.DialTCP(addr, time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
status, err := fn(t.Context())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if status != watchdog.StatusOK {
|
||||
t.Error("incorrect status for available addr")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDialTCPError(t *testing.T) {
|
||||
addr := "127.0.0.1:65535"
|
||||
// check for non-existent addr
|
||||
fn, err := watchdog.DialTCP(addr, time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
status, err := fn(t.Context())
|
||||
if (status != watchdog.StatusDown) || (err == nil) {
|
||||
t.Errorf("incorrect status %s, expected %s", status, watchdog.StatusDown)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user