Skip to content

Commit 7a85934

Browse files
committed
chore: add nil checks and testcases
1 parent 0f71ec9 commit 7a85934

File tree

13 files changed

+333
-49
lines changed

13 files changed

+333
-49
lines changed

internal/cmd/rabbitmq/credentials/create/create.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7878
return fmt.Errorf("create RabbitMQ credentials: %w", err)
7979
}
8080

81-
return outputResult(p, model, instanceLabel, resp)
81+
return outputResult(p, *model, instanceLabel, resp)
8282
},
8383
}
8484
configureFlags(cmd)
@@ -122,8 +122,20 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP
122122
return req
123123
}
124124

125-
func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *rabbitmq.CredentialsResponse) error {
125+
func outputResult(p *print.Printer, model inputModel, instanceLabel string, resp *rabbitmq.CredentialsResponse) error {
126+
if model.GlobalFlagModel == nil {
127+
return fmt.Errorf("no global flags available")
128+
}
129+
if resp == nil {
130+
return fmt.Errorf("no response available")
131+
}
132+
126133
if !model.ShowPassword {
134+
if resp.Raw == nil {
135+
resp.Raw = &rabbitmq.RawCredentials{Credentials: &rabbitmq.Credentials{}}
136+
} else if resp.Raw.Credentials == nil {
137+
resp.Raw.Credentials = &rabbitmq.Credentials{}
138+
}
127139
resp.Raw.Credentials.Password = utils.Ptr("hidden")
128140
}
129141
switch model.OutputFormat {

internal/cmd/rabbitmq/credentials/create/create_test.go

Lines changed: 45 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/rabbitmq"
1413
)
1514

@@ -200,3 +199,46 @@ func TestBuildRequest(t *testing.T) {
200199
})
201200
}
202201
}
202+
203+
func Test_outputResult(t *testing.T) {
204+
type args struct {
205+
model inputModel
206+
instanceLabel string
207+
resp *rabbitmq.CredentialsResponse
208+
}
209+
tests := []struct {
210+
name string
211+
args args
212+
wantErr bool
213+
}{
214+
{
215+
name: "empty",
216+
args: args{
217+
model: inputModel{
218+
GlobalFlagModel: &globalflags.GlobalFlagModel{},
219+
},
220+
instanceLabel: "",
221+
resp: &rabbitmq.CredentialsResponse{},
222+
},
223+
wantErr: false,
224+
},
225+
{
226+
name: "no flags",
227+
args: args{
228+
model: inputModel{},
229+
instanceLabel: "",
230+
resp: &rabbitmq.CredentialsResponse{},
231+
},
232+
wantErr: true,
233+
},
234+
}
235+
p := print.NewPrinter()
236+
p.Cmd = NewCmd(p)
237+
for _, tt := range tests {
238+
t.Run(tt.name, func(t *testing.T) {
239+
if err := outputResult(p, tt.args.model, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr {
240+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
241+
}
242+
})
243+
}
244+
}

internal/cmd/rabbitmq/credentials/describe/describe.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP
112112
}
113113

114114
func outputResult(p *print.Printer, outputFormat string, credentials *rabbitmq.CredentialsResponse) error {
115+
if credentials == nil {
116+
return fmt.Errorf("no response passed")
117+
}
115118
switch outputFormat {
116119
case print.JSONOutputFormat:
117120
details, err := json.MarshalIndent(credentials, "", " ")
@@ -131,7 +134,7 @@ func outputResult(p *print.Printer, outputFormat string, credentials *rabbitmq.C
131134
return nil
132135
default:
133136
table := tables.NewTable()
134-
table.AddRow("ID", *credentials.Id)
137+
table.AddRow("ID", utils.PtrString(credentials.Id))
135138
table.AddSeparator()
136139
// The username field cannot be set by the user so we only display it if it's not returned empty
137140
if credentials.HasRaw() && credentials.Raw.Credentials != nil {

internal/cmd/rabbitmq/credentials/describe/describe_test.go

Lines changed: 32 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/rabbitmq"
1413
)
1514

@@ -243,3 +242,33 @@ func TestBuildRequest(t *testing.T) {
243242
})
244243
}
245244
}
245+
246+
func Test_outputResult(t *testing.T) {
247+
type args struct {
248+
outputFormat string
249+
credentials *rabbitmq.CredentialsResponse
250+
}
251+
tests := []struct {
252+
name string
253+
args args
254+
wantErr bool
255+
}{
256+
{
257+
name: "empty",
258+
args: args{
259+
outputFormat: "",
260+
credentials: &rabbitmq.CredentialsResponse{},
261+
},
262+
wantErr: false,
263+
},
264+
}
265+
p := print.NewPrinter()
266+
p.Cmd = NewCmd(p)
267+
for _, tt := range tests {
268+
t.Run(tt.name, func(t *testing.T) {
269+
if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
270+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
271+
}
272+
})
273+
}
274+
}

internal/cmd/rabbitmq/credentials/list/list_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/rabbitmq"
1514
)
1615

@@ -207,3 +206,40 @@ func TestBuildRequest(t *testing.T) {
207206
})
208207
}
209208
}
209+
210+
func Test_outputResult(t *testing.T) {
211+
type args struct {
212+
outputFormat string
213+
credentials []rabbitmq.CredentialsListItem
214+
}
215+
tests := []struct {
216+
name string
217+
args args
218+
wantErr bool
219+
}{
220+
{
221+
name: "empty",
222+
args: args{},
223+
wantErr: false,
224+
},
225+
{
226+
name: "non empty list with empty elements",
227+
args: args{
228+
outputFormat: "",
229+
credentials: []rabbitmq.CredentialsListItem{
230+
{},
231+
},
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.credentials); (err != nil) != tt.wantErr {
241+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
242+
}
243+
})
244+
}
245+
}

internal/cmd/rabbitmq/instance/create/create.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient rabbitMQClie
257257
}
258258

259259
func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *rabbitmq.CreateInstanceResponse) error {
260+
if model == nil {
261+
return fmt.Errorf("no model passed")
262+
}
263+
if model.GlobalFlagModel == nil {
264+
return fmt.Errorf("no globalflags passed")
265+
}
260266
switch model.OutputFormat {
261267
case print.JSONOutputFormat:
262268
details, err := json.MarshalIndent(resp, "", " ")

internal/cmd/rabbitmq/instance/create/create_test.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import (
55
"fmt"
66
"testing"
77

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

@@ -488,3 +487,32 @@ func TestBuildRequest(t *testing.T) {
488487
})
489488
}
490489
}
490+
491+
func Test_outputResult(t *testing.T) {
492+
type args struct {
493+
model inputModel
494+
projectLabel string
495+
instanceId string
496+
resp *rabbitmq.CreateInstanceResponse
497+
}
498+
tests := []struct {
499+
name string
500+
args args
501+
wantErr bool
502+
}{
503+
{
504+
name: "empty",
505+
args: args{model: inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{}}},
506+
wantErr: false,
507+
},
508+
}
509+
p := print.NewPrinter()
510+
p.Cmd = NewCmd(p)
511+
for _, tt := range tests {
512+
t.Run(tt.name, func(t *testing.T) {
513+
if err := outputResult(p, &tt.args.model, tt.args.projectLabel, tt.args.instanceId, tt.args.resp); (err != nil) != tt.wantErr {
514+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
515+
}
516+
})
517+
}
518+
}

internal/cmd/rabbitmq/instance/describe/describe.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP
100100
}
101101

102102
func outputResult(p *print.Printer, outputFormat string, instance *rabbitmq.Instance) error {
103+
if instance == nil {
104+
return fmt.Errorf("no instance passed")
105+
}
103106
switch outputFormat {
104107
case print.JSONOutputFormat:
105108
details, err := json.MarshalIndent(instance, "", " ")
@@ -123,18 +126,22 @@ func outputResult(p *print.Printer, outputFormat string, instance *rabbitmq.Inst
123126
table.AddSeparator()
124127
table.AddRow("NAME", utils.PtrString(instance.Name))
125128
table.AddSeparator()
126-
table.AddRow("LAST OPERATION TYPE", utils.PtrString(instance.LastOperation.Type))
127-
table.AddSeparator()
128-
table.AddRow("LAST OPERATION STATE", utils.PtrString(instance.LastOperation.State))
129-
table.AddSeparator()
129+
if lastOperation := instance.LastOperation; lastOperation != nil {
130+
table.AddRow("LAST OPERATION TYPE", utils.PtrString(lastOperation.Type))
131+
table.AddSeparator()
132+
table.AddRow("LAST OPERATION STATE", utils.PtrString(lastOperation.State))
133+
table.AddSeparator()
134+
}
130135
table.AddRow("PLAN ID", utils.PtrString(instance.PlanId))
131136
// Only show ACL if it's present and not empty
132-
acl := (*instance.Parameters)[aclParameterKey]
133-
aclStr, ok := acl.(string)
134-
if ok {
135-
if aclStr != "" {
136-
table.AddSeparator()
137-
table.AddRow("ACL", aclStr)
137+
if parameters := instance.Parameters; parameters != nil {
138+
acl := (*instance.Parameters)[aclParameterKey]
139+
aclStr, ok := acl.(string)
140+
if ok {
141+
if aclStr != "" {
142+
table.AddSeparator()
143+
table.AddRow("ACL", aclStr)
144+
}
138145
}
139146
}
140147
err := table.Display(p)

internal/cmd/rabbitmq/instance/describe/describe_test.go

Lines changed: 41 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/rabbitmq"
1413
)
1514

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

0 commit comments

Comments
 (0)