Skip to content

fix: stackit beta image commands - add nil pointer checks and tests for the outputResult functions #608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/cmd/beta/image/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,14 @@ func createPayload(_ context.Context, model *inputModel) iaas.CreateImagePayload
}

func outputResult(p *print.Printer, model *inputModel, resp *iaas.ImageCreateResponse) error {
switch model.OutputFormat {
if model == nil {
return fmt.Errorf("input model is nil")
}
var outputFormat string
if model.GlobalFlagModel != nil {
outputFormat = model.OutputFormat
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand Down
57 changes: 53 additions & 4 deletions internal/cmd/beta/image/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"strings"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
)

Expand Down Expand Up @@ -372,3 +371,53 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
model *inputModel
resp *iaas.ImageCreateResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "nil",
args: args{
model: nil,
resp: nil,
},
wantErr: true,
},
{
name: "empty input",
args: args{
model: &inputModel{},
resp: &iaas.ImageCreateResponse{},
},
wantErr: false,
},
{
name: "output json",
args: args{
model: &inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{
OutputFormat: print.JSONOutputFormat,
},
},
resp: nil,
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.model, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2 changes: 2 additions & 0 deletions internal/cmd/beta/image/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get image name: %v", err)
imageName = model.ImageId
} else if imageName == "" {
imageName = model.ImageId
}

if !model.AssumeYes {
Expand Down
9 changes: 6 additions & 3 deletions internal/cmd/beta/image/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("get image: %w", err)
}

if err := outputResult(p, model, image); err != nil {
if err := outputResult(p, model.OutputFormat, image); err != nil {
return err
}

Expand Down Expand Up @@ -95,8 +95,11 @@ func parseInput(p *print.Printer, cmd *cobra.Command, cliArgs []string) (*inputM
return &model, nil
}

func outputResult(p *print.Printer, model *inputModel, resp *iaas.Image) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, resp *iaas.Image) error {
if resp == nil {
return fmt.Errorf("image not found")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions internal/cmd/beta/image/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
resp *iaas.Image
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{
resp: &iaas.Image{},
},
wantErr: false,
},
{
name: "nil",
args: args{},
wantErr: true,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
12 changes: 11 additions & 1 deletion internal/cmd/beta/image/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
} else if projectLabel == "" {
projectLabel = model.ProjectId
}

// Call API
Expand Down Expand Up @@ -108,10 +110,18 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
return nil, &errors.ProjectIdError{}
}

limit := flags.FlagToInt64Pointer(p, cmd, limitFlag)
if limit != nil && *limit < 1 {
return nil, &errors.FlagValidationError{
Flag: limitFlag,
Details: "must be greater than 0",
}
}

model := inputModel{
GlobalFlagModel: globalFlags,
LabelSelector: flags.FlagToStringPointer(p, cmd, labelSelectorFlag),
Limit: flags.FlagToInt64Pointer(p, cmd, limitFlag),
Limit: limit,
}

if p.IsVerbosityDebug() {
Expand Down
38 changes: 38 additions & 0 deletions internal/cmd/beta/image/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,41 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
items []iaas.Image
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{
outputFormat: "",
items: []iaas.Image{},
},
wantErr: false,
},
{
name: "output format json",
args: args{
outputFormat: print.JSONOutputFormat,
items: []iaas.Image{},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2 changes: 2 additions & 0 deletions internal/cmd/beta/image/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.WarningLevel, "cannot retrieve image name: %v", err)
imageLabel = model.Id
} else if imageLabel == "" {
imageLabel = model.Id
}

if !model.AssumeYes {
Expand Down