Skip to content

fix: stackit beta network-interface commands - add nil pointer checks and tests for the outputResult functions #605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions internal/cmd/beta/network-interface/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
} else if projectLabel == "" {
projectLabel = model.ProjectId
}

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

return outputResult(p, model, resp)
return outputResult(p, model.OutputFormat, model.ProjectId, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -226,8 +228,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return req.CreateNicPayload(payload)
}

func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat, projectId string, nic *iaas.NIC) error {
if nic == nil {
return fmt.Errorf("nic is empty")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(nic, "", " ")
if err != nil {
Expand All @@ -245,7 +250,7 @@ func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error {

return nil
default:
p.Outputf("Created network interface for project %q.\nNIC ID: %s\n", model.ProjectId, utils.PtrString(nic.Id))
p.Outputf("Created network interface for project %q.\nNIC ID: %s\n", projectId, utils.PtrString(nic.Id))
return nil
}
}
35 changes: 35 additions & 0 deletions internal/cmd/beta/network-interface/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
projectId string
nic *iaas.NIC
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty nic",
args: args{
nic: &iaas.NIC{},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectId, tt.args.nic); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
17 changes: 10 additions & 7 deletions internal/cmd/beta/network-interface/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
}

func outputResult(p *print.Printer, outputFormat string, nic *iaas.NIC) error {
if nic == nil {
return fmt.Errorf("nic is empty")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(nic, "", " ")
Expand All @@ -136,35 +139,35 @@ func outputResult(p *print.Printer, outputFormat string, nic *iaas.NIC) error {
return nil
default:
table := tables.NewTable()
table.AddRow("ID", *nic.Id)
table.AddRow("ID", utils.PtrString(nic.Id))
table.AddSeparator()
table.AddRow("NETWORK ID", *nic.NetworkId)
table.AddRow("NETWORK ID", utils.PtrString(nic.NetworkId))
table.AddSeparator()
if nic.Name != nil {
table.AddRow("NAME", *nic.Name)
table.AddRow("NAME", utils.PtrString(nic.Name))
table.AddSeparator()
}
if nic.Ipv4 != nil {
table.AddRow("IPV4", *nic.Ipv4)
table.AddRow("IPV4", utils.PtrString(nic.Ipv4))
table.AddSeparator()
}
if nic.Ipv6 != nil {
table.AddRow("IPV6", *nic.Ipv6)
table.AddRow("IPV6", utils.PtrString(nic.Ipv6))
table.AddSeparator()
}
table.AddRow("MAC", utils.PtrString(nic.Mac))
table.AddSeparator()
table.AddRow("NIC SECURITY", utils.PtrString(nic.NicSecurity))
if nic.AllowedAddresses != nil && len(*nic.AllowedAddresses) > 0 {
allowedAddresses := []string{}
var allowedAddresses []string
for _, value := range *nic.AllowedAddresses {
allowedAddresses = append(allowedAddresses, *value.String)
}
table.AddSeparator()
table.AddRow("ALLOWED ADDRESSES", strings.Join(allowedAddresses, "\n"))
}
if nic.Labels != nil && len(*nic.Labels) > 0 {
labels := []string{}
var labels []string
for key, value := range *nic.Labels {
labels = append(labels, fmt.Sprintf("%s: %s", key, value))
}
Expand Down
34 changes: 34 additions & 0 deletions internal/cmd/beta/network-interface/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
nic *iaas.NIC
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty nic",
args: args{
nic: &iaas.NIC{},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.nic); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2 changes: 2 additions & 0 deletions internal/cmd/beta/network-interface/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
if err != nil {
p.Debug(print.ErrorLevel, "get network name: %v", err)
networkLabel = *model.NetworkId
} else if networkLabel == "" {
networkLabel = *model.NetworkId
}
p.Info("No network interfaces found for network %q\n", networkLabel)
return nil
Expand Down
27 changes: 27 additions & 0 deletions internal/cmd/beta/network-interface/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,30 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
nics []iaas.NIC
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.nics); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
11 changes: 7 additions & 4 deletions internal/cmd/beta/network-interface/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("update network interface: %w", err)
}

return outputResult(p, model, resp)
return outputResult(p, model.OutputFormat, model.ProjectId, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -218,8 +218,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return req.UpdateNicPayload(payload)
}

func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat, projectId string, nic *iaas.NIC) error {
if nic == nil {
return fmt.Errorf("nic is empty")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(nic, "", " ")
if err != nil {
Expand All @@ -237,7 +240,7 @@ func outputResult(p *print.Printer, model *inputModel, nic *iaas.NIC) error {

return nil
default:
p.Outputf("Updated network interface for project %q.\n", model.ProjectId)
p.Outputf("Updated network interface for project %q.\n", projectId)
return nil
}
}
35 changes: 35 additions & 0 deletions internal/cmd/beta/network-interface/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
projectId string
nic *iaas.NIC
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "set empty nic",
args: args{
nic: &iaas.NIC{},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectId, tt.args.nic); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}