Skip to content

Commit

Permalink
opts/UlimitOpt: sort lists by name
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Sep 10, 2020
1 parent 60abe96 commit 866e4b1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
13 changes: 8 additions & 5 deletions opts/ulimit.go
Expand Up @@ -2,6 +2,7 @@ package opts

import (
"fmt"
"sort"

"github.com/docker/go-units"
)
Expand All @@ -11,7 +12,7 @@ type UlimitOpt struct {
values *map[string]*units.Ulimit
}

// NewUlimitOpt creates a new UlimitOpt
// NewUlimitOpt creates a new UlimitOpt. Ulimits are not validated.
func NewUlimitOpt(ref *map[string]*units.Ulimit) *UlimitOpt {
if ref == nil {
ref = &map[string]*units.Ulimit{}
Expand All @@ -31,23 +32,25 @@ func (o *UlimitOpt) Set(val string) error {
return nil
}

// String returns Ulimit values as a string.
// String returns Ulimit values as a string. Values are sorted by name.
func (o *UlimitOpt) String() string {
var out []string
for _, v := range *o.values {
out = append(out, v.String())
}

sort.Strings(out)
return fmt.Sprintf("%v", out)
}

// GetList returns a slice of pointers to Ulimits.
// GetList returns a slice of pointers to Ulimits. Values are sorted by name.
func (o *UlimitOpt) GetList() []*units.Ulimit {
var ulimits []*units.Ulimit
for _, v := range *o.values {
ulimits = append(ulimits, v)
}

sort.SliceStable(ulimits, func(i, j int) bool {
return ulimits[i].Name < ulimits[j].Name
})
return ulimits
}

Expand Down
46 changes: 29 additions & 17 deletions opts/ulimit_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/docker/go-units"
"gotest.tools/v3/assert"
)

func TestUlimitOpt(t *testing.T) {
Expand All @@ -14,29 +15,40 @@ func TestUlimitOpt(t *testing.T) {
ulimitOpt := NewUlimitOpt(&ulimitMap)

expected := "[nofile=512:1024]"
if ulimitOpt.String() != expected {
t.Fatalf("Expected %v, got %v", expected, ulimitOpt)
}
assert.Equal(t, ulimitOpt.String(), expected)

// Valid ulimit append to opts
if err := ulimitOpt.Set("core=1024:1024"); err != nil {
t.Fatal(err)
}
err := ulimitOpt.Set("core=1024:1024")
assert.NilError(t, err)

err = ulimitOpt.Set("nofile")
assert.ErrorContains(t, err, "invalid ulimit argument")

// Invalid ulimit type returns an error and do not append to opts
if err := ulimitOpt.Set("notavalidtype=1024:1024"); err == nil {
t.Fatalf("Expected error on invalid ulimit type")
}
expected = "[nofile=512:1024 core=1024:1024]"
expected2 := "[core=1024:1024 nofile=512:1024]"
result := ulimitOpt.String()
if result != expected && result != expected2 {
t.Fatalf("Expected %v or %v, got %v", expected, expected2, ulimitOpt)
}
err = ulimitOpt.Set("notavalidtype=1024:1024")
assert.ErrorContains(t, err, "invalid ulimit type")

expected = "[core=1024:1024 nofile=512:1024]"
assert.Equal(t, ulimitOpt.String(), expected)

// And test GetList
ulimits := ulimitOpt.GetList()
if len(ulimits) != 2 {
t.Fatalf("Expected a ulimit list of 2, got %v", ulimits)
assert.Equal(t, len(ulimits), 2)
}

func TestUlimitOptSorting(t *testing.T) {
ulimitOpt := NewUlimitOpt(&map[string]*units.Ulimit{
"nofile": {Name: "nofile", Hard: 1024, Soft: 512},
"core": {Name: "core", Hard: 1024, Soft: 1024},
})

expected := []*units.Ulimit{
{Name: "core", Hard: 1024, Soft: 1024},
{Name: "nofile", Hard: 1024, Soft: 512},
}

ulimits := ulimitOpt.GetList()
assert.DeepEqual(t, ulimits, expected)

assert.Equal(t, ulimitOpt.String(), "[core=1024:1024 nofile=512:1024]")
}

0 comments on commit 866e4b1

Please sign in to comment.