Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bindings: convert optional nil pointers to empty sets #231

Merged
merged 1 commit into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ovsdb/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,18 @@ func NativeToOvs(column *ColumnSchema, rawElem interface{}) (interface{}, error)
case TypeSet:
var ovsSet OvsSet
if column.TypeObj.Key.Type == TypeUUID {
var ovsSlice []interface{}
ovsSlice := []interface{}{}
if _, ok := rawElem.([]string); ok {
for _, v := range rawElem.([]string) {
uuid := UUID{GoUUID: v}
ovsSlice = append(ovsSlice, uuid)
}
} else if _, ok := rawElem.(*string); ok {
v := rawElem.(*string)
uuid := UUID{GoUUID: *v}
ovsSlice = append(ovsSlice, uuid)
if v != nil {
uuid := UUID{GoUUID: *v}
ovsSlice = append(ovsSlice, uuid)
}
} else {
return nil, fmt.Errorf("uuid slice was neither []string or *string")
}
Expand Down
17 changes: 17 additions & 0 deletions ovsdb/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,23 @@ func TestOvsToNativeAndNativeToOvs(t *testing.T) {
native: &aUUID0,
ovs: aSingleUUIDSet,
},
{
name: "null UUID set with min 0 max 1",
schema: []byte(`{
"type":{
"key": {
"refTable": "SomeOtherTAble",
"refType": "weak",
"type": "uuid"
},
"min": 0,
"max": 1
}
}`),
input: es,
native: (*string)(nil),
ovs: es,
},
{
name: "A string with min 0 max 1",
schema: []byte(`{
Expand Down
60 changes: 59 additions & 1 deletion test/ovs/ovs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ type bridgeType struct {
Ports []string `ovsdb:"ports"`
Status map[string]string `ovsdb:"status"`
BridgeFailMode *BridgeFailMode `ovsdb:"fail_mode"`
IPFIX *string `ovsdb:"ipfix"`
}

// ovsType is the simplified ORM model of the Bridge table
Expand All @@ -133,9 +134,16 @@ type ovsType struct {
Bridges []string `ovsdb:"bridges"`
}

// ipfixType is a simplified ORM model for the IPFIX table
type ipfixType struct {
UUID string `ovsdb:"_uuid"`
Targets []string `ovsdb:"targets"`
}

var defDB, _ = model.NewDBModel("Open_vSwitch", map[string]model.Model{
"Open_vSwitch": &ovsType{},
"Bridge": &bridgeType{}})
"Bridge": &bridgeType{},
"IPFIX": &ipfixType{}})

func (suite *OVSIntegrationSuite) TestConnectReconnect() {
assert.True(suite.T(), suite.client.Connected())
Expand Down Expand Up @@ -588,3 +596,53 @@ func (suite *OVSIntegrationSuite) createBridge(bridgeName string) (string, error
_, err = ovsdb.CheckOperationResults(reply, operations)
return reply[0].UUID.GoUUID, err
}

func (suite *OVSIntegrationSuite) TestCreateIPFIX() {
// Create a IPFIX row and update the bridge in the same transaction
uuid, err := suite.createBridge("br-ipfix")
require.NoError(suite.T(), err)
namedUUID := "gopher"
ipfix := ipfixType{
UUID: namedUUID,
Targets: []string{"127.0.0.1:6650"},
}
insertOp, err := suite.client.Create(&ipfix)
require.NoError(suite.T(), err)

bridge := bridgeType{
UUID: uuid,
IPFIX: &namedUUID,
}
updateOps, err := suite.client.Where(&bridge).Update(&bridge, &bridge.IPFIX)
require.NoError(suite.T(), err)
operations := append(insertOp, updateOps...)
reply, err := suite.client.Transact(context.TODO(), operations...)
require.NoError(suite.T(), err)
opErrs, err := ovsdb.CheckOperationResults(reply, operations)
if err != nil {
for _, oe := range opErrs {
suite.T().Error(oe)
}
}

// Delete the IPFIX row by removing it's strong reference
bridge.IPFIX = nil
updateOps, err = suite.client.Where(&bridge).Update(&bridge, &bridge.IPFIX)
require.NoError(suite.T(), err)
reply, err = suite.client.Transact(context.TODO(), updateOps...)
require.NoError(suite.T(), err)
opErrs, err = ovsdb.CheckOperationResults(reply, updateOps)
if err != nil {
for _, oe := range opErrs {
suite.T().Error(oe)
}
}
require.NoError(suite.T(), err)

//Assert the IPFIX table is empty
ipfixes := []ipfixType{}
err = suite.client.List(&ipfixes)
require.NoError(suite.T(), err)
require.Empty(suite.T(), ipfixes)

}