@@ -2,39 +2,103 @@ package main
22
33import (
44 "fmt"
5+ "runtime"
56
67 "github.com/mndrix/tap-go"
78 rspec "github.com/opencontainers/runtime-spec/specs-go"
89 "github.com/opencontainers/runtime-tools/cgroups"
910 "github.com/opencontainers/runtime-tools/validation/util"
1011)
1112
12- func main () {
13- page := "1GB"
14- var limit uint64 = 52985 * 1024 * 1024 * 1024 // multiple of hugepage size
15- g , err := util .GetDefaultGenerator ()
16- if err != nil {
17- util .Fatal (err )
13+ func testHugetlbCgroups () error {
14+ t := tap .New ()
15+ t .Header (0 )
16+ defer t .AutoPlan ()
17+
18+ // limit =~ 100 * page size
19+ // NOTE: on some systems, pagesize "1GB" doesn't seem to work.
20+ // Ideally we should auto-detect the value.
21+ cases := []struct {
22+ page string
23+ limit uint64
24+ }{
25+ {"2MB" , 100 * 2 * 1024 * 1024 },
26+ {"1GB" , 100 * 1024 * 1024 * 1024 },
27+ {"2MB" , 100 * 2 * 1024 * 1024 },
28+ {"1GB" , 100 * 1024 * 1024 * 1024 },
1829 }
19- g .SetLinuxCgroupsPath (cgroups .AbsCgroupPath )
20- g .AddLinuxResourcesHugepageLimit (page , limit )
21- err = util .RuntimeOutsideValidate (g , nil , func (config * rspec.Spec , t * tap.T , state * rspec.State ) error {
22- cg , err := cgroups .FindCgroup ()
30+
31+ for _ , c := range cases {
32+ g , err := util .GetDefaultGenerator ()
2333 if err != nil {
2434 return err
2535 }
26- lhd , err := cg .GetHugepageLimitData (state .Pid , config .Linux .CgroupsPath )
36+ g .SetLinuxCgroupsPath (cgroups .AbsCgroupPath )
37+ g .AddLinuxResourcesHugepageLimit (c .page , c .limit )
38+ err = util .RuntimeOutsideValidate (g , t , func (config * rspec.Spec , t * tap.T , state * rspec.State ) error {
39+ cg , err := cgroups .FindCgroup ()
40+ if err != nil {
41+ return err
42+ }
43+ lhd , err := cg .GetHugepageLimitData (state .Pid , config .Linux .CgroupsPath )
44+ if err != nil {
45+ return err
46+ }
47+ for _ , lhl := range lhd {
48+ if lhl .Pagesize != c .page {
49+ continue
50+ }
51+ t .Ok (lhl .Limit == c .limit , "hugepage limit is set correctly" )
52+ t .Diagnosticf ("expect: %d, actual: %d" , c .limit , lhl .Limit )
53+ }
54+ return nil
55+ })
2756 if err != nil {
2857 return err
2958 }
30- for _ , lhl := range lhd {
31- if lhl .Pagesize == page && lhl .Limit != limit {
32- return fmt .Errorf ("hugepage %s limit is not set correctly, expect: %d, actual: %d" , page , limit , lhl .Limit )
33- }
34- }
59+ }
60+
61+ return nil
62+ }
63+
64+ func testWrongHugetlb () error {
65+ // We deliberately set the page size to a wrong value, "3MB", to see
66+ // if the container really returns an error.
67+ page := "3MB"
68+ var limit uint64 = 100 * 3 * 1024 * 1024
69+
70+ g , err := util .GetDefaultGenerator ()
71+ if err != nil {
72+ return err
73+ }
74+
75+ t := tap .New ()
76+ t .Header (0 )
77+ defer t .AutoPlan ()
78+
79+ g .SetLinuxCgroupsPath (cgroups .AbsCgroupPath )
80+ g .AddLinuxResourcesHugepageLimit (page , limit )
81+
82+ err = util .RuntimeOutsideValidate (g , t , func (config * rspec.Spec , t * tap.T , state * rspec.State ) error {
3583 return nil
3684 })
37- if err != nil {
85+ t .Ok (err != nil , "hugepage invalid pagesize results in an errror" )
86+ if err == nil {
87+ t .Diagnosticf ("expect: err != nil, actual: err == nil" )
88+ }
89+ return err
90+ }
91+
92+ func main () {
93+ if "linux" != runtime .GOOS {
94+ util .Fatal (fmt .Errorf ("linux-specific cgroup test" ))
95+ }
96+
97+ if err := testHugetlbCgroups (); err != nil {
98+ util .Fatal (err )
99+ }
100+
101+ if err := testWrongHugetlb (); err == nil {
38102 util .Fatal (err )
39103 }
40104}
0 commit comments