-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
38 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
package health | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDNSProbeCheck(t *testing.T) { | ||
assert.NoError(t, DNSProbeCheck("google.com", 5*time.Second)()) | ||
assert.Error(t, DNSProbeCheck("never.ever.where.com", 5*time.Second)()) | ||
} |
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,107 @@ | ||
package health | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewHandler(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
method string | ||
path string | ||
failHealth bool | ||
failReady bool | ||
expectedCode int | ||
}{ | ||
{ | ||
name: "Non-existent URL", | ||
method: http.MethodPost, | ||
path: "/neverwhere", | ||
expectedCode: http.StatusNotFound, | ||
}, | ||
{ | ||
name: "POST Method not allowed health", | ||
method: http.MethodPost, | ||
path: "/healthz", | ||
expectedCode: http.StatusMethodNotAllowed, | ||
}, | ||
{ | ||
name: "POST Method not allowed ready", | ||
method: http.MethodPost, | ||
path: "/ready", | ||
expectedCode: http.StatusMethodNotAllowed, | ||
}, | ||
{ | ||
name: "No checks health", | ||
method: http.MethodGet, | ||
path: "/healthz", | ||
expectedCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "No checks ready", | ||
method: http.MethodGet, | ||
path: "/ready", | ||
expectedCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "Health succeed Ready fail health", | ||
method: http.MethodGet, | ||
path: "/healthz", | ||
expectedCode: http.StatusOK, | ||
failReady: true, | ||
}, | ||
{ | ||
name: "Health succeed Ready fail ready", | ||
method: http.MethodGet, | ||
path: "/ready", | ||
expectedCode: http.StatusServiceUnavailable, | ||
failReady: true, | ||
}, | ||
{ | ||
name: "Health fail Ready succeed health", | ||
method: http.MethodGet, | ||
path: "/healthz", | ||
expectedCode: http.StatusServiceUnavailable, | ||
failHealth: true, | ||
}, | ||
{ | ||
name: "Health fail Ready succeed ready", | ||
method: http.MethodGet, | ||
path: "/ready", | ||
expectedCode: http.StatusServiceUnavailable, | ||
failHealth: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
h := NewChecksHandler("/healthz", "/ready") | ||
|
||
if test.failHealth { | ||
h.AddLiveness("Liveness check test", func() error { | ||
return errors.New("Liveness check failed") | ||
}) | ||
} | ||
|
||
if test.failReady { | ||
h.AddReadiness("Readiness check test", func() error { | ||
return errors.New("Readiness check failed") | ||
}) | ||
} | ||
|
||
req, err := http.NewRequest(test.method, test.path, nil) | ||
assert.NoError(t, err) | ||
|
||
reqStr := test.method + " " + test.path | ||
httpRecorder := httptest.NewRecorder() | ||
h.Handler().ServeHTTP(httpRecorder, req) | ||
assert.Equal(t, test.expectedCode, httpRecorder.Code, | ||
"Result codes don't match %q. [%s]", reqStr, test.name) | ||
}) | ||
} | ||
} |
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,14 @@ | ||
package health | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHTTPGetCheck(t *testing.T) { | ||
assert.NoError(t, HTTPGetCheck("http://httpbin.org/get", 5*time.Second)(), "Simple HTTP GET request shouldn't fail") | ||
assert.Error(t, HTTPGetCheck("http://httpbin.org/relative-redirect/:1", 5*time.Second)(), "Redirrect is not HTTP 200: OK") | ||
assert.Error(t, HTTPGetCheck("http://httpbin.org/nonexistent", 5*time.Second)(), "Non-existing site exists") | ||
} |