opts/UlimitOpt: sort lists by name

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-09-10 11:51:35 +02:00
parent 60abe967b5
commit 866e4b10a1
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 38 additions and 23 deletions

View File

@ -2,6 +2,7 @@ package opts
import ( import (
"fmt" "fmt"
"sort"
"github.com/docker/go-units" "github.com/docker/go-units"
) )
@ -11,7 +12,7 @@ type UlimitOpt struct {
values *map[string]*units.Ulimit 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 { func NewUlimitOpt(ref *map[string]*units.Ulimit) *UlimitOpt {
if ref == nil { if ref == nil {
ref = &map[string]*units.Ulimit{} ref = &map[string]*units.Ulimit{}
@ -31,23 +32,25 @@ func (o *UlimitOpt) Set(val string) error {
return nil 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 { func (o *UlimitOpt) String() string {
var out []string var out []string
for _, v := range *o.values { for _, v := range *o.values {
out = append(out, v.String()) out = append(out, v.String())
} }
sort.Strings(out)
return fmt.Sprintf("%v", 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 { func (o *UlimitOpt) GetList() []*units.Ulimit {
var ulimits []*units.Ulimit var ulimits []*units.Ulimit
for _, v := range *o.values { for _, v := range *o.values {
ulimits = append(ulimits, v) ulimits = append(ulimits, v)
} }
sort.SliceStable(ulimits, func(i, j int) bool {
return ulimits[i].Name < ulimits[j].Name
})
return ulimits return ulimits
} }

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/docker/go-units" "github.com/docker/go-units"
"gotest.tools/v3/assert"
) )
func TestUlimitOpt(t *testing.T) { func TestUlimitOpt(t *testing.T) {
@ -14,29 +15,40 @@ func TestUlimitOpt(t *testing.T) {
ulimitOpt := NewUlimitOpt(&ulimitMap) ulimitOpt := NewUlimitOpt(&ulimitMap)
expected := "[nofile=512:1024]" expected := "[nofile=512:1024]"
if ulimitOpt.String() != expected { assert.Equal(t, ulimitOpt.String(), expected)
t.Fatalf("Expected %v, got %v", expected, ulimitOpt)
}
// Valid ulimit append to opts // Valid ulimit append to opts
if err := ulimitOpt.Set("core=1024:1024"); err != nil { err := ulimitOpt.Set("core=1024:1024")
t.Fatal(err) 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 // Invalid ulimit type returns an error and do not append to opts
if err := ulimitOpt.Set("notavalidtype=1024:1024"); err == nil { err = ulimitOpt.Set("notavalidtype=1024:1024")
t.Fatalf("Expected error on invalid ulimit type") assert.ErrorContains(t, err, "invalid ulimit type")
}
expected = "[nofile=512:1024 core=1024:1024]" expected = "[core=1024:1024 nofile=512:1024]"
expected2 := "[core=1024:1024 nofile=512:1024]" assert.Equal(t, ulimitOpt.String(), expected)
result := ulimitOpt.String()
if result != expected && result != expected2 {
t.Fatalf("Expected %v or %v, got %v", expected, expected2, ulimitOpt)
}
// And test GetList // And test GetList
ulimits := ulimitOpt.GetList() ulimits := ulimitOpt.GetList()
if len(ulimits) != 2 { assert.Equal(t, len(ulimits), 2)
t.Fatalf("Expected a ulimit list of 2, got %v", ulimits) }
}
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]")
} }