| 1 | // Copyright (c) 2017 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | //go:build linux |
| 22 | // +build linux |
| 23 | |
| 24 | package cgroups |
| 25 | |
| 26 | import ( |
| 27 | "path/filepath" |
| 28 | "testing" |
| 29 | |
| 30 | "github.com/stretchr/testify/assert" |
| 31 | ) |
| 32 | |
| 33 | func TestCGroupParamPath(t *testing.T) { |
| 34 | cgroup := NewCGroup("/sys/fs/cgroup/cpu") |
| 35 | assert.Equal(t, "/sys/fs/cgroup/cpu", cgroup.Path()) |
| 36 | assert.Equal(t, "/sys/fs/cgroup/cpu/cpu.cfs_quota_us", cgroup.ParamPath("cpu.cfs_quota_us")) |
| 37 | } |
| 38 | |
| 39 | func TestCGroupReadFirstLine(t *testing.T) { |
| 40 | testTable := []struct { |
| 41 | name string |
| 42 | paramName string |
| 43 | expectedContent string |
| 44 | shouldHaveError bool |
| 45 | }{ |
| 46 | { |
| 47 | name: "cpu", |
| 48 | paramName: "cpu.cfs_period_us", |
| 49 | expectedContent: "100000", |
| 50 | shouldHaveError: false, |
| 51 | }, |
| 52 | { |
| 53 | name: "absent", |
| 54 | paramName: "cpu.stat", |
| 55 | expectedContent: "", |
| 56 | shouldHaveError: true, |
| 57 | }, |
| 58 | { |
| 59 | name: "empty", |
| 60 | paramName: "cpu.cfs_quota_us", |
| 61 | expectedContent: "", |
| 62 | shouldHaveError: true, |
| 63 | }, |
| 64 | } |
| 65 | |
| 66 | for _, tt := range testTable { |
| 67 | cgroupPath := filepath.Join(testDataCGroupsPath, tt.name) |
| 68 | cgroup := NewCGroup(cgroupPath) |
| 69 | |
| 70 | content, err := cgroup.readFirstLine(tt.paramName) |
| 71 | assert.Equal(t, tt.expectedContent, content, tt.name) |
| 72 | |
| 73 | if tt.shouldHaveError { |
| 74 | assert.Error(t, err, tt.name) |
| 75 | } else { |
| 76 | assert.NoError(t, err, tt.name) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestCGroupReadInt(t *testing.T) { |
| 82 | testTable := []struct { |
| 83 | name string |
| 84 | paramName string |
| 85 | expectedValue int |
| 86 | shouldHaveError bool |
| 87 | }{ |
| 88 | { |
| 89 | name: "cpu", |
| 90 | paramName: "cpu.cfs_period_us", |
| 91 | expectedValue: 100000, |
| 92 | shouldHaveError: false, |
| 93 | }, |
| 94 | { |
| 95 | name: "empty", |
| 96 | paramName: "cpu.cfs_quota_us", |
| 97 | expectedValue: 0, |
| 98 | shouldHaveError: true, |
| 99 | }, |
| 100 | { |
| 101 | name: "invalid", |
| 102 | paramName: "cpu.cfs_quota_us", |
| 103 | expectedValue: 0, |
| 104 | shouldHaveError: true, |
| 105 | }, |
| 106 | { |
| 107 | name: "absent", |
| 108 | paramName: "cpu.cfs_quota_us", |
| 109 | expectedValue: 0, |
| 110 | shouldHaveError: true, |
| 111 | }, |
| 112 | } |
| 113 | |
| 114 | for _, tt := range testTable { |
| 115 | cgroupPath := filepath.Join(testDataCGroupsPath, tt.name) |
| 116 | cgroup := NewCGroup(cgroupPath) |
| 117 | |
| 118 | value, err := cgroup.readInt(tt.paramName) |
| 119 | assert.Equal(t, tt.expectedValue, value, "%s/%s", tt.name, tt.paramName) |
| 120 | |
| 121 | if tt.shouldHaveError { |
| 122 | assert.Error(t, err, tt.name) |
| 123 | } else { |
| 124 | assert.NoError(t, err, tt.name) |
| 125 | } |
| 126 | } |
| 127 | } |