Skip to content

Commit c30fd8f

Browse files
committed
- add nil pointer checks
- add tests for the outputResult functions within the network-area commands
1 parent 6213bb5 commit c30fd8f

File tree

25 files changed

+394
-25
lines changed

25 files changed

+394
-25
lines changed

internal/cmd/beta/network-area/create/create.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
8484
p.Debug(print.ErrorLevel, "get organization name: %v", err)
8585
orgLabel = *model.OrganizationId
8686
}
87+
if orgLabel == "" {
88+
orgLabel = *model.OrganizationId
89+
}
8790
} else {
8891
p.Debug(print.ErrorLevel, "configure resource manager client: %v", err)
8992
}
@@ -103,7 +106,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
103106
return fmt.Errorf("create network area: %w", err)
104107
}
105108

106-
return outputResult(p, model, orgLabel, resp)
109+
return outputResult(p, model.OutputFormat, orgLabel, resp)
107110
},
108111
}
109112
configureFlags(cmd)
@@ -178,8 +181,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
178181
return req.CreateNetworkAreaPayload(payload)
179182
}
180183

181-
func outputResult(p *print.Printer, model *inputModel, orgLabel string, networkArea *iaas.NetworkArea) error {
182-
switch model.OutputFormat {
184+
func outputResult(p *print.Printer, outputFormat, orgLabel string, networkArea *iaas.NetworkArea) error {
185+
if networkArea == nil {
186+
return fmt.Errorf("network area is nil")
187+
}
188+
switch outputFormat {
183189
case print.JSONOutputFormat:
184190
details, err := json.MarshalIndent(networkArea, "", " ")
185191
if err != nil {

internal/cmd/beta/network-area/create/create_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,38 @@ func TestBuildRequest(t *testing.T) {
246246
})
247247
}
248248
}
249+
250+
func Test_outputResult(t *testing.T) {
251+
type args struct {
252+
outputFormat string
253+
orgLabel string
254+
networkArea *iaas.NetworkArea
255+
}
256+
tests := []struct {
257+
name string
258+
args args
259+
wantErr bool
260+
}{
261+
{
262+
name: "empty",
263+
args: args{},
264+
wantErr: true,
265+
},
266+
{
267+
name: "set empty network area",
268+
args: args{
269+
networkArea: &iaas.NetworkArea{},
270+
},
271+
wantErr: false,
272+
},
273+
}
274+
p := print.NewPrinter()
275+
p.Cmd = NewCmd(p)
276+
for _, tt := range tests {
277+
t.Run(tt.name, func(t *testing.T) {
278+
if err := outputResult(p, tt.args.outputFormat, tt.args.orgLabel, tt.args.networkArea); (err != nil) != tt.wantErr {
279+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
280+
}
281+
})
282+
}
283+
}

internal/cmd/beta/network-area/delete/delete.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
6363
p.Debug(print.ErrorLevel, "get network area name: %v", err)
6464
networkAreaLabel = model.AreaId
6565
}
66+
if networkAreaLabel == "" {
67+
networkAreaLabel = model.AreaId
68+
}
6669

6770
if !model.AssumeYes {
6871
prompt := fmt.Sprintf("Are you sure you want to delete network area %q?", networkAreaLabel)

internal/cmd/beta/network-area/describe/describe.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
127127
}
128128

129129
func outputResult(p *print.Printer, outputFormat string, networkArea *iaas.NetworkArea, attachedProjects []string) error {
130+
if networkArea == nil {
131+
return fmt.Errorf("network area is nil")
132+
}
130133
switch outputFormat {
131134
case print.JSONOutputFormat:
132135
details, err := json.MarshalIndent(networkArea, "", " ")
@@ -146,16 +149,19 @@ func outputResult(p *print.Printer, outputFormat string, networkArea *iaas.Netwo
146149
return nil
147150
default:
148151
var routes []string
149-
if networkArea.Ipv4.Routes != nil {
150-
for _, route := range *networkArea.Ipv4.Routes {
151-
routes = append(routes, fmt.Sprintf("next hop: %s\nprefix: %s", *route.Nexthop, *route.Prefix))
152+
var networkRanges []string
153+
154+
if networkArea.Ipv4 != nil {
155+
if networkArea.Ipv4.Routes != nil {
156+
for _, route := range *networkArea.Ipv4.Routes {
157+
routes = append(routes, fmt.Sprintf("next hop: %s\nprefix: %s", *route.Nexthop, *route.Prefix))
158+
}
152159
}
153-
}
154160

155-
var networkRanges []string
156-
if networkArea.Ipv4.NetworkRanges != nil {
157-
for _, networkRange := range *networkArea.Ipv4.NetworkRanges {
158-
networkRanges = append(networkRanges, *networkRange.Prefix)
161+
if networkArea.Ipv4.NetworkRanges != nil {
162+
for _, networkRange := range *networkArea.Ipv4.NetworkRanges {
163+
networkRanges = append(networkRanges, *networkRange.Prefix)
164+
}
159165
}
160166
}
161167

internal/cmd/beta/network-area/describe/describe_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,38 @@ func TestBuildRequest(t *testing.T) {
227227
})
228228
}
229229
}
230+
231+
func TestOutputResult(t *testing.T) {
232+
type args struct {
233+
outputFormat string
234+
networkArea *iaas.NetworkArea
235+
attachedProjects []string
236+
}
237+
tests := []struct {
238+
name string
239+
args args
240+
wantErr bool
241+
}{
242+
{
243+
name: "empty",
244+
args: args{},
245+
wantErr: true,
246+
},
247+
{
248+
name: "set networkArea",
249+
args: args{
250+
networkArea: &iaas.NetworkArea{},
251+
},
252+
wantErr: false,
253+
},
254+
}
255+
p := print.NewPrinter()
256+
p.Cmd = NewCmd(p)
257+
for _, tt := range tests {
258+
t.Run(tt.name, func(t *testing.T) {
259+
if err := outputResult(p, tt.args.outputFormat, tt.args.networkArea, tt.args.attachedProjects); (err != nil) != tt.wantErr {
260+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
261+
}
262+
})
263+
}
264+
}

internal/cmd/beta/network-area/list/list.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
8181
p.Debug(print.ErrorLevel, "get organization name: %v", err)
8282
orgLabel = *model.OrganizationId
8383
}
84+
if orgLabel == "" {
85+
orgLabel = *model.OrganizationId
86+
}
8487
} else {
8588
p.Debug(print.ErrorLevel, "configure resource manager client: %v", err)
8689
}

internal/cmd/beta/network-area/list/list_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,30 @@ func TestBuildRequest(t *testing.T) {
186186
})
187187
}
188188
}
189+
190+
func TestOutputResult(t *testing.T) {
191+
type args struct {
192+
outputFormat string
193+
networkAreas []iaas.NetworkArea
194+
}
195+
tests := []struct {
196+
name string
197+
args args
198+
wantErr bool
199+
}{
200+
{
201+
name: "empty",
202+
args: args{},
203+
wantErr: false,
204+
},
205+
}
206+
p := print.NewPrinter()
207+
p.Cmd = NewCmd(p)
208+
for _, tt := range tests {
209+
t.Run(tt.name, func(t *testing.T) {
210+
if err := outputResult(p, tt.args.outputFormat, tt.args.networkAreas); (err != nil) != tt.wantErr {
211+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
212+
}
213+
})
214+
}
215+
}

internal/cmd/beta/network-area/network-range/create/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
8888
return err
8989
}
9090

91-
return outputResult(p, model, networkAreaLabel, networkRange)
91+
return outputResult(p, model.OutputFormat, networkAreaLabel, networkRange)
9292
},
9393
}
9494
configureFlags(cmd)
@@ -138,8 +138,8 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
138138
return req.CreateNetworkAreaRangePayload(payload)
139139
}
140140

141-
func outputResult(p *print.Printer, model *inputModel, networkAreaLabel string, networkRange iaas.NetworkRange) error {
142-
switch model.OutputFormat {
141+
func outputResult(p *print.Printer, outputFormat, networkAreaLabel string, networkRange iaas.NetworkRange) error {
142+
switch outputFormat {
143143
case print.JSONOutputFormat:
144144
details, err := json.MarshalIndent(networkRange, "", " ")
145145
if err != nil {

internal/cmd/beta/network-area/network-range/create/create_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,31 @@ func TestBuildRequest(t *testing.T) {
215215
})
216216
}
217217
}
218+
219+
func TestOutputResult(t *testing.T) {
220+
type args struct {
221+
outputFormat string
222+
networkAreaLabel string
223+
networkRange iaas.NetworkRange
224+
}
225+
tests := []struct {
226+
name string
227+
args args
228+
wantErr bool
229+
}{
230+
{
231+
name: "empty",
232+
args: args{},
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.networkAreaLabel, tt.args.networkRange); (err != nil) != tt.wantErr {
241+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
242+
}
243+
})
244+
}
245+
}

internal/cmd/beta/network-area/network-range/delete/delete.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,17 @@ func NewCmd(p *print.Printer) *cobra.Command {
6161
p.Debug(print.ErrorLevel, "get network area name: %v", err)
6262
networkAreaLabel = *model.NetworkAreaId
6363
}
64+
if networkAreaLabel == "" {
65+
networkAreaLabel = *model.NetworkAreaId
66+
}
6467
networkRangeLabel, err := iaasUtils.GetNetworkRangePrefix(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId, model.NetworkRangeId)
6568
if err != nil {
6669
p.Debug(print.ErrorLevel, "get network range prefix: %v", err)
6770
networkRangeLabel = model.NetworkRangeId
6871
}
72+
if networkRangeLabel == "" {
73+
networkRangeLabel = model.NetworkRangeId
74+
}
6975

7076
if !model.AssumeYes {
7177
prompt := fmt.Sprintf("Are you sure you want to delete network range %q on STACKIT Network Area (SNA) %q?", networkRangeLabel, networkAreaLabel)

0 commit comments

Comments
 (0)