-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RHTAPWATCH-570): Update exporters code with actual metrics
Update the dsexporter code and related unit tests with actual metrics. Signed-off-by: Homaja Marisetty <[email protected]>
- Loading branch information
Showing
2 changed files
with
112 additions
and
37 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,46 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
func TestCustomCollector(t *testing.T) { | ||
exporter := NewCustomCollector() | ||
prometheus.MustRegister(exporter) | ||
|
||
// Simulate collecting metrics and check the exported metric value. | ||
metrics := prometheus.DefaultGatherer | ||
metricFamilies, err := metrics.Gather() | ||
assert.NoError(t, err) | ||
|
||
var RequestCountValue float64 | ||
for _, mf := range metricFamilies { | ||
if mf.GetName() == "request_count" { | ||
RequestCountValue = mf.GetMetric()[0].GetCounter().GetValue() | ||
break | ||
} | ||
func TestAvailabilityHandler(t *testing.T) { | ||
mockExporter := NewCustomCollector() | ||
|
||
req, err := http.NewRequest("GET", "/?service=test.com&check=test", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Check whether the exported metric value is initially 0. | ||
assert.Equal(t, float64(0), RequestCountValue) | ||
rec := httptest.NewRecorder() | ||
availabilityHandler(rec, req, mockExporter) | ||
|
||
// Increment the requestCounter by calling the Inc method. | ||
exporter.requestCounter.Inc() | ||
if status := rec.Code; status != http.StatusOK { | ||
t.Errorf("Service unavailable") | ||
} | ||
|
||
// Collecting metrics again | ||
metricFamilies, err = metrics.Gather() | ||
assert.NoError(t, err) | ||
expected := fmt.Sprintf("Service: test.com, Check Name: test, Availability:",) | ||
assert.Contains(t, rec.Body.String(), expected) | ||
} | ||
|
||
for _, mf := range metricFamilies { | ||
if mf.GetName() == "request_count" { | ||
RequestCountValue = mf.GetMetric()[0].GetCounter().GetValue() | ||
break | ||
} | ||
func TestcheckIfServiceUpPass(t *testing.T) { | ||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
defer mockServer.Close() | ||
|
||
serviceUrl := mockServer.URL | ||
|
||
available := checkIfServiceUp(serviceUrl) | ||
|
||
if available == 0 { | ||
t.Errorf("Service not accessible") | ||
} | ||
} | ||
|
||
func TestcheckIfServiceUpFail(t *testing.T) { | ||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNoContent) | ||
})) | ||
defer mockServer.Close() | ||
|
||
serviceUrl := mockServer.URL | ||
|
||
// Check whether the exported metric value is now 1 after incrementing. | ||
assert.Equal(t, float64(1), RequestCountValue) | ||
available := checkIfServiceUp(serviceUrl) | ||
|
||
if available == 0 { | ||
t.Errorf("Service not accessible") | ||
} | ||
} | ||
|
||
func TestCustomCollector(t *testing.T) { | ||
|
||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
if r.URL.Path == "/metrics" { | ||
promhttp.Handler().ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
})) | ||
|
||
defer mockServer.Close() | ||
|
||
resp, err := http.Get(mockServer.URL + "/metrics") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("Service unavailable") | ||
} | ||
} |