@@ -3,10 +3,16 @@ package firecracker
33import (
44 "context"
55 "fmt"
6+ "os"
7+ "path/filepath"
68 "reflect"
79 "testing"
810
911 log "github.com/sirupsen/logrus"
12+
13+ models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
14+ ops "github.com/firecracker-microvm/firecracker-go-sdk/client/operations"
15+ "github.com/firecracker-microvm/firecracker-go-sdk/fctesting"
1016)
1117
1218func TestHandlerListAppend (t * testing.T ) {
@@ -480,6 +486,157 @@ func TestHandlerListAppendAfter(t *testing.T) {
480486 }
481487}
482488
489+ func TestHandlers (t * testing.T ) {
490+ called := ""
491+ metadata := map [string ]string {
492+ "foo" : "bar" ,
493+ "baz" : "qux" ,
494+ }
495+
496+ cases := []struct {
497+ Handler Handler
498+ Client fctesting.MockClient
499+ Config Config
500+ }{
501+ {
502+ Handler : BootstrapLoggingHandler ,
503+ Client : fctesting.MockClient {
504+ PutLoggerFn : func (params * ops.PutLoggerParams ) (* ops.PutLoggerNoContent , error ) {
505+ called = BootstrapLoggingHandler .Name
506+ return nil , nil
507+ },
508+ },
509+ Config : Config {
510+ LogLevel : "Debug" ,
511+ LogFifo : filepath .Join (testDataPath , "firecracker.log" ),
512+ MetricsFifo : filepath .Join (testDataPath , "firecracker-metrics" ),
513+ },
514+ },
515+ {
516+ Handler : CreateMachineHandler ,
517+ Client : fctesting.MockClient {
518+ PutMachineConfigurationFn : func (params * ops.PutMachineConfigurationParams ) (* ops.PutMachineConfigurationNoContent , error ) {
519+ called = CreateMachineHandler .Name
520+ return & ops.PutMachineConfigurationNoContent {}, nil
521+ },
522+ GetMachineConfigFn : func (params * ops.GetMachineConfigParams ) (* ops.GetMachineConfigOK , error ) {
523+ return & ops.GetMachineConfigOK {
524+ Payload : & models.MachineConfiguration {},
525+ }, nil
526+ },
527+ },
528+ Config : Config {},
529+ },
530+ {
531+ Handler : CreateBootSourceHandler ,
532+ Client : fctesting.MockClient {
533+ PutGuestBootSourceFn : func (params * ops.PutGuestBootSourceParams ) (* ops.PutGuestBootSourceNoContent , error ) {
534+ called = CreateBootSourceHandler .Name
535+ return & ops.PutGuestBootSourceNoContent {}, nil
536+ },
537+ },
538+ Config : Config {},
539+ },
540+ {
541+ Handler : AttachDrivesHandler ,
542+ Client : fctesting.MockClient {
543+ PutGuestDriveByIDFn : func (params * ops.PutGuestDriveByIDParams ) (* ops.PutGuestDriveByIDNoContent , error ) {
544+ called = AttachDrivesHandler .Name
545+ return & ops.PutGuestDriveByIDNoContent {}, nil
546+ },
547+ },
548+ Config : Config {
549+ Drives : NewDrivesBuilder ("/foo/bar" ).Build (),
550+ },
551+ },
552+ {
553+ Handler : CreateNetworkInterfacesHandler ,
554+ Client : fctesting.MockClient {
555+ PutGuestNetworkInterfaceByIDFn : func (params * ops.PutGuestNetworkInterfaceByIDParams ) (* ops.PutGuestNetworkInterfaceByIDNoContent , error ) {
556+ called = CreateNetworkInterfacesHandler .Name
557+ return & ops.PutGuestNetworkInterfaceByIDNoContent {}, nil
558+ },
559+ },
560+ Config : Config {
561+ NetworkInterfaces : []NetworkInterface {
562+ {
563+ MacAddress : "macaddress" ,
564+ HostDevName : "host" ,
565+ },
566+ },
567+ },
568+ },
569+ {
570+ Handler : AddVsocksHandler ,
571+ Client : fctesting.MockClient {
572+ PutGuestVsockByIDFn : func (params * ops.PutGuestVsockByIDParams ) (* ops.PutGuestVsockByIDCreated , * ops.PutGuestVsockByIDNoContent , error ) {
573+ called = AddVsocksHandler .Name
574+ return & ops.PutGuestVsockByIDCreated {}, & ops.PutGuestVsockByIDNoContent {}, nil
575+ },
576+ },
577+ Config : Config {
578+ VsockDevices : []VsockDevice {
579+ {
580+ Path : "path" ,
581+ CID : 123 ,
582+ },
583+ },
584+ },
585+ },
586+ {
587+ Handler : NewSetMetadataHandler (metadata ),
588+ Client : fctesting.MockClient {
589+ PutMmdsFn : func (params * ops.PutMmdsParams ) (* ops.PutMmdsNoContent , error ) {
590+ called = SetMetadataHandlerName
591+ if ! reflect .DeepEqual (metadata , params .Body ) {
592+ return nil , fmt .Errorf ("incorrect metadata value: %v" , params .Body )
593+ }
594+ return & ops.PutMmdsNoContent {}, nil
595+ },
596+ },
597+ Config : Config {},
598+ },
599+ }
600+
601+ ctx := context .Background ()
602+ socketpath := filepath .Join (testDataPath , "socket" )
603+ cfg := Config {}
604+
605+ defer func () {
606+ os .Remove (cfg .SocketPath )
607+ os .Remove (cfg .LogFifo )
608+ os .Remove (cfg .MetricsFifo )
609+ }()
610+
611+ for _ , c := range cases {
612+ t .Run (c .Handler .Name , func (t * testing.T ) {
613+ // cache in case test exited early and can be cleaned up later
614+ cfg = c .Config
615+ // resetting called for the next test
616+ called = ""
617+
618+ client := NewClient (socketpath , log .NewEntry (log .New ()), true , WithOpsClient (& c .Client ))
619+ m , err := NewMachine (ctx , c .Config , WithClient (client ))
620+ if err != nil {
621+ t .Fatalf ("failed to create machine: %v" , err )
622+ }
623+
624+ if err := c .Handler .Fn (ctx , m ); err != nil {
625+ t .Errorf ("failed to call handler function: %v" , err )
626+ }
627+
628+ if e , a := c .Handler .Name , called ; e != a {
629+ t .Errorf ("expected %v, but received %v" , e , a )
630+ }
631+
632+ // clean up any created resources
633+ os .Remove (c .Config .SocketPath )
634+ os .Remove (c .Config .LogFifo )
635+ os .Remove (c .Config .MetricsFifo )
636+ })
637+ }
638+ }
639+
483640func compareHandlerLists (l1 , l2 HandlerList ) bool {
484641 if l1 .Len () != l2 .Len () {
485642 return false
0 commit comments