| 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 cgroupsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/ |
| 25 | |
| 26 | import ( |
| 27 | "path/filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/" |
| 28 | "testingscip-go gomod github.com/golang/go/src go1.18 testing/" |
| 29 | |
| 30 | "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/" |
| 31 | ) |
| 32 | |
| 33 | func TestCGroupParamPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestCGroupParamPath().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 34 | cgroup := NewCGroupscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewCGroup().("/sys/fs/cgroup/cpu") |
| 35 | assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, "/sys/fs/cgroup/cpu", cgroup.Pathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroup#Path().()) |
| 36 | assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, "/sys/fs/cgroup/cpu/cpu.cfs_quota_us", cgroup.ParamPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroup#ParamPath().("cpu.cfs_quota_us")) |
| 37 | } |
| 38 | |
| 39 | func TestCGroupReadFirstLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestCGroupReadFirstLine().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 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 := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataCGroupsPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataCGroupsPath., tt.name) |
| 68 | cgroup := NewCGroupscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewCGroup().(cgroupPath) |
| 69 | |
| 70 | content, err := cgroup.readFirstLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroup#readFirstLine().(tt.paramName) |
| 71 | assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.expectedContent, content, tt.name) |
| 72 | |
| 73 | if tt.shouldHaveError { |
| 74 | assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Errorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Error().(t, err, tt.name) |
| 75 | } else { |
| 76 | assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/NoError().(t, err, tt.name) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestCGroupReadIntscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestCGroupReadInt().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 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 := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataCGroupsPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataCGroupsPath., tt.name) |
| 116 | cgroup := NewCGroupscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewCGroup().(cgroupPath) |
| 117 | |
| 118 | value, err := cgroup.readIntscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroup#readInt().(tt.paramName) |
| 119 | assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.expectedValue, value, "%s/%s", tt.name, tt.paramName) |
| 120 | |
| 121 | if tt.shouldHaveError { |
| 122 | assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Errorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Error().(t, err, tt.name) |
| 123 | } else { |
| 124 | assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/NoError().(t, err, tt.name) |
| 125 | } |
| 126 | } |
| 127 | } |