aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Yong Tang <yongtang@users.noreply.github.com> 2016-09-07 02:01:27 -0700
committerGravatar Miek Gieben <miek@miek.nl> 2016-09-07 10:01:27 +0100
commit684330fd28f65a7a8d1ad01fb4c6c5db78240f29 (patch)
treedb29e89f1d091c9b323b7e49ce48a416a7a81adc
parent3fab9b1bfa412f577ba12f981eb9492fc7227d20 (diff)
downloadcoredns-684330fd28f65a7a8d1ad01fb4c6c5db78240f29.tar.gz
coredns-684330fd28f65a7a8d1ad01fb4c6c5db78240f29.tar.zst
coredns-684330fd28f65a7a8d1ad01fb4c6c5db78240f29.zip
Add a test for health middleware (#246)
This commit adds a simple test of `TestHealth` for the middleware of health so that there is basic coverage. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
-rw-r--r--middleware/health/health_test.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/middleware/health/health_test.go b/middleware/health/health_test.go
new file mode 100644
index 000000000..c5ef9cc42
--- /dev/null
+++ b/middleware/health/health_test.go
@@ -0,0 +1,37 @@
+package health
+
+import (
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "testing"
+)
+
+func TestHealth(t *testing.T) {
+ // We use a random port instead of a fixed port like 8080 that may have been
+ // occupied by some other process.
+ health := Health{Addr: ":0"}
+ if err := health.Startup(); err != nil {
+ t.Fatalf("Unable to startup the health server: %v", err)
+ }
+ defer health.Shutdown()
+
+ // Reconstruct the http address based on the port allocated by operating system.
+ address := fmt.Sprintf("http://%s%s", health.ln.Addr().String(), path)
+
+ response, err := http.Get(address)
+ if err != nil {
+ t.Fatalf("Unable to query %s: %v", address, err)
+ }
+ defer response.Body.Close()
+ if response.StatusCode != 200 {
+ t.Errorf("Invalid status code: expecting '200', got '%d'", response.StatusCode)
+ }
+ content, err := ioutil.ReadAll(response.Body)
+ if err != nil {
+ t.Fatalf("Unable to get response body from %s: %v", address, err)
+ }
+ if string(content) != "OK" {
+ t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
+ }
+}