Skip to content

Commit 9bd08cb

Browse files
authored
RabbitMQ: Nil checks and testcases for output functions (#645)
* chore: add nil checks and testcases * fix: integrate review findings
1 parent 0f71ec9 commit 9bd08cb

File tree

13 files changed

+374
-50
lines changed

13 files changed

+374
-50
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: 55 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,56 @@ 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: "nil response",
227+
args: args{
228+
model: inputModel{
229+
GlobalFlagModel: &globalflags.GlobalFlagModel{},
230+
},
231+
instanceLabel: "",
232+
},
233+
wantErr: true,
234+
},
235+
{
236+
name: "no flags",
237+
args: args{
238+
model: inputModel{},
239+
instanceLabel: "",
240+
resp: &rabbitmq.CredentialsResponse{},
241+
},
242+
wantErr: true,
243+
},
244+
}
245+
p := print.NewPrinter()
246+
p.Cmd = NewCmd(p)
247+
for _, tt := range tests {
248+
t.Run(tt.name, func(t *testing.T) {
249+
if err := outputResult(p, tt.args.model, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr {
250+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
251+
}
252+
})
253+
}
254+
}

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: 39 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,40 @@ 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+
name: "empty",
266+
args: args{
267+
outputFormat: "",
268+
},
269+
wantErr: true,
270+
},
271+
}
272+
p := print.NewPrinter()
273+
p.Cmd = NewCmd(p)
274+
for _, tt := range tests {
275+
t.Run(tt.name, func(t *testing.T) {
276+
if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
277+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
278+
}
279+
})
280+
}
281+
}

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,16 @@ 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+
}
266+
if resp == nil {
267+
return fmt.Errorf("no response passed")
268+
}
269+
260270
switch model.OutputFormat {
261271
case print.JSONOutputFormat:
262272
details, err := json.MarshalIndent(resp, "", " ")

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

Lines changed: 46 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,46 @@ 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{
506+
model: inputModel{
507+
GlobalFlagModel: &globalflags.GlobalFlagModel{},
508+
},
509+
resp: &rabbitmq.CreateInstanceResponse{},
510+
},
511+
wantErr: false,
512+
},
513+
{
514+
name: "empty",
515+
args: args{
516+
model: inputModel{
517+
GlobalFlagModel: &globalflags.GlobalFlagModel{},
518+
},
519+
},
520+
wantErr: true,
521+
},
522+
}
523+
p := print.NewPrinter()
524+
p.Cmd = NewCmd(p)
525+
for _, tt := range tests {
526+
t.Run(tt.name, func(t *testing.T) {
527+
if err := outputResult(p, &tt.args.model, tt.args.projectLabel, tt.args.instanceId, tt.args.resp); (err != nil) != tt.wantErr {
528+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
529+
}
530+
})
531+
}
532+
}

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)

0 commit comments

Comments
 (0)