fix e2e tests for cgroups

This commit is contained in:
Nicholas Orlowsky 2023-07-16 19:02:37 -04:00
commit 8621dfc66d
No known key found for this signature in database
GPG key ID: 58832FD3AC16C706
3 changed files with 100 additions and 3 deletions

View file

@ -38,7 +38,7 @@ import (
func NumCPU() int {
cpus := runtime.NumCPU()
cgroupVersion := getCgroupVersion()
cgroupVersion := GetCgroupVersion()
cpuQuota := int64(-1)
cpuPeriod := int64(-1)
@ -60,7 +60,7 @@ func NumCPU() int {
return int(math.Ceil(float64(cpuQuota) / float64(cpuPeriod)))
}
func getCgroupVersion() int64 {
func GetCgroupVersion() int64 {
// /sys/fs/cgroup/cgroup.controllers will not exist with cgroupsv1
if _, err := os.Stat("/sys/fs/cgroup/cgroup.controllers"); err == nil {
return 2

View file

@ -26,4 +26,4 @@ import (
// NumCPU ...
func NumCPU() int {
return runtime.NumCPU()
}
}

View file

@ -0,0 +1,97 @@
//go:build linux
// +build linux
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cgroups
import (
"log"
"os"
"github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/assert"
"k8s.io/ingress-nginx/test/e2e/framework"
"path/filepath"
"k8s.io/ingress-nginx/pkg/util/runtime"
libcontainercgroups "github.com/opencontainers/runc/libcontainer/cgroups"
)
var _ = framework.IngressNginxDescribeSerial("[CGroups] cgroups", func() {
f := framework.NewDefaultFramework("cgroups")
ginkgo.BeforeEach(func() {
f.NewEchoDeployment()
f.NewSlowEchoDeployment()
})
ginkgo.It("detects cgroups version v1", func() {
cgroupPath, err := libcontainercgroups.FindCgroupMountpoint("", "cpu")
if err != nil {
log.Fatal(err)
}
quotaFile, err := os.Create(filepath.Join(cgroupPath,"cpu.cfs_quota_us"))
if err != nil {
log.Fatal(err)
}
periodFile, err := os.Create(filepath.Join(cgroupPath,"cpu.cfs_period_us"))
if err != nil {
log.Fatal(err)
}
quotaFile.WriteString("4");
quotaFile.Sync();
periodFile.WriteString("2");
periodFile.Sync();
assert.Equal(ginkgo.GinkgoT(), runtime.GetCgroupVersion(), int64(1))
assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), 2)
os.Remove(filepath.Join(cgroupPath,"cpu.cfs_quota_us"))
os.Remove(filepath.Join(cgroupPath,"cpu.cfs_period_us"))
})
ginkgo.It("detect cgroups version v2", func() {
if err := os.MkdirAll("/sys/fs/cgroup/", os.ModePerm); err != nil {
log.Fatal(err)
}
os.Create("/sys/fs/cgroup/cgroup.controllers")
file, err := os.Create("/sys/fs/cgroup/cpu.max")
if err != nil {
log.Fatal(err)
}
file.WriteString("4 2");
file.Sync();
assert.Equal(ginkgo.GinkgoT(), runtime.GetCgroupVersion(), int64(2))
assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), 2)
os.Remove("/sys/fs/cgroup/cpu.max")
os.Remove("/sys/fs/cgroup/cgroup.controllers")
})
})