Skip to content

Commit 4e8d53f

Browse files
authored
fix: stackit beta image commands - add nil pointer checks and tests for the outputResult functions (#608)
* add nil pointer checks * add tests for the outputResult functions within the image commands
1 parent 566ed85 commit 4e8d53f

File tree

8 files changed

+154
-9
lines changed

8 files changed

+154
-9
lines changed

internal/cmd/beta/image/create/create.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,14 @@ func createPayload(_ context.Context, model *inputModel) iaas.CreateImagePayload
377377
}
378378

379379
func outputResult(p *print.Printer, model *inputModel, resp *iaas.ImageCreateResponse) error {
380-
switch model.OutputFormat {
380+
if model == nil {
381+
return fmt.Errorf("input model is nil")
382+
}
383+
var outputFormat string
384+
if model.GlobalFlagModel != nil {
385+
outputFormat = model.OutputFormat
386+
}
387+
switch outputFormat {
381388
case print.JSONOutputFormat:
382389
details, err := json.MarshalIndent(resp, "", " ")
383390
if err != nil {

internal/cmd/beta/image/create/create_test.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import (
66
"strings"
77
"testing"
88

9-
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
10-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
11-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
12-
139
"github.com/google/go-cmp/cmp"
1410
"github.com/google/go-cmp/cmp/cmpopts"
1511
"github.com/google/uuid"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1615
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1716
)
1817

@@ -372,3 +371,53 @@ func TestBuildRequest(t *testing.T) {
372371
})
373372
}
374373
}
374+
375+
func TestOutputResult(t *testing.T) {
376+
type args struct {
377+
model *inputModel
378+
resp *iaas.ImageCreateResponse
379+
}
380+
tests := []struct {
381+
name string
382+
args args
383+
wantErr bool
384+
}{
385+
{
386+
name: "nil",
387+
args: args{
388+
model: nil,
389+
resp: nil,
390+
},
391+
wantErr: true,
392+
},
393+
{
394+
name: "empty input",
395+
args: args{
396+
model: &inputModel{},
397+
resp: &iaas.ImageCreateResponse{},
398+
},
399+
wantErr: false,
400+
},
401+
{
402+
name: "output json",
403+
args: args{
404+
model: &inputModel{
405+
GlobalFlagModel: &globalflags.GlobalFlagModel{
406+
OutputFormat: print.JSONOutputFormat,
407+
},
408+
},
409+
resp: nil,
410+
},
411+
wantErr: false,
412+
},
413+
}
414+
p := print.NewPrinter()
415+
p.Cmd = NewCmd(p)
416+
for _, tt := range tests {
417+
t.Run(tt.name, func(t *testing.T) {
418+
if err := outputResult(p, tt.args.model, tt.args.resp); (err != nil) != tt.wantErr {
419+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
420+
}
421+
})
422+
}
423+
}

internal/cmd/beta/image/delete/delete.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
5656
if err != nil {
5757
p.Debug(print.ErrorLevel, "get image name: %v", err)
5858
imageName = model.ImageId
59+
} else if imageName == "" {
60+
imageName = model.ImageId
5961
}
6062

6163
if !model.AssumeYes {

internal/cmd/beta/image/describe/describe.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
5656
return fmt.Errorf("get image: %w", err)
5757
}
5858

59-
if err := outputResult(p, model, image); err != nil {
59+
if err := outputResult(p, model.OutputFormat, image); err != nil {
6060
return err
6161
}
6262

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

98-
func outputResult(p *print.Printer, model *inputModel, resp *iaas.Image) error {
99-
switch model.OutputFormat {
98+
func outputResult(p *print.Printer, outputFormat string, resp *iaas.Image) error {
99+
if resp == nil {
100+
return fmt.Errorf("image not found")
101+
}
102+
switch outputFormat {
100103
case print.JSONOutputFormat:
101104
details, err := json.MarshalIndent(resp, "", " ")
102105
if err != nil {

internal/cmd/beta/image/describe/describe_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,37 @@ func TestBuildRequest(t *testing.T) {
192192
})
193193
}
194194
}
195+
196+
func TestOutputResult(t *testing.T) {
197+
type args struct {
198+
outputFormat string
199+
resp *iaas.Image
200+
}
201+
tests := []struct {
202+
name string
203+
args args
204+
wantErr bool
205+
}{
206+
{
207+
name: "empty",
208+
args: args{
209+
resp: &iaas.Image{},
210+
},
211+
wantErr: false,
212+
},
213+
{
214+
name: "nil",
215+
args: args{},
216+
wantErr: true,
217+
},
218+
}
219+
p := print.NewPrinter()
220+
p.Cmd = NewCmd(p)
221+
for _, tt := range tests {
222+
t.Run(tt.name, func(t *testing.T) {
223+
if err := outputResult(p, tt.args.outputFormat, tt.args.resp); (err != nil) != tt.wantErr {
224+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
225+
}
226+
})
227+
}
228+
}

internal/cmd/beta/image/list/list.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
6868
if err != nil {
6969
p.Debug(print.ErrorLevel, "get project name: %v", err)
7070
projectLabel = model.ProjectId
71+
} else if projectLabel == "" {
72+
projectLabel = model.ProjectId
7173
}
7274

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

113+
limit := flags.FlagToInt64Pointer(p, cmd, limitFlag)
114+
if limit != nil && *limit < 1 {
115+
return nil, &errors.FlagValidationError{
116+
Flag: limitFlag,
117+
Details: "must be greater than 0",
118+
}
119+
}
120+
111121
model := inputModel{
112122
GlobalFlagModel: globalFlags,
113123
LabelSelector: flags.FlagToStringPointer(p, cmd, labelSelectorFlag),
114-
Limit: flags.FlagToInt64Pointer(p, cmd, limitFlag),
124+
Limit: limit,
115125
}
116126

117127
if p.IsVerbosityDebug() {

internal/cmd/beta/image/list/list_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,41 @@ func TestBuildRequest(t *testing.T) {
210210
})
211211
}
212212
}
213+
214+
func Test_outputResult(t *testing.T) {
215+
type args struct {
216+
outputFormat string
217+
items []iaas.Image
218+
}
219+
tests := []struct {
220+
name string
221+
args args
222+
wantErr bool
223+
}{
224+
{
225+
name: "empty",
226+
args: args{
227+
outputFormat: "",
228+
items: []iaas.Image{},
229+
},
230+
wantErr: false,
231+
},
232+
{
233+
name: "output format json",
234+
args: args{
235+
outputFormat: print.JSONOutputFormat,
236+
items: []iaas.Image{},
237+
},
238+
wantErr: false,
239+
},
240+
}
241+
p := print.NewPrinter()
242+
p.Cmd = NewCmd(p)
243+
for _, tt := range tests {
244+
t.Run(tt.name, func(t *testing.T) {
245+
if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr {
246+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
247+
}
248+
})
249+
}
250+
}

internal/cmd/beta/image/update/update.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
134134
if err != nil {
135135
p.Debug(print.WarningLevel, "cannot retrieve image name: %v", err)
136136
imageLabel = model.Id
137+
} else if imageLabel == "" {
138+
imageLabel = model.Id
137139
}
138140

139141
if !model.AssumeYes {

0 commit comments

Comments
 (0)