Path: blob/main/components/common-go/cgroups/v2/cpu_test.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cgroups_v256import (7"fmt"8"math"9"os"10"path/filepath"11"testing"1213"github.com/stretchr/testify/assert"14)1516func TestMax(t *testing.T) {17values := []struct {18scenario string19fileQuota string20filePeriod string21expectedQuota uint6422expectedPeriod uint6423}{24{25scenario: "cpu consumption is restricted",26fileQuota: "200000",27filePeriod: "100000",28expectedQuota: 200_000,29expectedPeriod: 100_000,30},31{32scenario: "cpu consumption is unrestricted",33fileQuota: "max",34filePeriod: "100000",35expectedQuota: math.MaxUint64,36expectedPeriod: 100_000,37},38}3940for _, v := range values {41mountPoint := createMaxFile(t, v.fileQuota, v.filePeriod)42cpu := NewCpuControllerWithMount(mountPoint, "cgroup")43quota, period, err := cpu.Max()44if err != nil {45t.Fatal(err)46}4748assert.Equal(t, uint64(v.expectedQuota), quota, v.scenario)49assert.Equal(t, uint64(v.expectedPeriod), period, v.scenario)50}51}5253func TestMaxNotExist(t *testing.T) {54cpu := NewCpuControllerWithMount("/this/does/not", "exist")55_, _, err := cpu.Max()5657assert.ErrorIs(t, err, os.ErrNotExist)58}5960func createMaxFile(t *testing.T, quota, period string) string {61mountPoint, err := os.MkdirTemp("", "test.max")62if err != nil {63t.Fatal(err)64}65cgroupPath := filepath.Join(mountPoint, "cgroup")66if err := os.MkdirAll(cgroupPath, 0755); err != nil {67t.Fatal(err)68}6970if err := os.WriteFile(filepath.Join(cgroupPath, "cpu.max"), []byte(fmt.Sprintf("%v %v\n", quota, period)), 0755); err != nil {71t.Fatalf("failed to create cpu.max file: %v", err)72}7374return mountPoint75}767778