| 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 TestNewCGroupsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestNewCGroups().(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 | cgroupsProcCGroupPath := 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().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "cgroups", "cgroup") |
| 35 | cgroupsProcMountInfoPath := 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().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "cgroups", "mountinfo") |
| 36 | |
| 37 | testTable := []struct { |
| 38 | subsys string |
| 39 | path string |
| 40 | }{ |
| 41 | {_cgroupSubsysCPUscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_cgroupSubsysCPU., "/sys/fs/cgroup/cpu,cpuacct"}, |
| 42 | {_cgroupSubsysCPUAcctscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_cgroupSubsysCPUAcct., "/sys/fs/cgroup/cpu,cpuacct"}, |
| 43 | {_cgroupSubsysCPUSetscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_cgroupSubsysCPUSet., "/sys/fs/cgroup/cpuset"}, |
| 44 | {_cgroupSubsysMemoryscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_cgroupSubsysMemory., "/sys/fs/cgroup/memory/large"}, |
| 45 | } |
| 46 | |
| 47 | cgroups, err := NewCGroupsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewCGroups().(cgroupsProcMountInfoPath, cgroupsProcCGroupPath) |
| 48 | 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, len(testTable), len(cgroups)) |
| 49 | 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) |
| 50 | |
| 51 | for _, tt := range testTable { |
| 52 | cgroup, exists := cgroups[tt.subsys] |
| 53 | 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, true, exists, "%q expected to present in `cgroups`", tt.subsys) |
| 54 | 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.path, cgroup.pathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroup#path., "%q expected for `cgroups[%q].path`, got %q", tt.path, tt.subsys, cgroup.pathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroup#path.) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestNewCGroupsWithErrorsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestNewCGroupsWithErrors().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 59 | testTable := []struct { |
| 60 | mountInfoPath string |
| 61 | cgroupPath string |
| 62 | }{ |
| 63 | {"non-existing-file", "/dev/null"}, |
| 64 | {"/dev/null", "non-existing-file"}, |
| 65 | { |
| 66 | "/dev/null", |
| 67 | 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().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "invalid-cgroup", "cgroup"), |
| 68 | }, |
| 69 | { |
| 70 | 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().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "invalid-mountinfo", "mountinfo"), |
| 71 | "/dev/null", |
| 72 | }, |
| 73 | { |
| 74 | 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().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "untranslatable", "mountinfo"), |
| 75 | 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().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "untranslatable", "cgroup"), |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | for _, tt := range testTable { |
| 80 | cgroups, err := NewCGroupsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewCGroups().(tt.mountInfoPath, tt.cgroupPath) |
| 81 | assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Nilscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Nil().(t, cgroups) |
| 82 | 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) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestCGroupsCPUQuotascip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestCGroupsCPUQuota().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 87 | testTable := []struct { |
| 88 | name string |
| 89 | expectedQuota float64 |
| 90 | expectedDefined bool |
| 91 | shouldHaveError bool |
| 92 | }{ |
| 93 | { |
| 94 | name: "cpu", |
| 95 | expectedQuota: 6.0, |
| 96 | expectedDefined: true, |
| 97 | shouldHaveError: false, |
| 98 | }, |
| 99 | { |
| 100 | name: "undefined", |
| 101 | expectedQuota: -1.0, |
| 102 | expectedDefined: false, |
| 103 | shouldHaveError: false, |
| 104 | }, |
| 105 | { |
| 106 | name: "zero-period", |
| 107 | expectedQuota: -1.0, |
| 108 | expectedDefined: false, |
| 109 | shouldHaveError: false, |
| 110 | }, |
| 111 | { |
| 112 | name: "undefined-period", |
| 113 | expectedQuota: -1.0, |
| 114 | expectedDefined: false, |
| 115 | shouldHaveError: true, |
| 116 | }, |
| 117 | } |
| 118 | |
| 119 | cgroups := make(CGroupsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups#) |
| 120 | |
| 121 | quota, defined, err := cgroups.CPUQuotascip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups#CPUQuota().() |
| 122 | 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, -1.0, quota, "nonexistent") |
| 123 | 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, false, defined, "nonexistent") |
| 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, "nonexistent") |
| 125 | |
| 126 | for _, tt := range testTable { |
| 127 | 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) |
| 128 | cgroups[_cgroupSubsysCPUscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/_cgroupSubsysCPU.] = NewCGroupscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewCGroup().(cgroupPath) |
| 129 | |
| 130 | quota, defined, err := cgroups.CPUQuotascip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups#CPUQuota().() |
| 131 | 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.expectedQuota, quota, tt.name) |
| 132 | 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.expectedDefined, defined, tt.name) |
| 133 | |
| 134 | if tt.shouldHaveError { |
| 135 | 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) |
| 136 | } else { |
| 137 | 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) |
| 138 | } |
| 139 | } |
| 140 | } |