@@ -181,3 +181,97 @@ func TestDeleteChassis(t *testing.T) {
181181 })
182182 }
183183}
184+
185+ func TestCreateOrUpdateChassis (t * testing.T ) {
186+ uuid1 := "b9998337-2498-4d1e-86e6-fc0417abb2f0"
187+ uuid2 := "b9998337-2498-4d1e-86e6-fc0417abb2f1"
188+ uuid3 := "b9998337-2498-4d1e-86e6-fc0417abb2f2"
189+ tests := []struct {
190+ desc string
191+ chassis * sbdb.Chassis
192+ encaps []* sbdb.Encap
193+ initialDB []libovsdbtest.TestData
194+ expectedDB []libovsdbtest.TestData
195+ }{
196+ {
197+ desc : "create new chassis with encap records" ,
198+ chassis : & sbdb.Chassis {Name : "test1" },
199+ encaps : []* sbdb.Encap {{ChassisName : "test1" , IP : "10.0.0.10" , Type : "geneve" },
200+ {ChassisName : "test1" , IP : "10.0.0.11" , Type : "geneve" }},
201+ initialDB : []libovsdbtest.TestData {},
202+ expectedDB : []libovsdbtest.TestData {
203+ & sbdb.Chassis {UUID : uuid1 , Name : "test1" , Encaps : []string {uuid2 , uuid3 }},
204+ & sbdb.Encap {UUID : uuid2 , ChassisName : "test1" , IP : "10.0.0.10" , Type : "geneve" },
205+ & sbdb.Encap {UUID : uuid3 , ChassisName : "test1" , IP : "10.0.0.11" , Type : "geneve" },
206+ },
207+ },
208+ {
209+ desc : "update chassis by inserting new encap record" ,
210+ chassis : & sbdb.Chassis {Name : "test2" },
211+ encaps : []* sbdb.Encap {{ChassisName : "test2" , IP : "10.0.0.10" , Type : "geneve" },
212+ {ChassisName : "test2" , IP : "10.0.0.11" , Type : "geneve" }},
213+ initialDB : []libovsdbtest.TestData {
214+ & sbdb.Chassis {UUID : uuid1 , Name : "test2" , Encaps : []string {uuid2 }},
215+ & sbdb.Encap {UUID : uuid2 , ChassisName : "test2" , IP : "10.0.0.10" , Type : "geneve" },
216+ },
217+ expectedDB : []libovsdbtest.TestData {
218+ & sbdb.Chassis {UUID : uuid1 , Name : "test2" , Encaps : []string {uuid2 , uuid3 }},
219+ & sbdb.Encap {UUID : uuid2 , ChassisName : "test2" , IP : "10.0.0.10" , Type : "geneve" },
220+ & sbdb.Encap {UUID : uuid3 , ChassisName : "test2" , IP : "10.0.0.11" , Type : "geneve" },
221+ },
222+ },
223+ {
224+ desc : "update chassis by removing obsolete encap record" ,
225+ chassis : & sbdb.Chassis {Name : "test3" },
226+ encaps : []* sbdb.Encap {{ChassisName : "test3" , IP : "10.0.0.11" , Type : "geneve" }},
227+ initialDB : []libovsdbtest.TestData {
228+ & sbdb.Chassis {UUID : uuid1 , Name : "test3" , Encaps : []string {uuid2 , uuid3 }},
229+ & sbdb.Encap {UUID : uuid2 , ChassisName : "test3" , IP : "10.0.0.10" , Type : "geneve" },
230+ & sbdb.Encap {UUID : uuid3 , ChassisName : "test3" , IP : "10.0.0.11" , Type : "geneve" },
231+ },
232+ expectedDB : []libovsdbtest.TestData {
233+ & sbdb.Chassis {UUID : uuid1 , Name : "test3" , Encaps : []string {uuid3 }},
234+ & sbdb.Encap {UUID : uuid3 , ChassisName : "test3" , IP : "10.0.0.11" , Type : "geneve" },
235+ },
236+ },
237+ {
238+ desc : "update chassis by adding new encap record and deleting the old one" ,
239+ chassis : & sbdb.Chassis {Name : "test4" },
240+ encaps : []* sbdb.Encap {{ChassisName : "test4" , IP : "10.0.0.11" , Type : "geneve" }},
241+ initialDB : []libovsdbtest.TestData {
242+ & sbdb.Chassis {UUID : uuid1 , Name : "test4" , Encaps : []string {uuid2 }},
243+ & sbdb.Encap {UUID : uuid2 , ChassisName : "test4" , IP : "10.0.0.10" , Type : "geneve" },
244+ },
245+ expectedDB : []libovsdbtest.TestData {
246+ & sbdb.Chassis {UUID : uuid1 , Name : "test4" , Encaps : []string {uuid3 }},
247+ & sbdb.Encap {UUID : uuid3 , ChassisName : "test4" , IP : "10.0.0.11" , Type : "geneve" },
248+ },
249+ },
250+ }
251+ for _ , tt := range tests {
252+ t .Run (tt .desc , func (t * testing.T ) {
253+ dbSetup := libovsdbtest.TestSetup {
254+ SBData : tt .initialDB ,
255+ }
256+ sbClient , cleanup , err := libovsdbtest .NewSBTestHarness (dbSetup , nil )
257+ if err != nil {
258+ t .Fatalf ("%s: failed to set up test harness: %v" , tt .desc , err )
259+ }
260+ t .Cleanup (cleanup .Cleanup )
261+
262+ err = CreateOrUpdateChassis (sbClient , tt .chassis , tt .encaps ... )
263+ if err != nil {
264+ t .Fatal (fmt .Errorf ("%s: got unexpected error: %v" , tt .desc , err ))
265+ }
266+
267+ matcher := libovsdbtest .HaveDataIgnoringUUIDs (tt .expectedDB )
268+ match , err := matcher .Match (sbClient )
269+ if err != nil {
270+ t .Fatalf ("%s: matcher error: %v" , tt .desc , err )
271+ }
272+ if ! match {
273+ t .Fatalf ("%s: DB state did not match: %s" , tt .desc , matcher .FailureMessage (sbClient ))
274+ }
275+ })
276+ }
277+ }
0 commit comments