@@ -2,6 +2,7 @@ package adhoc
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"io"
6
7
"os"
7
8
"testing"
@@ -17,6 +18,9 @@ import (
17
18
"google.golang.org/grpc/status"
18
19
19
20
"github.com/grafana/synthetic-monitoring-agent/internal/feature"
21
+ "github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
22
+ "github.com/grafana/synthetic-monitoring-agent/internal/model"
23
+ "github.com/grafana/synthetic-monitoring-agent/internal/prober"
20
24
"github.com/grafana/synthetic-monitoring-agent/internal/prober/logger"
21
25
"github.com/grafana/synthetic-monitoring-agent/internal/pusher"
22
26
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
@@ -347,3 +351,255 @@ func (p *testProber) Probe(ctx context.Context, target string, registry *prometh
347
351
_ = logger .Log ("msg" , "test" )
348
352
return true , 1
349
353
}
354
+
355
+ // Add mock secrets store
356
+ type testSecretStore struct {}
357
+
358
+ func (s testSecretStore ) GetSecretCredentials (ctx context.Context , tenantId model.GlobalID ) (* sm.SecretStore , error ) {
359
+ if tenantId == 0 {
360
+ return nil , errors .New ("invalid tenant ID" )
361
+ }
362
+
363
+ return & sm.SecretStore {
364
+ Url : "http://example.com" ,
365
+ Token : "test-token" ,
366
+ }, nil
367
+ }
368
+
369
+ func TestDefaultRunnerFactory (t * testing.T ) {
370
+ t .Parallel ()
371
+
372
+ features := feature .NewCollection ()
373
+ require .NoError (t , features .Set (feature .K6 ))
374
+
375
+ logger := zerolog .New (io .Discard )
376
+ if testing .Verbose () {
377
+ logger = zerolog .New (os .Stdout )
378
+ }
379
+
380
+ // Initialize the mockRunner and secretStore
381
+ mockRunner := & testK6Runner {}
382
+ secretStore := & testSecretStore {}
383
+
384
+ testcases := map [string ]struct {
385
+ request * sm.AdHocRequest
386
+ expectError bool
387
+ errCheck func (error ) bool
388
+ shouldPanic bool
389
+ }{
390
+ "ping check" : {
391
+ request : & sm.AdHocRequest {
392
+ AdHocCheck : sm.AdHocCheck {
393
+ Id : "test-ping" ,
394
+ TenantId : 1000 ,
395
+ Target : "example.com" ,
396
+ Timeout : 1000 ,
397
+ Settings : sm.CheckSettings {
398
+ Ping : & sm.PingSettings {},
399
+ },
400
+ },
401
+ },
402
+ },
403
+ "http check" : {
404
+ request : & sm.AdHocRequest {
405
+ AdHocCheck : sm.AdHocCheck {
406
+ Id : "test-http" ,
407
+ TenantId : 1000 ,
408
+ Target : "http://example.com" ,
409
+ Timeout : 1000 ,
410
+ Settings : sm.CheckSettings {
411
+ Http : & sm.HttpSettings {},
412
+ },
413
+ },
414
+ },
415
+ },
416
+ "dns check" : {
417
+ request : & sm.AdHocRequest {
418
+ AdHocCheck : sm.AdHocCheck {
419
+ Id : "test-dns" ,
420
+ TenantId : 1000 ,
421
+ Target : "example.com" ,
422
+ Timeout : 1000 ,
423
+ Settings : sm.CheckSettings {
424
+ Dns : & sm.DnsSettings {},
425
+ },
426
+ },
427
+ },
428
+ },
429
+ "tcp check" : {
430
+ request : & sm.AdHocRequest {
431
+ AdHocCheck : sm.AdHocCheck {
432
+ Id : "test-tcp" ,
433
+ TenantId : 1000 ,
434
+ Target : "example.com:80" ,
435
+ Timeout : 1000 ,
436
+ Settings : sm.CheckSettings {
437
+ Tcp : & sm.TcpSettings {},
438
+ },
439
+ },
440
+ },
441
+ },
442
+ "k6 scripted check" : {
443
+ request : & sm.AdHocRequest {
444
+ AdHocCheck : sm.AdHocCheck {
445
+ Id : "test-scripted" ,
446
+ TenantId : 1000 ,
447
+ Target : "test-target" ,
448
+ Timeout : 1000 ,
449
+ Settings : sm.CheckSettings {
450
+ Scripted : & sm.ScriptedSettings {},
451
+ },
452
+ },
453
+ },
454
+ },
455
+ "k6 multihttp check" : {
456
+ request : & sm.AdHocRequest {
457
+ AdHocCheck : sm.AdHocCheck {
458
+ Id : "test-multihttp" ,
459
+ TenantId : 1000 ,
460
+ Target : "test-target" ,
461
+ Timeout : 1000 ,
462
+ Settings : sm.CheckSettings {
463
+ Multihttp : & sm.MultiHttpSettings {
464
+ Entries : []* sm.MultiHttpEntry {
465
+ {
466
+ Request : & sm.MultiHttpEntryRequest {
467
+ Url : "http://example.com" ,
468
+ },
469
+ },
470
+ },
471
+ },
472
+ },
473
+ },
474
+ },
475
+ },
476
+ "k6 browser check" : {
477
+ request : & sm.AdHocRequest {
478
+ AdHocCheck : sm.AdHocCheck {
479
+ Id : "test-browser" ,
480
+ TenantId : 1000 ,
481
+ Target : "test-target" ,
482
+ Timeout : 1000 ,
483
+ Settings : sm.CheckSettings {
484
+ Browser : & sm.BrowserSettings {},
485
+ },
486
+ },
487
+ },
488
+ },
489
+ "zero timeout" : {
490
+ request : & sm.AdHocRequest {
491
+ AdHocCheck : sm.AdHocCheck {
492
+ Id : "test-zero-timeout" ,
493
+ TenantId : 1000 ,
494
+ Target : "example.com" ,
495
+ Timeout : 0 ,
496
+ Settings : sm.CheckSettings {
497
+ Ping : & sm.PingSettings {},
498
+ },
499
+ },
500
+ },
501
+ },
502
+ "empty settings" : {
503
+ request : & sm.AdHocRequest {
504
+ AdHocCheck : sm.AdHocCheck {
505
+ Id : "test-empty-settings" ,
506
+ TenantId : 1000 ,
507
+ Target : "example.com" ,
508
+ Timeout : 1000 ,
509
+ Settings : sm.CheckSettings {},
510
+ },
511
+ },
512
+ expectError : true ,
513
+ shouldPanic : true ,
514
+ },
515
+ "nil request" : {
516
+ request : nil ,
517
+ expectError : true ,
518
+ errCheck : func (err error ) bool {
519
+ return errors .Is (err , errInvalidAdHocRequest )
520
+ },
521
+ },
522
+ "zero tenant" : {
523
+ request : & sm.AdHocRequest {
524
+ AdHocCheck : sm.AdHocCheck {
525
+ Id : "test-zero-tenant" ,
526
+ TenantId : 0 ,
527
+ Target : "example.com" ,
528
+ Timeout : 1000 ,
529
+ Settings : sm.CheckSettings {},
530
+ },
531
+ },
532
+ expectError : true ,
533
+ errCheck : func (err error ) bool {
534
+ return errors .Is (err , errInvalidAdHocRequest )
535
+ },
536
+ },
537
+ }
538
+
539
+ for name , tc := range testcases {
540
+ tc := tc
541
+ t .Run (name , func (t * testing.T ) {
542
+ t .Parallel ()
543
+
544
+ h := & Handler {
545
+ logger : logger ,
546
+ features : features ,
547
+ probe : & sm.Probe {
548
+ Name : "test-probe" ,
549
+ },
550
+ proberFactory : prober .NewProberFactory (mockRunner , 0 , features , secretStore ),
551
+ }
552
+
553
+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
554
+ defer cancel ()
555
+
556
+ if tc .shouldPanic {
557
+ require .Panics (t , func () {
558
+ _ , _ = h .defaultRunnerFactory (ctx , tc .request )
559
+ })
560
+ return
561
+ }
562
+
563
+ runner , err := h .defaultRunnerFactory (ctx , tc .request )
564
+ if tc .expectError {
565
+ require .Error (t , err )
566
+ if tc .errCheck != nil {
567
+ require .True (t , tc .errCheck (err ), "unexpected error: %v" , err )
568
+ }
569
+ require .Nil (t , runner )
570
+ return
571
+ }
572
+
573
+ require .NoError (t , err )
574
+ require .NotNil (t , runner )
575
+
576
+ // Verify runner fields
577
+ require .Equal (t , logger , runner .logger )
578
+ require .NotNil (t , runner .prober )
579
+ require .Equal (t , tc .request .AdHocCheck .Id , runner .id )
580
+ require .Equal (t , "test-probe" , runner .probe )
581
+
582
+ // For k6-based checks, verify the grace period is added
583
+ switch tc .request .AdHocCheck .Type () {
584
+ case sm .CheckTypeMultiHttp , sm .CheckTypeScripted , sm .CheckTypeBrowser :
585
+ expectedTimeout := time .Duration (tc .request .AdHocCheck .Timeout )* time .Millisecond + k6AdhocGraceTime
586
+ require .Equal (t , expectedTimeout , runner .timeout )
587
+
588
+ default :
589
+ expectedTimeout := time .Duration (tc .request .AdHocCheck .Timeout ) * time .Millisecond
590
+ require .Equal (t , expectedTimeout , runner .timeout )
591
+ }
592
+ })
593
+ }
594
+ }
595
+
596
+ // Add mock k6runner
597
+ type testK6Runner struct {}
598
+
599
+ func (r * testK6Runner ) WithLogger (logger * zerolog.Logger ) k6runner.Runner {
600
+ return r
601
+ }
602
+
603
+ func (r * testK6Runner ) Run (ctx context.Context , script k6runner.Script ) (* k6runner.RunResponse , error ) {
604
+ return & k6runner.RunResponse {}, nil
605
+ }
0 commit comments