From 17fc65f81ed48b2b66a1615e8d2c701b9d3f2129 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Fri, 11 Oct 2024 13:10:00 +0200 Subject: [PATCH] Add a test for switch connection map handling (#584) --- cmd/metal-api/internal/metal/switch.go | 4 +- cmd/metal-api/internal/metal/switch_test.go | 72 +++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/cmd/metal-api/internal/metal/switch.go b/cmd/metal-api/internal/metal/switch.go index e3adf8a8c..f481192ca 100644 --- a/cmd/metal-api/internal/metal/switch.go +++ b/cmd/metal-api/internal/metal/switch.go @@ -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 } diff --git a/cmd/metal-api/internal/metal/switch_test.go b/cmd/metal-api/internal/metal/switch_test.go index 9ef480925..434a4ae22 100644 --- a/cmd/metal-api/internal/metal/switch_test.go +++ b/cmd/metal-api/internal/metal/switch_test.go @@ -3,6 +3,8 @@ package metal import ( "reflect" "testing" + + "github.com/google/go-cmp/cmp" ) var ( @@ -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) + } + }) + } +}