util: Rename SortedStrSliceCompare and move to util package
This commit is contained in:
@@ -32,6 +32,7 @@ import (
|
|||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/purpleidea/mgmt/recwatch"
|
"github.com/purpleidea/mgmt/recwatch"
|
||||||
|
"github.com/purpleidea/mgmt/util"
|
||||||
|
|
||||||
multierr "github.com/hashicorp/go-multierror"
|
multierr "github.com/hashicorp/go-multierror"
|
||||||
errwrap "github.com/pkg/errors"
|
errwrap "github.com/pkg/errors"
|
||||||
@@ -331,7 +332,7 @@ func (obj *NetRes) addrCheckApply(apply bool) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if the kernel routes are intact and the addrs match, we're done
|
// if the kernel routes are intact and the addrs match, we're done
|
||||||
err = StrSortedSliceCompare(obj.Addrs, ifaceAddrs)
|
err = util.SortedStrSliceCompare(obj.Addrs, ifaceAddrs)
|
||||||
if err == nil && kernelOK {
|
if err == nil && kernelOK {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
@@ -561,7 +562,7 @@ func (obj *NetRes) Compare(r Res) bool {
|
|||||||
if (obj.Addrs == nil) != (res.Addrs == nil) {
|
if (obj.Addrs == nil) != (res.Addrs == nil) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if err := StrSortedSliceCompare(obj.Addrs, res.Addrs); err != nil {
|
if err := util.SortedStrSliceCompare(obj.Addrs, res.Addrs); err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if obj.Gateway != res.Gateway {
|
if obj.Gateway != res.Gateway {
|
||||||
|
|||||||
@@ -270,27 +270,3 @@ func GetGID(group string) (int, error) {
|
|||||||
|
|
||||||
return -1, errwrap.Wrapf(err, "group lookup error (%s)", group)
|
return -1, errwrap.Wrapf(err, "group lookup error (%s)", group)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StrSortedSliceCompare takes two lists of strings and returns whether or not
|
|
||||||
// they are equivalent. It will return nil if both sets contain the same
|
|
||||||
// elements, regardless of order, and an error if they do not.
|
|
||||||
func StrSortedSliceCompare(a, b []string) error {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return fmt.Errorf("slices have different lengths: %d vs %d", len(a), len(b))
|
|
||||||
}
|
|
||||||
|
|
||||||
// make a copy of each to sort, so we don't reorder the inputs
|
|
||||||
x := make([]string, len(a))
|
|
||||||
y := make([]string, len(b))
|
|
||||||
copy(x, a)
|
|
||||||
copy(y, b)
|
|
||||||
|
|
||||||
sort.Strings(x)
|
|
||||||
sort.Strings(y)
|
|
||||||
for i := range x {
|
|
||||||
if x[i] != y[i] {
|
|
||||||
return fmt.Errorf("values do not match: %s vs %s", x[i], y[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -378,45 +378,3 @@ func TestCurrentUserGroupById(t *testing.T) {
|
|||||||
t.Errorf("gid didn't match current user's: %s vs %s", strconv.Itoa(gid), currentGID)
|
t.Errorf("gid didn't match current user's: %s vs %s", strconv.Itoa(gid), currentGID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStrSortedSliceCompare0(t *testing.T) {
|
|
||||||
slice1 := []string{"foo", "bar", "baz"}
|
|
||||||
slice2 := []string{"bar", "foo", "baz"}
|
|
||||||
|
|
||||||
if err := StrSortedSliceCompare(slice1, slice2); err != nil {
|
|
||||||
t.Errorf("slices were not evaluated as equivalent: %v, %v", slice1, slice2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStrSortedSliceCompare1(t *testing.T) {
|
|
||||||
slice1 := []string{"foo", "bar", "baz"}
|
|
||||||
slice2 := []string{"fi", "fi", "fo"}
|
|
||||||
|
|
||||||
if err := StrSortedSliceCompare(slice1, slice2); err == nil {
|
|
||||||
t.Errorf("slices were evaluated as equivalent: %v, %v", slice1, slice2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStrSortedSliceCompare2(t *testing.T) {
|
|
||||||
slice1 := []string{"foo", "bar", "baz"}
|
|
||||||
slice2 := []string{"foo", "bar"}
|
|
||||||
|
|
||||||
if err := StrSortedSliceCompare(slice1, slice2); err == nil {
|
|
||||||
t.Errorf("slices were evaluated as equivalent: %v, %v", slice1, slice2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStrSortedSliceCompare3(t *testing.T) {
|
|
||||||
slice0 := []string{"foo", "bar", "baz"}
|
|
||||||
slice1 := []string{"zip", "zap", "zop"}
|
|
||||||
|
|
||||||
_ = StrSortedSliceCompare(slice0, slice1)
|
|
||||||
|
|
||||||
if slice0[0] != "foo" || slice0[1] != "bar" || slice0[2] != "baz" {
|
|
||||||
t.Errorf("input slice reordered to: %v", slice0)
|
|
||||||
}
|
|
||||||
|
|
||||||
if slice1[0] != "zip" || slice1[1] != "zap" || slice1[2] != "zop" {
|
|
||||||
t.Errorf("input slice reordered to: %v", slice1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
25
util/util.go
25
util/util.go
@@ -19,6 +19,7 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -376,3 +377,27 @@ func SystemBusPrivateUsable() (conn *dbus.Conn, err error) {
|
|||||||
}
|
}
|
||||||
return conn, nil // success
|
return conn, nil // success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SortedStrSliceCompare takes two lists of strings and returns whether or not
|
||||||
|
// they are equivalent. It will return nil if both sets contain the same
|
||||||
|
// elements, regardless of order, and an error if they do not.
|
||||||
|
func SortedStrSliceCompare(a, b []string) error {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return fmt.Errorf("slices have different lengths: %d vs %d", len(a), len(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
// make a copy of each to sort, so we don't reorder the inputs
|
||||||
|
x := make([]string, len(a))
|
||||||
|
y := make([]string, len(b))
|
||||||
|
copy(x, a)
|
||||||
|
copy(y, b)
|
||||||
|
|
||||||
|
sort.Strings(x)
|
||||||
|
sort.Strings(y)
|
||||||
|
for i := range x {
|
||||||
|
if x[i] != y[i] {
|
||||||
|
return fmt.Errorf("values do not match: %s vs %s", x[i], y[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -813,3 +813,45 @@ func TestUtilFlattenListWithSplit1(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSortedStrSliceCompare0(t *testing.T) {
|
||||||
|
slice0 := []string{"foo", "bar", "baz"}
|
||||||
|
slice1 := []string{"bar", "foo", "baz"}
|
||||||
|
|
||||||
|
if err := SortedStrSliceCompare(slice0, slice1); err != nil {
|
||||||
|
t.Errorf("slices were not evaluated as equivalent: %v, %v", slice0, slice1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSortedStrSliceCompare1(t *testing.T) {
|
||||||
|
slice0 := []string{"foo", "bar", "baz"}
|
||||||
|
slice1 := []string{"fi", "fi", "fo"}
|
||||||
|
|
||||||
|
if err := SortedStrSliceCompare(slice0, slice1); err == nil {
|
||||||
|
t.Errorf("slices were evaluated as equivalent: %v, %v", slice0, slice1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSortedStrSliceCompare2(t *testing.T) {
|
||||||
|
slice0 := []string{"foo", "bar", "baz"}
|
||||||
|
slice1 := []string{"foo", "bar"}
|
||||||
|
|
||||||
|
if err := SortedStrSliceCompare(slice0, slice1); err == nil {
|
||||||
|
t.Errorf("slices were evaluated as equivalent: %v, %v", slice0, slice1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSortedStrSliceCompare3(t *testing.T) {
|
||||||
|
slice0 := []string{"foo", "bar", "baz"}
|
||||||
|
slice1 := []string{"zip", "zap", "zop"}
|
||||||
|
|
||||||
|
_ = SortedStrSliceCompare(slice0, slice1)
|
||||||
|
|
||||||
|
if slice0[0] != "foo" || slice0[1] != "bar" || slice0[2] != "baz" {
|
||||||
|
t.Errorf("input slice reordered to: %v", slice0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if slice1[0] != "zip" || slice1[1] != "zap" || slice1[2] != "zop" {
|
||||||
|
t.Errorf("input slice reordered to: %v", slice1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user