Skip to content

Commit

Permalink
updating functions
Browse files Browse the repository at this point in the history
  • Loading branch information
prinikasn committed Sep 21, 2023
1 parent 787b202 commit 080c75b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 42 deletions.
33 changes: 29 additions & 4 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package system

import (
"context"
"fmt"
"io"
"time"

Expand Down Expand Up @@ -153,23 +154,47 @@ func (p *PingOperation) Execute(ctx context.Context, c *internal.Clients) ([]*sp

// SwitchControlProcessorOperation represents the parameters of a SwitchControlProcessor operation.
type SwitchControlProcessorOperation struct {
req *spb.SwitchControlProcessorRequest
subcomponentName string
isPathSet bool
path *tpb.Path
}

// NewSwitchControlProcessorOperation creates an empty SwitchControlProcessorOperation.
func NewSwitchControlProcessorOperation() *SwitchControlProcessorOperation {
return &SwitchControlProcessorOperation{req: &spb.SwitchControlProcessorRequest{}}
return &SwitchControlProcessorOperation{}
}

// PathFromSubcomponentName sets the path for route processor to switch from the subcomponentName.
func (s *SwitchControlProcessorOperation) PathFromSubcomponentName(n string) *SwitchControlProcessorOperation {
s.subcomponentName = n
return s
}

// Path specifies the path of the route processor to switch.
func (s *SwitchControlProcessorOperation) Path(p *tpb.Path) *SwitchControlProcessorOperation {
s.req.ControlProcessor = p
s.isPathSet = true
s.path = p
return s
}

// Execute performs the SwitchControlProcessor operation.
func (s *SwitchControlProcessorOperation) Execute(ctx context.Context, c *internal.Clients) (*spb.SwitchControlProcessorResponse, error) {
return c.System().SwitchControlProcessor(ctx, s.req)
if s.subcomponentName != "" && s.isPathSet {
return nil, fmt.Errorf("cannot set both PathFromSubcomponentName %v and Path %v", s.subcomponentName, s.path.String())
}
req := &spb.SwitchControlProcessorRequest{ControlProcessor: s.path}
if s.subcomponentName != "" {
req = &spb.SwitchControlProcessorRequest{
ControlProcessor: &tpb.Path{
Origin: "openconfig",
Elem: []*tpb.PathElem{
{Name: "components"},
{Name: "component", Key: map[string]string{"name": s.subcomponentName}},
},
},
}
}
return c.System().SwitchControlProcessor(ctx, req)
}

// TimeOperation represents the parameters of a Time operation.
Expand Down
92 changes: 54 additions & 38 deletions system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,95 +133,111 @@ func (tc *fakeTracerouteClient) Recv() (*spb.TracerouteResponse, error) {
return resp, tc.err
}

func TestSwitchControlProcessor(t *testing.T) {
func TestPing(t *testing.T) {
tests := []struct {
desc string
op *system.SwitchControlProcessorOperation
want *spb.SwitchControlProcessorResponse
op *system.PingOperation
want []*spb.PingResponse
wantErr string
}{
{
desc: "Test SwitchControlProcessor",
op: system.NewSwitchControlProcessorOperation().Path(&tpb.Path{
Origin: "openconfig",
Elem: []*tpb.PathElem{
{Name: "components"},
{Name: "component", Key: map[string]string{"name": "RP0"}},
},
}),
want: &spb.SwitchControlProcessorResponse{Version: "new"},
desc: "ping with source",
op: system.NewPingOperation().Destination("1.2.3.4").Source("5.6.7.8"),
want: []*spb.PingResponse{{Source: "5.6.7.8"}},
},
{
desc: "SwitchControlProcessor returns error",
op: system.NewSwitchControlProcessorOperation(),
wantErr: "SwitchControlProcessor operation error",
desc: "ping with source and count",
op: system.NewPingOperation().Destination("1.2.3.4").Source("5.6.7.8").Count(7),
want: []*spb.PingResponse{{Source: "5.6.7.8", Sent: 7, Received: 7}},
},
{
desc: "ping with multiple response",
op: system.NewPingOperation().Destination("1.2.3.4").Source("5.6.7.8").Count(7),
want: []*spb.PingResponse{{Source: "5.6.7.8", Sent: 1, Received: 1}, {Source: "5.6.7.8", Sent: 2, Received: 2}},
},
{
desc: "ping returns error",
op: system.NewPingOperation().Destination("1.2.3.4").Source("5.6.7.8").Count(7),
wantErr: "ping operation error",
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
var fakeClient internal.Clients
fakeClient.SystemClient = &fakeSystemClient{SwitchControlProcessorFn: func(context.Context, *spb.SwitchControlProcessorRequest, ...grpc.CallOption) (*spb.SwitchControlProcessorResponse, error) {
fakeClient.SystemClient = &fakeSystemClient{PingFn: func(context.Context, *spb.PingRequest, ...grpc.CallOption) (spb.System_PingClient, error) {
if tt.wantErr != "" {
return nil, fmt.Errorf(tt.wantErr)
}
return tt.want, nil
return &fakePingClient{resp: tt.want}, nil
}}

got, gotErr := tt.op.Execute(context.Background(), &fakeClient)
if (gotErr == nil) != (tt.wantErr == "") || (gotErr != nil && !strings.Contains(gotErr.Error(), tt.wantErr)) {
t.Errorf("Execute() got unexpected error %v want %s", gotErr, tt.wantErr)
}
if tt.want != got {
t.Errorf("Execute() got unexpected response want %v got %v", tt.want, got)
if diff := cmp.Diff(tt.want, got, protocmp.Transform()); diff != "" {
t.Errorf("Execute() got unexpected response diff (-want +got): %s", diff)
}
})
}
}

func TestPing(t *testing.T) {
func TestSwitchControlProcessor(t *testing.T) {
tests := []struct {
desc string
op *system.PingOperation
want []*spb.PingResponse
op *system.SwitchControlProcessorOperation
want *spb.SwitchControlProcessorResponse
wantErr string
}{
{
desc: "ping with source",
op: system.NewPingOperation().Destination("1.2.3.4").Source("5.6.7.8"),
want: []*spb.PingResponse{{Source: "5.6.7.8"}},
desc: "Test SwitchControlProcessor with Path",
op: system.NewSwitchControlProcessorOperation().Path(&tpb.Path{
Origin: "openconfig",
Elem: []*tpb.PathElem{
{Name: "components"},
{Name: "component", Key: map[string]string{"name": "RP0"}},
},
}),
want: &spb.SwitchControlProcessorResponse{Version: "new"},
},
{
desc: "ping with source and count",
op: system.NewPingOperation().Destination("1.2.3.4").Source("5.6.7.8").Count(7),
want: []*spb.PingResponse{{Source: "5.6.7.8", Sent: 7, Received: 7}},
desc: "Test SwitchControlProcessor with PathFromSubcomponentName",
op: system.NewSwitchControlProcessorOperation().PathFromSubcomponentName("RP0"),
want: &spb.SwitchControlProcessorResponse{Version: "new"},
},
{
desc: "ping with multiple response",
op: system.NewPingOperation().Destination("1.2.3.4").Source("5.6.7.8").Count(7),
want: []*spb.PingResponse{{Source: "5.6.7.8", Sent: 1, Received: 1}, {Source: "5.6.7.8", Sent: 2, Received: 2}},
desc: "Test SwitchControlProcessor with PathFromSubcomponentName and Path returns error",
op: system.NewSwitchControlProcessorOperation().PathFromSubcomponentName("RP0").Path(&tpb.Path{
Origin: "openconfig",
Elem: []*tpb.PathElem{
{Name: "components"},
{Name: "component", Key: map[string]string{"name": "RP0"}},
},
}),
wantErr: "cannot set both PathFromSubcomponentName ",
},
{
desc: "ping returns error",
op: system.NewPingOperation().Destination("1.2.3.4").Source("5.6.7.8").Count(7),
wantErr: "ping operation error",
desc: "SwitchControlProcessor returns error",
op: system.NewSwitchControlProcessorOperation(),
wantErr: "SwitchControlProcessor operation error",
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
var fakeClient internal.Clients
fakeClient.SystemClient = &fakeSystemClient{PingFn: func(context.Context, *spb.PingRequest, ...grpc.CallOption) (spb.System_PingClient, error) {
fakeClient.SystemClient = &fakeSystemClient{SwitchControlProcessorFn: func(context.Context, *spb.SwitchControlProcessorRequest, ...grpc.CallOption) (*spb.SwitchControlProcessorResponse, error) {
if tt.wantErr != "" {
return nil, fmt.Errorf(tt.wantErr)
}
return &fakePingClient{resp: tt.want}, nil
return tt.want, nil
}}

got, gotErr := tt.op.Execute(context.Background(), &fakeClient)
if (gotErr == nil) != (tt.wantErr == "") || (gotErr != nil && !strings.Contains(gotErr.Error(), tt.wantErr)) {
t.Errorf("Execute() got unexpected error %v want %s", gotErr, tt.wantErr)
}
if diff := cmp.Diff(tt.want, got, protocmp.Transform()); diff != "" {
t.Errorf("Execute() got unexpected response diff (-want +got): %s", diff)
if tt.want != got {
t.Errorf("Execute() got unexpected response want %v got %v", tt.want, got)
}
})
}
Expand Down

0 comments on commit 080c75b

Please sign in to comment.