|
| 1 | +package v1alpha1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "k8s.io/apimachinery/pkg/api/resource" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAdjustMemory(t *testing.T) { |
| 11 | + tests := map[string]struct { |
| 12 | + beforeMemoryLimit string |
| 13 | + adjustment BroomAdjustment |
| 14 | + afterMemoryLimit string |
| 15 | + }{ |
| 16 | + "add adjustment": { |
| 17 | + beforeMemoryLimit: "100Mi", |
| 18 | + adjustment: BroomAdjustment{Type: AddAdjustment, Value: "100Mi"}, |
| 19 | + afterMemoryLimit: "200Mi", |
| 20 | + }, |
| 21 | + "mul adjustment": { |
| 22 | + beforeMemoryLimit: "100Mi", |
| 23 | + adjustment: BroomAdjustment{Type: MulAdjustment, Value: "2"}, |
| 24 | + afterMemoryLimit: "200Mi", |
| 25 | + }, |
| 26 | + "max limit reached with add adjustment": { |
| 27 | + beforeMemoryLimit: "100Mi", |
| 28 | + adjustment: BroomAdjustment{Type: AddAdjustment, Value: "100Mi", MaxLimit: resource.MustParse("150Mi")}, |
| 29 | + afterMemoryLimit: "150Mi", |
| 30 | + }, |
| 31 | + "max limit reached with mul adjustment": { |
| 32 | + beforeMemoryLimit: "100Mi", |
| 33 | + adjustment: BroomAdjustment{Type: MulAdjustment, Value: "2", MaxLimit: resource.MustParse("150Mi")}, |
| 34 | + afterMemoryLimit: "150Mi", |
| 35 | + }, |
| 36 | + "already at max limit": { |
| 37 | + beforeMemoryLimit: "100Mi", |
| 38 | + adjustment: BroomAdjustment{Type: AddAdjustment, Value: "100Mi", MaxLimit: resource.MustParse("100Mi")}, |
| 39 | + afterMemoryLimit: "100Mi", |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for name, test := range tests { |
| 44 | + test := test |
| 45 | + t.Run(name, func(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + m := resource.MustParse(test.beforeMemoryLimit) |
| 48 | + adj := test.adjustment |
| 49 | + err := adj.AdjustMemory(&m) |
| 50 | + assert.Nil(t, err) |
| 51 | + assert.True(t, m.Equal(resource.MustParse(test.afterMemoryLimit))) |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
0 commit comments