Skip to content

Commit 8429679

Browse files
committed
fix: add unit tests for error classification
1 parent 0c5d456 commit 8429679

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

collector/nginx_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package collector
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/prometheus/client_golang/prometheus"
8+
)
9+
10+
func TestIsNetworkError(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
errorMsg string
14+
want bool
15+
}{
16+
{
17+
name: "network connection error",
18+
errorMsg: "failed to get http://localhost:8080/stub_status: connection refused",
19+
want: true,
20+
},
21+
{
22+
name: "network timeout error",
23+
errorMsg: "failed to get http://localhost:8080/stub_status: timeout",
24+
want: true,
25+
},
26+
{
27+
name: "HTTP error",
28+
errorMsg: "expected 200 response, got 404",
29+
want: false,
30+
},
31+
{
32+
name: "parse error",
33+
errorMsg: "failed to parse response body",
34+
want: false,
35+
},
36+
}
37+
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
if got := isNetworkError(tt.errorMsg); got != tt.want {
41+
t.Errorf("isNetworkError() = %v, want %v", got, tt.want)
42+
}
43+
})
44+
}
45+
}
46+
47+
func TestIsHTTPError(t *testing.T) {
48+
tests := []struct {
49+
name string
50+
errorMsg string
51+
want bool
52+
}{
53+
{
54+
name: "HTTP 404 error",
55+
errorMsg: "expected 200 response, got 404",
56+
want: true,
57+
},
58+
{
59+
name: "HTTP 500 error",
60+
errorMsg: "expected 200 response, got 500",
61+
want: true,
62+
},
63+
{
64+
name: "network error",
65+
errorMsg: "failed to get http://localhost:8080/stub_status: connection refused",
66+
want: false,
67+
},
68+
{
69+
name: "parse error",
70+
errorMsg: "failed to parse response body",
71+
want: false,
72+
},
73+
}
74+
75+
for _, tt := range tests {
76+
t.Run(tt.name, func(t *testing.T) {
77+
if got := isHTTPError(tt.errorMsg); got != tt.want {
78+
t.Errorf("isHTTPError() = %v, want %v", got, tt.want)
79+
}
80+
})
81+
}
82+
}
83+
84+
func TestNewScrapeSuccessMetric(t *testing.T) {
85+
metric := newScrapeSuccessMetric("nginx", map[string]string{"job": "nginx"})
86+
87+
if metric == nil {
88+
t.Error("newScrapeSuccessMetric() returned nil")
89+
}
90+
91+
desc := metric.Desc().String()
92+
if !strings.Contains(desc, "nginx_scrape_success") {
93+
t.Errorf("metric description should contain 'nginx_scrape_success', got: %s", desc)
94+
}
95+
}
96+
97+
func TestNewScrapeDurationMetric(t *testing.T) {
98+
metric := newScrapeDurationMetric("nginx", map[string]string{"job": "nginx"})
99+
100+
if metric == nil {
101+
t.Error("newScrapeDurationMetric() returned nil")
102+
}
103+
104+
desc := metric.Desc().String()
105+
if !strings.Contains(desc, "nginx_scrape_duration_seconds") {
106+
t.Errorf("metric description should contain 'nginx_scrape_duration_seconds', got: %s", desc)
107+
}
108+
}
109+
110+
func TestNewScrapeErrorsTotalMetric(t *testing.T) {
111+
metric := newScrapeErrorsTotalMetric("nginx", map[string]string{"job": "nginx"})
112+
113+
if metric == nil {
114+
t.Error("newScrapeErrorsTotalMetric() returned nil")
115+
}
116+
117+
ch := make(chan *prometheus.Desc, 10)
118+
metric.Describe(ch)
119+
close(ch)
120+
121+
count := 0
122+
for range ch {
123+
count++
124+
}
125+
126+
if count == 0 {
127+
t.Error("metric should have descriptions")
128+
}
129+
}

0 commit comments

Comments
 (0)