From cbf9e1011daa2e7876cdd9014a554fb01189828e Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Thu, 27 Dec 2018 12:42:12 +0200 Subject: [PATCH] Add tests for metrics server check --- pkg/controller/observer_test.go | 35 +++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/pkg/controller/observer_test.go b/pkg/controller/observer_test.go index eadbd7bd..bd95a1b4 100644 --- a/pkg/controller/observer_test.go +++ b/pkg/controller/observer_test.go @@ -10,7 +10,6 @@ import ( func TestCanaryObserver_GetDeploymentCounter(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { json := `{"status":"success","data":{"resultType":"vector","result":[{"metric":{},"value":[1545905245.458,"100"]}]}}` - w.WriteHeader(http.StatusAccepted) w.Write([]byte(json)) })) defer ts.Close() @@ -33,7 +32,6 @@ func TestCanaryObserver_GetDeploymentCounter(t *testing.T) { func TestCanaryObserver_GetDeploymentHistogram(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { json := `{"status":"success","data":{"resultType":"vector","result":[{"metric":{},"value":[1545905245.596,"0.2"]}]}}` - w.WriteHeader(http.StatusAccepted) w.Write([]byte(json)) })) defer ts.Close() @@ -51,3 +49,36 @@ func TestCanaryObserver_GetDeploymentHistogram(t *testing.T) { t.Errorf("Got %v wanted %v", val, 200*time.Millisecond) } } + +func TestCheckMetricsServer(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + json := `{"status":"success","data":{"config.file":"/etc/prometheus/prometheus.yml"}}` + w.Write([]byte(json)) + })) + defer ts.Close() + + ok, err := CheckMetricsServer(ts.URL) + if err != nil { + t.Fatal(err.Error()) + } + + if !ok { + t.Errorf("Got %v wanted %v", ok, true) + } +} + +func TestCheckMetricsServer_Offline(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadGateway) + })) + defer ts.Close() + + ok, err := CheckMetricsServer(ts.URL) + if err == nil { + t.Errorf("Got no error wanted %v", http.StatusBadGateway) + } + + if ok { + t.Errorf("Got %v wanted %v", ok, true) + } +}