Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2736 from nojnhuh/metricsstore-coverage
Browse files Browse the repository at this point in the history
tests(metricsstore): Refactor to improve coverage
  • Loading branch information
nojnhuh authored Mar 4, 2021
2 parents e8df0fc + 1607023 commit 36b9159
Showing 1 changed file with 38 additions and 41 deletions.
79 changes: 38 additions & 41 deletions pkg/metricsstore/metricsstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"

tassert "github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
setup()
code := m.Run()
teardown()
os.Exit(code)
}

func setup() {
DefaultMetricsStore.Start(
DefaultMetricsStore.K8sAPIEventCounter,
Expand All @@ -31,58 +23,63 @@ func teardown() {
)
}

func TestK8sAPIEventCounter(t *testing.T) {
assert := tassert.New(t)
func TestMetricsStore(t *testing.T) {
setup()
defer teardown()

apiEventCount := 3
t.Run("K8sAPIEventCounter", func(t *testing.T) {
assert := tassert.New(t)

for i := 1; i <= apiEventCount; i++ {
DefaultMetricsStore.K8sAPIEventCounter.WithLabelValues("add", "foo").Inc()
apiEventCount := 3

handler := DefaultMetricsStore.Handler()
for i := 1; i <= apiEventCount; i++ {
DefaultMetricsStore.K8sAPIEventCounter.WithLabelValues("add", "foo").Inc()

req, err := http.NewRequest("GET", "/metrics", nil)
assert.Nil(err)
handler := DefaultMetricsStore.Handler()

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
req, err := http.NewRequest("GET", "/metrics", nil)
assert.Nil(err)

assert.Equal(http.StatusOK, rr.Code)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)

expectedResp := fmt.Sprintf(`# HELP osm_k8s_api_event_count represents the number of events received from the Kubernetes API Server
assert.Equal(http.StatusOK, rr.Code)

expectedResp := fmt.Sprintf(`# HELP osm_k8s_api_event_count represents the number of events received from the Kubernetes API Server
# TYPE osm_k8s_api_event_count counter
osm_k8s_api_event_count{namespace="foo",type="add"} %d
`, i /* api event count */)
assert.Contains(rr.Body.String(), expectedResp)
}
}
assert.Contains(rr.Body.String(), expectedResp)
}
})

func TestProxyConnectCount(t *testing.T) {
assert := tassert.New(t)
t.Run("ProxyConnectCount", func(t *testing.T) {
assert := tassert.New(t)

proxiesConnected := 5
proxiesDisconnected := 2
proxiesConnected := 5
proxiesDisconnected := 2

for i := 1; i <= proxiesConnected; i++ {
DefaultMetricsStore.ProxyConnectCount.Inc()
}
for i := 1; i <= proxiesDisconnected; i++ {
DefaultMetricsStore.ProxyConnectCount.Dec()
}
for i := 1; i <= proxiesConnected; i++ {
DefaultMetricsStore.ProxyConnectCount.Inc()
}
for i := 1; i <= proxiesDisconnected; i++ {
DefaultMetricsStore.ProxyConnectCount.Dec()
}

handler := DefaultMetricsStore.Handler()
handler := DefaultMetricsStore.Handler()

req, err := http.NewRequest("GET", "/metrics", nil)
assert.Nil(err)
req, err := http.NewRequest("GET", "/metrics", nil)
assert.Nil(err)

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)

assert.Equal(http.StatusOK, rr.Code)
assert.Equal(http.StatusOK, rr.Code)

expectedResp := fmt.Sprintf(`# HELP osm_proxy_connect_count represents the number of proxies connected to OSM controller
expectedResp := fmt.Sprintf(`# HELP osm_proxy_connect_count represents the number of proxies connected to OSM controller
# TYPE osm_proxy_connect_count gauge
osm_proxy_connect_count %d
`, proxiesConnected-proxiesDisconnected)
assert.Contains(rr.Body.String(), expectedResp)
assert.Contains(rr.Body.String(), expectedResp)
})
}

0 comments on commit 36b9159

Please sign in to comment.