|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cgroups |
| 18 | + |
| 19 | +import ( |
| 20 | + "io/ioutil" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +const cpuacctStatData = `user 1 |
| 27 | +system 2 |
| 28 | +sched_delay 3 |
| 29 | +` |
| 30 | + |
| 31 | +func TestGetUsage(t *testing.T) { |
| 32 | + mock, err := newMock() |
| 33 | + if err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + defer mock.delete() |
| 37 | + cpuacct := NewCpuacct(mock.root) |
| 38 | + if cpuacct == nil { |
| 39 | + t.Fatal("cpuacct is nil") |
| 40 | + } |
| 41 | + err = os.Mkdir(filepath.Join(mock.root, string(Cpuacct), "test"), defaultDirPerm) |
| 42 | + if err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | + current := filepath.Join(mock.root, string(Cpuacct), "test", "cpuacct.stat") |
| 46 | + if err = ioutil.WriteFile( |
| 47 | + current, |
| 48 | + []byte(cpuacctStatData), |
| 49 | + defaultFilePerm, |
| 50 | + ); err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + user, kernel, err := cpuacct.getUsage("test") |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("can't get usage: %v", err) |
| 56 | + } |
| 57 | + index := []uint64{ |
| 58 | + user, |
| 59 | + kernel, |
| 60 | + } |
| 61 | + for i, v := range index { |
| 62 | + expected := ((uint64(i) + 1) * nanosecondsInSecond) / clockTicks |
| 63 | + if v != expected { |
| 64 | + t.Errorf("expected value at index %d to be %d but received %d", i, expected, v) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments