Skip to content

Commit b5c5cad

Browse files
authored
chore(volume): Add nil pointer checks and tests for outputResult functions (#623)
Signed-off-by: Alexander Dahmen <[email protected]>
1 parent f8bb8e9 commit b5c5cad

File tree

11 files changed

+242
-27
lines changed

11 files changed

+242
-27
lines changed

internal/cmd/beta/volume/create/create.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
199199
}
200200

201201
func outputResult(p *print.Printer, model *inputModel, projectLabel string, volume *iaas.Volume) error {
202+
if volume == nil {
203+
return fmt.Errorf("volume response is empty")
204+
}
202205
switch model.OutputFormat {
203206
case print.JSONOutputFormat:
204207
details, err := json.MarshalIndent(volume, "", " ")

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
10-
117
"github.com/google/go-cmp/cmp"
128
"github.com/google/go-cmp/cmp/cmpopts"
139
"github.com/google/uuid"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1413
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1514
)
1615

@@ -284,3 +283,39 @@ func TestBuildRequest(t *testing.T) {
284283
})
285284
}
286285
}
286+
287+
func TestOutputResult(t *testing.T) {
288+
type args struct {
289+
model *inputModel
290+
projectLabel string
291+
volume *iaas.Volume
292+
}
293+
tests := []struct {
294+
name string
295+
args args
296+
wantErr bool
297+
}{
298+
{
299+
name: "empty",
300+
args: args{},
301+
wantErr: true,
302+
},
303+
{
304+
name: "volume as argument",
305+
args: args{
306+
model: fixtureInputModel(),
307+
volume: &iaas.Volume{},
308+
},
309+
wantErr: false,
310+
},
311+
}
312+
p := print.NewPrinter()
313+
p.Cmd = NewCmd(p)
314+
for _, tt := range tests {
315+
t.Run(tt.name, func(t *testing.T) {
316+
if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.volume); (err != nil) != tt.wantErr {
317+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
318+
}
319+
})
320+
}
321+
}

internal/cmd/beta/volume/delete/delete.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ func NewCmd(p *print.Printer) *cobra.Command {
5656
return err
5757
}
5858

59-
volumeLabel, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, model.VolumeId)
59+
volumeLabel := model.VolumeId
60+
volumeName, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, model.VolumeId)
6061
if err != nil {
6162
p.Debug(print.ErrorLevel, "get volume name: %v", err)
62-
volumeLabel = model.VolumeId
63+
} else if volumeName != "" {
64+
volumeLabel = volumeName
6365
}
6466

6567
if !model.AssumeYes {

internal/cmd/beta/volume/describe/describe.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
102102
}
103103

104104
func outputResult(p *print.Printer, outputFormat string, volume *iaas.Volume) error {
105+
if volume == nil {
106+
return fmt.Errorf("volume response is empty")
107+
}
105108
switch outputFormat {
106109
case print.JSONOutputFormat:
107110
details, err := json.MarshalIndent(volume, "", " ")

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9-
107
"github.com/google/go-cmp/cmp"
118
"github.com/google/go-cmp/cmp/cmpopts"
129
"github.com/google/uuid"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1312
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1413
)
1514

@@ -216,3 +215,37 @@ func TestBuildRequest(t *testing.T) {
216215
})
217216
}
218217
}
218+
219+
func TestOutputResult(t *testing.T) {
220+
type args struct {
221+
outputFormat string
222+
volume *iaas.Volume
223+
}
224+
tests := []struct {
225+
name string
226+
args args
227+
wantErr bool
228+
}{
229+
{
230+
name: "empty",
231+
args: args{},
232+
wantErr: true,
233+
},
234+
{
235+
name: "volume as argument",
236+
args: args{
237+
volume: &iaas.Volume{},
238+
},
239+
wantErr: false,
240+
},
241+
}
242+
p := print.NewPrinter()
243+
p.Cmd = NewCmd(p)
244+
for _, tt := range tests {
245+
t.Run(tt.name, func(t *testing.T) {
246+
if err := outputResult(p, tt.args.outputFormat, tt.args.volume); (err != nil) != tt.wantErr {
247+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
248+
}
249+
})
250+
}
251+
}

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
10-
117
"github.com/google/go-cmp/cmp"
128
"github.com/google/go-cmp/cmp/cmpopts"
139
"github.com/google/uuid"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1413
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1514
)
1615

@@ -202,3 +201,37 @@ func TestBuildRequest(t *testing.T) {
202201
})
203202
}
204203
}
204+
205+
func TestOutputResult(t *testing.T) {
206+
type args struct {
207+
outputFormat string
208+
volumes []iaas.Volume
209+
}
210+
tests := []struct {
211+
name string
212+
args args
213+
wantErr bool
214+
}{
215+
{
216+
name: "empty",
217+
args: args{},
218+
wantErr: false,
219+
},
220+
{
221+
name: "set empty volume",
222+
args: args{
223+
volumes: []iaas.Volume{{}},
224+
},
225+
wantErr: false,
226+
},
227+
}
228+
p := print.NewPrinter()
229+
p.Cmd = NewCmd(p)
230+
for _, tt := range tests {
231+
t.Run(tt.name, func(t *testing.T) {
232+
if err := outputResult(p, tt.args.outputFormat, tt.args.volumes); (err != nil) != tt.wantErr {
233+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
234+
}
235+
})
236+
}
237+
}

internal/cmd/beta/volume/performance-class/describe/describe.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
102102
}
103103

104104
func outputResult(p *print.Printer, outputFormat string, performanceClass *iaas.VolumePerformanceClass) error {
105+
if performanceClass == nil {
106+
return fmt.Errorf("performanceClass response is empty")
107+
}
105108
switch outputFormat {
106109
case print.JSONOutputFormat:
107110
details, err := json.MarshalIndent(performanceClass, "", " ")

internal/cmd/beta/volume/performance-class/describe/describe_test.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9-
107
"github.com/google/go-cmp/cmp"
118
"github.com/google/go-cmp/cmp/cmpopts"
129
"github.com/google/uuid"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1312
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1413
)
1514

@@ -210,3 +209,37 @@ func TestBuildRequest(t *testing.T) {
210209
})
211210
}
212211
}
212+
213+
func TestOutputResult(t *testing.T) {
214+
type args struct {
215+
outputFormat string
216+
performanceClass *iaas.VolumePerformanceClass
217+
}
218+
tests := []struct {
219+
name string
220+
args args
221+
wantErr bool
222+
}{
223+
{
224+
name: "empty",
225+
args: args{},
226+
wantErr: true,
227+
},
228+
{
229+
name: "volume performance class as argument",
230+
args: args{
231+
performanceClass: &iaas.VolumePerformanceClass{},
232+
},
233+
wantErr: false,
234+
},
235+
}
236+
p := print.NewPrinter()
237+
p.Cmd = NewCmd(p)
238+
for _, tt := range tests {
239+
t.Run(tt.name, func(t *testing.T) {
240+
if err := outputResult(p, tt.args.outputFormat, tt.args.performanceClass); (err != nil) != tt.wantErr {
241+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
242+
}
243+
})
244+
}
245+
}

internal/cmd/beta/volume/performance-class/list/list_test.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
10-
117
"github.com/google/go-cmp/cmp"
128
"github.com/google/go-cmp/cmp/cmpopts"
139
"github.com/google/uuid"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1413
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1514
)
1615

@@ -202,3 +201,37 @@ func TestBuildRequest(t *testing.T) {
202201
})
203202
}
204203
}
204+
205+
func TestOutputResult(t *testing.T) {
206+
type args struct {
207+
outputFormat string
208+
performanceClasses []iaas.VolumePerformanceClass
209+
}
210+
tests := []struct {
211+
name string
212+
args args
213+
wantErr bool
214+
}{
215+
{
216+
name: "empty",
217+
args: args{},
218+
wantErr: false,
219+
},
220+
{
221+
name: "set empty volume",
222+
args: args{
223+
performanceClasses: []iaas.VolumePerformanceClass{{}},
224+
},
225+
wantErr: false,
226+
},
227+
}
228+
p := print.NewPrinter()
229+
p.Cmd = NewCmd(p)
230+
for _, tt := range tests {
231+
t.Run(tt.name, func(t *testing.T) {
232+
if err := outputResult(p, tt.args.outputFormat, tt.args.performanceClasses); (err != nil) != tt.wantErr {
233+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
234+
}
235+
})
236+
}
237+
}

internal/cmd/beta/volume/update/update.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
9090
return fmt.Errorf("update volume: %w", err)
9191
}
9292

93-
return outputResult(p, model, volumeLabel, resp)
93+
return outputResult(p, model.OutputFormat, volumeLabel, resp)
9494
},
9595
}
9696
configureFlags(cmd)
@@ -152,8 +152,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
152152
return req.UpdateVolumePayload(payload)
153153
}
154154

155-
func outputResult(p *print.Printer, model *inputModel, volumeLabel string, volume *iaas.Volume) error {
156-
switch model.OutputFormat {
155+
func outputResult(p *print.Printer, outputFormat, volumeLabel string, volume *iaas.Volume) error {
156+
if volume == nil {
157+
return fmt.Errorf("volume response is empty")
158+
}
159+
switch outputFormat {
157160
case print.JSONOutputFormat:
158161
details, err := json.MarshalIndent(volume, "", " ")
159162
if err != nil {

0 commit comments

Comments
 (0)