Skip to content

Commit ff37f2a

Browse files
authored
chore(load-balancer): Add nil pointer checks and tests for the outputResult functions (#624)
Signed-off-by: Alexander Dahmen <[email protected]>
1 parent e6cf71b commit ff37f2a

File tree

15 files changed

+373
-42
lines changed

15 files changed

+373
-42
lines changed

internal/cmd/load-balancer/describe/describe.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *loadbalance
9999
}
100100

101101
func outputResult(p *print.Printer, outputFormat string, loadBalancer *loadbalancer.LoadBalancer) error {
102+
if loadBalancer == nil {
103+
return fmt.Errorf("loadbalancer response is empty")
104+
}
102105
switch outputFormat {
103106
case print.JSONOutputFormat:
104107
details, err := json.MarshalIndent(loadBalancer, "", " ")

internal/cmd/load-balancer/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/loadbalancer"
1413
)
1514

@@ -206,3 +205,37 @@ func TestBuildRequest(t *testing.T) {
206205
})
207206
}
208207
}
208+
209+
func TestOutputResult(t *testing.T) {
210+
type args struct {
211+
outputFormat string
212+
loadBalancer *loadbalancer.LoadBalancer
213+
}
214+
tests := []struct {
215+
name string
216+
args args
217+
wantErr bool
218+
}{
219+
{
220+
name: "empty",
221+
args: args{},
222+
wantErr: true,
223+
},
224+
{
225+
name: "only loadbalancer as argument",
226+
args: args{
227+
loadBalancer: &loadbalancer.LoadBalancer{},
228+
},
229+
wantErr: false,
230+
},
231+
}
232+
p := print.NewPrinter()
233+
p.Cmd = NewCmd(p)
234+
for _, tt := range tests {
235+
t.Run(tt.name, func(t *testing.T) {
236+
if err := outputResult(p, tt.args.outputFormat, tt.args.loadBalancer); (err != nil) != tt.wantErr {
237+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
238+
}
239+
})
240+
}
241+
}

internal/cmd/load-balancer/generate-payload/generate_payload.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,16 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *loadbalance
213213
}
214214

215215
func outputCreateResult(p *print.Printer, filePath *string, payload *loadbalancer.CreateLoadBalancerPayload) error {
216+
if payload == nil {
217+
return fmt.Errorf("loadbalancer payload is empty")
218+
}
216219
payloadBytes, err := json.MarshalIndent(*payload, "", " ")
217220
if err != nil {
218221
return fmt.Errorf("marshal create load balancer payload: %w", err)
219222
}
220223

221224
if filePath != nil {
222-
err = fileutils.WriteToFile(*filePath, string(payloadBytes))
225+
err = fileutils.WriteToFile(utils.PtrString(filePath), string(payloadBytes))
223226
if err != nil {
224227
return fmt.Errorf("write create load balancer payload to the file: %w", err)
225228
}
@@ -231,13 +234,16 @@ func outputCreateResult(p *print.Printer, filePath *string, payload *loadbalance
231234
}
232235

233236
func outputUpdateResult(p *print.Printer, filePath *string, payload *loadbalancer.UpdateLoadBalancerPayload) error {
237+
if payload == nil {
238+
return fmt.Errorf("loadbalancer payload is empty")
239+
}
234240
payloadBytes, err := json.MarshalIndent(*payload, "", " ")
235241
if err != nil {
236242
return fmt.Errorf("marshal update load balancer payload: %w", err)
237243
}
238244

239245
if filePath != nil {
240-
err = fileutils.WriteToFile(*filePath, string(payloadBytes))
246+
err = fileutils.WriteToFile(utils.PtrString(filePath), string(payloadBytes))
241247
if err != nil {
242248
return fmt.Errorf("write update load balancer payload to the file: %w", err)
243249
}

internal/cmd/load-balancer/generate-payload/generate_payload_test.go

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +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"
14-
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"
1513
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
1614
)
1715

@@ -310,3 +308,71 @@ func TestModifyListeners(t *testing.T) {
310308
})
311309
}
312310
}
311+
312+
func TestOutputCreateResult(t *testing.T) {
313+
type args struct {
314+
filePath *string
315+
payload *loadbalancer.CreateLoadBalancerPayload
316+
}
317+
tests := []struct {
318+
name string
319+
args args
320+
wantErr bool
321+
}{
322+
{
323+
name: "empty",
324+
args: args{},
325+
wantErr: true,
326+
},
327+
{
328+
name: "only loadbalancer payload as argument",
329+
args: args{
330+
payload: &loadbalancer.CreateLoadBalancerPayload{},
331+
},
332+
wantErr: false,
333+
},
334+
}
335+
p := print.NewPrinter()
336+
p.Cmd = NewCmd(p)
337+
for _, tt := range tests {
338+
t.Run(tt.name, func(t *testing.T) {
339+
if err := outputCreateResult(p, tt.args.filePath, tt.args.payload); (err != nil) != tt.wantErr {
340+
t.Errorf("outputCreateResult() error = %v, wantErr %v", err, tt.wantErr)
341+
}
342+
})
343+
}
344+
}
345+
346+
func TestOutputUpdateResult(t *testing.T) {
347+
type args struct {
348+
filePath *string
349+
payload *loadbalancer.UpdateLoadBalancerPayload
350+
}
351+
tests := []struct {
352+
name string
353+
args args
354+
wantErr bool
355+
}{
356+
{
357+
name: "empty",
358+
args: args{},
359+
wantErr: true,
360+
},
361+
{
362+
name: "only loadbalancer payload as argument",
363+
args: args{
364+
payload: &loadbalancer.UpdateLoadBalancerPayload{},
365+
},
366+
wantErr: false,
367+
},
368+
}
369+
p := print.NewPrinter()
370+
p.Cmd = NewCmd(p)
371+
for _, tt := range tests {
372+
t.Run(tt.name, func(t *testing.T) {
373+
if err := outputUpdateResult(p, tt.args.filePath, tt.args.payload); (err != nil) != tt.wantErr {
374+
t.Errorf("outputUpdateResult() error = %v, wantErr %v", err, tt.wantErr)
375+
}
376+
})
377+
}
378+
}

internal/cmd/load-balancer/list/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ func outputResult(p *print.Printer, outputFormat string, loadBalancers []loadbal
159159
utils.PtrString(l.Name),
160160
utils.PtrString(l.Status),
161161
externalAdress,
162-
len(*l.Listeners),
163-
len(*l.TargetPools),
162+
len(utils.PtrString(l.Listeners)),
163+
len(utils.PtrString(l.TargetPools)),
164164
)
165165
}
166166
err := table.Display(p)

internal/cmd/load-balancer/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/loadbalancer"
1514
)
1615

@@ -183,3 +182,37 @@ func TestBuildRequest(t *testing.T) {
183182
})
184183
}
185184
}
185+
186+
func TestOutputResult(t *testing.T) {
187+
type args struct {
188+
outputFormat string
189+
loadBalancers []loadbalancer.LoadBalancer
190+
}
191+
tests := []struct {
192+
name string
193+
args args
194+
wantErr bool
195+
}{
196+
{
197+
name: "empty",
198+
args: args{},
199+
wantErr: false,
200+
},
201+
{
202+
name: "empty loadbalancer in loadbalancers slice",
203+
args: args{
204+
loadBalancers: []loadbalancer.LoadBalancer{{}},
205+
},
206+
wantErr: false,
207+
},
208+
}
209+
p := print.NewPrinter()
210+
p.Cmd = NewCmd(p)
211+
for _, tt := range tests {
212+
t.Run(tt.name, func(t *testing.T) {
213+
if err := outputResult(p, tt.args.outputFormat, tt.args.loadBalancers); (err != nil) != tt.wantErr {
214+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
215+
}
216+
})
217+
}
218+
}

internal/cmd/load-balancer/observability-credentials/add/add.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
9191
return fmt.Errorf("add Load Balancer observability credentials: %w", err)
9292
}
9393

94-
return outputResult(p, model, projectLabel, resp)
94+
return outputResult(p, model.OutputFormat, projectLabel, resp)
9595
},
9696
}
9797
configureFlags(cmd)
@@ -144,12 +144,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *loadbalance
144144
return req
145145
}
146146

147-
func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *loadbalancer.CreateCredentialsResponse) error {
148-
if resp.Credential == nil {
147+
func outputResult(p *print.Printer, outputFormat, projectLabel string, resp *loadbalancer.CreateCredentialsResponse) error {
148+
if resp == nil || resp.Credential == nil {
149149
return fmt.Errorf("nil observability credentials response")
150150
}
151151

152-
switch model.OutputFormat {
152+
switch outputFormat {
153153
case print.JSONOutputFormat:
154154
details, err := json.MarshalIndent(resp, "", " ")
155155
if err != nil {

internal/cmd/load-balancer/observability-credentials/add/add_test.go

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

7+
"github.com/google/go-cmp/cmp"
8+
"github.com/google/go-cmp/cmp/cmpopts"
9+
"github.com/google/uuid"
710
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
811
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
912
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1013
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
11-
12-
"github.com/google/go-cmp/cmp"
13-
"github.com/google/go-cmp/cmp/cmpopts"
14-
"github.com/google/uuid"
1514
)
1615

1716
var projectIdFlag = globalflags.ProjectIdFlag
@@ -193,3 +192,45 @@ func TestBuildRequest(t *testing.T) {
193192
})
194193
}
195194
}
195+
196+
func TestOutputResult(t *testing.T) {
197+
type args struct {
198+
outputFormat string
199+
projectLabel string
200+
resp *loadbalancer.CreateCredentialsResponse
201+
}
202+
tests := []struct {
203+
name string
204+
args args
205+
wantErr bool
206+
}{
207+
{
208+
name: "empty",
209+
args: args{},
210+
wantErr: true,
211+
},
212+
{
213+
name: "credentials response with empty Credential",
214+
args: args{
215+
resp: &loadbalancer.CreateCredentialsResponse{},
216+
},
217+
wantErr: true,
218+
},
219+
{
220+
name: "only credentials response as argument",
221+
args: args{
222+
resp: &loadbalancer.CreateCredentialsResponse{Credential: &loadbalancer.CredentialsResponse{}},
223+
},
224+
wantErr: false,
225+
},
226+
}
227+
p := print.NewPrinter()
228+
p.Cmd = NewCmd(p)
229+
for _, tt := range tests {
230+
t.Run(tt.name, func(t *testing.T) {
231+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr {
232+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
233+
}
234+
})
235+
}
236+
}

internal/cmd/load-balancer/observability-credentials/describe/describe.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ func outputResult(p *print.Printer, outputFormat string, credentials *loadbalanc
115115
return nil
116116
default:
117117
if credentials == nil || credentials.Credential == nil {
118-
p.Info("No credentials found")
119-
return nil
118+
return fmt.Errorf("credentials response is empty")
120119
}
121120

122121
table := tables.NewTable()

0 commit comments

Comments
 (0)