From 3f5343b8434d51866488d1104453ab8a60251938 Mon Sep 17 00:00:00 2001 From: Geon Kim Date: Wed, 10 Jan 2024 02:02:48 +0900 Subject: [PATCH] test(memlimit): add FromSystem tests --- .github/workflows/test.yml | 8 ++++ memlimit/cgroups_test.go | 10 +++-- memlimit/memlimit_test.go | 60 +++++++++++++++++++++++---- memlimit/memlimit_unsupported_test.go | 58 +++++++++++++++++++++++++- 4 files changed, 122 insertions(+), 14 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b573fcd..701967f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,6 +25,10 @@ jobs: run: | docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.19 go test -v ./... -expected=4530896896 + - name: Run tests in Go container (system memory limit) + run: | + docker run --rm -v=$(pwd):/app -w=/app golang:1.19 go test -v ./... -expected-system=$(($(awk '/MemTotal/ {print $2}' /proc/meminfo) * 1024)) + test-ubuntu-22_04: runs-on: ubuntu-22.04 @@ -46,3 +50,7 @@ jobs: - name: Run tests in Go container (4321m) run: | docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.19 go test -v ./... -expected=4530896896 + + - name: Run tests in Go container (system memory limit) + run: | + docker run --rm -v=$(pwd):/app -w=/app golang:1.19 go test -v ./... -expected-system=$(($(awk '/MemTotal/ {print $2}' /proc/meminfo) * 1024)) \ No newline at end of file diff --git a/memlimit/cgroups_test.go b/memlimit/cgroups_test.go index 9986500..537b764 100644 --- a/memlimit/cgroups_test.go +++ b/memlimit/cgroups_test.go @@ -10,6 +10,10 @@ import ( ) func TestFromCgroup(t *testing.T) { + if expected == 0 { + t.Skip() + } + limit, err := FromCgroup() if cgVersion == cgroups.Unavailable && err != ErrNoCgroup { t.Fatalf("FromCgroup() error = %v, wantErr %v", err, ErrNoCgroup) @@ -24,7 +28,7 @@ func TestFromCgroup(t *testing.T) { } func TestFromCgroupV1(t *testing.T) { - if cgVersion != cgroups.Legacy { + if expected == 0 || cgVersion != cgroups.Legacy { t.Skip() } limit, err := FromCgroupV1() @@ -37,7 +41,7 @@ func TestFromCgroupV1(t *testing.T) { } func TestFromCgroupHybrid(t *testing.T) { - if cgVersion != cgroups.Hybrid { + if expected == 0 || cgVersion != cgroups.Hybrid { t.Skip() } limit, err := FromCgroupHybrid() @@ -50,7 +54,7 @@ func TestFromCgroupHybrid(t *testing.T) { } func TestFromCgroupV2(t *testing.T) { - if cgVersion != cgroups.Unified { + if expected == 0 || cgVersion != cgroups.Unified { t.Skip() } limit, err := FromCgroupV2() diff --git a/memlimit/memlimit_test.go b/memlimit/memlimit_test.go index bf6e141..7414594 100644 --- a/memlimit/memlimit_test.go +++ b/memlimit/memlimit_test.go @@ -13,12 +13,14 @@ import ( ) var ( - cgVersion cgroups.CGMode - expected uint64 + cgVersion cgroups.CGMode + expected uint64 + expectedSystem uint64 ) func TestMain(m *testing.M) { flag.Uint64Var(&expected, "expected", 0, "Expected cgroup's memory limit") + flag.Uint64Var(&expectedSystem, "expected-system", 0, "Expected system memory limit") flag.Parse() cgVersion = cgroups.Mode() @@ -45,7 +47,7 @@ func TestSetGoMemLimit(t *testing.T) { }, want: int64(float64(expected) * 0.5), wantErr: nil, - skip: cgVersion == cgroups.Unavailable, + skip: expected == 0 || cgVersion == cgroups.Unavailable, }, { name: "0.9", @@ -54,7 +56,7 @@ func TestSetGoMemLimit(t *testing.T) { }, want: int64(float64(expected) * 0.9), wantErr: nil, - skip: cgVersion == cgroups.Unavailable, + skip: expected == 0 || cgVersion == cgroups.Unavailable, }, { name: "Unavailable", @@ -103,7 +105,7 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) { }, want: int64(float64(expected) * 0.9), wantErr: nil, - skip: cgVersion == cgroups.Unavailable, + skip: expected == 0 || cgVersion == cgroups.Unavailable, }, { name: "FromCgroup_Unavaliable", @@ -113,7 +115,7 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) { }, want: 0, wantErr: ErrNoCgroup, - skip: cgVersion != cgroups.Unavailable, + skip: expected == 0 || cgVersion != cgroups.Unavailable, }, { name: "FromCgroupV1", @@ -123,7 +125,7 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) { }, want: int64(float64(expected) * 0.9), wantErr: nil, - skip: cgVersion != cgroups.Legacy, + skip: expected == 0 || cgVersion != cgroups.Legacy, }, { name: "FromCgroupHybrid", @@ -133,7 +135,7 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) { }, want: int64(float64(expected) * 0.9), wantErr: nil, - skip: cgVersion != cgroups.Hybrid, + skip: expected == 0 || cgVersion != cgroups.Hybrid, }, { name: "FromCgroupV2", @@ -143,7 +145,47 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) { }, want: int64(float64(expected) * 0.9), wantErr: nil, - skip: cgVersion != cgroups.Unified, + skip: expected == 0 || cgVersion != cgroups.Unified, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.skip { + t.Skip() + } + got, err := SetGoMemLimitWithProvider(tt.args.provider, tt.args.ratio) + if err != tt.wantErr { + t.Errorf("SetGoMemLimitWithProvider() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("SetGoMemLimitWithProvider() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestSetGoMemLimitWithProvider_WithSystemProvider(t *testing.T) { + type args struct { + provider Provider + ratio float64 + } + tests := []struct { + name string + args args + want int64 + wantErr error + skip bool + }{ + { + name: "FromSystem", + args: args{ + provider: FromSystem, + ratio: 0.9, + }, + want: int64(float64(expectedSystem) * 0.9), + wantErr: nil, + skip: expectedSystem == 0, }, } for _, tt := range tests { diff --git a/memlimit/memlimit_unsupported_test.go b/memlimit/memlimit_unsupported_test.go index a15811b..e55ceb8 100644 --- a/memlimit/memlimit_unsupported_test.go +++ b/memlimit/memlimit_unsupported_test.go @@ -4,9 +4,23 @@ package memlimit import ( + "errors" + "flag" + "os" "testing" ) +var ( + expected uint64 +) + +func TestMain(m *testing.M) { + flag.Uint64Var(&expected, "expected", 0, "Expected memory limit") + flag.Parse() + + os.Exit(m.Run()) +} + func TestSetGoMemLimit(t *testing.T) { type args struct { ratio float64 @@ -37,7 +51,7 @@ func TestSetGoMemLimit(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := SetGoMemLimit(tt.args.ratio) - if err != tt.wantErr { + if !errors.Is(err, tt.wantErr) { t.Errorf("SetGoMemLimit() error = %v, wantErr %v", err, tt.wantErr) return } @@ -99,7 +113,47 @@ func TestSetGoMemLimitWithProvider_WithCgroupProvider(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := SetGoMemLimitWithProvider(tt.args.provider, tt.args.ratio) - if err != tt.wantErr { + if !errors.Is(err, tt.wantErr) { + t.Errorf("SetGoMemLimitWithProvider() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("SetGoMemLimitWithProvider() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestSetGoMemLimitWithProvider_WithSystemProvider(t *testing.T) { + type args struct { + provider Provider + ratio float64 + } + tests := []struct { + name string + args args + want int64 + wantErr error + skip bool + }{ + { + name: "FromSystem", + args: args{ + provider: FromSystem, + ratio: 0.9, + }, + want: int64(float64(expected) * 0.9), + wantErr: nil, + skip: expected == 0, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.skip { + t.Skip() + } + got, err := SetGoMemLimitWithProvider(tt.args.provider, tt.args.ratio) + if !errors.Is(err, tt.wantErr) { t.Errorf("SetGoMemLimitWithProvider() error = %v, wantErr %v", err, tt.wantErr) return }