Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/common-go/cgroups/v2/cpu_test.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cgroups_v2
6
7
import (
8
"fmt"
9
"math"
10
"os"
11
"path/filepath"
12
"testing"
13
14
"github.com/stretchr/testify/assert"
15
)
16
17
func TestMax(t *testing.T) {
18
values := []struct {
19
scenario string
20
fileQuota string
21
filePeriod string
22
expectedQuota uint64
23
expectedPeriod uint64
24
}{
25
{
26
scenario: "cpu consumption is restricted",
27
fileQuota: "200000",
28
filePeriod: "100000",
29
expectedQuota: 200_000,
30
expectedPeriod: 100_000,
31
},
32
{
33
scenario: "cpu consumption is unrestricted",
34
fileQuota: "max",
35
filePeriod: "100000",
36
expectedQuota: math.MaxUint64,
37
expectedPeriod: 100_000,
38
},
39
}
40
41
for _, v := range values {
42
mountPoint := createMaxFile(t, v.fileQuota, v.filePeriod)
43
cpu := NewCpuControllerWithMount(mountPoint, "cgroup")
44
quota, period, err := cpu.Max()
45
if err != nil {
46
t.Fatal(err)
47
}
48
49
assert.Equal(t, uint64(v.expectedQuota), quota, v.scenario)
50
assert.Equal(t, uint64(v.expectedPeriod), period, v.scenario)
51
}
52
}
53
54
func TestMaxNotExist(t *testing.T) {
55
cpu := NewCpuControllerWithMount("/this/does/not", "exist")
56
_, _, err := cpu.Max()
57
58
assert.ErrorIs(t, err, os.ErrNotExist)
59
}
60
61
func createMaxFile(t *testing.T, quota, period string) string {
62
mountPoint, err := os.MkdirTemp("", "test.max")
63
if err != nil {
64
t.Fatal(err)
65
}
66
cgroupPath := filepath.Join(mountPoint, "cgroup")
67
if err := os.MkdirAll(cgroupPath, 0755); err != nil {
68
t.Fatal(err)
69
}
70
71
if err := os.WriteFile(filepath.Join(cgroupPath, "cpu.max"), []byte(fmt.Sprintf("%v %v\n", quota, period)), 0755); err != nil {
72
t.Fatalf("failed to create cpu.max file: %v", err)
73
}
74
75
return mountPoint
76
}
77
78