Skip to content

Commit b9afd39

Browse files
committed
metrics: Add tests for metrics, especially error cases
1 parent f5d24d6 commit b9afd39

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

backends/s3/s3_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package s3
22

33
import (
44
"context"
5+
"strings"
56
"testing"
67
"time"
78

89
"github.com/minio/minio-go/v7"
10+
"github.com/prometheus/client_golang/prometheus/testutil"
911
"github.com/stretchr/testify/assert"
1012
"github.com/stretchr/testify/require"
1113
"github.com/testcontainers/testcontainers-go"
@@ -64,15 +66,49 @@ func getBackend(ctx context.Context, t *testing.T) (b *Backend) {
6466
return b
6567
}
6668

69+
func getBadBackend(ctx context.Context, url string, t *testing.T) (b *Backend) {
70+
b, err := New(ctx, Options{
71+
EndpointURL: url,
72+
AccessKey: "foo",
73+
SecretKey: "bar",
74+
Bucket: "test-bucket",
75+
CreateBucket: false,
76+
DialTimeout: 1 * time.Second,
77+
})
78+
require.NoError(t, err)
79+
return b
80+
}
81+
6782
func TestBackend(t *testing.T) {
6883
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
6984
defer cancel()
70-
7185
b := getBackend(ctx, t)
7286
tester.DoBackendTests(t, b)
7387
assert.Len(t, b.lastMarker, 0)
7488
}
7589

90+
func TestMetrics(t *testing.T) {
91+
bTimeout := getBadBackend(context.Background(), "http://1.2.3.4:1234", t)
92+
93+
_, err := bTimeout.List(context.Background(), "")
94+
assert.Error(t, err)
95+
96+
expectedMetric := "# HELP storage_s3_call_error_by_type_total S3 API call errors by method and error type\n# TYPE storage_s3_call_error_by_type_total counter\nstorage_s3_call_error_by_type_total{error=\"Timeout\",method=\"list\"} 1\nstorage_s3_call_error_by_type_total{error=\"NotFound\",method=\"load\"} 3\n"
97+
98+
err = testutil.CollectAndCompare(metricCallErrorsType, strings.NewReader(expectedMetric), "storage_s3_call_error_by_type_total")
99+
assert.NoError(t, err)
100+
101+
bBadHost := getBadBackend(context.Background(), "http://nosuchhost:1234", t)
102+
103+
_, err = bBadHost.List(context.Background(), "")
104+
assert.Error(t, err)
105+
106+
expectedMetric += "storage_s3_call_error_by_type_total{error=\"lookup nosuchhost: no such host\",method=\"list\"} 1\n"
107+
108+
err = testutil.CollectAndCompare(metricCallErrorsType, strings.NewReader(expectedMetric), "storage_s3_call_error_by_type_total")
109+
assert.NoError(t, err)
110+
}
111+
76112
func TestBackend_marker(t *testing.T) {
77113
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
78114
defer cancel()

0 commit comments

Comments
 (0)