Skip to content

Commit

Permalink
Added fake Bundle() interface to support ovs controller testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravi Sankar Penta committed Apr 13, 2018
1 parent a393fab commit eac29d5
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/util/ovs/fake_ovs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"sort"
"strings"

kerrs "k8s.io/apimachinery/pkg/util/errors"
)

// ovsFake implements a fake ovs.Interface for testing purposes
Expand Down Expand Up @@ -248,6 +250,52 @@ func (tx *ovsFakeTx) EndTransaction() error {
return err
}

func getFlowRule(bundleFlow string) (string, bool, error) {
var parts []string
isAdd := false
addPrefix := "flow add"
delPrefix := "flow delete"

if strings.Contains(bundleFlow, addPrefix) {
parts = strings.Split(bundleFlow, addPrefix)
isAdd = true
} else if strings.Contains(bundleFlow, delPrefix) {
parts = strings.Split(bundleFlow, delPrefix)
} else {
return "", isAdd, fmt.Errorf("invalid bundle flow %q", bundleFlow)
}

if len(parts) != 2 {
return "", isAdd, fmt.Errorf("invalid bundle flow %q", bundleFlow)
}
return strings.TrimLeft(parts[1], " "), isAdd, nil
}

func (fake *ovsFake) Bundle(bundleFlows []string) error {
errs := []error{}
tx := fake.NewTransaction()

for _, bflow := range bundleFlows {
flow, isAdd, err := getFlowRule(bflow)
if err != nil {
errs = append(errs, err)
continue
}

if isAdd {
tx.AddFlow(flow)
} else {
tx.DeleteFlows(flow)
}
}

if err := tx.EndTransaction(); err != nil {
errs = append(errs, err)
}

return kerrs.NewAggregate(errs)
}

func (fake *ovsFake) DumpFlows(flow string, args ...interface{}) ([]string, error) {
if err := fake.ensureExists(); err != nil {
return nil, err
Expand Down

0 comments on commit eac29d5

Please sign in to comment.