| 1 |
/* |
| 2 |
* cgroup_test.go - Tests for cgroup CPU and memory limit reading. |
| 3 |
* |
| 4 |
* Copyright 2026 Google LLC |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 7 |
* use this file except in compliance with the License. You may obtain a copy of |
| 8 |
* the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 |
* License for the specific language governing permissions and limitations under |
| 16 |
* the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package cgroup |
| 20 |
|
| 21 |
import ( |
| 22 |
"encoding/json" |
| 23 |
"errors" |
| 24 |
"math" |
| 25 |
"os" |
| 26 |
"path/filepath" |
| 27 |
"strconv" |
| 28 |
"testing" |
| 29 |
) |
| 30 |
|
| 31 |
func writeFile(t *testing.T, path, content string) { |
| 32 |
t.Helper() |
| 33 |
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { |
| 34 |
t.Fatal(err) |
| 35 |
} |
| 36 |
if err := os.WriteFile(path, []byte(content), 0644); err != nil { |
| 37 |
t.Fatal(err) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestCgroupV1Unsupported(t *testing.T) { |
| 42 |
content := `12:memory:/docker/abc123 |
| 43 |
11:cpu,cpuacct:/docker/abc123 |
| 44 |
` |
| 45 |
root := t.TempDir() |
| 46 |
writeFile(t, filepath.Join(root, "proc/self/cgroup"), content) |
| 47 |
_, err := NewFromRoot(root) |
| 48 |
if !errors.Is(err, ErrV1Detected) { |
| 49 |
t.Fatalf("NewFromRoot() error = %v, want %v", err, ErrV1Detected) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// testdataExpected holds the expected values from a testdata/*/expected.json. |
| 54 |
// Null fields indicate that ErrNoLimit is expected. |
| 55 |
type testdataExpected struct { |
| 56 |
CPUQuota *float64 `json:"cpu_quota"` |
| 57 |
MemoryLimit *int64 `json:"memory_limit"` |
| 58 |
} |
| 59 |
|
| 60 |
// TestWithRootFromTestdata runs NewFromRoot, CPUQuota, and MemoryLimit |
| 61 |
// against filesystem snapshots captured from real Docker containers by |
| 62 |
// bin/snapshot-cgroup. Each subdirectory of testdata/ is a separate test |
| 63 |
// case containing a proc/ and sys/ tree plus an expected.json. |
| 64 |
// |
| 65 |
// Regenerate with: bin/gen-cgroup-testdata |
| 66 |
func TestWithRootFromTestdata(t *testing.T) { |
| 67 |
entries, err := os.ReadDir("testdata") |
| 68 |
if err != nil { |
| 69 |
t.Fatalf("no testdata directory: %v", err) |
| 70 |
} |
| 71 |
|
| 72 |
for _, entry := range entries { |
| 73 |
if !entry.IsDir() { |
| 74 |
continue |
| 75 |
} |
| 76 |
name := entry.Name() |
| 77 |
root := filepath.Join("testdata", name) |
| 78 |
|
| 79 |
t.Run(name, func(t *testing.T) { |
| 80 |
data, err := os.ReadFile(filepath.Join(root, "expected.json")) |
| 81 |
if err != nil { |
| 82 |
t.Fatalf("reading expected.json: %v", err) |
| 83 |
} |
| 84 |
var want testdataExpected |
| 85 |
if err := json.Unmarshal(data, &want); err != nil { |
| 86 |
t.Fatalf("parsing expected.json: %v", err) |
| 87 |
} |
| 88 |
|
| 89 |
cg, err := NewFromRoot(root) |
| 90 |
if err != nil { |
| 91 |
t.Fatalf("NewFromRoot(%q): %v", root, err) |
| 92 |
} |
| 93 |
|
| 94 |
gotCPU, err := cg.CPUQuota() |
| 95 |
if want.CPUQuota == nil { |
| 96 |
if !errors.Is(err, ErrNoLimit) { |
| 97 |
t.Errorf("CPUQuota() error = %v, want ErrNoLimit", err) |
| 98 |
} |
| 99 |
} else if err != nil { |
| 100 |
t.Fatalf("CPUQuota(): %v", err) |
| 101 |
} else if math.Abs(gotCPU-*want.CPUQuota) > 0.001 { |
| 102 |
t.Errorf("CPUQuota() = %v, want %v", gotCPU, *want.CPUQuota) |
| 103 |
} |
| 104 |
|
| 105 |
gotMem, err := cg.MemoryLimit() |
| 106 |
if want.MemoryLimit == nil { |
| 107 |
if !errors.Is(err, ErrNoLimit) { |
| 108 |
t.Errorf("MemoryLimit() error = %v, want ErrNoLimit", err) |
| 109 |
} |
| 110 |
} else if err != nil { |
| 111 |
t.Fatalf("MemoryLimit(): %v", err) |
| 112 |
} else if gotMem != *want.MemoryLimit { |
| 113 |
t.Errorf("MemoryLimit() = %v, want %v", gotMem, *want.MemoryLimit) |
| 114 |
} |
| 115 |
}) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// TestIntegrationCgroupLimits calls the real New(), CPUQuota(), and |
| 120 |
// MemoryLimit() against the live kernel cgroup interface. It is intended to |
| 121 |
// run inside a Docker container started with --cpus and --memory flags. |
| 122 |
// |
| 123 |
// The test is skipped unless CGROUP_EXPECTED_CPU_QUOTA and |
| 124 |
// CGROUP_EXPECTED_MEMORY_LIMIT are set in the environment. |
| 125 |
func TestIntegrationCgroupLimits(t *testing.T) { |
| 126 |
cpuStr := os.Getenv("CGROUP_EXPECTED_CPU_QUOTA") |
| 127 |
memStr := os.Getenv("CGROUP_EXPECTED_MEMORY_LIMIT") |
| 128 |
if cpuStr == "" && memStr == "" { |
| 129 |
t.Skip("set CGROUP_EXPECTED_CPU_QUOTA and CGROUP_EXPECTED_MEMORY_LIMIT to run") |
| 130 |
} |
| 131 |
|
| 132 |
cg, err := New() |
| 133 |
if err != nil { |
| 134 |
t.Fatalf("New() error: %v", err) |
| 135 |
} |
| 136 |
|
| 137 |
if cpuStr != "" { |
| 138 |
wantCPU, err := strconv.ParseFloat(cpuStr, 64) |
| 139 |
if err != nil { |
| 140 |
t.Fatalf("bad CGROUP_EXPECTED_CPU_QUOTA %q: %v", cpuStr, err) |
| 141 |
} |
| 142 |
gotCPU, err := cg.CPUQuota() |
| 143 |
if err != nil { |
| 144 |
t.Fatalf("CPUQuota() error: %v", err) |
| 145 |
} |
| 146 |
if math.Abs(gotCPU-wantCPU) > 0.001 { |
| 147 |
t.Errorf("CPUQuota() = %v, want %v", gotCPU, wantCPU) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if memStr != "" { |
| 152 |
wantMem, err := strconv.ParseInt(memStr, 10, 64) |
| 153 |
if err != nil { |
| 154 |
t.Fatalf("bad CGROUP_EXPECTED_MEMORY_LIMIT %q: %v", memStr, err) |
| 155 |
} |
| 156 |
gotMem, err := cg.MemoryLimit() |
| 157 |
if err != nil { |
| 158 |
t.Fatalf("MemoryLimit() error: %v", err) |
| 159 |
} |
| 160 |
if gotMem != wantMem { |
| 161 |
t.Errorf("MemoryLimit() = %v, want %v", gotMem, wantMem) |
| 162 |
} |
| 163 |
} |
| 164 |
} |