2016-12-23 14:09:12 -05:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/go-units"
|
2020-09-10 05:51:35 -04:00
|
|
|
"gotest.tools/v3/assert"
|
2016-12-23 14:09:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUlimitOpt(t *testing.T) {
|
|
|
|
ulimitMap := map[string]*units.Ulimit{
|
2019-04-02 05:26:29 -04:00
|
|
|
"nofile": {Name: "nofile", Hard: 1024, Soft: 512},
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ulimitOpt := NewUlimitOpt(&ulimitMap)
|
|
|
|
|
|
|
|
expected := "[nofile=512:1024]"
|
2020-09-10 05:51:35 -04:00
|
|
|
assert.Equal(t, ulimitOpt.String(), expected)
|
2016-12-23 14:09:12 -05:00
|
|
|
|
|
|
|
// Valid ulimit append to opts
|
2020-09-10 05:51:35 -04:00
|
|
|
err := ulimitOpt.Set("core=1024:1024")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = ulimitOpt.Set("nofile")
|
|
|
|
assert.ErrorContains(t, err, "invalid ulimit argument")
|
2016-12-23 14:09:12 -05:00
|
|
|
|
|
|
|
// Invalid ulimit type returns an error and do not append to opts
|
2020-09-10 05:51:35 -04:00
|
|
|
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)
|
2016-12-23 14:09:12 -05:00
|
|
|
|
|
|
|
// And test GetList
|
|
|
|
ulimits := ulimitOpt.GetList()
|
2020-09-10 05:51:35 -04:00
|
|
|
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},
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
2020-09-10 05:51:35 -04:00
|
|
|
|
|
|
|
ulimits := ulimitOpt.GetList()
|
|
|
|
assert.DeepEqual(t, ulimits, expected)
|
|
|
|
|
|
|
|
assert.Equal(t, ulimitOpt.String(), "[core=1024:1024 nofile=512:1024]")
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|