-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathulimit_test.go
179 lines (160 loc) · 5.04 KB
/
ulimit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package units
import (
"fmt"
"math"
"strconv"
"testing"
)
func ExampleParseUlimit() {
fmt.Println(ParseUlimit("nofile=512:1024"))
fmt.Println(ParseUlimit("nofile=1024"))
fmt.Println(ParseUlimit("cpu=2:4"))
fmt.Println(ParseUlimit("cpu=6"))
}
func TestParseUlimitValid(t *testing.T) {
u1 := &Ulimit{"nofile", 1024, 512}
if u2, _ := ParseUlimit("nofile=512:1024"); *u1 != *u2 {
t.Fatalf("expected %q, but got %q", u1, u2)
}
}
func TestParseUlimitInvalidLimitType(t *testing.T) {
if _, err := ParseUlimit("notarealtype=1024:1024"); err == nil {
t.Fatalf("expected error on invalid ulimit type")
}
}
func TestParseUlimitBadFormat(t *testing.T) {
if _, err := ParseUlimit("nofile:1024:1024"); err == nil {
t.Fatal("expected error on bad syntax")
}
if _, err := ParseUlimit("nofile"); err == nil {
t.Fatal("expected error on bad syntax")
}
if _, err := ParseUlimit("nofile="); err == nil {
t.Fatal("expected error on bad syntax")
}
if _, err := ParseUlimit("nofile=:"); err == nil {
t.Fatal("expected error on bad syntax")
}
if _, err := ParseUlimit("nofile=:1024"); err == nil {
t.Fatal("expected error on bad syntax")
}
}
func TestParseUlimitHardLessThanSoft(t *testing.T) {
if _, err := ParseUlimit("nofile=1024:1"); err == nil {
t.Fatal("expected error on hard limit less than soft limit")
}
if _, err := ParseUlimit("nofile=-1:1024"); err == nil {
t.Fatal("expected error on hard limit less than soft limit")
}
}
func TestParseUlimitUnlimited(t *testing.T) {
tt := []struct {
in string
expected Ulimit
}{
{
in: "nofile=-1",
expected: Ulimit{Name: "nofile", Soft: -1, Hard: -1},
},
{
in: "nofile=1024",
expected: Ulimit{Name: "nofile", Soft: 1024, Hard: 1024},
},
{
in: "nofile=1024:-1",
expected: Ulimit{Name: "nofile", Soft: 1024, Hard: -1},
},
{
in: "nofile=-1:-1",
expected: Ulimit{Name: "nofile", Soft: -1, Hard: -1},
},
}
for _, tc := range tt {
t.Run(tc.in, func(t *testing.T) {
u, err := ParseUlimit(tc.in)
if err != nil {
t.Fatalf("unexpected error when setting unlimited hard limit: %v", err)
}
if u.Name != tc.expected.Name {
t.Fatalf("unexpected name. expected %s, got %s", tc.expected.Name, u.Name)
}
if u.Soft != tc.expected.Soft {
t.Fatalf("unexpected soft limit. expected %d, got %d", tc.expected.Soft, u.Soft)
}
if u.Hard != tc.expected.Hard {
t.Fatalf("unexpected hard limit. expected %d, got %d", tc.expected.Hard, u.Hard)
}
})
}
}
func TestParseUlimitInvalidValueType(t *testing.T) {
if _, err := ParseUlimit("nofile=asdf"); err == nil {
t.Fatal("expected error on bad value type, but got no error")
} else if _, ok := err.(*strconv.NumError); !ok {
t.Fatalf("expected error on bad value type, but got `%s`", err)
}
if _, err := ParseUlimit("nofile=1024:asdf"); err == nil {
t.Fatal("expected error on bad value type, but got no error")
} else if _, ok := err.(*strconv.NumError); !ok {
t.Fatalf("expected error on bad value type, but got `%s`", err)
}
}
func TestParseUlimitTooManyValueArgs(t *testing.T) {
if _, err := ParseUlimit("nofile=1024:1:50"); err == nil {
t.Fatalf("expected error on more than two value arguments")
}
}
func TestUlimitStringOutput(t *testing.T) {
u := &Ulimit{"nofile", 1024, 512}
if s := u.String(); s != "nofile=512:1024" {
t.Fatal("expected String to return nofile=512:1024, but got", s)
}
}
func TestGetRlimit(t *testing.T) {
tt := []struct {
ulimit Ulimit
rlimit Rlimit
}{
{Ulimit{"core", 10, 12}, Rlimit{rlimitCore, 10, 12}},
{Ulimit{"cpu", 1, 10}, Rlimit{rlimitCPU, 1, 10}},
{Ulimit{"data", 5, 0}, Rlimit{rlimitData, 5, 0}},
{Ulimit{"fsize", 2, 2}, Rlimit{rlimitFsize, 2, 2}},
{Ulimit{"locks", 0, 0}, Rlimit{rlimitLocks, 0, 0}},
{Ulimit{"memlock", 10, 10}, Rlimit{rlimitMemlock, 10, 10}},
{Ulimit{"msgqueue", 9, 1}, Rlimit{rlimitMsgqueue, 9, 1}},
{Ulimit{"nice", 9, 9}, Rlimit{rlimitNice, 9, 9}},
{Ulimit{"nofile", 4, 100}, Rlimit{rlimitNofile, 4, 100}},
{Ulimit{"nproc", 5, 5}, Rlimit{rlimitNproc, 5, 5}},
{Ulimit{"rss", 0, 5}, Rlimit{rlimitRss, 0, 5}},
{Ulimit{"rtprio", 100, 65}, Rlimit{rlimitRtprio, 100, 65}},
{Ulimit{"rttime", 55, 102}, Rlimit{rlimitRttime, 55, 102}},
{Ulimit{"sigpending", 14, 20}, Rlimit{rlimitSigpending, 14, 20}},
{Ulimit{"stack", 1, 1}, Rlimit{rlimitStack, 1, 1}},
{Ulimit{"stack", -1, -1}, Rlimit{rlimitStack, math.MaxUint64, math.MaxUint64}},
}
for _, te := range tt {
res, err := te.ulimit.GetRlimit()
if err != nil {
t.Errorf("expected not to fail: %s", err)
}
if res.Type != te.rlimit.Type {
t.Errorf("expected Type to be %d but got %d",
te.rlimit.Type, res.Type)
}
if res.Soft != te.rlimit.Soft {
t.Errorf("expected Soft to be %d but got %d",
te.rlimit.Soft, res.Soft)
}
if res.Hard != te.rlimit.Hard {
t.Errorf("expected Hard to be %d but got %d",
te.rlimit.Hard, res.Hard)
}
}
}
func TestGetRlimitBadUlimitName(t *testing.T) {
name := "bla"
uLimit := Ulimit{name, 0, 0}
if _, err := uLimit.GetRlimit(); err == nil {
t.Error("expected error on bad Ulimit name")
}
}