github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/opts/ulimit_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/khulnasoft-lab/go-units"
     7  	"gotest.tools/v3/assert"
     8  )
     9  
    10  func TestUlimitOpt(t *testing.T) {
    11  	ulimitMap := map[string]*units.Ulimit{
    12  		"nofile": {Name: "nofile", Hard: 1024, Soft: 512},
    13  	}
    14  
    15  	ulimitOpt := NewUlimitOpt(&ulimitMap)
    16  
    17  	expected := "[nofile=512:1024]"
    18  	assert.Equal(t, ulimitOpt.String(), expected)
    19  
    20  	// Valid ulimit append to opts
    21  	err := ulimitOpt.Set("core=1024:1024")
    22  	assert.NilError(t, err)
    23  
    24  	err = ulimitOpt.Set("nofile")
    25  	assert.ErrorContains(t, err, "invalid ulimit argument")
    26  
    27  	// Invalid ulimit type returns an error and do not append to opts
    28  	err = ulimitOpt.Set("notavalidtype=1024:1024")
    29  	assert.ErrorContains(t, err, "invalid ulimit type")
    30  
    31  	expected = "[core=1024:1024 nofile=512:1024]"
    32  	assert.Equal(t, ulimitOpt.String(), expected)
    33  
    34  	// And test GetList
    35  	ulimits := ulimitOpt.GetList()
    36  	assert.Equal(t, len(ulimits), 2)
    37  }
    38  
    39  func TestUlimitOptSorting(t *testing.T) {
    40  	ulimitOpt := NewUlimitOpt(&map[string]*units.Ulimit{
    41  		"nofile": {Name: "nofile", Hard: 1024, Soft: 512},
    42  		"core":   {Name: "core", Hard: 1024, Soft: 1024},
    43  	})
    44  
    45  	expected := []*units.Ulimit{
    46  		{Name: "core", Hard: 1024, Soft: 1024},
    47  		{Name: "nofile", Hard: 1024, Soft: 512},
    48  	}
    49  
    50  	ulimits := ulimitOpt.GetList()
    51  	assert.DeepEqual(t, ulimits, expected)
    52  
    53  	assert.Equal(t, ulimitOpt.String(), "[core=1024:1024 nofile=512:1024]")
    54  }