Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package tests
import (
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/openfaas/faas/gateway/metrics"
"github.com/openfaas/faas/gateway/requests"
)
type FakePrometheusQueryFetcher struct {
}
func (q FakePrometheusQueryFetcher) Fetch(query string) (*metrics.VectorQueryResponse, error) {
return &metrics.VectorQueryResponse{}, nil
}
func makeFakePrometheusQueryFetcher() FakePrometheusQueryFetcher {
return FakePrometheusQueryFetcher{}
}
func Test_PrometheusMetrics_MixedInto_Services(t *testing.T) {
functionsHandler := makeFunctionsHandler()
fakeQuery := makeFakePrometheusQueryFetcher()
handler := metrics.AddMetricsHandler(functionsHandler, fakeQuery)
rr := httptest.NewRecorder()
request, _ := http.NewRequest(http.MethodGet, "/system/functions", nil)
handler.ServeHTTP(rr, request)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
if rr.Header().Get("Content-Type") != "application/json" {
t.Errorf("Want application/json content-type, got: %s", rr.Header().Get("Content-Type"))
}
if len(rr.Body.String()) == 0 {
t.Errorf("Want content-length > 0, got: %d", len(rr.Body.String()))
}
}
func Test_FunctionsHandler_ReturnsJSONAndOneFunction(t *testing.T) {
functionsHandler := makeFunctionsHandler()
rr := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, "/system/functions", nil)
if err != nil {
t.Fatal(err)
}
functionsHandler.ServeHTTP(rr, request)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
if rr.Header().Get("Content-Type") != "application/json" {
t.Errorf("Want application/json content-type, got: %s", rr.Header().Get("Content-Type"))
}
if len(rr.Body.String()) == 0 {
t.Errorf("Want content-length > 0, got: %d", len(rr.Body.String()))
}
}
func makeFunctionsHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
functions := []requests.Function{
requests.Function{
Name: "echo",
Replicas: 0,
},
}
bytesOut, marshalErr := json.Marshal(&functions)
if marshalErr != nil {
log.Fatal(marshalErr.Error())
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := w.Write(bytesOut)
if err != nil {
log.Fatal(err)
}
}
}