Skip to content

RabbitMQ: Nil checks and testcases for output functions #645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions internal/cmd/rabbitmq/credentials/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create RabbitMQ credentials: %w", err)
}

return outputResult(p, model, instanceLabel, resp)
return outputResult(p, *model, instanceLabel, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -122,8 +122,20 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP
return req
}

func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *rabbitmq.CredentialsResponse) error {
func outputResult(p *print.Printer, model inputModel, instanceLabel string, resp *rabbitmq.CredentialsResponse) error {
if model.GlobalFlagModel == nil {
return fmt.Errorf("no global flags available")
}
if resp == nil {
return fmt.Errorf("no response available")
}

if !model.ShowPassword {
if resp.Raw == nil {
resp.Raw = &rabbitmq.RawCredentials{Credentials: &rabbitmq.Credentials{}}
} else if resp.Raw.Credentials == nil {
resp.Raw.Credentials = &rabbitmq.Credentials{}
}
resp.Raw.Credentials.Password = utils.Ptr("hidden")
}
switch model.OutputFormat {
Expand Down
58 changes: 55 additions & 3 deletions internal/cmd/rabbitmq/credentials/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-sdk-go/services/rabbitmq"
)

Expand Down Expand Up @@ -200,3 +199,56 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
model inputModel
instanceLabel string
resp *rabbitmq.CredentialsResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{
model: inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{},
},
instanceLabel: "",
resp: &rabbitmq.CredentialsResponse{},
},
wantErr: false,
},
{
name: "nil response",
args: args{
model: inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{},
},
instanceLabel: "",
},
wantErr: true,
},
{
name: "no flags",
args: args{
model: inputModel{},
instanceLabel: "",
resp: &rabbitmq.CredentialsResponse{},
},
wantErr: true,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.model, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
5 changes: 4 additions & 1 deletion internal/cmd/rabbitmq/credentials/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP
}

func outputResult(p *print.Printer, outputFormat string, credentials *rabbitmq.CredentialsResponse) error {
if credentials == nil {
return fmt.Errorf("no response passed")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(credentials, "", " ")
Expand All @@ -131,7 +134,7 @@ func outputResult(p *print.Printer, outputFormat string, credentials *rabbitmq.C
return nil
default:
table := tables.NewTable()
table.AddRow("ID", *credentials.Id)
table.AddRow("ID", utils.PtrString(credentials.Id))
table.AddSeparator()
// The username field cannot be set by the user so we only display it if it's not returned empty
if credentials.HasRaw() && credentials.Raw.Credentials != nil {
Expand Down
42 changes: 39 additions & 3 deletions internal/cmd/rabbitmq/credentials/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-sdk-go/services/rabbitmq"
)

Expand Down Expand Up @@ -243,3 +242,40 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
credentials *rabbitmq.CredentialsResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{
outputFormat: "",
credentials: &rabbitmq.CredentialsResponse{},
},
wantErr: false,
},
{
name: "empty",
args: args{
outputFormat: "",
},
wantErr: true,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
44 changes: 40 additions & 4 deletions internal/cmd/rabbitmq/credentials/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/rabbitmq"
)

Expand Down Expand Up @@ -207,3 +206,40 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
credentials []rabbitmq.CredentialsListItem
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "non empty list with empty elements",
args: args{
outputFormat: "",
credentials: []rabbitmq.CredentialsListItem{
{},
},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
10 changes: 10 additions & 0 deletions internal/cmd/rabbitmq/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient rabbitMQClie
}

func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *rabbitmq.CreateInstanceResponse) error {
if model == nil {
return fmt.Errorf("no model passed")
}
if model.GlobalFlagModel == nil {
return fmt.Errorf("no globalflags passed")
}
if resp == nil {
return fmt.Errorf("no response passed")
}

switch model.OutputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
Expand Down
50 changes: 46 additions & 4 deletions internal/cmd/rabbitmq/instance/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"fmt"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/rabbitmq"
)

Expand Down Expand Up @@ -488,3 +487,46 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
model inputModel
projectLabel string
instanceId string
resp *rabbitmq.CreateInstanceResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{
model: inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{},
},
resp: &rabbitmq.CreateInstanceResponse{},
},
wantErr: false,
},
{
name: "empty",
args: args{
model: inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{},
},
},
wantErr: true,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, &tt.args.model, tt.args.projectLabel, tt.args.instanceId, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
27 changes: 17 additions & 10 deletions internal/cmd/rabbitmq/instance/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *rabbitmq.AP
}

func outputResult(p *print.Printer, outputFormat string, instance *rabbitmq.Instance) error {
if instance == nil {
return fmt.Errorf("no instance passed")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(instance, "", " ")
Expand All @@ -123,18 +126,22 @@ func outputResult(p *print.Printer, outputFormat string, instance *rabbitmq.Inst
table.AddSeparator()
table.AddRow("NAME", utils.PtrString(instance.Name))
table.AddSeparator()
table.AddRow("LAST OPERATION TYPE", utils.PtrString(instance.LastOperation.Type))
table.AddSeparator()
table.AddRow("LAST OPERATION STATE", utils.PtrString(instance.LastOperation.State))
table.AddSeparator()
if lastOperation := instance.LastOperation; lastOperation != nil {
table.AddRow("LAST OPERATION TYPE", utils.PtrString(lastOperation.Type))
table.AddSeparator()
table.AddRow("LAST OPERATION STATE", utils.PtrString(lastOperation.State))
table.AddSeparator()
}
table.AddRow("PLAN ID", utils.PtrString(instance.PlanId))
// Only show ACL if it's present and not empty
acl := (*instance.Parameters)[aclParameterKey]
aclStr, ok := acl.(string)
if ok {
if aclStr != "" {
table.AddSeparator()
table.AddRow("ACL", aclStr)
if parameters := instance.Parameters; parameters != nil {
acl := (*instance.Parameters)[aclParameterKey]
aclStr, ok := acl.(string)
if ok {
if aclStr != "" {
table.AddSeparator()
table.AddRow("ACL", aclStr)
}
}
}
err := table.Display(p)
Expand Down
Loading