Skip to content

Commit cfd7d68

Browse files
committed
Add integration test in CI
1 parent 5d605cb commit cfd7d68

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ jobs:
106106
# make test
107107
# make test-teardown
108108

109+
test-cgroup-integration:
110+
name: Test cgroup integration
111+
runs-on: ubuntu-latest
112+
steps:
113+
- uses: actions/checkout@v4
114+
- name: Run cgroup integration test
115+
run: |
116+
docker run --rm \
117+
--cpus=0.5 --memory=128m \
118+
-e CGROUP_EXPECTED_CPU_QUOTA=0.5 \
119+
-e CGROUP_EXPECTED_MEMORY_LIMIT=134217728 \
120+
-v "$PWD":/src -w /src \
121+
golang:1.25 \
122+
go test -run TestIntegrationCgroupLimits -v ./cgroup/
123+
109124
run-cli-tests:
110125
name: Run command-line interface tests
111126
runs-on: ubuntu-latest

cgroup/cgroup_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"math"
2525
"os"
2626
"path/filepath"
27+
"strconv"
2728
"strings"
2829
"testing"
2930
)
@@ -539,3 +540,45 @@ func TestWithRootFromTestdata(t *testing.T) {
539540
})
540541
}
541542
}
543+
544+
// TestIntegrationCgroupLimits calls the real CPUQuota() and MemoryLimit()
545+
// functions against the live kernel cgroup interface. It is intended to run
546+
// inside a Docker container started with --cpus and --memory flags.
547+
//
548+
// The test is skipped unless CGROUP_EXPECTED_CPU_QUOTA and
549+
// CGROUP_EXPECTED_MEMORY_LIMIT are set in the environment.
550+
func TestIntegrationCgroupLimits(t *testing.T) {
551+
cpuStr := os.Getenv("CGROUP_EXPECTED_CPU_QUOTA")
552+
memStr := os.Getenv("CGROUP_EXPECTED_MEMORY_LIMIT")
553+
if cpuStr == "" && memStr == "" {
554+
t.Skip("set CGROUP_EXPECTED_CPU_QUOTA and CGROUP_EXPECTED_MEMORY_LIMIT to run")
555+
}
556+
557+
if cpuStr != "" {
558+
wantCPU, err := strconv.ParseFloat(cpuStr, 64)
559+
if err != nil {
560+
t.Fatalf("bad CGROUP_EXPECTED_CPU_QUOTA %q: %v", cpuStr, err)
561+
}
562+
gotCPU, err := CPUQuota()
563+
if err != nil {
564+
t.Fatalf("CPUQuota() error: %v", err)
565+
}
566+
if math.Abs(gotCPU-wantCPU) > 0.001 {
567+
t.Errorf("CPUQuota() = %v, want %v", gotCPU, wantCPU)
568+
}
569+
}
570+
571+
if memStr != "" {
572+
wantMem, err := strconv.ParseInt(memStr, 10, 64)
573+
if err != nil {
574+
t.Fatalf("bad CGROUP_EXPECTED_MEMORY_LIMIT %q: %v", memStr, err)
575+
}
576+
gotMem, err := MemoryLimit()
577+
if err != nil {
578+
t.Fatalf("MemoryLimit() error: %v", err)
579+
}
580+
if gotMem != wantMem {
581+
t.Errorf("MemoryLimit() = %v, want %v", gotMem, wantMem)
582+
}
583+
}
584+
}

0 commit comments

Comments
 (0)