Skip to content

Commit

Permalink
write e2e tests for cgroups
Browse files Browse the repository at this point in the history
  • Loading branch information
nickorlow committed Jul 16, 2023
1 parent 475adf7 commit 3814e1f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1,323 deletions.
4 changes: 2 additions & 2 deletions pkg/util/runtime/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
func NumCPU() int {
cpus := runtime.NumCPU()

cgroupVersion := getCgroupVersion()
cgroupVersion := GetCgroupVersion()
cpuQuota := int64(-1)
cpuPeriod := int64(-1)

Expand All @@ -64,7 +64,7 @@ func IsCgroupAvaliable() bool {
return true
}

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
Expand Down
74 changes: 63 additions & 11 deletions test/e2e/cgroups/cgroups_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import (

"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() {
Expand All @@ -40,26 +43,75 @@ var _ = framework.IngressNginxDescribeSerial("[CGroups] cgroups", func() {
})

ginkgo.It("detects if cgroups is avaliable", func() {
assert.Equal(ginkgo.GinkgoT(), runtime.IsCgroupAvaliable(), 1)
assert.True(ginkgo.GinkgoT(), runtime.IsCgroupAvaliable())
})

ginkgo.It("detects cgroups version v1", func() {
assert.Equal(ginkgo.GinkgoT(), runtime.readCgroupFileToInt64(), 1)
})
cgroupPath, err := libcontainercgroups.FindCgroupMountpoint("", "cpu")
if err != nil {
log.Fatal(err)
}

ginkgo.It("detects number of CPUs properly in cgroups v1", func() {
assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), -1)
})
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"))

ginkgo.It("detects cgroups version v2", func() {
// create cgroups2 files
if err := os.MkdirAll("a/b/c/d", os.ModePerm); err != nil {
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("detects number of CPUs properly in cgroups v2", func() {
assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), -1)
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")
})

// ginkgo.It("detects number of CPUs properly in cgroups v1", func() {
// assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), -1)
// })

// ginkgo.It("detects cgroups version v2", func() {
// // create cgroups2 files
// if err := os.MkdirAll("a/b/c/d", os.ModePerm); err != nil {
// log.Fatal(err)
// }

// })

// ginkgo.It("detects number of CPUs properly in cgroups v2", func() {
// assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), -1)
// })
})
41 changes: 20 additions & 21 deletions test/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,26 @@ import (
"k8s.io/ingress-nginx/test/e2e/framework"

// tests to run
// _ "k8s.io/ingress-nginx/test/e2e/admission"
// _ "k8s.io/ingress-nginx/test/e2e/annotations"
// _ "k8s.io/ingress-nginx/test/e2e/annotations/modsecurity"
_ "k8s.io/ingress-nginx/test/e2e/cgroups"
// _ "k8s.io/ingress-nginx/test/e2e/dbg"
// _ "k8s.io/ingress-nginx/test/e2e/defaultbackend"
// _ "k8s.io/ingress-nginx/test/e2e/endpointslices"
// _ "k8s.io/ingress-nginx/test/e2e/gracefulshutdown"
// _ "k8s.io/ingress-nginx/test/e2e/ingress"
// _ "k8s.io/ingress-nginx/test/e2e/leaks"
// _ "k8s.io/ingress-nginx/test/e2e/loadbalance"
// _ "k8s.io/ingress-nginx/test/e2e/lua"
// _ "k8s.io/ingress-nginx/test/e2e/nginx"
// _ "k8s.io/ingress-nginx/test/e2e/security"
// _ "k8s.io/ingress-nginx/test/e2e/servicebackend"
// _ "k8s.io/ingress-nginx/test/e2e/settings"
// _ "k8s.io/ingress-nginx/test/e2e/settings/modsecurity"
// _ "k8s.io/ingress-nginx/test/e2e/settings/ocsp"
// _ "k8s.io/ingress-nginx/test/e2e/ssl"
// _ "k8s.io/ingress-nginx/test/e2e/status"
// _ "k8s.io/ingress-nginx/test/e2e/tcpudp"
_ "k8s.io/ingress-nginx/test/e2e/admission"
_ "k8s.io/ingress-nginx/test/e2e/annotations"
_ "k8s.io/ingress-nginx/test/e2e/annotations/modsecurity"
_ "k8s.io/ingress-nginx/test/e2e/dbg"
_ "k8s.io/ingress-nginx/test/e2e/defaultbackend"
_ "k8s.io/ingress-nginx/test/e2e/endpointslices"
_ "k8s.io/ingress-nginx/test/e2e/gracefulshutdown"
_ "k8s.io/ingress-nginx/test/e2e/ingress"
_ "k8s.io/ingress-nginx/test/e2e/leaks"
_ "k8s.io/ingress-nginx/test/e2e/loadbalance"
_ "k8s.io/ingress-nginx/test/e2e/lua"
_ "k8s.io/ingress-nginx/test/e2e/nginx"
_ "k8s.io/ingress-nginx/test/e2e/security"
_ "k8s.io/ingress-nginx/test/e2e/servicebackend"
_ "k8s.io/ingress-nginx/test/e2e/settings"
_ "k8s.io/ingress-nginx/test/e2e/settings/modsecurity"
_ "k8s.io/ingress-nginx/test/e2e/settings/ocsp"
_ "k8s.io/ingress-nginx/test/e2e/ssl"
_ "k8s.io/ingress-nginx/test/e2e/status"
_ "k8s.io/ingress-nginx/test/e2e/tcpudp"
)

// RunE2ETests checks configuration parameters (specified through flags) and then runs
Expand Down
Loading

0 comments on commit 3814e1f

Please sign in to comment.