Skip to content

Commit 9fc01b2

Browse files
authored
test: improve coverage on http and metrics (#5712)
* chore(tests): Adding in missing tests for CustomRoundTrip struct. * chore(tests): Adding in missing unit test. * chore(tests): Refactored and addressed missing coverage in models.go
1 parent 0e7c3af commit 9fc01b2

File tree

3 files changed

+172
-12
lines changed

3 files changed

+172
-12
lines changed

pkg/http/http_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ package http
1818

1919
import (
2020
"fmt"
21+
"io"
2122
"net/http"
23+
"net/url"
2224
"testing"
2325

26+
"github.com/stretchr/testify/assert"
2427
"github.com/stretchr/testify/require"
2528
)
2629

@@ -57,3 +60,98 @@ func TestNewInstrumentedClient(t *testing.T) {
5760
_, ok = result2.Transport.(*CustomRoundTripper)
5861
require.True(t, ok)
5962
}
63+
64+
func TestCancelRequest(t *testing.T) {
65+
for _, tt := range []struct {
66+
title string
67+
customRoundTripper CustomRoundTripper
68+
request *http.Request
69+
}{
70+
{
71+
title: "CancelRequest does nothing",
72+
customRoundTripper: CustomRoundTripper{},
73+
request: &http.Request{},
74+
},
75+
} {
76+
t.Run(tt.title, func(t *testing.T) {
77+
tt.customRoundTripper.CancelRequest(tt.request)
78+
})
79+
}
80+
}
81+
82+
type mockRoundTripper struct {
83+
response *http.Response
84+
error error
85+
}
86+
87+
func (mrt mockRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
88+
return mrt.response, mrt.error
89+
}
90+
91+
func TestRoundTrip(t *testing.T) {
92+
for _, tt := range []struct {
93+
title string
94+
nextRoundTripper mockRoundTripper
95+
request *http.Request
96+
method string
97+
url string
98+
body io.Reader
99+
100+
expectError bool
101+
expectedResponse *http.Response
102+
}{
103+
{
104+
title: "RoundTrip returns no error",
105+
nextRoundTripper: mockRoundTripper{},
106+
request: &http.Request{
107+
Method: http.MethodGet,
108+
URL: &url.URL{
109+
Scheme: "HTTPS",
110+
Host: "test.local",
111+
Path: "/path",
112+
},
113+
Body: nil,
114+
},
115+
expectError: false,
116+
expectedResponse: nil,
117+
},
118+
{
119+
title: "RoundTrip extracts status from request",
120+
nextRoundTripper: mockRoundTripper{
121+
response: &http.Response{
122+
StatusCode: http.StatusOK,
123+
},
124+
},
125+
request: &http.Request{
126+
Method: http.MethodGet,
127+
URL: &url.URL{
128+
Scheme: "HTTPS",
129+
Host: "test.local",
130+
Path: "/path",
131+
},
132+
Body: nil,
133+
},
134+
expectError: false,
135+
expectedResponse: &http.Response{
136+
StatusCode: http.StatusOK,
137+
},
138+
},
139+
} {
140+
t.Run(tt.title, func(t *testing.T) {
141+
req, err := http.NewRequest(tt.method, tt.url, tt.body)
142+
customRoundTripper := CustomRoundTripper{
143+
next: tt.nextRoundTripper,
144+
}
145+
146+
resp, err := customRoundTripper.RoundTrip(req)
147+
148+
if tt.expectError {
149+
assert.Error(t, err)
150+
} else {
151+
assert.NoError(t, err)
152+
}
153+
154+
assert.Equal(t, tt.expectedResponse, resp)
155+
})
156+
}
157+
}

pkg/metrics/metrics_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ func TestMustRegister(t *testing.T) {
7272
},
7373
expected: 0,
7474
},
75+
{
76+
name: "skip if metric exists",
77+
metrics: []IMetric{
78+
NewGaugeWithOpts(prometheus.GaugeOpts{Name: "existing_metric"}),
79+
NewGaugeWithOpts(prometheus.GaugeOpts{Name: "existing_metric"}),
80+
},
81+
expected: 1,
82+
},
7583
}
7684

7785
for _, tt := range tests {

pkg/metrics/models_test.go

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,75 @@ func TestGaugeV_SetWithLabels(t *testing.T) {
113113
assert.Len(t, m.Label, 2)
114114
}
115115

116-
func TestNewBuildInfoCollector(t *testing.T) {
117-
metric := NewGaugeFuncMetric(prometheus.GaugeOpts{
118-
Namespace: Namespace,
119-
Name: "build_info",
120-
ConstLabels: prometheus.Labels{
121-
"version": "0.0.1",
122-
"goversion": "1.24",
123-
"arch": "arm64",
116+
func TestNewGaugeFuncMetric(t *testing.T) {
117+
tests := []struct {
118+
name string
119+
metricName string
120+
subSystem string
121+
constLabels prometheus.Labels
122+
expectedFqName string
123+
expectedDescString string
124+
expectedGaugeFuncReturn float64
125+
}{
126+
{
127+
name: "NewGaugeFuncMetric returns build_info",
128+
metricName: "build_info",
129+
subSystem: "",
130+
constLabels: prometheus.Labels{
131+
"version": "0.0.1",
132+
"goversion": "1.24",
133+
"arch": "arm64",
134+
},
135+
expectedFqName: "external_dns_build_info",
136+
expectedDescString: "version=\"0.0.1\"",
137+
expectedGaugeFuncReturn: 1,
138+
},
139+
{
140+
name: "NewGaugeFuncMetric subsystem alters name",
141+
metricName: "metricName",
142+
subSystem: "subSystem",
143+
constLabels: prometheus.Labels{},
144+
expectedFqName: "external_dns_subSystem_metricName",
145+
expectedDescString: "",
146+
expectedGaugeFuncReturn: 1,
124147
},
125-
})
148+
{
149+
name: "NewGaugeFuncMetric GaugeFunc returns 1",
150+
metricName: "metricName",
151+
subSystem: "",
152+
constLabels: prometheus.Labels{},
153+
expectedFqName: "external_dns_metricName",
154+
expectedDescString: "",
155+
expectedGaugeFuncReturn: 1,
156+
},
157+
}
158+
159+
for _, tt := range tests {
160+
t.Run(tt.name, func(t *testing.T) {
161+
metric := NewGaugeFuncMetric(prometheus.GaugeOpts{
162+
Namespace: Namespace,
163+
Name: tt.metricName,
164+
Subsystem: tt.subSystem,
165+
ConstLabels: tt.constLabels,
166+
})
167+
168+
desc := metric.GaugeFunc.Desc()
169+
170+
assert.Equal(t, tt.expectedFqName, reflect.ValueOf(desc).Elem().FieldByName("fqName").String())
171+
assert.Contains(t, desc.String(), tt.expectedDescString)
126172

127-
desc := metric.GaugeFunc.Desc()
173+
testRegistry := prometheus.NewRegistry()
174+
err := testRegistry.Register(metric.GaugeFunc)
175+
require.NoError(t, err)
128176

129-
assert.Equal(t, "external_dns_build_info", reflect.ValueOf(desc).Elem().FieldByName("fqName").String())
130-
assert.Contains(t, desc.String(), "version=\"0.0.1\"")
177+
metricFamily, err := testRegistry.Gather()
178+
require.NoError(t, err)
179+
require.Len(t, metricFamily, 1)
180+
181+
require.NotNil(t, metricFamily[0].Metric[0].Gauge)
182+
assert.InDelta(t, tt.expectedGaugeFuncReturn, metricFamily[0].Metric[0].GetGauge().GetValue(), 0.0001)
183+
})
184+
}
131185
}
132186

133187
func TestSummaryV_SetWithLabels(t *testing.T) {

0 commit comments

Comments
 (0)