Skip to content

fix(object-storage): add nil pointer checks for cmd outputs #612

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
14 changes: 9 additions & 5 deletions internal/cmd/object-storage/bucket/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
s.Stop()
}

return outputResult(p, model, resp)
return outputResult(p, model.OutputFormat, model.Async, model.BucketName, resp)
},
}
return cmd
Expand Down Expand Up @@ -126,8 +126,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
return req
}

func outputResult(p *print.Printer, model *inputModel, resp *objectstorage.CreateBucketResponse) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, async bool, bucketName string, resp *objectstorage.CreateBucketResponse) error {
if resp == nil {
return fmt.Errorf("create bucket response is empty")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand All @@ -146,10 +150,10 @@ func outputResult(p *print.Printer, model *inputModel, resp *objectstorage.Creat
return nil
default:
operationState := "Created"
if model.Async {
if async {
operationState = "Triggered creation of"
}
p.Outputf("%s bucket %q\n", operationState, model.BucketName)
p.Outputf("%s bucket %q\n", operationState, bucketName)
return nil
}
}
36 changes: 36 additions & 0 deletions internal/cmd/object-storage/bucket/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,39 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
bucketName string
createBucketResponse *objectstorage.CreateBucketResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty create bucket response",
args: args{
createBucketResponse: &objectstorage.CreateBucketResponse{},
},
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.async, tt.args.bucketName, tt.args.createBucketResponse); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
4 changes: 4 additions & 0 deletions internal/cmd/object-storage/bucket/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
}

func outputResult(p *print.Printer, outputFormat string, bucket *objectstorage.Bucket) error {
if bucket == nil {
return fmt.Errorf("bucket is empty")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(bucket, "", " ")
Expand Down
34 changes: 34 additions & 0 deletions internal/cmd/object-storage/bucket/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
bucket *objectstorage.Bucket
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty bucket",
args: args{
bucket: &objectstorage.Bucket{},
},
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.bucket); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
4 changes: 4 additions & 0 deletions internal/cmd/object-storage/bucket/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
}

func outputResult(p *print.Printer, outputFormat string, buckets []objectstorage.Bucket) error {
if buckets == nil {
return fmt.Errorf("buckets is empty")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(buckets, "", " ")
Expand Down
34 changes: 34 additions & 0 deletions internal/cmd/object-storage/bucket/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
buckets []objectstorage.Bucket
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty create bucket response",
args: args{
buckets: []objectstorage.Bucket{},
},
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.buckets); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
10 changes: 7 additions & 3 deletions internal/cmd/object-storage/credentials-group/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create Object Storage credentials group: %w", err)
}

return outputResult(p, model, resp)
return outputResult(p, model.OutputFormat, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -112,8 +112,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
return req
}

func outputResult(p *print.Printer, model *inputModel, resp *objectstorage.CreateCredentialsGroupResponse) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, resp *objectstorage.CreateCredentialsGroupResponse) error {
if resp == nil || resp.CredentialsGroup == nil {
return fmt.Errorf("create createndials group response is empty")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,46 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
createCredentialsGroupResponse *objectstorage.CreateCredentialsGroupResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty create credentials group response",
args: args{
createCredentialsGroupResponse: &objectstorage.CreateCredentialsGroupResponse{},
},
wantErr: true,
},
{
name: "set create credentials group response",
args: args{
createCredentialsGroupResponse: &objectstorage.CreateCredentialsGroupResponse{
CredentialsGroup: &objectstorage.CredentialsGroup{},
},
},
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.createCredentialsGroupResponse); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
41 changes: 41 additions & 0 deletions internal/cmd/object-storage/credentials-group/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
credentialsGroups []objectstorage.CredentialsGroup
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "set empty credentials groups",
args: args{
credentialsGroups: []objectstorage.CredentialsGroup{},
},
wantErr: false,
},
{
name: "set empty credentials group",
args: args{
credentialsGroups: []objectstorage.CredentialsGroup{{}},
},
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.credentialsGroups); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
10 changes: 7 additions & 3 deletions internal/cmd/object-storage/credentials/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create Object Storage credentials: %w", err)
}

return outputResult(p, model, credentialsGroupLabel, resp)
return outputResult(p, model.OutputFormat, credentialsGroupLabel, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -137,8 +137,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
return req
}

func outputResult(p *print.Printer, model *inputModel, credentialsGroupLabel string, resp *objectstorage.CreateAccessKeyResponse) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat, credentialsGroupLabel string, resp *objectstorage.CreateAccessKeyResponse) error {
if resp == nil {
return fmt.Errorf("create access key response is empty")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions internal/cmd/object-storage/credentials/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
credentialsGroupLabel string
createAccessKeyResponse *objectstorage.CreateAccessKeyResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty create access key response",
args: args{
createAccessKeyResponse: &objectstorage.CreateAccessKeyResponse{},
},
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.credentialsGroupLabel, tt.args.createAccessKeyResponse); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
41 changes: 41 additions & 0 deletions internal/cmd/object-storage/credentials/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
credentials []objectstorage.AccessKey
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "set empty credentials",
args: args{
credentials: []objectstorage.AccessKey{},
},
wantErr: false,
},
{
name: "set empty access key",
args: args{
credentials: []objectstorage.AccessKey{{}},
},
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)
}
})
}
}
Loading