1package units
2
3import (
4	"fmt"
5	"strconv"
6	"testing"
7)
8
9func ExampleParseUlimit() {
10	fmt.Println(ParseUlimit("nofile=512:1024"))
11	fmt.Println(ParseUlimit("nofile=1024"))
12	fmt.Println(ParseUlimit("cpu=2:4"))
13	fmt.Println(ParseUlimit("cpu=6"))
14}
15
16func TestParseUlimitValid(t *testing.T) {
17	u1 := &Ulimit{"nofile", 1024, 512}
18	if u2, _ := ParseUlimit("nofile=512:1024"); *u1 != *u2 {
19		t.Fatalf("expected %q, but got %q", u1, u2)
20	}
21}
22
23func TestParseUlimitInvalidLimitType(t *testing.T) {
24	if _, err := ParseUlimit("notarealtype=1024:1024"); err == nil {
25		t.Fatalf("expected error on invalid ulimit type")
26	}
27}
28
29func TestParseUlimitBadFormat(t *testing.T) {
30	if _, err := ParseUlimit("nofile:1024:1024"); err == nil {
31		t.Fatal("expected error on bad syntax")
32	}
33
34	if _, err := ParseUlimit("nofile"); err == nil {
35		t.Fatal("expected error on bad syntax")
36	}
37
38	if _, err := ParseUlimit("nofile="); err == nil {
39		t.Fatal("expected error on bad syntax")
40	}
41	if _, err := ParseUlimit("nofile=:"); err == nil {
42		t.Fatal("expected error on bad syntax")
43	}
44	if _, err := ParseUlimit("nofile=:1024"); err == nil {
45		t.Fatal("expected error on bad syntax")
46	}
47}
48
49func TestParseUlimitHardLessThanSoft(t *testing.T) {
50	if _, err := ParseUlimit("nofile=1024:1"); err == nil {
51		t.Fatal("expected error on hard limit less than soft limit")
52	}
53}
54
55func TestParseUlimitInvalidValueType(t *testing.T) {
56	if _, err := ParseUlimit("nofile=asdf"); err == nil {
57		t.Fatal("expected error on bad value type, but got no error")
58	} else if _, ok := err.(*strconv.NumError); !ok {
59		t.Fatalf("expected error on bad value type, but got `%s`", err)
60	}
61
62	if _, err := ParseUlimit("nofile=1024:asdf"); err == nil {
63		t.Fatal("expected error on bad value type, but got no error")
64	} else if _, ok := err.(*strconv.NumError); !ok {
65		t.Fatalf("expected error on bad value type, but got `%s`", err)
66	}
67}
68
69func TestParseUlimitTooManyValueArgs(t *testing.T) {
70	if _, err := ParseUlimit("nofile=1024:1:50"); err == nil {
71		t.Fatalf("expected error on more than two value arguments")
72	}
73}
74
75func TestUlimitStringOutput(t *testing.T) {
76	u := &Ulimit{"nofile", 1024, 512}
77	if s := u.String(); s != "nofile=512:1024" {
78		t.Fatal("expected String to return nofile=512:1024, but got", s)
79	}
80}
81
82func TestGetRlimit(t *testing.T) {
83	tt := []struct {
84		ulimit Ulimit
85		rlimit Rlimit
86	}{
87		{Ulimit{"core", 10, 12}, Rlimit{rlimitCore, 10, 12}},
88		{Ulimit{"cpu", 1, 10}, Rlimit{rlimitCPU, 1, 10}},
89		{Ulimit{"data", 5, 0}, Rlimit{rlimitData, 5, 0}},
90		{Ulimit{"fsize", 2, 2}, Rlimit{rlimitFsize, 2, 2}},
91		{Ulimit{"locks", 0, 0}, Rlimit{rlimitLocks, 0, 0}},
92		{Ulimit{"memlock", 10, 10}, Rlimit{rlimitMemlock, 10, 10}},
93		{Ulimit{"msgqueue", 9, 1}, Rlimit{rlimitMsgqueue, 9, 1}},
94		{Ulimit{"nice", 9, 9}, Rlimit{rlimitNice, 9, 9}},
95		{Ulimit{"nofile", 4, 100}, Rlimit{rlimitNofile, 4, 100}},
96		{Ulimit{"nproc", 5, 5}, Rlimit{rlimitNproc, 5, 5}},
97		{Ulimit{"rss", 0, 5}, Rlimit{rlimitRss, 0, 5}},
98		{Ulimit{"rtprio", 100, 65}, Rlimit{rlimitRtprio, 100, 65}},
99		{Ulimit{"rttime", 55, 102}, Rlimit{rlimitRttime, 55, 102}},
100		{Ulimit{"sigpending", 14, 20}, Rlimit{rlimitSigpending, 14, 20}},
101		{Ulimit{"stack", 1, 1}, Rlimit{rlimitStack, 1, 1}},
102	}
103
104	for _, te := range tt {
105		res, err := te.ulimit.GetRlimit()
106		if err != nil {
107			t.Errorf("expected not to fail: %s", err)
108		}
109		if res.Type != te.rlimit.Type {
110			t.Errorf("expected Type to be %d but got %d",
111				te.rlimit.Type, res.Type)
112		}
113		if res.Soft != te.rlimit.Soft {
114			t.Errorf("expected Soft to be %d but got %d",
115				te.rlimit.Soft, res.Soft)
116		}
117		if res.Hard != te.rlimit.Hard {
118			t.Errorf("expected Hard to be %d but got %d",
119				te.rlimit.Hard, res.Hard)
120		}
121
122	}
123}
124
125func TestGetRlimitBadUlimitName(t *testing.T) {
126	name := "bla"
127	uLimit := Ulimit{name, 0, 0}
128	if _, err := uLimit.GetRlimit(); err == nil {
129		t.Error("expected error on bad Ulimit name")
130	}
131}
132