Skip to content
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

Add a test for switch connection map handling #584

Merged
merged 1 commit into from
Oct 11, 2024
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
4 changes: 2 additions & 2 deletions cmd/metal-api/internal/metal/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (c ConnectionMap) ByNicName() (map[string]Connection, error) {
res := make(map[string]Connection)
for _, cons := range c {
for _, con := range cons {
if con2, has := res[con.Nic.Name]; has {
return nil, fmt.Errorf("connection map has duplicate connections for nic %s; con1: %v, con2: %v", con.Nic.Name, con, con2)
if _, has := res[con.Nic.Name]; has {
return nil, fmt.Errorf("switch port %s is connected to more than one machine", con.Nic.Name)
}
res[con.Nic.Name] = con
}
Expand Down
72 changes: 72 additions & 0 deletions cmd/metal-api/internal/metal/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package metal
import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
)

var (
Expand Down Expand Up @@ -277,3 +279,73 @@ func TestSwitch_ConnectMachine2(t *testing.T) {
})
}
}

func TestConnectionMap_ByNicName(t *testing.T) {
tests := []struct {
name string
c ConnectionMap
want map[string]Connection
wantErr bool
wantErrmessage string
}{
{
name: "one machine connected to one switch",
c: ConnectionMap{
"m1": Connections{
Connection{MachineID: "m1", Nic: Nic{MacAddress: "11:11", Name: "swp1"}},
},
},
want: map[string]Connection{
"swp1": {MachineID: "m1", Nic: Nic{MacAddress: "11:11", Name: "swp1"}},
},
wantErr: false,
},
{
name: "two machines connected to one switch",
c: ConnectionMap{
"m1": Connections{
Connection{MachineID: "m1", Nic: Nic{MacAddress: "11:11", Name: "swp1"}},
},
"m2": Connections{
Connection{MachineID: "m2", Nic: Nic{MacAddress: "21:11", Name: "swp2"}},
},
},
want: map[string]Connection{
"swp1": {MachineID: "m1", Nic: Nic{MacAddress: "11:11", Name: "swp1"}},
"swp2": {MachineID: "m2", Nic: Nic{MacAddress: "21:11", Name: "swp2"}},
},
wantErr: false,
},
{
name: "two machines connected to one switch at the same port",
c: ConnectionMap{
"m1": Connections{
Connection{MachineID: "m1", Nic: Nic{MacAddress: "11:11", Name: "swp1"}},
},
"m2": Connections{
Connection{MachineID: "m2", Nic: Nic{MacAddress: "21:11", Name: "swp1"}},
},
},
want: nil,
wantErr: true,
wantErrmessage: "switch port swp1 is connected to more than one machine",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.c.ByNicName()
if (err != nil) != tt.wantErr {
t.Errorf("ConnectionMap.ByNicName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && tt.wantErrmessage != err.Error() {
t.Errorf("ConnectionMap.ByNicName() error = %v, wantErr %v", err, tt.wantErr)
return
}

if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("ConnectionMap.ByNicName() diff: %s", diff)
}
})
}
}
Loading