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

libmem: typo and thinko fixes. #381

Merged
merged 5 commits into from
Oct 15, 2024
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
9 changes: 6 additions & 3 deletions pkg/resmgr/lib/memory/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,12 @@ func NewNodeMask(ids ...ID) NodeMask {
return NodeMask(0).Set(ids...)
}

// ParseNodeMaskparses the given string representation of a NodeMask.
// ParseNodeMask parses the given string representation of a NodeMask.
func ParseNodeMask(str string) (NodeMask, error) {
m := NodeMask(0)
if str == "" {
return m, nil
}
for _, s := range strings.Split(str, ",") {
switch minmax := strings.SplitN(s, "-", 2); len(minmax) {
case 2:
Expand All @@ -232,15 +235,15 @@ func ParseNodeMask(str string) (NodeMask, error) {
return 0, fmt.Errorf("%w: invalid range (%d - %d) in node mask %q",
ErrInvalidNodeMask, beg, end, str)
}
for id := beg; id < end; id++ {
for id := beg; id <= end; id++ {
if id > MaxNodeID {
return 0, fmt.Errorf("%w: invalid node ID in mask %q (range %d-%d)",
ErrInvalidNodeMask, str, beg, end)
}
m |= (1 << id)
}
case 1:
id, err := strconv.ParseInt(minmax[1], 10, 32)
id, err := strconv.ParseInt(minmax[0], 10, 32)
if err != nil {
return 0, fmt.Errorf("%w: failed to parse node mask %q: %w",
ErrInvalidNodeMask, str, err)
Expand Down
56 changes: 56 additions & 0 deletions pkg/resmgr/lib/memory/nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,62 @@ import (
. "github.com/containers/nri-plugins/pkg/resmgr/lib/memory"
)

func TestParseNodeMask(t *testing.T) {
type testCase struct {
name string
mask string
result NodeMask
fail bool
}
for _, tc := range []*testCase{
{
name: "empty mask",
mask: "",
result: NodeMask(0),
},
{
name: "single node mask",
mask: "0",
result: NewNodeMask(0),
},
{
name: "multiple nodes mask, no ranges",
mask: "0,2,4,6,8,11,17",
result: NewNodeMask(0, 2, 4, 6, 8, 11, 17),
},
{
name: "multiple nodes mask, with ranges",
mask: "0-5,11-17",
result: NewNodeMask(0, 1, 2, 3, 4, 5, 11, 12, 13, 14, 15, 16, 17),
},
{
name: "invalid mask, missing ID",
mask: "0,2,4,6,8,11,17,",
fail: true,
},
{
name: "invalid mask, invalid ID",
mask: "0,2,xyzzy,11",
fail: true,
},
{
name: "invalid mask, too large ID",
mask: "0,2,4,6,8,11,17,100",
fail: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
m, err := ParseNodeMask(tc.mask)
if tc.fail {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tc.result, m)
}
})
}
}

func TestMemsetString(t *testing.T) {
type testCase struct {
name string
Expand Down