@@ -2,186 +2,15 @@ package benchspy
22
33import (
44 "context"
5- "errors"
65 "net/http"
76 "net/http/httptest"
8- "os"
97 "testing"
108 "time"
119
12- "github.com/smartcontractkit/chainlink-testing-framework/lib/client"
1310 "github.com/smartcontractkit/chainlink-testing-framework/wasp"
1411 "github.com/stretchr/testify/assert"
15- "github.com/stretchr/testify/require"
1612)
1713
18- // TestLokiClient_QueryLogs tests the LokiClient's ability to query Loki logs
19- func TestBenchSpy_LokiClient_SuccessfulQuery (t * testing.T ) {
20- // Create a mock Loki server using httptest
21- mockServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
22- assert .Equal (t , "/loki/api/v1/query_range" , r .URL .Path )
23- w .WriteHeader (http .StatusOK )
24- _ , err := w .Write ([]byte (`{
25- "data": {
26- "result": [
27- {
28- "stream": {"namespace": "test"},
29- "values": [
30- ["1234567890", "Log message 1"],
31- ["1234567891", "Log message 2"]
32- ]
33- }
34- ]
35- }
36- }` ))
37- assert .NoError (t , err )
38- }))
39- defer mockServer .Close ()
40-
41- // Create a BasicAuth object for testing
42- auth := client.LokiBasicAuth {
43- Login : "test-login" ,
44- Password : "test-password" ,
45- }
46-
47- // Set the query parameters
48- queryParams := client.LokiQueryParams {
49- Query : `{namespace="test"}` ,
50- StartTime : time .Now ().Add (- 1 * time .Hour ),
51- EndTime : time .Now (),
52- Limit : 100 ,
53- }
54-
55- // Create the Loki client with the mock server URL
56- lokiClient := client .NewLokiClient (mockServer .URL , "test-tenant" , auth , queryParams )
57-
58- // Query logs
59- logEntries , err := lokiClient .QueryLogs (context .Background ())
60- assert .NoError (t , err )
61- assert .Len (t , logEntries , 2 )
62-
63- // Verify the content of the log entries
64- assert .Equal (t , "1234567890" , logEntries [0 ].Timestamp )
65- assert .Equal (t , "Log message 1" , logEntries [0 ].Log )
66- assert .Equal (t , "1234567891" , logEntries [1 ].Timestamp )
67- assert .Equal (t , "Log message 2" , logEntries [1 ].Log )
68- }
69-
70- func TestBenchSpy_LokiClient_AuthenticationFailure (t * testing.T ) {
71- // Create a mock Loki server
72- mockServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
73- assert .Equal (t , "/loki/api/v1/query_range" , r .URL .Path )
74- w .WriteHeader (http .StatusUnauthorized ) // Simulate authentication failure
75- }))
76- defer mockServer .Close ()
77-
78- // Create a Loki client with incorrect credentials
79- auth := client.LokiBasicAuth {
80- Login : "wrong-login" ,
81- Password : "wrong-password" ,
82- }
83- queryParams := client.LokiQueryParams {
84- Query : `{namespace="test"}` ,
85- StartTime : time .Now ().Add (- 1 * time .Hour ),
86- EndTime : time .Now (),
87- Limit : 100 ,
88- }
89- lokiClient := client .NewLokiClient (mockServer .URL , "test-tenant" , auth , queryParams )
90-
91- // Query logs and expect an error
92- logEntries , err := lokiClient .QueryLogs (context .Background ())
93- assert .Nil (t , logEntries )
94- assert .Error (t , err )
95- var lokiErr * client.LokiAPIError
96- if errors .As (err , & lokiErr ) {
97- assert .Equal (t , http .StatusUnauthorized , lokiErr .StatusCode )
98- } else {
99- t .Fatalf ("Expected LokiAPIError, got %v" , err )
100- }
101- }
102-
103- func TestBenchSpy_LokiClient_InternalServerError (t * testing.T ) {
104- // Create a mock Loki server
105- mockServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
106- assert .Equal (t , "/loki/api/v1/query_range" , r .URL .Path )
107- w .WriteHeader (http .StatusInternalServerError ) // Simulate server error
108- _ , err := w .Write ([]byte (`{"message": "internal server error"}` )) // Error message in the response body
109- assert .NoError (t , err )
110- }))
111- defer mockServer .Close ()
112-
113- // Create a Loki client
114- auth := client.LokiBasicAuth {
115- Login : "test-login" ,
116- Password : "test-password" ,
117- }
118- queryParams := client.LokiQueryParams {
119- Query : `{namespace="test"}` ,
120- StartTime : time .Now ().Add (- 1 * time .Hour ),
121- EndTime : time .Now (),
122- Limit : 100 ,
123- }
124- lokiClient := client .NewLokiClient (mockServer .URL , "test-tenant" , auth , queryParams )
125-
126- // Query logs and expect an error
127- logEntries , err := lokiClient .QueryLogs (context .Background ())
128- assert .Nil (t , logEntries )
129- assert .Error (t , err )
130- var lokiErr * client.LokiAPIError
131- if errors .As (err , & lokiErr ) {
132- assert .Equal (t , http .StatusInternalServerError , lokiErr .StatusCode )
133- } else {
134- t .Fatalf ("Expected LokiAPIError, got %v" , err )
135- }
136- }
137-
138- func TestBenchSpy_LokiClient_DebugMode (t * testing.T ) {
139- err := os .Setenv ("RESTY_DEBUG" , "true" )
140- require .NoError (t , err )
141- t .Cleanup (func () { _ = os .Unsetenv ("RESTY_DEBUG" ) })
142-
143- // Create a mock Loki server
144- mockServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
145- assert .Equal (t , "/loki/api/v1/query_range" , r .URL .Path )
146- w .WriteHeader (http .StatusOK )
147- _ , err := w .Write ([]byte (`{
148- "data": {
149- "result": [
150- {
151- "stream": {"namespace": "test"},
152- "values": [
153- ["1234567890", "Log message 1"],
154- ["1234567891", "Log message 2"]
155- ]
156- }
157- ]
158- }
159- }` ))
160- assert .NoError (t , err )
161- }))
162- defer mockServer .Close ()
163-
164- // Create a Loki client
165- auth := client.LokiBasicAuth {
166- Login : "test-login" ,
167- Password : "test-password" ,
168- }
169- queryParams := client.LokiQueryParams {
170- Query : `{namespace="test"}` ,
171- StartTime : time .Now ().Add (- 1 * time .Hour ),
172- EndTime : time .Now (),
173- Limit : 100 ,
174- }
175- lokiClient := client .NewLokiClient (mockServer .URL , "test-tenant" , auth , queryParams )
176-
177- // Query logs
178- logEntries , err := lokiClient .QueryLogs (context .Background ())
179- assert .NoError (t , err )
180- assert .Len (t , logEntries , 2 )
181-
182- // Check if debug mode was enabled
183- assert .True (t , lokiClient .RestyClient .Debug )
184- }
18514func TestBenchSpy_NewLokiQueryExecutor (t * testing.T ) {
18615 queries := map [string ]string {
18716 "query1" : "test query 1" ,
0 commit comments