Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmasi committed May 9, 2024
1 parent 38902f1 commit d94c968
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
6 changes: 1 addition & 5 deletions dataplane/forwarding/fwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,11 +851,7 @@ func (e *Server) InfoList(context.Context, *fwdpb.InfoListRequest) (*fwdpb.InfoL

// InfoElement retrieves the contents of a specific information element.
func (e *Server) InfoElement(_ context.Context, request *fwdpb.InfoElementRequest) (*fwdpb.InfoElementReply, error) {
content, err := e.info.Element(request.GetName(), request.GetType(), request.GetFrame(), request.GetStartHeader())
if err != nil {
return nil, err
}
return &fwdpb.InfoElementReply{Content: content}, nil
return e.info.Element(request.GetName(), request.GetType(), request.GetFrame(), request.GetStartHeader())
}

// Operation processes incoming OperationRequests and extracts the contained Request.
Expand Down
2 changes: 2 additions & 0 deletions dataplane/forwarding/fwdaction/actions/mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func (r *recordPort) Write(packet fwdpacket.Packet) (fwdaction.State, error) {
return fwdaction.CONSUME, nil
}

func (recordPort) Desc() *fwdpb.PortDesc { return nil }

// String returns an empty string.
func (recordPort) String() string { return "" }

Expand Down
14 changes: 14 additions & 0 deletions dataplane/forwarding/fwdaction/mock_fwdport/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions dataplane/forwarding/fwdport/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type testPort struct {
allocated bool
}

func (testPort) Desc() *fwdpb.PortDesc { return nil }

// Name returns the name of the port.
func (testPort) Name() string { return "" }

Expand Down
2 changes: 1 addition & 1 deletion dataplane/forwarding/fwdport/ports/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type kernelPort struct {
devName string
input fwdaction.Actions
output fwdaction.Actions
desc *fwdpb.Desc
desc *fwdpb.PortDesc
ctx *fwdcontext.Context // Forwarding context containing the port
handle packetHandle
doneCh chan struct{}
Expand Down
44 changes: 36 additions & 8 deletions dataplane/forwarding/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func CounterInfo(obj fwdobject.Object) string {
}

// PortInfo returns the details of the specified Port.
// TODO: Use PortInfo, probably needs to return object info string alongside the PortElementInfo return value.
func PortInfo(ctx *fwdcontext.Context, arg interface{}) (*fwdpb.PortElementInfo, error) {

Check failure on line 114 in dataplane/forwarding/info.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
port, ok := arg.(fwdport.Port)
if !ok {
Expand Down Expand Up @@ -167,6 +166,9 @@ func ContextInfo(ctx *fwdcontext.Context, _ interface{}) string {
}

// A Handler returns the contents of an info element.
//
// TODO(dgrau): Handler should return a *fwdpb.InfoElementReply so there can be
// structured infomation per entry/arg type.
type Handler func(*fwdcontext.Context, interface{}) string

// An entry represents a Handler and its argument.
Expand Down Expand Up @@ -347,28 +349,54 @@ func (l *InfoList) lookupPacket(e *entry, arg []byte, l2 fwdpb.PacketHeaderId, d
}

// Element retrieves the contents of a specific information element.
func (l *InfoList) Element(name string, infoType fwdpb.InfoType, frame []byte, start fwdpb.PacketHeaderId) (string, error) {
func (l *InfoList) Element(name string, infoType fwdpb.InfoType, frame []byte, start fwdpb.PacketHeaderId) (*fwdpb.InfoElementReply, error) {
l.mu.Lock()
defer l.mu.Unlock()
e, ok := l.handlers[name]
if !ok {
return "", fmt.Errorf("infolist: Unable to find handler for %v", name)
return nil, fmt.Errorf("infolist: Unable to find handler for %v", name)
}

switch infoType {
case fwdpb.InfoType_INFO_TYPE_ALL:
return l.allInfo(e)
reply := &fwdpb.InfoElementReply{}
switch e.arg.(type) {
case *fwdport.Port:
pi, err := PortInfo(e.ctx, e.arg)
if err != nil {
return nil, fmt.Errorf("unable to get PortInfo: %v", err)
}
reply.ElementInfo = &fwdpb.InfoElementReply_PortInfo{PortInfo: pi}
}
ai, err := l.allInfo(e)
if err != nil {
return nil, err
}
reply.Content = ai
return reply, nil

case fwdpb.InfoType_INFO_TYPE_LOOKUP:
return l.lookupInfo(e, frame, start)
li, err := l.lookupInfo(e, frame, start)
if err != nil {
return nil, err
}
return &fwdpb.InfoElementReply{Content: li}, nil

case fwdpb.InfoType_INFO_TYPE_PORT_INPUT:
return l.lookupPacket(e, frame, start, fwdpb.PortAction_PORT_ACTION_INPUT)
lp, err := l.lookupPacket(e, frame, start, fwdpb.PortAction_PORT_ACTION_INPUT)
if err != nil {
return nil, err
}
return &fwdpb.InfoElementReply{Content: lp}, nil

case fwdpb.InfoType_INFO_TYPE_PORT_OUTPUT:
return l.lookupPacket(e, frame, start, fwdpb.PortAction_PORT_ACTION_OUTPUT)
lp, err := l.lookupPacket(e, frame, start, fwdpb.PortAction_PORT_ACTION_OUTPUT)
if err != nil {
return nil, err
}
return &fwdpb.InfoElementReply{Content: lp}, nil

default:
return "", fmt.Errorf("infolist: Unable to handle infoType %v for %v", infoType, name)
return nil, fmt.Errorf("infolist: Unable to handle infoType %v for %v", infoType, name)
}
}

0 comments on commit d94c968

Please sign in to comment.