@@ -40,6 +40,182 @@ func TestParse(t *testing.T) {
4040 })
4141}
4242
43+ // TestGetBDFbyVFID tests the GetBDFbyVFID function
44+ func TestGetBDFbyVFID (t * testing.T ) {
45+ t .Run ("valid config with matching vfID" , func (t * testing.T ) {
46+ // Create a temporary directory and config file
47+ tempDir := t .TempDir ()
48+ configPath := filepath .Join (tempDir , "vf-config.json" )
49+
50+ // Write test config data
51+ configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]}`
52+ err := os .WriteFile (configPath , []byte (configData ), 0644 )
53+ require .NoError (t , err )
54+
55+ // Test the function
56+ bdf , err := GetBDFbyVFID (configPath , 9 )
57+ assert .NoError (t , err )
58+ assert .Equal (t , "0000:1:1.6" , bdf )
59+ })
60+
61+ t .Run ("valid config with non-matching vfID" , func (t * testing.T ) {
62+ // Create a temporary directory and config file
63+ tempDir := t .TempDir ()
64+ configPath := filepath .Join (tempDir , "vf-config.json" )
65+
66+ // Write test config data
67+ configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]}`
68+ err := os .WriteFile (configPath , []byte (configData ), 0644 )
69+ require .NoError (t , err )
70+
71+ // Test the function with non-matching vfID
72+ _ , err = GetBDFbyVFID (configPath , 10 )
73+ assert .Error (t , err )
74+ assert .Contains (t , err .Error (), "not found specified vfID 10" )
75+ })
76+
77+ t .Run ("invalid config file" , func (t * testing.T ) {
78+ // Create a temporary directory and config file
79+ tempDir := t .TempDir ()
80+ configPath := filepath .Join (tempDir , "vf-config.json" )
81+
82+ // Write invalid config data
83+ configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}`
84+ err := os .WriteFile (configPath , []byte (configData ), 0644 )
85+ require .NoError (t , err )
86+
87+ // Test the function
88+ _ , err = GetBDFbyVFID (configPath , 9 )
89+ assert .Error (t , err )
90+ })
91+
92+ t .Run ("non-existent config file" , func (t * testing.T ) {
93+ // Test the function with non-existent config file
94+ _ , err := GetBDFbyVFID ("/non/existent/path" , 9 )
95+ assert .Error (t , err )
96+ })
97+
98+ t .Run ("empty path with defaultNUSAConfigPath existing" , func (t * testing.T ) {
99+ // Create a temporary directory and config file
100+ tempDir := t .TempDir ()
101+
102+ // Save original value
103+ originalPath := defaultNUSAConfigPath
104+ // Temporarily change the default path to our temp directory
105+ defaultNUSAConfigPath = filepath .Join (tempDir , "eni_topo" )
106+
107+ // Make sure the default path doesn't exist
108+ _ , err := os .Stat (defaultNUSAConfigPath )
109+ if ! os .IsNotExist (err ) {
110+ os .Remove (defaultNUSAConfigPath )
111+ }
112+
113+ // Write test config data to the default path
114+ configData := `[{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]`
115+ err = os .WriteFile (defaultNUSAConfigPath , []byte (configData ), 0644 )
116+ require .NoError (t , err )
117+
118+ // Test the function with empty path
119+ bdf , err := GetBDFbyVFID ("" , 9 )
120+ assert .NoError (t , err )
121+ assert .Equal (t , "0000:1:1.6" , bdf )
122+
123+ // Restore original value
124+ defaultNUSAConfigPath = originalPath
125+ })
126+
127+ t .Run ("empty path with HcENIHostConfigPath existing" , func (t * testing.T ) {
128+ // Create a temporary directory and config file
129+ tempDir := t .TempDir ()
130+
131+ // Save original values
132+ originalNUSAPath := defaultNUSAConfigPath
133+ originalHCPath := HcENIHostConfigPath
134+
135+ // Temporarily change the paths to our temp directory
136+ defaultNUSAConfigPath = filepath .Join (tempDir , "eni_topo" )
137+ HcENIHostConfigPath = filepath .Join (tempDir , "vf-topo-vpc" )
138+
139+ // Make sure the defaultNUSAConfigPath doesn't exist
140+ _ , err := os .Stat (defaultNUSAConfigPath )
141+ if ! os .IsNotExist (err ) {
142+ os .Remove (defaultNUSAConfigPath )
143+ }
144+
145+ // Write test config data to the HcENIHostConfigPath
146+ configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]}`
147+ err = os .WriteFile (HcENIHostConfigPath , []byte (configData ), 0644 )
148+ require .NoError (t , err )
149+
150+ // Test the function with empty path
151+ bdf , err := GetBDFbyVFID ("" , 9 )
152+ assert .NoError (t , err )
153+ assert .Equal (t , "0000:1:1.6" , bdf )
154+
155+ // Restore original values
156+ defaultNUSAConfigPath = originalNUSAPath
157+ HcENIHostConfigPath = originalHCPath
158+ })
159+
160+ t .Run ("empty path with neither config file existing" , func (t * testing.T ) {
161+ // Save original values
162+ originalNUSAPath := defaultNUSAConfigPath
163+ originalHCPath := HcENIHostConfigPath
164+
165+ // Temporarily change the paths to non-existent files
166+ tempDir := t .TempDir ()
167+ defaultNUSAConfigPath = filepath .Join (tempDir , "non-existent-eni_topo" )
168+ HcENIHostConfigPath = filepath .Join (tempDir , "non-existent-vf-topo-vpc" )
169+
170+ // Test the function with empty path
171+ _ , err := GetBDFbyVFID ("" , 9 )
172+ assert .Error (t , err )
173+
174+ // Restore original values
175+ defaultNUSAConfigPath = originalNUSAPath
176+ HcENIHostConfigPath = originalHCPath
177+ })
178+
179+ t .Run ("multiple VFs with matching vfID" , func (t * testing.T ) {
180+ // Create a temporary directory and config file
181+ tempDir := t .TempDir ()
182+ configPath := filepath .Join (tempDir , "vf-config.json" )
183+
184+ // Write test config data with multiple VFs
185+ configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 8,"bdf": "0000:1:1.5"}, {"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}, {"pf_id": 1,"vf_id": 10,"bdf": "0000:1:1.7"}]}`
186+ err := os .WriteFile (configPath , []byte (configData ), 0644 )
187+ require .NoError (t , err )
188+
189+ // Test the function
190+ bdf , err := GetBDFbyVFID (configPath , 9 )
191+ assert .NoError (t , err )
192+ assert .Equal (t , "0000:1:1.6" , bdf )
193+ })
194+
195+ t .Run ("eni controller config format" , func (t * testing.T ) {
196+ // Create a temporary directory and config file
197+ tempDir := t .TempDir ()
198+ configPath := filepath .Join (tempDir , "eni-controller-config.json" )
199+
200+ // Write test config data in eni controller format
201+ configData := `[{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]`
202+ err := os .WriteFile (configPath , []byte (configData ), 0644 )
203+ require .NoError (t , err )
204+
205+ // Temporarily change the default path to test eni controller format
206+ originalPath := defaultNUSAConfigPath
207+ defaultNUSAConfigPath = configPath
208+
209+ // Test the function
210+ bdf , err := GetBDFbyVFID (configPath , 9 )
211+ assert .NoError (t , err )
212+ assert .Equal (t , "0000:1:1.6" , bdf )
213+
214+ // Restore original value
215+ defaultNUSAConfigPath = originalPath
216+ })
217+ }
218+
43219// TestGetPFBDF tests the getPFBDF function with mocked sysfs structure
44220func TestGetPFBDF (t * testing.T ) {
45221 t .Run ("invalid BDF format" , func (t * testing.T ) {
0 commit comments