Skip to content

Commit e6cf71b

Browse files
authored
chore(sqlserverflex): Add nil pointer checks and tests for the outputResult functions (#625)
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent b5c5cad commit e6cf71b

File tree

20 files changed

+519
-67
lines changed

20 files changed

+519
-67
lines changed

internal/cmd/beta/sqlserverflex/database/create/create.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7979
}
8080
s.Stop()
8181

82-
return outputResult(p, model, resp)
82+
return outputResult(p, model.OutputFormat, model.DatabaseName, resp)
8383
},
8484
}
8585
configureFlags(cmd)
@@ -132,8 +132,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *sqlserverfl
132132
return req
133133
}
134134

135-
func outputResult(p *print.Printer, model *inputModel, resp *sqlserverflex.CreateDatabaseResponse) error {
136-
switch model.OutputFormat {
135+
func outputResult(p *print.Printer, outputFormat, databaseName string, resp *sqlserverflex.CreateDatabaseResponse) error {
136+
if resp == nil {
137+
return fmt.Errorf("sqlserverflex response is empty")
138+
}
139+
switch outputFormat {
137140
case print.JSONOutputFormat:
138141
details, err := json.MarshalIndent(resp, "", " ")
139142
if err != nil {
@@ -151,7 +154,7 @@ func outputResult(p *print.Printer, model *inputModel, resp *sqlserverflex.Creat
151154

152155
return nil
153156
default:
154-
p.Outputf("Created database %q\n", model.DatabaseName)
157+
p.Outputf("Created database %q\n", databaseName)
155158
return nil
156159
}
157160
}

internal/cmd/beta/sqlserverflex/database/create/create_test.go

Lines changed: 38 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-sdk-go/services/sqlserverflex"
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-sdk-go/services/sqlserverflex"
1413
)
1514

1615
var projectIdFlag = globalflags.ProjectIdFlag
@@ -255,3 +254,38 @@ func TestBuildRequest(t *testing.T) {
255254
})
256255
}
257256
}
257+
258+
func TestOutputResult(t *testing.T) {
259+
type args struct {
260+
outputFormat string
261+
databaseName string
262+
resp *sqlserverflex.CreateDatabaseResponse
263+
}
264+
tests := []struct {
265+
name string
266+
args args
267+
wantErr bool
268+
}{
269+
{
270+
name: "empty",
271+
args: args{},
272+
wantErr: true,
273+
},
274+
{
275+
name: "only sql response as argument",
276+
args: args{
277+
resp: &sqlserverflex.CreateDatabaseResponse{},
278+
},
279+
wantErr: false,
280+
},
281+
}
282+
p := print.NewPrinter()
283+
p.Cmd = NewCmd(p)
284+
for _, tt := range tests {
285+
t.Run(tt.name, func(t *testing.T) {
286+
if err := outputResult(p, tt.args.outputFormat, tt.args.databaseName, tt.args.resp); (err != nil) != tt.wantErr {
287+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
288+
}
289+
})
290+
}
291+
}

internal/cmd/beta/sqlserverflex/database/describe/describe.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,29 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *sqlserverfl
109109
return req
110110
}
111111

112-
func outputResult(p *print.Printer, outputFormat string, database *sqlserverflex.GetDatabaseResponse) error {
112+
func outputResult(p *print.Printer, outputFormat string, resp *sqlserverflex.GetDatabaseResponse) error {
113+
if resp == nil || resp.Database == nil {
114+
return fmt.Errorf("database response is empty")
115+
}
113116
switch outputFormat {
114117
case print.JSONOutputFormat:
115-
details, err := json.MarshalIndent(database, "", " ")
118+
details, err := json.MarshalIndent(resp, "", " ")
116119
if err != nil {
117120
return fmt.Errorf("marshal SQLServer Flex database: %w", err)
118121
}
119122
p.Outputln(string(details))
120123

121124
return nil
122125
case print.YAMLOutputFormat:
123-
details, err := yaml.MarshalWithOptions(database, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
126+
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
124127
if err != nil {
125128
return fmt.Errorf("marshal SQLServer Flex database: %w", err)
126129
}
127130
p.Outputln(string(details))
128131

129132
return nil
130133
default:
131-
database := database.Database
134+
database := resp.Database
132135
table := tables.NewTable()
133136
table.AddRow("ID", utils.PtrString(database.Id))
134137
table.AddSeparator()

internal/cmd/beta/sqlserverflex/database/describe/describe_test.go

Lines changed: 44 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-sdk-go/services/sqlserverflex"
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-sdk-go/services/sqlserverflex"
1413
)
1514

1615
var projectIdFlag = globalflags.ProjectIdFlag
@@ -237,3 +236,44 @@ func TestBuildRequest(t *testing.T) {
237236
})
238237
}
239238
}
239+
240+
func TestOutputResult(t *testing.T) {
241+
type args struct {
242+
outputFormat string
243+
resp *sqlserverflex.GetDatabaseResponse
244+
}
245+
tests := []struct {
246+
name string
247+
args args
248+
wantErr bool
249+
}{
250+
{
251+
name: "empty",
252+
args: args{},
253+
wantErr: true,
254+
},
255+
{
256+
name: "empty response",
257+
args: args{
258+
resp: &sqlserverflex.GetDatabaseResponse{},
259+
},
260+
wantErr: true,
261+
},
262+
{
263+
name: "only database as argument",
264+
args: args{
265+
resp: &sqlserverflex.GetDatabaseResponse{Database: &sqlserverflex.SingleDatabase{}},
266+
},
267+
wantErr: false,
268+
},
269+
}
270+
p := print.NewPrinter()
271+
p.Cmd = NewCmd(p)
272+
for _, tt := range tests {
273+
t.Run(tt.name, func(t *testing.T) {
274+
if err := outputResult(p, tt.args.outputFormat, tt.args.resp); (err != nil) != tt.wantErr {
275+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
276+
}
277+
})
278+
}
279+
}

internal/cmd/beta/sqlserverflex/database/list/list_test.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ 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-
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex"
11-
127
"github.com/google/go-cmp/cmp"
138
"github.com/google/go-cmp/cmp/cmpopts"
149
"github.com/google/uuid"
1510
"github.com/spf13/cobra"
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"
14+
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex"
1615
)
1716

1817
var projectIdFlag = globalflags.ProjectIdFlag
@@ -210,3 +209,37 @@ func TestBuildRequest(t *testing.T) {
210209
})
211210
}
212211
}
212+
213+
func TestOutputResult(t *testing.T) {
214+
type args struct {
215+
outputFormat string
216+
databases []sqlserverflex.Database
217+
}
218+
tests := []struct {
219+
name string
220+
args args
221+
wantErr bool
222+
}{
223+
{
224+
name: "empty",
225+
args: args{},
226+
wantErr: false,
227+
},
228+
{
229+
name: "empty database in databases slice",
230+
args: args{
231+
databases: []sqlserverflex.Database{{}},
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.databases); (err != nil) != tt.wantErr {
241+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
242+
}
243+
})
244+
}
245+
}

internal/cmd/beta/sqlserverflex/instance/create/create.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient sqlServerFle
262262
}
263263

264264
func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *sqlserverflex.CreateInstanceResponse) error {
265+
if resp == nil {
266+
return fmt.Errorf("sqlserverflex response is empty")
267+
}
265268
switch model.OutputFormat {
266269
case print.JSONOutputFormat:
267270
details, err := json.MarshalIndent(resp, "", " ")

internal/cmd/beta/sqlserverflex/instance/create/create_test.go

Lines changed: 39 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/sqlserverflex"
1615
)
1716

@@ -492,3 +491,39 @@ func TestBuildRequest(t *testing.T) {
492491
})
493492
}
494493
}
494+
495+
func TestOutputResult(t *testing.T) {
496+
type args struct {
497+
model *inputModel
498+
projectLabel string
499+
resp *sqlserverflex.CreateInstanceResponse
500+
}
501+
tests := []struct {
502+
name string
503+
args args
504+
wantErr bool
505+
}{
506+
{
507+
name: "empty",
508+
args: args{},
509+
wantErr: true,
510+
},
511+
{
512+
name: "sql instance as argument",
513+
args: args{
514+
model: fixtureInputModel(),
515+
resp: &sqlserverflex.CreateInstanceResponse{},
516+
},
517+
wantErr: false,
518+
},
519+
}
520+
p := print.NewPrinter()
521+
p.Cmd = NewCmd(p)
522+
for _, tt := range tests {
523+
t.Run(tt.name, func(t *testing.T) {
524+
if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr {
525+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
526+
}
527+
})
528+
}
529+
}

internal/cmd/beta/sqlserverflex/instance/describe/describe.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *sqlserverfl
9999
}
100100

101101
func outputResult(p *print.Printer, outputFormat string, instance *sqlserverflex.Instance) error {
102+
if instance == nil {
103+
return fmt.Errorf("instance response is empty")
104+
}
102105
switch outputFormat {
103106
case print.JSONOutputFormat:
104107
details, err := json.MarshalIndent(instance, "", " ")
@@ -130,21 +133,24 @@ func outputResult(p *print.Printer, outputFormat string, instance *sqlserverflex
130133
table.AddSeparator()
131134
table.AddRow("STATUS", utils.PtrString(instance.Status))
132135
table.AddSeparator()
133-
table.AddRow("STORAGE SIZE (GB)", utils.PtrString(instance.Storage.Size))
134-
table.AddSeparator()
136+
if instance.Storage != nil {
137+
table.AddRow("STORAGE SIZE (GB)", utils.PtrString(instance.Storage.Size))
138+
table.AddSeparator()
139+
}
135140
table.AddRow("VERSION", utils.PtrString(instance.Version))
136141
table.AddSeparator()
137142
table.AddRow("BACKUP SCHEDULE (UTC)", utils.PtrString(instance.BackupSchedule))
138143
table.AddSeparator()
139144
table.AddRow("ACL", acls)
140145
table.AddSeparator()
141-
table.AddRow("FLAVOR DESCRIPTION", utils.PtrString(instance.Flavor.Description))
142-
table.AddSeparator()
143-
table.AddRow("CPU", utils.PtrString(instance.Flavor.Cpu))
144-
table.AddSeparator()
145-
table.AddRow("RAM (GB)", utils.PtrString(instance.Flavor.Memory))
146-
table.AddSeparator()
147-
146+
if instance.Flavor != nil {
147+
table.AddRow("FLAVOR DESCRIPTION", utils.PtrString(instance.Flavor.Description))
148+
table.AddSeparator()
149+
table.AddRow("CPU", utils.PtrString(instance.Flavor.Cpu))
150+
table.AddSeparator()
151+
table.AddRow("RAM (GB)", utils.PtrString(instance.Flavor.Memory))
152+
table.AddSeparator()
153+
}
148154
err := table.Display(p)
149155
if err != nil {
150156
return fmt.Errorf("render table: %w", err)

0 commit comments

Comments
 (0)