Debian Codegraph @

internal/cgroups/cgroups_test.go

raw symbols SCIP
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 TestNewCGroups(t *testing.T) {
34 cgroupsProcCGroupPath := filepath.Join(testDataProcPath, "cgroups", "cgroup")
35 cgroupsProcMountInfoPath := filepath.Join(testDataProcPath, "cgroups", "mountinfo")
36
37 testTable := []struct {
38 subsys string
39 path string
40 }{
41 {_cgroupSubsysCPU, "/sys/fs/cgroup/cpu,cpuacct"},
42 {_cgroupSubsysCPUAcct, "/sys/fs/cgroup/cpu,cpuacct"},
43 {_cgroupSubsysCPUSet, "/sys/fs/cgroup/cpuset"},
44 {_cgroupSubsysMemory, "/sys/fs/cgroup/memory/large"},
45 }
46
47 cgroups, err := NewCGroups(cgroupsProcMountInfoPath, cgroupsProcCGroupPath)
48 assert.Equal(t, len(testTable), len(cgroups))
49 assert.NoError(t, err)
50
51 for _, tt := range testTable {
52 cgroup, exists := cgroups[tt.subsys]
53 assert.Equal(t, true, exists, "%q expected to present in `cgroups`", tt.subsys)
54 assert.Equal(t, tt.path, cgroup.path, "%q expected for `cgroups[%q].path`, got %q", tt.path, tt.subsys, cgroup.path)
55 }
56 }
57
58 func TestNewCGroupsWithErrors(t *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 filepath.Join(testDataProcPath, "invalid-cgroup", "cgroup"),
68 },
69 {
70 filepath.Join(testDataProcPath, "invalid-mountinfo", "mountinfo"),
71 "/dev/null",
72 },
73 {
74 filepath.Join(testDataProcPath, "untranslatable", "mountinfo"),
75 filepath.Join(testDataProcPath, "untranslatable", "cgroup"),
76 },
77 }
78
79 for _, tt := range testTable {
80 cgroups, err := NewCGroups(tt.mountInfoPath, tt.cgroupPath)
81 assert.Nil(t, cgroups)
82 assert.Error(t, err)
83 }
84 }
85
86 func TestCGroupsCPUQuota(t *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(CGroups)
120
121 quota, defined, err := cgroups.CPUQuota()
122 assert.Equal(t, -1.0, quota, "nonexistent")
123 assert.Equal(t, false, defined, "nonexistent")
124 assert.NoError(t, err, "nonexistent")
125
126 for _, tt := range testTable {
127 cgroupPath := filepath.Join(testDataCGroupsPath, tt.name)
128 cgroups[_cgroupSubsysCPU] = NewCGroup(cgroupPath)
129
130 quota, defined, err := cgroups.CPUQuota()
131 assert.Equal(t, tt.expectedQuota, quota, tt.name)
132 assert.Equal(t, tt.expectedDefined, defined, tt.name)
133
134 if tt.shouldHaveError {
135 assert.Error(t, err, tt.name)
136 } else {
137 assert.NoError(t, err, tt.name)
138 }
139 }
140 }