Skip to content

chore (postgresflex): Fix potential nil pointer and add test cases #626

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
11 changes: 7 additions & 4 deletions internal/cmd/postgresflex/backup/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("describe backup for PostgreSQL Flex instance: %w", err)
}

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

func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, backup postgresflex.Backup) error {
backupStartTime, err := time.Parse(time.RFC3339, *backup.StartTime)
func outputResult(p *print.Printer, outputFormat string, backup postgresflex.Backup) error {
if backup.StartTime == nil || *backup.StartTime == "" {
return fmt.Errorf("start time not defined")
}
backupStartTime, err := time.Parse(time.RFC3339, utils.PtrString(backup.StartTime))
if err != nil {
return fmt.Errorf("parse backup start time: %w", err)
}
Expand All @@ -119,7 +122,7 @@ func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, bac
if err != nil {
return fmt.Errorf("marshal backup for PostgreSQL Flex backup: %w", err)
}
cmd.Println(string(details))
p.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
Expand Down
40 changes: 38 additions & 2 deletions internal/cmd/postgresflex/backup/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package describe
import (
"context"
"testing"

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

"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/postgresflex"
)

Expand Down Expand Up @@ -235,3 +237,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
backup postgresflex.Backup
}
tests := []struct {
name string
args args
wantErr bool
}{
{"empty", args{}, true},
{"standard", args{outputFormat: "", backup: postgresflex.Backup{StartTime: utils.Ptr(time.Now().Format(time.RFC3339))}}, false},
{"complete", args{outputFormat: "", backup: postgresflex.Backup{
EndTime: utils.Ptr(time.Now().Format(time.RFC3339)),
Id: utils.Ptr("id"),
Labels: &[]string{"foo", "bar", "baz"},
Name: utils.Ptr("name"),
Options: &map[string]string{"test1": "test1", "test2": "test2"},
Size: utils.Ptr(int64(42)),
StartTime: utils.Ptr(time.Now().Format(time.RFC3339)),
}}, 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.backup); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/cmd/postgresflex/backup/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func outputResult(p *print.Printer, outputFormat string, backups []postgresflex.
for i := range backups {
backup := backups[i]

backupStartTime, err := time.Parse(time.RFC3339, *backup.StartTime)
backupStartTime, err := time.Parse(time.RFC3339, utils.PtrString(backup.StartTime))
if err != nil {
return fmt.Errorf("parse backup start time: %w", err)
}
Expand Down
53 changes: 50 additions & 3 deletions internal/cmd/postgresflex/backup/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package list
import (
"context"
"testing"

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

"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/postgresflex"
)

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

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
backups []postgresflex.Backup
}
tests := []struct {
name string
args args
wantErr bool
}{
{"empty", args{}, false},
{"standard", args{outputFormat: "", backups: []postgresflex.Backup{}}, false},
{"complete", args{outputFormat: "", backups: []postgresflex.Backup{
{
EndTime: utils.Ptr(time.Now().Format(time.RFC3339)),
Id: utils.Ptr("id"),
Labels: &[]string{"foo", "bar", "baz"},
Name: utils.Ptr("name"),
Options: &map[string]string{"test1": "test1", "test2": "test2"},
Size: utils.Ptr(int64(42)),
StartTime: utils.Ptr(time.Now().Format(time.RFC3339)),
},
{
EndTime: utils.Ptr(time.Now().Format(time.RFC3339)),
Id: utils.Ptr("id"),
Labels: &[]string{"foo", "bar", "baz"},
Name: utils.Ptr("name"),
Options: &map[string]string{"test1": "test1", "test2": "test2"},
Size: utils.Ptr(int64(42)),
StartTime: utils.Ptr(time.Now().Format(time.RFC3339)),
},
},
}, 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.backups); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
11 changes: 7 additions & 4 deletions internal/cmd/postgresflex/instance/clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
s.Stop()
}

return outputResult(p, model, instanceLabel, instanceId, resp)
return outputResult(p, model.OutputFormat, model.Async, instanceLabel, instanceId, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -205,8 +205,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient PostgreSQLFl
return req, nil
}

func outputResult(p *print.Printer, model *inputModel, instanceLabel, instanceId string, resp *postgresflex.CloneInstanceResponse) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, async bool, instanceLabel, instanceId string, resp *postgresflex.CloneInstanceResponse) error {
if resp == nil {
return fmt.Errorf("response not set")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand All @@ -225,7 +228,7 @@ func outputResult(p *print.Printer, model *inputModel, instanceLabel, instanceId
return nil
default:
operationState := "Cloned"
if model.Async {
if async {
operationState = "Triggered cloning of"
}
p.Info("%s instance from instance %q. New Instance ID: %s\n", operationState, instanceLabel, instanceId)
Expand Down
39 changes: 35 additions & 4 deletions internal/cmd/postgresflex/instance/clone/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"testing"
"time"

"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/postgresflex"
)

Expand Down Expand Up @@ -529,3 +528,35 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
OutputFormat string
instanceLabel string
instanceId string
async bool
resp *postgresflex.CloneInstanceResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{"empty", args{}, true},
{"standard", args{
instanceLabel: "foo",
instanceId: "bar",
resp: &postgresflex.CloneInstanceResponse{InstanceId: utils.Ptr("id")},
}, 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.instanceLabel, tt.args.instanceId, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
11 changes: 7 additions & 4 deletions internal/cmd/postgresflex/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
s.Stop()
}

return outputResult(p, model, projectLabel, instanceId, resp)
return outputResult(p, model.OutputFormat, model.Async, projectLabel, instanceId, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -273,8 +273,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient PostgreSQLFl
return req, nil
}

func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId string, resp *postgresflex.CreateInstanceResponse) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel, instanceId string, resp *postgresflex.CreateInstanceResponse) error {
if resp == nil {
return fmt.Errorf("no response passed")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand All @@ -293,7 +296,7 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel, instanceId
return nil
default:
operationState := "Created"
if model.Async {
if async {
operationState = "Triggered creation of"
}
p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, instanceId)
Expand Down
42 changes: 38 additions & 4 deletions internal/cmd/postgresflex/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/postgresflex"
)

Expand Down Expand Up @@ -521,3 +520,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
async bool
projectLabel string
instanceId string
resp *postgresflex.CreateInstanceResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{"empty", args{}, true},
{"standard", args{
outputFormat: "",
async: false,
projectLabel: "label",
instanceId: "4711",
resp: &postgresflex.CreateInstanceResponse{Id: utils.Ptr("id")},
},
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.projectLabel, tt.args.instanceId, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading