Debian Codegraph @

internal/memorylimiter/cgroups/cgroup_test.go

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