Skip to content

Commit 317ce8f

Browse files
authored
fix: stackit beta network-interface commands - add nil pointer checks and tests for the outputResult functions (#605)
* add nil pointer checks * add tests for the outputResult functions within the network-interface commands
1 parent b3fb7b1 commit 317ce8f

File tree

8 files changed

+159
-15
lines changed

8 files changed

+159
-15
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
8282
if err != nil {
8383
p.Debug(print.ErrorLevel, "get project name: %v", err)
8484
projectLabel = model.ProjectId
85+
} else if projectLabel == "" {
86+
projectLabel = model.ProjectId
8587
}
8688

8789
if !model.AssumeYes {
@@ -99,7 +101,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
99101
return fmt.Errorf("create network interface: %w", err)
100102
}
101103

102-
return outputResult(p, model, resp)
104+
return outputResult(p, model.OutputFormat, model.ProjectId, resp)
103105
},
104106
}
105107
configureFlags(cmd)
@@ -226,8 +228,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
226228
return req.CreateNicPayload(payload)
227229
}
228230

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

246251
return nil
247252
default:
248-
p.Outputf("Created network interface for project %q.\nNIC ID: %s\n", model.ProjectId, utils.PtrString(nic.Id))
253+
p.Outputf("Created network interface for project %q.\nNIC ID: %s\n", projectId, utils.PtrString(nic.Id))
249254
return nil
250255
}
251256
}

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
8282
if err != nil {
8383
p.Debug(print.ErrorLevel, "get network name: %v", err)
8484
networkLabel = *model.NetworkId
85+
} else if networkLabel == "" {
86+
networkLabel = *model.NetworkId
8587
}
8688
p.Info("No network interfaces found for network %q\n", networkLabel)
8789
return nil

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: 7 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,11 @@ 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+
if nic == nil {
223+
return fmt.Errorf("nic is empty")
224+
}
225+
switch outputFormat {
223226
case print.JSONOutputFormat:
224227
details, err := json.MarshalIndent(nic, "", " ")
225228
if err != nil {
@@ -237,7 +240,7 @@ func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error {
237240

238241
return nil
239242
default:
240-
p.Outputf("Updated network interface for project %q.\n", model.ProjectId)
243+
p.Outputf("Updated network interface for project %q.\n", projectId)
241244
return nil
242245
}
243246
}

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: true,
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)