1
0
mirror of https://github.com/moby/moby.git synced 2025-07-14 15:41:16 +03:00
Files
moby/daemon/stats_unix_test.go
Lee Gaines 6d7a370fe5 Refactor CPU usage stats test to use go:embed
Refactor the system CPU usage testing approach for improved maintainability:

1. Extract the core CPU usage parsing logic into a new `readSystemCPUUsage`
   function that accepts an io.Reader, making it more testable and modular.

2. Use go:embed directive to embed the test data file at compile time,
   eliminating runtime file operations and making tests more reliable.

3. Simplify the test by removing global variable mocking in favor of a more
   direct approach with the new reader-based function.

4. Maintain full test coverage for the long "intr" line edge case which was
   crucial for the original bug fix, while making the test more maintainable.

This change preserves the original test behavior while improving code quality,
testability, and making the tests self-contained.

Signed-off-by: Lee Gaines <leetgaines@gmail.com>
2025-05-16 10:23:49 -04:00

23 lines
437 B
Go

//go:build !windows
package daemon
import (
_ "embed"
"strings"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
//go:embed testdata/stat
var statData string
func TestGetSystemCPUUsageParsing(t *testing.T) {
input := strings.NewReader(statData)
cpuUsage, cpuNum, _ := readSystemCPUUsage(input)
assert.Check(t, is.Equal(cpuUsage, uint64(65647090000000)))
assert.Check(t, is.Equal(cpuNum, uint32(128)))
}