github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/common/cgroup/cgroup_test.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //+build linux 16 17 package cgroup 18 19 import ( 20 "io" 21 "reflect" 22 "strings" 23 "testing" 24 ) 25 26 func TestParseCgroups(t *testing.T) { 27 cg1 := `#subsys_name hierarchy num_cgroups enabled 28 cpuset 2 1 1 29 cpu 3 1 1 30 cpuacct 3 1 1 31 blkio 4 1 1 32 memory 6 1 1 33 devices 7 47 1 34 freezer 8 1 1 35 net_cls 5 1 1` 36 37 cg2 := `#subsys_name hierarchy num_cgroups enabled 38 cpuset 8 441 1 39 cpu 4 31 1 40 cpuacct 4 31 1 41 blkio 2 13 1 42 memory 0 1 0 43 devices 3 88 1 44 freezer 7 432 1 45 net_cls 6 432 1 46 perf_event 5 432 1 47 net_prio 6 432 1` 48 49 cg3 := `#subsys_name hierarchy num_cgroups enabled 50 cpuset 1 441 1 51 cpu 4 31 1 52 cpuacct 4 31 0 53 blkio 2 13 1 54 memory 0 1 0 55 devices 3 88 1 56 freezer 7 432 1 57 net_cls 6 432 1 58 perf_event 5 432 0 59 net_prio 6 432 1` 60 61 tests := []struct { 62 input io.Reader 63 output map[int][]string 64 }{ 65 { 66 input: strings.NewReader(cg1), 67 output: map[int][]string{ 68 2: []string{"cpuset"}, 69 3: []string{"cpu", "cpuacct"}, 70 4: []string{"blkio"}, 71 6: []string{"memory"}, 72 7: []string{"devices"}, 73 8: []string{"freezer"}, 74 5: []string{"net_cls"}, 75 }, 76 }, 77 { 78 input: strings.NewReader(cg2), 79 output: map[int][]string{ 80 8: []string{"cpuset"}, 81 4: []string{"cpu", "cpuacct"}, 82 2: []string{"blkio"}, 83 3: []string{"devices"}, 84 7: []string{"freezer"}, 85 6: []string{"net_cls", "net_prio"}, 86 5: []string{"perf_event"}, 87 }, 88 }, 89 { 90 input: strings.NewReader(cg3), 91 output: map[int][]string{ 92 1: []string{"cpuset"}, 93 4: []string{"cpu"}, 94 2: []string{"blkio"}, 95 3: []string{"devices"}, 96 7: []string{"freezer"}, 97 6: []string{"net_cls", "net_prio"}, 98 }, 99 }, 100 } 101 102 for i, tt := range tests { 103 o, err := parseCgroups(tt.input) 104 if err != nil { 105 t.Errorf("#%d: unexpected error `%v`", i, err) 106 } 107 eq := reflect.DeepEqual(o, tt.output) 108 if !eq { 109 t.Errorf("#%d: expected `%v` got `%v`", i, tt.output, o) 110 } 111 } 112 }