Skip to content

Commit

Permalink
Add OS Activate and OS Verify Operations
Browse files Browse the repository at this point in the history
  • Loading branch information
asafsonnv committed Mar 18, 2024
1 parent d529c60 commit 21dd3ed
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
51 changes: 51 additions & 0 deletions os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,42 @@ import (
"github.com/openconfig/gnoigo/internal"
)

// ActivateOperation represents the parameters of a Activate operation.
type ActivateOperation struct {
req *ospb.ActivateRequest
}

// Version specifies the OS version that is required to be activated.
func (a *ActivateOperation) Version(version string) *ActivateOperation {
a.req.Version = version
return a
}

// Standby specifies whether to activate the standby supervisor.
func (a *ActivateOperation) Standby(standby bool) *ActivateOperation {
a.req.StandbySupervisor = standby
return a
}

// NoReboot specifies whether to skip an immediate reboot after changing the
// next bootable OS version. If false, an immediate reboot is initiated after
// changing the activated OS version. If true, a separate action to reboot is
// required to start using the activated OS version.
func (a *ActivateOperation) NoReboot(noReboot bool) *ActivateOperation {
a.req.NoReboot = noReboot
return a
}

// NewActivateOperation creates an empty ActivateOperation.
func NewActivateOperation() *ActivateOperation {
return &ActivateOperation{req: &ospb.ActivateRequest{}}
}

// Execute performs the Activate operation.
func (a *ActivateOperation) Execute(ctx context.Context, c *internal.Clients) (*ospb.ActivateResponse, error) {
return c.OS().Activate(ctx, a.req)
}

// InstallOperation represents the parameters of a Install operation.
type InstallOperation struct {
req *ospb.TransferRequest
Expand Down Expand Up @@ -170,3 +206,18 @@ func (i *InstallOperation) Execute(ctx context.Context, c *internal.Clients) (*o
}
return installResp, nil
}

// VerifyOperation represents the parameters of a Verify operation.
type VerifyOperation struct {
req *ospb.VerifyRequest
}

// NewVerifyOperation creates an empty VerifyOperation.
func NewVerifyOperation() *VerifyOperation {
return &VerifyOperation{req: &ospb.VerifyRequest{}}
}

// Execute performs the Verify operation.
func (v *VerifyOperation) Execute(ctx context.Context, c *internal.Clients) (*ospb.VerifyResponse, error) {
return c.OS().Verify(ctx, v.req)
}
90 changes: 89 additions & 1 deletion os/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ import (

type fakeOSClient struct {
ospb.OSClient
InstallFn func(context.Context, ...grpc.CallOption) (ospb.OS_InstallClient, error)
InstallFn func(context.Context, ...grpc.CallOption) (ospb.OS_InstallClient, error)
ActivateFn func(context.Context, *ospb.ActivateRequest, ...grpc.CallOption) (*ospb.ActivateResponse, error)
VerifyFn func(context.Context, *ospb.VerifyRequest, ...grpc.CallOption) (*ospb.VerifyResponse, error)
}

func (fg *fakeOSClient) OS() ospb.OSClient {
Expand All @@ -44,6 +46,14 @@ func (fg *fakeOSClient) Install(ctx context.Context, opts ...grpc.CallOption) (o
return fg.InstallFn(ctx, opts...)
}

func (fg *fakeOSClient) Activate(ctx context.Context, in *ospb.ActivateRequest, opts ...grpc.CallOption) (*ospb.ActivateResponse, error) {
return fg.ActivateFn(ctx, in, opts...)
}

func (fg *fakeOSClient) Verify(ctx context.Context, in *ospb.VerifyRequest, opts ...grpc.CallOption) (*ospb.VerifyResponse, error) {
return fg.VerifyFn(ctx, in, opts...)
}

type fakeInstallClient struct {
ospb.OS_InstallClient
gotSent []*ospb.InstallRequest
Expand Down Expand Up @@ -171,3 +181,81 @@ func TestInstall(t *testing.T) {
})
}
}

func TestActivate(t *testing.T) {
tests := []struct {
desc string
op *gos.ActivateOperation
want *ospb.ActivateResponse
wantErr string
}{
{
desc: "Test Activate",
op: gos.NewActivateOperation(),
want: &ospb.ActivateResponse{Response: &ospb.ActivateResponse_ActivateOk{}},
},
{
desc: "Activate returns error",
op: gos.NewActivateOperation(),
wantErr: "Activate operation error",
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
var fakeClient internal.Clients
fakeClient.OSClient = &fakeOSClient{ActivateFn: func(context.Context, *ospb.ActivateRequest, ...grpc.CallOption) (*ospb.ActivateResponse, error) {
if tt.wantErr != "" {
return nil, fmt.Errorf(tt.wantErr)
}
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 tt.want != got {
t.Errorf("Execute() got unexpected response want %v got %v", tt.want, got)
}
})
}
}

func TestVerify(t *testing.T) {
tests := []struct {
desc string
op *gos.VerifyOperation
want *ospb.VerifyResponse
wantErr string
}{
{
desc: "Test Verify",
op: gos.NewVerifyOperation(),
want: &ospb.VerifyResponse{Version: "1.2.3"},
},
{
desc: "Verify returns error",
op: gos.NewVerifyOperation(),
wantErr: "Verify operation error",
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
var fakeClient internal.Clients
fakeClient.OSClient = &fakeOSClient{VerifyFn: func(context.Context, *ospb.VerifyRequest, ...grpc.CallOption) (*ospb.VerifyResponse, error) {
if tt.wantErr != "" {
return nil, fmt.Errorf(tt.wantErr)
}
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 tt.want != got {
t.Errorf("Execute() got unexpected response want %v got %v", tt.want, got)
}
})
}
}

0 comments on commit 21dd3ed

Please sign in to comment.