Skip to content

Commit 59094c5

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

File tree

8 files changed

+158
-15
lines changed

8 files changed

+158
-15
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
8383
p.Debug(print.ErrorLevel, "get project name: %v", err)
8484
projectLabel = model.ProjectId
8585
}
86+
if projectLabel == "" {
87+
projectLabel = model.ProjectId
88+
}
8689

8790
if !model.AssumeYes {
8891
prompt := fmt.Sprintf("Are you sure you want to create a network interface for project %q?", projectLabel)
@@ -99,7 +102,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
99102
return fmt.Errorf("create network interface: %w", err)
100103
}
101104

102-
return outputResult(p, model, resp)
105+
return outputResult(p, model.OutputFormat, model.ProjectId, resp)
103106
},
104107
}
105108
configureFlags(cmd)
@@ -226,8 +229,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
226229
return req.CreateNicPayload(payload)
227230
}
228231

229-
func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error {
230-
switch model.OutputFormat {
232+
func outputResult(p *print.Printer, outputFormat, projectId string, nic *iaas.NIC) error {
233+
if nic == nil {
234+
return fmt.Errorf("nic is empty")
235+
}
236+
switch outputFormat {
231237
case print.JSONOutputFormat:
232238
details, err := json.MarshalIndent(nic, "", " ")
233239
if err != nil {
@@ -245,7 +251,7 @@ func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error {
245251

246252
return nil
247253
default:
248-
p.Outputf("Created network interface for project %q.\nNIC ID: %s\n", model.ProjectId, utils.PtrString(nic.Id))
254+
p.Outputf("Created network interface for project %q.\nNIC ID: %s\n", projectId, utils.PtrString(nic.Id))
249255
return nil
250256
}
251257
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,38 @@ func TestBuildRequest(t *testing.T) {
260260
})
261261
}
262262
}
263+
264+
func Test_outputResult(t *testing.T) {
265+
type args struct {
266+
outputFormat string
267+
projectId string
268+
nic *iaas.NIC
269+
}
270+
tests := []struct {
271+
name string
272+
args args
273+
wantErr bool
274+
}{
275+
{
276+
name: "empty",
277+
args: args{},
278+
wantErr: true,
279+
},
280+
{
281+
name: "set empty nic",
282+
args: args{
283+
nic: &iaas.NIC{},
284+
},
285+
wantErr: false,
286+
},
287+
}
288+
p := print.NewPrinter()
289+
p.Cmd = NewCmd(p)
290+
for _, tt := range tests {
291+
t.Run(tt.name, func(t *testing.T) {
292+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectId, tt.args.nic); (err != nil) != tt.wantErr {
293+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
294+
}
295+
})
296+
}
297+
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
117117
}
118118

119119
func outputResult(p *print.Printer, outputFormat string, nic *iaas.NIC) error {
120+
if nic == nil {
121+
return fmt.Errorf("nic is empty")
122+
}
120123
switch outputFormat {
121124
case print.JSONOutputFormat:
122125
details, err := json.MarshalIndent(nic, "", " ")
@@ -136,35 +139,35 @@ func outputResult(p *print.Printer, outputFormat string, nic *iaas.NIC) error {
136139
return nil
137140
default:
138141
table := tables.NewTable()
139-
table.AddRow("ID", *nic.Id)
142+
table.AddRow("ID", utils.PtrString(nic.Id))
140143
table.AddSeparator()
141-
table.AddRow("NETWORK ID", *nic.NetworkId)
144+
table.AddRow("NETWORK ID", utils.PtrString(nic.NetworkId))
142145
table.AddSeparator()
143146
if nic.Name != nil {
144-
table.AddRow("NAME", *nic.Name)
147+
table.AddRow("NAME", utils.PtrString(nic.Name))
145148
table.AddSeparator()
146149
}
147150
if nic.Ipv4 != nil {
148-
table.AddRow("IPV4", *nic.Ipv4)
151+
table.AddRow("IPV4", utils.PtrString(nic.Ipv4))
149152
table.AddSeparator()
150153
}
151154
if nic.Ipv6 != nil {
152-
table.AddRow("IPV6", *nic.Ipv6)
155+
table.AddRow("IPV6", utils.PtrString(nic.Ipv6))
153156
table.AddSeparator()
154157
}
155158
table.AddRow("MAC", utils.PtrString(nic.Mac))
156159
table.AddSeparator()
157160
table.AddRow("NIC SECURITY", utils.PtrString(nic.NicSecurity))
158161
if nic.AllowedAddresses != nil && len(*nic.AllowedAddresses) > 0 {
159-
allowedAddresses := []string{}
162+
var allowedAddresses []string
160163
for _, value := range *nic.AllowedAddresses {
161164
allowedAddresses = append(allowedAddresses, *value.String)
162165
}
163166
table.AddSeparator()
164167
table.AddRow("ALLOWED ADDRESSES", strings.Join(allowedAddresses, "\n"))
165168
}
166169
if nic.Labels != nil && len(*nic.Labels) > 0 {
167-
labels := []string{}
170+
var labels []string
168171
for key, value := range *nic.Labels {
169172
labels = append(labels, fmt.Sprintf("%s: %s", key, value))
170173
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,37 @@ func TestBuildRequest(t *testing.T) {
200200
})
201201
}
202202
}
203+
204+
func Test_outputResult(t *testing.T) {
205+
type args struct {
206+
outputFormat string
207+
nic *iaas.NIC
208+
}
209+
tests := []struct {
210+
name string
211+
args args
212+
wantErr bool
213+
}{
214+
{
215+
name: "empty",
216+
args: args{},
217+
wantErr: true,
218+
},
219+
{
220+
name: "set empty nic",
221+
args: args{
222+
nic: &iaas.NIC{},
223+
},
224+
wantErr: false,
225+
},
226+
}
227+
p := print.NewPrinter()
228+
p.Cmd = NewCmd(p)
229+
for _, tt := range tests {
230+
t.Run(tt.name, func(t *testing.T) {
231+
if err := outputResult(p, tt.args.outputFormat, tt.args.nic); (err != nil) != tt.wantErr {
232+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
233+
}
234+
})
235+
}
236+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
8383
p.Debug(print.ErrorLevel, "get network name: %v", err)
8484
networkLabel = *model.NetworkId
8585
}
86+
if networkLabel == "" {
87+
networkLabel = *model.NetworkId
88+
}
8689
p.Info("No network interfaces found for network %q\n", networkLabel)
8790
return nil
8891
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,30 @@ func TestBuildRequest(t *testing.T) {
205205
})
206206
}
207207
}
208+
209+
func TestOutputResult(t *testing.T) {
210+
type args struct {
211+
outputFormat string
212+
nics []iaas.NIC
213+
}
214+
tests := []struct {
215+
name string
216+
args args
217+
wantErr bool
218+
}{
219+
{
220+
name: "empty",
221+
args: args{},
222+
wantErr: false,
223+
},
224+
}
225+
p := print.NewPrinter()
226+
p.Cmd = NewCmd(p)
227+
for _, tt := range tests {
228+
t.Run(tt.name, func(t *testing.T) {
229+
if err := outputResult(p, tt.args.outputFormat, tt.args.nics); (err != nil) != tt.wantErr {
230+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
231+
}
232+
})
233+
}
234+
}

internal/cmd/beta/network-interface/update/update.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
9595
return fmt.Errorf("update network interface: %w", err)
9696
}
9797

98-
return outputResult(p, model, resp)
98+
return outputResult(p, model.OutputFormat, model.ProjectId, resp)
9999
},
100100
}
101101
configureFlags(cmd)
@@ -218,8 +218,8 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
218218
return req.UpdateNicPayload(payload)
219219
}
220220

221-
func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error {
222-
switch model.OutputFormat {
221+
func outputResult(p *print.Printer, outputFormat, projectId string, nic *iaas.NIC) error {
222+
switch outputFormat {
223223
case print.JSONOutputFormat:
224224
details, err := json.MarshalIndent(nic, "", " ")
225225
if err != nil {
@@ -237,7 +237,7 @@ func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error {
237237

238238
return nil
239239
default:
240-
p.Outputf("Updated network interface for project %q.\n", model.ProjectId)
240+
p.Outputf("Updated network interface for project %q.\n", projectId)
241241
return nil
242242
}
243243
}

internal/cmd/beta/network-interface/update/update_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,38 @@ func TestBuildRequest(t *testing.T) {
291291
})
292292
}
293293
}
294+
295+
func Test_outputResult(t *testing.T) {
296+
type args struct {
297+
outputFormat string
298+
projectId string
299+
nic *iaas.NIC
300+
}
301+
tests := []struct {
302+
name string
303+
args args
304+
wantErr bool
305+
}{
306+
{
307+
name: "empty",
308+
args: args{},
309+
wantErr: false,
310+
},
311+
{
312+
name: "set empty nic",
313+
args: args{
314+
nic: &iaas.NIC{},
315+
},
316+
wantErr: false,
317+
},
318+
}
319+
p := print.NewPrinter()
320+
p.Cmd = NewCmd(p)
321+
for _, tt := range tests {
322+
t.Run(tt.name, func(t *testing.T) {
323+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectId, tt.args.nic); (err != nil) != tt.wantErr {
324+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
325+
}
326+
})
327+
}
328+
}

0 commit comments

Comments
 (0)