Skip to content

Commit 9edbabe

Browse files
authored
Merge pull request #80 from xibz/handler_tests
Adds handler tests, removes Metadata field, and removes machine_mock.go
2 parents 479ac13 + b829bcc commit 9edbabe

File tree

5 files changed

+157
-173
lines changed

5 files changed

+157
-173
lines changed

fctesting/machine_mock.go

Lines changed: 0 additions & 167 deletions
This file was deleted.

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/go-openapi/strfmt v0.17.1
77
github.com/go-openapi/swag v0.17.1
88
github.com/go-openapi/validate v0.17.1
9-
github.com/golang/mock v1.1.1
109
github.com/sirupsen/logrus v1.1.1
1110
github.com/stretchr/testify v1.2.2
1211
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ github.com/go-openapi/swag v0.17.1/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/
3535
github.com/go-openapi/validate v0.17.0/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4=
3636
github.com/go-openapi/validate v0.17.1 h1:RfQTLHm/gEu0oSUmbTOy0PMufjkE5/pPfnqYpor3WLc=
3737
github.com/go-openapi/validate v0.17.1/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4=
38-
github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
39-
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
4038
github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
4139
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4240
github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe h1:CHRGQ8V7OlCYtwaKPJi3iA7J+YdNKdo8j7nG5IgDhjs=

handlers_test.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ package firecracker
33
import (
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

1218
func 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+
483640
func compareHandlerLists(l1, l2 HandlerList) bool {
484641
if l1.Len() != l2.Len() {
485642
return false

machine.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,6 @@ func (cfg *Config) Validate() error {
131131

132132
// Machine is the main object for manipulating Firecracker microVMs
133133
type Machine struct {
134-
// Metadata is the associated metadata that will be sent to the firecracker
135-
// process
136-
Metadata interface{}
137134
// Handlers holds the set of handlers that are run for validation and start
138135
Handlers Handlers
139136

0 commit comments

Comments
 (0)