mirror of
https://github.com/openfaas/faas.git
synced 2025-06-09 16:56:47 +00:00
Update test for metrics server
Updates metrics server test so that it checks for the status code and that bytes are returned. Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
parent
634ec22c8d
commit
fa93655d90
@ -1,11 +1,13 @@
|
|||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_RegisterServer(t *testing.T) {
|
func Test_Register_ProvidesBytes(t *testing.T) {
|
||||||
|
|
||||||
metricsPort := 31111
|
metricsPort := 31111
|
||||||
|
|
||||||
@ -15,7 +17,41 @@ func Test_RegisterServer(t *testing.T) {
|
|||||||
cancel := make(chan bool)
|
cancel := make(chan bool)
|
||||||
go metricsServer.Serve(cancel)
|
go metricsServer.Serve(cancel)
|
||||||
|
|
||||||
time.AfterFunc(time.Millisecond*500, func() {
|
defer func() {
|
||||||
cancel <- true
|
cancel <- true
|
||||||
})
|
}()
|
||||||
|
|
||||||
|
retries := 10
|
||||||
|
|
||||||
|
for i := 0; i < retries; i++ {
|
||||||
|
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/metrics", metricsPort), nil)
|
||||||
|
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Logf("cannot get metrics, or not ready: %s", err.Error())
|
||||||
|
|
||||||
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
wantStatus := http.StatusOK
|
||||||
|
if res.StatusCode != wantStatus {
|
||||||
|
t.Errorf("metrics gave wrong status, want: %d, got: %d", wantStatus, res.StatusCode)
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.Body == nil {
|
||||||
|
t.Errorf("metrics response should have a body")
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Errorf("unable to get expected response from metrics server")
|
||||||
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user