Skip to content

Commit

Permalink
test: api_ulimit_test: add test for ulimit
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Jia <chuanchang.jia@gmail.com>
  • Loading branch information
chuanchang committed Oct 19, 2018
1 parent b2542f1 commit 6842dd1
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apis/opts/config/ulimit.go
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"sort"

"github.com/alibaba/pouch/apis/types"

Expand Down Expand Up @@ -35,6 +36,8 @@ func (u *Ulimit) String() string {
str = append(str, ul.String())
}

// make sure string list is sorted
sort.Strings(str)
return fmt.Sprintf("%v", str)
}

Expand Down
217 changes: 217 additions & 0 deletions apis/opts/config/ulimit_test.go
@@ -0,0 +1,217 @@
package config

import (
"fmt"
"reflect"
"testing"

"github.com/alibaba/pouch/apis/types"
units "github.com/docker/go-units"
"github.com/stretchr/testify/assert"
)

func TestUlimitSet(t *testing.T) {
type args struct {
val string
}
tests := []struct {
name string
args args
err error
wantErr bool
wantStr string
}{
// TODO: Add test cases.
{
name: "valid ulimit format 1",
args: args{
val: "nofile=512:1024",
},
wantErr: false,
},
{
name: "valid ulimit format 2",
args: args{
val: "nofile=1024",
},
wantErr: false,
},
{
name: "valid ulimit format 3",
args: args{
val: "cpu=2:4",
},
wantErr: false,
},
{
name: "valid ulimit format 4",
args: args{
val: "cpu=6",
},
wantErr: false,
},
{
name: "invalid ulimit value type 1",
args: args{
val: "",
},
wantErr: true,
err: fmt.Errorf("invalid ulimit argument: "),
},
{
name: "invalid ulimit value type 2",
args: args{
val: "nofile=512:1024:2048",
},
wantErr: true,
err: fmt.Errorf("too many limit value arguments - 512:1024:2048, can only have up to two, `soft[:hard]`"),
},
{
name: "the hard ulimit value is less than soft",
args: args{
val: "nofile=1024:1",
},
wantErr: true,
err: fmt.Errorf("ulimit soft limit must be less than or equal to hard limit: 1024 > 1"),
},
{
name: "bad ulimit format",
args: args{
val: "cpu:512:1024",
},
wantErr: true,
err: fmt.Errorf("invalid ulimit argument: cpu:512:1024"),
},
{
name: "invalid ulimit type",
args: args{
val: "foo=1024:1024",
},
wantErr: true,
err: fmt.Errorf("invalid ulimit type: foo"),
},
}
{
ul := &Ulimit{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ul.Set(tt.args.val)
if (err != nil) != tt.wantErr {
t.Errorf("Ulimit.Set() error = %v, wantErr %v", err, tt.wantErr)
}
if (tt.err != nil) && !reflect.DeepEqual(err, tt.err) {
t.Errorf("Ulimit.Set() = %v, want %v", err, tt.err)
}
})
}
}
}

func TestUlimitString(t *testing.T) {
type args struct {
val string
}
tests := []struct {
name string
args args
err error
wantErr bool
wantStr string
}{
// TODO: Add test cases.
{
name: "valid ulimit format",
args: args{
val: "nofile=1:1024",
},
wantErr: false,
wantStr: "[nofile=1:1024]",
},
}
{
ul := &Ulimit{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ul.Set(tt.args.val)
if (err != nil) != tt.wantErr {
t.Errorf("Ulimit.Set() error = %v, wantErr %v", err, tt.wantErr)
}
out := ul.String()
if !reflect.DeepEqual(out, tt.wantStr) {
t.Errorf("Ulimit.String() = %v, want %v", out, tt.wantStr)
}
})
}
}
}

func TestUlimitType(t *testing.T) {
type args struct {
val string
}
tests := []struct {
name string
args args
err error
wantErr bool
wantStr string
}{
{
name: "get type of Ulimit",
wantErr: false,
wantStr: "value",
},
}
{
ul := &Ulimit{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := ul.Type()
if !tt.wantErr && !reflect.DeepEqual(out, tt.wantStr) {
t.Errorf("Ulimit.String() = %v, want %v", out, tt.wantStr)
}
})
}
}
}

func TestUlimitValue(t *testing.T) {
assert := assert.New(t)
type args struct {
values map[string]*units.Ulimit
}
tests := []struct {
name string
args args
want types.Ulimit
}{
// TODO: Add test cases.
{
name: "get all values as type Ulimit",
want: types.Ulimit{
Name: "nofile",
Hard: int64(1024),
Soft: int64(512),
},
args: args{
values: map[string]*units.Ulimit{
"a": {Name: "nofile", Hard: int64(1024), Soft: int64(512)},
},
},
},
}
{
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ul := Ulimit{values: tt.args.values}
got := ul.Value()
assert.NotEmpty(t, got)
for _, v := range got {
if !reflect.DeepEqual(*v, tt.want) {
t.Errorf("ul.value() = %v, want %v", *v, tt.want)
}
}
})
}
}
}

0 comments on commit 6842dd1

Please sign in to comment.