Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: api_ulimit_test: add test for ulimit
Signed-off-by: Alex Jia <chuanchang.jia@gmail.com>
- Loading branch information
1 parent
b2542f1
commit 6842dd1
Showing
2 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} |