Files
team/server/channels/app/platform/container_limits_linux_test.go
2381664c84 [MM-67975] Add container CPU and memory limits to support packet diagnostics (#35835)
* MM-67975: Add container CPU and memory limits to support packet diagnostics

Add two new optional fields to SupportPacketDiagnostics.Server:
- container_cpu_limit (float64): effective CPU limit in CPUs (e.g. 0.5, 2.0)
- container_memory_limit_mb (uint64): memory limit in MB

Both fields are populated from cgroups v2 (/sys/fs/cgroup/memory.max and
/sys/fs/cgroup/cpu.max) on Linux. Fields use omitempty so they are absent
from the output on bare metal or when no container limits are configured,
preserving backwards compatibility.

Non-Linux builds compile and produce no output for these fields via a
container_limits_other.go stub, following the existing memory_linux.go /
memory_other.go pattern.

* Fix golangci-lint errors: govet shadow and gofmt formatting

- Fix govet shadow: use var memBytes uint64 + err = instead of memBytes, err :=
  to avoid shadowing the outer err variable (which is reused after this block)
- Fix gofmt: add extra space before // 512 MB comment to align with
  the longer v2CPUMax line in the same assignment block

* fix(platform): ceil memory MB conversion and return zero-values for missing cgroup files

- Use ceiling division for MemoryLimitMB so sub-1MB limits map to 1 instead
  of 0 (which omitempty would silently drop)
- Absorb os.ErrNotExist in getContainerLimits so non-v2/bare-metal hosts
  return ContainerLimits{}, nil as the function comment promises
- Update test: missing cgroup file now asserts zero-values, not an error

* test(platform): add subtest locking ceil-to-MB behavior for sub-MB memory limits

* test(platform): add subtest for missing cpu.max returning zero values

Locks in the os.ErrNotExist branch for cpu.max (lines 57-60 in
container_limits_linux.go), which was previously unreachable via the
existing missing-memory test since that test returns early before
reading cpu.max.

---------

Co-authored-by: Hanzo Dev <[email protected]>
2026-04-23 13:25:34 +00:00

88 lines
2.8 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//go:build linux
package platform
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func writeTempCgroupFile(t *testing.T, dir, name, content string) string {
t.Helper()
path := filepath.Join(dir, name)
require.NoError(t, os.WriteFile(path, []byte(content), 0o644))
return path
}
func TestGetContainerLimits(t *testing.T) {
dir := t.TempDir()
originalPaths := cgroupPaths
t.Cleanup(func() { cgroupPaths = originalPaths })
t.Run("limited CPU and memory", func(t *testing.T) {
cgroupPaths.v2MemoryMax = writeTempCgroupFile(t, dir, "mem_limited", "536870912\n") // 512 MB
cgroupPaths.v2CPUMax = writeTempCgroupFile(t, dir, "cpu_limited", "200000 100000\n") // 2 CPUs
limits, err := getContainerLimits()
require.NoError(t, err)
assert.Equal(t, uint64(512), limits.MemoryLimitMB)
assert.InDelta(t, 2.0, limits.CPULimit, 0.001)
})
t.Run("unlimited CPU and memory", func(t *testing.T) {
cgroupPaths.v2MemoryMax = writeTempCgroupFile(t, dir, "mem_unlimited", "max\n")
cgroupPaths.v2CPUMax = writeTempCgroupFile(t, dir, "cpu_unlimited", "max 100000\n")
limits, err := getContainerLimits()
require.NoError(t, err)
assert.Equal(t, uint64(0), limits.MemoryLimitMB)
assert.Equal(t, float64(0), limits.CPULimit)
})
t.Run("fractional CPU limit", func(t *testing.T) {
cgroupPaths.v2MemoryMax = writeTempCgroupFile(t, dir, "mem_frac", "max\n")
cgroupPaths.v2CPUMax = writeTempCgroupFile(t, dir, "cpu_frac", "50000 100000\n") // 0.5 CPUs
limits, err := getContainerLimits()
require.NoError(t, err)
assert.InDelta(t, 0.5, limits.CPULimit, 0.001)
})
t.Run("sub-MB memory rounds up to 1MB", func(t *testing.T) {
cgroupPaths.v2MemoryMax = writeTempCgroupFile(t, dir, "mem_tiny", "524288\n") // 0.5 MB
cgroupPaths.v2CPUMax = writeTempCgroupFile(t, dir, "cpu_tiny", "max 100000\n")
limits, err := getContainerLimits()
require.NoError(t, err)
assert.Equal(t, uint64(1), limits.MemoryLimitMB)
})
t.Run("missing cgroup files returns zero values", func(t *testing.T) {
cgroupPaths.v2MemoryMax = filepath.Join(dir, "nonexistent_memory")
cgroupPaths.v2CPUMax = writeTempCgroupFile(t, dir, "cpu_present", "max 100000\n")
limits, err := getContainerLimits()
require.NoError(t, err)
assert.Equal(t, uint64(0), limits.MemoryLimitMB)
assert.Equal(t, float64(0), limits.CPULimit)
})
t.Run("missing cpu.max returns zero values", func(t *testing.T) {
cgroupPaths.v2MemoryMax = writeTempCgroupFile(t, dir, "mem_no_cpu", "max\n")
cgroupPaths.v2CPUMax = filepath.Join(dir, "nonexistent_cpu")
limits, err := getContainerLimits()
require.NoError(t, err)
assert.Equal(t, uint64(0), limits.MemoryLimitMB)
assert.Equal(t, float64(0), limits.CPULimit)
})
}