Skip to content

Commit c0e5514

Browse files
committed
Add more validation and test on instance_limits
Signed-off-by: Justin Jung <[email protected]>
1 parent 025a93a commit c0e5514

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

pkg/configs/instance_limits.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ func (cfg *InstanceLimits) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix strin
2020
}
2121

2222
func (cfg *InstanceLimits) Validate(monitoredResources flagext.StringSliceCSV) error {
23+
if cfg.CPUUtilization > 1 || cfg.CPUUtilization < 0 {
24+
return errors.New("cpu_utilization must be between 0 and 1")
25+
}
26+
2327
if cfg.CPUUtilization > 0 && !strings.Contains(monitoredResources.String(), string(resource.CPU)) {
2428
return errors.New("monitored_resources config must include \"cpu\" as well")
2529
}
2630

31+
if cfg.HeapUtilization > 1 || cfg.HeapUtilization < 0 {
32+
return errors.New("heap_utilization must be between 0 and 1")
33+
}
34+
2735
if cfg.HeapUtilization > 0 && !strings.Contains(monitoredResources.String(), string(resource.Heap)) {
2836
return errors.New("monitored_resources config must include \"heap\" as well")
2937
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package configs
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func Test_Validate(t *testing.T) {
11+
for name, tc := range map[string]struct {
12+
instanceLimits InstanceLimits
13+
monitoredResources []string
14+
err error
15+
}{
16+
"correct config should pass validation": {
17+
instanceLimits: InstanceLimits{
18+
CPUUtilization: 0.5,
19+
HeapUtilization: 0.5,
20+
},
21+
monitoredResources: []string{"cpu", "heap"},
22+
err: nil,
23+
},
24+
"utilization config less than 0 should fail validation": {
25+
instanceLimits: InstanceLimits{
26+
CPUUtilization: -0.5,
27+
HeapUtilization: 0.5,
28+
},
29+
monitoredResources: []string{"cpu", "heap"},
30+
err: errors.New("cpu_utilization must be between 0 and 1"),
31+
},
32+
"utilization config greater than 1 should fail validation": {
33+
instanceLimits: InstanceLimits{
34+
CPUUtilization: 0.5,
35+
HeapUtilization: 1.5,
36+
},
37+
monitoredResources: []string{"cpu", "heap"},
38+
err: errors.New("heap_utilization must be between 0 and 1"),
39+
},
40+
"missing cpu in monitored_resources config should fail validation": {
41+
instanceLimits: InstanceLimits{
42+
CPUUtilization: 0.5,
43+
},
44+
monitoredResources: []string{"heap"},
45+
err: errors.New("monitored_resources config must include \"cpu\" as well"),
46+
},
47+
"missing heap in monitored_resources config should fail validation": {
48+
instanceLimits: InstanceLimits{
49+
HeapUtilization: 0.5,
50+
},
51+
monitoredResources: []string{"cpu"},
52+
err: errors.New("monitored_resources config must include \"heap\" as well"),
53+
},
54+
} {
55+
t.Run(name, func(t *testing.T) {
56+
err := tc.instanceLimits.Validate(tc.monitoredResources)
57+
if tc.err != nil {
58+
require.Errorf(t, err, tc.err.Error())
59+
} else {
60+
require.NoError(t, err)
61+
}
62+
})
63+
}
64+
}

0 commit comments

Comments
 (0)