Skip to content

Commit

Permalink
Add a test for switch connection map handling (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Oct 11, 2024
1 parent 84cf9d8 commit 17fc65f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
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)
}
})
}
}

0 comments on commit 17fc65f

Please sign in to comment.