@@ -2,10 +2,12 @@ package s3
22
33import (
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+
6782func 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\n storage_s3_call_error_by_type_total{error=\" Timeout\" ,method=\" list\" } 1\n storage_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+
76112func TestBackend_marker (t * testing.T ) {
77113 ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
78114 defer cancel ()
0 commit comments