88 "path/filepath"
99 "testing"
1010
11+ "github.com/google/go-cmp/cmp"
1112 "github.com/google/uuid"
13+ "github.com/grafana/xk6-disruptor/pkg/agent/protocol"
1214 "github.com/grafana/xk6-disruptor/pkg/testutils/grpc/ping"
1315 "google.golang.org/grpc"
1416 "google.golang.org/grpc/codes"
@@ -187,6 +189,7 @@ func Test_ProxyHandler(t *testing.T) {
187189 expectStatus codes.Code
188190 }
189191
192+ // TODO: Add test for excluded endpoints
190193 testCases := []TestCase {
191194 {
192195 title : "default proxy" ,
@@ -207,7 +210,7 @@ func Test_ProxyHandler(t *testing.T) {
207210 expectStatus : codes .OK ,
208211 },
209212 {
210- title : "error injection " ,
213+ title : "error injection" ,
211214 disruption : Disruption {
212215 AverageDelay : 0 ,
213216 DelayVariation : 0 ,
@@ -331,3 +334,125 @@ func Test_ProxyHandler(t *testing.T) {
331334 })
332335 }
333336}
337+
338+ func Test_ProxyMetrics (t * testing.T ) {
339+ t .Parallel ()
340+
341+ type TestCase struct {
342+ title string
343+ disruption Disruption
344+ expectedMetrics map [string ]uint
345+ }
346+
347+ // TODO: Add test for excluded endpoints
348+ testCases := []TestCase {
349+ {
350+ title : "passthrough" ,
351+ disruption : Disruption {
352+ AverageDelay : 0 ,
353+ DelayVariation : 0 ,
354+ ErrorRate : 0.0 ,
355+ StatusCode : 0 ,
356+ StatusMessage : "" ,
357+ },
358+ expectedMetrics : map [string ]uint {
359+ protocol .MetricRequests : 1 ,
360+ },
361+ },
362+ {
363+ title : "error injection" ,
364+ disruption : Disruption {
365+ AverageDelay : 0 ,
366+ DelayVariation : 0 ,
367+ ErrorRate : 1.0 ,
368+ StatusCode : int32 (codes .Internal ),
369+ StatusMessage : "Internal server error" ,
370+ },
371+ expectedMetrics : map [string ]uint {
372+ protocol .MetricRequests : 1 ,
373+ protocol .MetricRequestsFaulted : 1 ,
374+ },
375+ },
376+ }
377+
378+ for _ , tc := range testCases {
379+ tc := tc
380+
381+ t .Run (tc .title , func (t * testing.T ) {
382+ t .Parallel ()
383+
384+ // start test server in a random unix socket
385+ serverSocket := filepath .Join (os .TempDir (), uuid .New ().String ())
386+ l , err := net .Listen ("unix" , serverSocket )
387+ if err != nil {
388+ t .Errorf ("error starting test server in unix:%s: %v" , serverSocket , err )
389+ return
390+ }
391+
392+ srv := grpc .NewServer ()
393+ ping .RegisterPingServiceServer (srv , ping .NewPingServer ())
394+ go func () {
395+ if serr := srv .Serve (l ); err != nil {
396+ t .Logf ("error in the server: %v" , serr )
397+ }
398+ }()
399+
400+ // start proxy in a random unix socket
401+ proxySocket := filepath .Join (os .TempDir (), uuid .New ().String ())
402+ config := ProxyConfig {
403+ Network : "unix" ,
404+ ListenAddress : proxySocket ,
405+ UpstreamAddress : fmt .Sprintf ("unix:%s" , serverSocket ),
406+ }
407+
408+ proxy , err := NewProxy (config , tc .disruption )
409+ if err != nil {
410+ t .Errorf ("error creating proxy: %v" , err )
411+ return
412+ }
413+
414+ defer func () {
415+ _ = proxy .Stop ()
416+ }()
417+
418+ go func () {
419+ if perr := proxy .Start (); perr != nil {
420+ t .Logf ("error starting proxy: %v" , perr )
421+ }
422+ }()
423+
424+ // connect client to proxy
425+ conn , err := grpc .DialContext (
426+ context .TODO (),
427+ fmt .Sprintf ("unix:%s" , proxySocket ),
428+ grpc .WithInsecure (),
429+ )
430+ if err != nil {
431+ t .Fatal (err )
432+ }
433+
434+ defer func () {
435+ _ = conn .Close ()
436+ }()
437+
438+ client := ping .NewPingServiceClient (conn )
439+
440+ var headers metadata.MD
441+ _ , _ = client .Ping (
442+ context .TODO (),
443+ & ping.PingRequest {
444+ Error : 0 ,
445+ Message : "ping" ,
446+ },
447+ grpc .Header (& headers ),
448+ grpc .WaitForReady (true ),
449+ )
450+
451+ metrics := proxy .Metrics ()
452+
453+ if diff := cmp .Diff (tc .expectedMetrics , metrics ); diff != "" {
454+ t .Fatalf ("expected metrics do not match returned:\n %s" , diff )
455+ }
456+ })
457+ }
458+ }
0 commit comments