@@ -18,14 +18,16 @@ type repContext struct {
1818}
1919
2020func setUpRepresentorLayout (vfPciAddress string , rep * repContext ) error {
21- path := filepath .Join (PciSysDir , vfPciAddress , "physfn/net" , rep .Name )
22- err := utilfs .Fs .MkdirAll (path , os .FileMode (0755 ))
23- if err != nil {
24- return err
21+ if vfPciAddress != "" {
22+ path := filepath .Join (PciSysDir , vfPciAddress , "physfn/net" , rep .Name )
23+ err := utilfs .Fs .MkdirAll (path , os .FileMode (0755 ))
24+ if err != nil {
25+ return err
26+ }
2527 }
2628
27- path = filepath .Join (NetSysDir , rep .Name )
28- err = utilfs .Fs .MkdirAll (path , os .FileMode (0755 ))
29+ path : = filepath .Join (NetSysDir , rep .Name )
30+ err : = utilfs .Fs .MkdirAll (path , os .FileMode (0755 ))
2931 if err != nil {
3032 return err
3133 }
@@ -54,19 +56,38 @@ func setUpRepresentorLayout(vfPciAddress string, rep *repContext) error {
5456//nolint:unparam
5557func setupUplinkRepresentorEnv (t * testing.T , uplink * repContext , vfPciAddress string , vfReps []* repContext ) func () {
5658 var err error
57- utilfs . Fs = utilfs . NewFakeFs ( )
59+ teardown := setupRepresentorEnv ( t , vfPciAddress , vfReps )
5860 defer func () {
5961 if err != nil {
62+ teardown ()
6063 t .Errorf ("setupUplinkRepresentorEnv, got %v" , err )
6164 }
6265 }()
63-
66+ // Setup uplink
6467 err = setUpRepresentorLayout (vfPciAddress , uplink )
68+ if err != nil {
69+ return nil
70+ }
71+
72+ return teardown
73+ }
74+
75+ func setupRepresentorEnv (t * testing.T , vfPciAddress string , vfReps []* repContext ) func () {
76+ var err error
77+ teardown := setupFakeFs (t )
78+
79+ defer func () {
80+ if err != nil {
81+ teardown ()
82+ t .Errorf ("setupRepresentorEnv, got %v" , err )
83+ }
84+ }()
85+
6586 for _ , rep := range vfReps {
6687 err = setUpRepresentorLayout (vfPciAddress , rep )
6788 }
6889
69- return func () { utilfs . Fs . RemoveAll ( "/" ) } //nolint:errcheck
90+ return teardown
7091}
7192
7293func TestGetUplinkRepresentorWithPhysPortNameSuccess (t * testing.T ) {
@@ -153,3 +174,62 @@ func TestGetUplinkRepresentorErrorMissingUplink(t *testing.T) {
153174 assert .Equal (t , "" , uplinkNetdev )
154175 assert .Contains (t , err .Error (), expectedError )
155176}
177+
178+ func TestGetVfRepresentorSmartNIC (t * testing.T ) {
179+ vfReps := []* repContext {
180+ {
181+ Name : "eth0" ,
182+ PhysPortName : "pf0vf0" ,
183+ PhysSwitchID : "c2cfc60003a1420c" ,
184+ },
185+ {
186+ Name : "eth1" ,
187+ PhysPortName : "pf0vf1" ,
188+ PhysSwitchID : "c2cfc60003a1420c" ,
189+ },
190+ {
191+ Name : "eth2" ,
192+ PhysPortName : "pf0vf2" ,
193+ PhysSwitchID : "c2cfc60003a1420c" ,
194+ },
195+ }
196+ teardown := setupRepresentorEnv (t , "" , vfReps )
197+ defer teardown ()
198+
199+ vfRep , err := GetVfRepresentorSmartNIC ("0" , "2" )
200+ assert .NoError (t , err )
201+ assert .Equal (t , "eth2" , vfRep )
202+ }
203+
204+ func TestGetVfRepresentorSmartNICNoRep (t * testing.T ) {
205+ vfReps := []* repContext {
206+ {
207+ Name : "eth0" ,
208+ PhysPortName : "pf0vf0" ,
209+ PhysSwitchID : "c2cfc60003a1420c" ,
210+ },
211+ {
212+ Name : "eth1" ,
213+ PhysPortName : "pf0vf1" ,
214+ PhysSwitchID : "c2cfc60003a1420c" ,
215+ },
216+ }
217+ teardown := setupRepresentorEnv (t , "" , vfReps )
218+ defer teardown ()
219+
220+ vfRep , err := GetVfRepresentorSmartNIC ("1" , "2" )
221+ assert .Error (t , err )
222+ assert .Equal (t , "" , vfRep )
223+ }
224+
225+ func TestGetVfRepresentorSmartNICInvalidPfID (t * testing.T ) {
226+ vfRep , err := GetVfRepresentorSmartNIC ("invalid" , "2" )
227+ assert .Error (t , err )
228+ assert .Equal (t , "" , vfRep )
229+ }
230+
231+ func TestGetVfRepresentorSmartNICInvalidVfIndex (t * testing.T ) {
232+ vfRep , err := GetVfRepresentorSmartNIC ("1" , "invalid" )
233+ assert .Error (t , err )
234+ assert .Equal (t , "" , vfRep )
235+ }
0 commit comments