Skip to content

Commit 4099fd1

Browse files
committed
chore(dns): Add nil pointer checks and tests for outputResult functions
Signed-off-by: Alexander Dahmen <[email protected]>
1 parent e12c8c2 commit 4099fd1

File tree

12 files changed

+264
-26
lines changed

12 files changed

+264
-26
lines changed

internal/cmd/dns/record-set/create/create.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClie
181181
}
182182

183183
func outputResult(p *print.Printer, model *inputModel, zoneLabel string, resp *dns.RecordSetResponse) error {
184+
if resp == nil {
185+
return fmt.Errorf("record set response is empty")
186+
}
184187
switch model.OutputFormat {
185188
case print.JSONOutputFormat:
186189
details, err := json.MarshalIndent(resp, "", " ")

internal/cmd/dns/record-set/create/create_test.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import (
66
"strings"
77
"testing"
88

9-
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
10-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
11-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
12-
139
"github.com/google/go-cmp/cmp"
1410
"github.com/google/go-cmp/cmp/cmpopts"
1511
"github.com/google/uuid"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1615
"github.com/stackitcloud/stackit-sdk-go/services/dns"
1716
)
1817

@@ -369,3 +368,39 @@ func TestBuildRequest(t *testing.T) {
369368
})
370369
}
371370
}
371+
372+
func TestOutputResult(t *testing.T) {
373+
type args struct {
374+
model *inputModel
375+
zoneLabel string
376+
resp *dns.RecordSetResponse
377+
}
378+
tests := []struct {
379+
name string
380+
args args
381+
wantErr bool
382+
}{
383+
{
384+
name: "empty",
385+
args: args{},
386+
wantErr: true,
387+
},
388+
{
389+
name: "only record set as argument",
390+
args: args{
391+
model: fixtureInputModel(),
392+
resp: &dns.RecordSetResponse{Rrset: &dns.RecordSet{}},
393+
},
394+
wantErr: false,
395+
},
396+
}
397+
p := print.NewPrinter()
398+
p.Cmd = NewCmd(p)
399+
for _, tt := range tests {
400+
t.Run(tt.name, func(t *testing.T) {
401+
if err := outputResult(p, tt.args.model, tt.args.zoneLabel, tt.args.resp); (err != nil) != tt.wantErr {
402+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
403+
}
404+
})
405+
}
406+
}

internal/cmd/dns/record-set/describe/describe.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClie
114114
}
115115

116116
func outputResult(p *print.Printer, outputFormat string, recordSet *dns.RecordSet) error {
117+
if recordSet == nil {
118+
return fmt.Errorf("record set response is empty")
119+
}
117120
switch outputFormat {
118121
case print.JSONOutputFormat:
119122
details, err := json.MarshalIndent(recordSet, "", " ")

internal/cmd/dns/record-set/describe/describe_test.go

Lines changed: 36 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/dns"
1413
)
1514

@@ -245,3 +244,37 @@ func TestBuildRequest(t *testing.T) {
245244
})
246245
}
247246
}
247+
248+
func TestOutputResult(t *testing.T) {
249+
type args struct {
250+
outputFormat string
251+
recordSet *dns.RecordSet
252+
}
253+
tests := []struct {
254+
name string
255+
args args
256+
wantErr bool
257+
}{
258+
{
259+
name: "empty",
260+
args: args{},
261+
wantErr: true,
262+
},
263+
{
264+
name: "only record set as argument",
265+
args: args{
266+
recordSet: &dns.RecordSet{Records: &[]dns.Record{}},
267+
},
268+
wantErr: false,
269+
},
270+
}
271+
p := print.NewPrinter()
272+
p.Cmd = NewCmd(p)
273+
for _, tt := range tests {
274+
t.Run(tt.name, func(t *testing.T) {
275+
if err := outputResult(p, tt.args.outputFormat, tt.args.recordSet); (err != nil) != tt.wantErr {
276+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
277+
}
278+
})
279+
}
280+
}

internal/cmd/dns/record-set/list/list_test.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import (
88
"strconv"
99
"testing"
1010

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-
1511
"github.com/google/go-cmp/cmp"
1612
"github.com/google/go-cmp/cmp/cmpopts"
1713
"github.com/google/uuid"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1817
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
1918
"github.com/stackitcloud/stackit-sdk-go/services/dns"
2019
)
@@ -501,3 +500,30 @@ func TestFetchRecordSets(t *testing.T) {
501500
})
502501
}
503502
}
503+
504+
func TestOutputResult(t *testing.T) {
505+
type args struct {
506+
outputFormat string
507+
recordSets []dns.RecordSet
508+
}
509+
tests := []struct {
510+
name string
511+
args args
512+
wantErr bool
513+
}{
514+
{
515+
name: "empty",
516+
args: args{},
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.outputFormat, tt.args.recordSets); (err != nil) != tt.wantErr {
525+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
526+
}
527+
})
528+
}
529+
}

internal/cmd/dns/zone/clone/clone.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClie
160160
}
161161

162162
func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *dns.ZoneResponse) error {
163+
if resp == nil {
164+
return fmt.Errorf("dns zone response is empty")
165+
}
163166
switch model.OutputFormat {
164167
case print.JSONOutputFormat:
165168
details, err := json.MarshalIndent(resp, "", " ")

internal/cmd/dns/zone/clone/clone_test.go

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

@@ -265,3 +264,39 @@ func TestBuildRequest(t *testing.T) {
265264
})
266265
}
267266
}
267+
268+
func TestOutputResult(t *testing.T) {
269+
type args struct {
270+
model *inputModel
271+
projectLabel string
272+
resp *dns.ZoneResponse
273+
}
274+
tests := []struct {
275+
name string
276+
args args
277+
wantErr bool
278+
}{
279+
{
280+
name: "empty",
281+
args: args{},
282+
wantErr: true,
283+
},
284+
{
285+
name: "only zone response as argument",
286+
args: args{
287+
model: fixtureInputModel(),
288+
resp: &dns.ZoneResponse{Zone: &dns.Zone{}},
289+
},
290+
wantErr: false,
291+
},
292+
}
293+
p := print.NewPrinter()
294+
p.Cmd = NewCmd(p)
295+
for _, tt := range tests {
296+
t.Run(tt.name, func(t *testing.T) {
297+
if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr {
298+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
299+
}
300+
})
301+
}
302+
}

internal/cmd/dns/zone/create/create.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClie
197197
}
198198

199199
func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *dns.ZoneResponse) error {
200+
if resp == nil {
201+
return fmt.Errorf("dns zone response is empty")
202+
}
200203
switch model.OutputFormat {
201204
case print.JSONOutputFormat:
202205
details, err := json.MarshalIndent(resp, "", " ")

internal/cmd/dns/zone/create/create_test.go

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

@@ -313,3 +312,39 @@ func TestBuildRequest(t *testing.T) {
313312
})
314313
}
315314
}
315+
316+
func TestOutputResult(t *testing.T) {
317+
type args struct {
318+
model *inputModel
319+
projectLabel string
320+
resp *dns.ZoneResponse
321+
}
322+
tests := []struct {
323+
name string
324+
args args
325+
wantErr bool
326+
}{
327+
{
328+
name: "empty",
329+
args: args{},
330+
wantErr: true,
331+
},
332+
{
333+
name: "only zone response as argument",
334+
args: args{
335+
model: fixtureInputModel(),
336+
resp: &dns.ZoneResponse{Zone: &dns.Zone{}},
337+
},
338+
wantErr: false,
339+
},
340+
}
341+
p := print.NewPrinter()
342+
p.Cmd = NewCmd(p)
343+
for _, tt := range tests {
344+
t.Run(tt.name, func(t *testing.T) {
345+
if err := outputResult(p, tt.args.model, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr {
346+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
347+
}
348+
})
349+
}
350+
}

internal/cmd/dns/zone/describe/describe.go

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

101101
func outputResult(p *print.Printer, outputFormat string, zone *dns.Zone) error {
102+
if zone == nil {
103+
return fmt.Errorf("zone response is empty")
104+
}
102105
switch outputFormat {
103106
case print.JSONOutputFormat:
104107
details, err := json.MarshalIndent(zone, "", " ")

0 commit comments

Comments
 (0)