Skip to content

Commit 1c79c47

Browse files
authored
chore(security-group): Add nil pointer checks and tests for the outputResult functions (#613)
Signed-off-by: Alexander Dahmen <[email protected]>
1 parent fdc925f commit 1c79c47

File tree

10 files changed

+220
-30
lines changed

10 files changed

+220
-30
lines changed

internal/cmd/beta/security-group/create/create.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7272
return fmt.Errorf("create security group: %w", err)
7373
}
7474

75-
if err := outputResult(p, model, group); err != nil {
75+
if err := outputResult(p, model.OutputFormat, *model.Name, *group); err != nil {
7676
return err
7777
}
7878

@@ -144,8 +144,8 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
144144
return request.CreateSecurityGroupPayload(payload)
145145
}
146146

147-
func outputResult(p *print.Printer, model *inputModel, resp *iaas.SecurityGroup) error {
148-
switch model.OutputFormat {
147+
func outputResult(p *print.Printer, outputFormat, name string, resp iaas.SecurityGroup) error {
148+
switch outputFormat {
149149
case print.JSONOutputFormat:
150150
details, err := json.MarshalIndent(resp, "", " ")
151151
if err != nil {
@@ -163,7 +163,7 @@ func outputResult(p *print.Printer, model *inputModel, resp *iaas.SecurityGroup)
163163

164164
return nil
165165
default:
166-
p.Outputf("Created security group %q\n", *model.Name)
166+
p.Outputf("Created security group %q\n", name)
167167
return nil
168168
}
169169
}

internal/cmd/beta/security-group/create/create_test.go

Lines changed: 31 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

@@ -264,3 +263,31 @@ func TestBuildRequest(t *testing.T) {
264263
})
265264
}
266265
}
266+
267+
func TestOutputResult(t *testing.T) {
268+
type args struct {
269+
outputFormat string
270+
resp iaas.SecurityGroup
271+
}
272+
tests := []struct {
273+
name string
274+
args args
275+
wantErr bool
276+
}{
277+
{
278+
name: "empty",
279+
args: args{},
280+
wantErr: false,
281+
},
282+
}
283+
284+
p := print.NewPrinter()
285+
p.Cmd = NewCmd(p)
286+
for _, tt := range tests {
287+
t.Run(tt.name, func(t *testing.T) {
288+
if err := outputResult(p, tt.args.outputFormat, tt.name, tt.args.resp); (err != nil) != tt.wantErr {
289+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
290+
}
291+
})
292+
}
293+
}

internal/cmd/beta/security-group/describe/describe.go

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

60-
if err := outputResult(p, model, group); err != nil {
60+
if err := outputResult(p, model.OutputFormat, group); err != nil {
6161
return err
6262
}
6363

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

99-
func outputResult(p *print.Printer, model *inputModel, resp *iaas.SecurityGroup) error {
100-
switch model.OutputFormat {
99+
func outputResult(p *print.Printer, outputFormat string, resp *iaas.SecurityGroup) error {
100+
if resp == nil {
101+
return fmt.Errorf("security group response is empty")
102+
}
103+
switch outputFormat {
101104
case print.JSONOutputFormat:
102105
details, err := json.MarshalIndent(resp, "", " ")
103106
if err != nil {

internal/cmd/beta/security-group/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

@@ -192,3 +191,37 @@ func TestBuildRequest(t *testing.T) {
192191
})
193192
}
194193
}
194+
195+
func TestOutputResult(t *testing.T) {
196+
type args struct {
197+
outputFormat string
198+
resp *iaas.SecurityGroup
199+
}
200+
tests := []struct {
201+
name string
202+
args args
203+
wantErr bool
204+
}{
205+
{
206+
name: "empty",
207+
args: args{},
208+
wantErr: true,
209+
},
210+
{
211+
name: "only security group as argument",
212+
args: args{
213+
resp: &iaas.SecurityGroup{},
214+
},
215+
wantErr: false,
216+
},
217+
}
218+
p := print.NewPrinter()
219+
p.Cmd = NewCmd(p)
220+
for _, tt := range tests {
221+
t.Run(tt.name, func(t *testing.T) {
222+
if err := outputResult(p, tt.args.outputFormat, tt.args.resp); (err != nil) != tt.wantErr {
223+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
224+
}
225+
})
226+
}
227+
}

internal/cmd/beta/security-group/list/list_test.go

Lines changed: 30 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

@@ -205,3 +204,30 @@ func TestBuildRequest(t *testing.T) {
205204
})
206205
}
207206
}
207+
208+
func TestOutputResult(t *testing.T) {
209+
type args struct {
210+
outputFormat string
211+
items []iaas.SecurityGroup
212+
}
213+
tests := []struct {
214+
name string
215+
args args
216+
wantErr bool
217+
}{
218+
{
219+
name: "empty",
220+
args: args{},
221+
wantErr: false,
222+
},
223+
}
224+
p := print.NewPrinter()
225+
p.Cmd = NewCmd(p)
226+
for _, tt := range tests {
227+
t.Run(tt.name, func(t *testing.T) {
228+
if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr {
229+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
230+
}
231+
})
232+
}
233+
}

internal/cmd/beta/security-group/rule/create/create.go

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

221221
func outputResult(p *print.Printer, model *inputModel, projectLabel, securityGroupName string, securityGroupRule *iaas.SecurityGroupRule) error {
222+
if securityGroupRule == nil {
223+
return fmt.Errorf("security group rule is empty")
224+
}
222225
switch model.OutputFormat {
223226
case print.JSONOutputFormat:
224227
details, err := json.MarshalIndent(securityGroupRule, "", " ")

internal/cmd/beta/security-group/rule/create/create_test.go

Lines changed: 40 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

@@ -333,3 +332,40 @@ func TestBuildRequest(t *testing.T) {
333332
})
334333
}
335334
}
335+
336+
func TestOutputResult(t *testing.T) {
337+
type args struct {
338+
model *inputModel
339+
projectLabel string
340+
securityGroupName string
341+
securityGroupRule *iaas.SecurityGroupRule
342+
}
343+
tests := []struct {
344+
name string
345+
args args
346+
wantErr bool
347+
}{
348+
{
349+
name: "empty",
350+
args: args{},
351+
wantErr: true,
352+
},
353+
{
354+
name: "only security group rule",
355+
args: args{
356+
model: fixtureInputModel(),
357+
securityGroupRule: &iaas.SecurityGroupRule{},
358+
},
359+
wantErr: false,
360+
},
361+
}
362+
p := print.NewPrinter()
363+
p.Cmd = NewCmd(p)
364+
for _, tt := range tests {
365+
t.Run(tt.name, func(t *testing.T) {
366+
if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.securityGroupName, tt.args.securityGroupRule); (err != nil) != tt.wantErr {
367+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
368+
}
369+
})
370+
}
371+
}

internal/cmd/beta/security-group/rule/describe/describe.go

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

115115
func outputResult(p *print.Printer, outputFormat string, securityGroupRule *iaas.SecurityGroupRule) error {
116+
if securityGroupRule == nil {
117+
return fmt.Errorf("security group rule is empty")
118+
}
116119
switch outputFormat {
117120
case print.JSONOutputFormat:
118121
details, err := json.MarshalIndent(securityGroupRule, "", " ")

internal/cmd/beta/security-group/rule/describe/describe_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

@@ -244,3 +243,37 @@ func TestBuildRequest(t *testing.T) {
244243
})
245244
}
246245
}
246+
247+
func TestOutputResult(t *testing.T) {
248+
type args struct {
249+
outputFormat string
250+
securityGroupRule *iaas.SecurityGroupRule
251+
}
252+
tests := []struct {
253+
name string
254+
args args
255+
wantErr bool
256+
}{
257+
{
258+
name: "empty",
259+
args: args{},
260+
wantErr: true,
261+
},
262+
{
263+
name: "only security group rule as argument",
264+
args: args{
265+
securityGroupRule: &iaas.SecurityGroupRule{},
266+
},
267+
wantErr: false,
268+
},
269+
}
270+
p := print.NewPrinter()
271+
p.Cmd = NewCmd(p)
272+
for _, tt := range tests {
273+
t.Run(tt.name, func(t *testing.T) {
274+
if err := outputResult(p, tt.args.outputFormat, tt.args.securityGroupRule); (err != nil) != tt.wantErr {
275+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
276+
}
277+
})
278+
}
279+
}

internal/cmd/beta/security-group/rule/list/list_test.go

Lines changed: 30 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

@@ -212,3 +211,30 @@ func TestBuildRequest(t *testing.T) {
212211
})
213212
}
214213
}
214+
215+
func TestOutputResult(t *testing.T) {
216+
type args struct {
217+
outputFormat string
218+
securityGroupRules []iaas.SecurityGroupRule
219+
}
220+
tests := []struct {
221+
name string
222+
args args
223+
wantErr bool
224+
}{
225+
{
226+
name: "empty",
227+
args: args{},
228+
wantErr: false,
229+
},
230+
}
231+
p := print.NewPrinter()
232+
p.Cmd = NewCmd(p)
233+
for _, tt := range tests {
234+
t.Run(tt.name, func(t *testing.T) {
235+
if err := outputResult(p, tt.args.outputFormat, tt.args.securityGroupRules); (err != nil) != tt.wantErr {
236+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
237+
}
238+
})
239+
}
240+
}

0 commit comments

Comments
 (0)