...
1
16
17 package v2
18
19 import (
20 "strings"
21 "testing"
22
23 "github.com/opencontainers/runtime-spec/specs-go"
24 "github.com/stretchr/testify/assert"
25 )
26
27 func TestParseCgroupFromReader(t *testing.T) {
28 cases := map[string]string{
29 "0::/user.slice/user-1001.slice/session-1.scope\n": "/user.slice/user-1001.slice/session-1.scope",
30 "2:cpuset:/foo\n1:name=systemd:/\n": "",
31 "2:cpuset:/foo\n1:name=systemd:/\n0::/user.slice/user-1001.slice/session-1.scope\n": "/user.slice/user-1001.slice/session-1.scope",
32 }
33 for s, expected := range cases {
34 g, err := parseCgroupFromReader(strings.NewReader(s))
35 if expected != "" {
36 if g != expected {
37 t.Errorf("expected %q, got %q", expected, g)
38 }
39 if err != nil {
40 t.Error(err)
41 }
42 } else {
43 if err == nil {
44 t.Error("error is expected")
45 }
46 }
47 }
48 }
49
50 func TestToResources(t *testing.T) {
51 var (
52 quota int64 = 8000
53 period uint64 = 10000
54 shares uint64 = 5000
55 )
56 weight := 1 + ((shares-2)*9999)/262142
57 res := specs.LinuxResources{CPU: &specs.LinuxCPU{Quota: "a, Period: &period, Shares: &shares}}
58 v2resources := ToResources(&res)
59
60 assert.Equal(t, weight, *v2resources.CPU.Weight)
61 assert.Equal(t, CPUMax("8000 10000"), v2resources.CPU.Max)
62
63 res2 := specs.LinuxResources{CPU: &specs.LinuxCPU{Period: &period}}
64 v2resources2 := ToResources(&res2)
65 assert.Equal(t, CPUMax("max 10000"), v2resources2.CPU.Max)
66 }
67
View as plain text