Skip to content

Commit bae8e28

Browse files
committed
add test for env parser
1 parent 1ccb26d commit bae8e28

File tree

3 files changed

+146
-7
lines changed

3 files changed

+146
-7
lines changed

pkg/epp/scheduling/filter_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,6 @@ func TestFilterFunc(t *testing.T) {
430430
}
431431
}
432432

433-
434-
435433
// TestLoRASoftAffinityDistribution tests that the loRASoftAffinityFilter function
436434
// properly distributes requests according to the loraAffinityThreshold
437435
func TestLoRASoftAffinityDistribution(t *testing.T) {
@@ -456,7 +454,6 @@ func TestLoRASoftAffinityDistribution(t *testing.T) {
456454
config.LoraAffinityThreshold = originalThreshold
457455
}()
458456

459-
460457
// Create a test request and pods
461458
req := &LLMRequest{
462459
Model: testAffinityModel,

pkg/epp/scheduling/scheduler.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/log"
2727
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
2828
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
29+
envutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/env"
2930
errutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/error"
3031
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
31-
envutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/env"
3232
)
3333

3434
// Config holds all the configuration values for the scheduler
@@ -130,8 +130,6 @@ var (
130130
}
131131
)
132132

133-
134-
135133
func NewScheduler(datastore datastore.Datastore) *Scheduler {
136134
return &Scheduler{
137135
datastore: datastore,
@@ -150,7 +148,7 @@ func (s *Scheduler) Schedule(ctx context.Context, req *LLMRequest) (targetPod ba
150148

151149
podMetrics := s.datastore.PodGetAll()
152150
logger.V(logutil.DEBUG).Info(fmt.Sprintf("Scheduling a request. Metrics: %+v", podMetrics))
153-
151+
154152
pods, err := s.filter.Filter(logger, req, podMetrics)
155153
if err != nil || len(pods) == 0 {
156154
return nil, fmt.Errorf("failed to apply filter, resulted %v pods, this should never happen: %w", len(pods), err)

pkg/epp/util/env/env_test.go

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package env
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/go-logr/logr/testr"
8+
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
9+
)
10+
11+
func TestGetEnvFloat(t *testing.T) {
12+
logger := testr.New(t)
13+
14+
tests := []struct {
15+
name string
16+
key string
17+
value string
18+
defaultVal float64
19+
expected float64
20+
setup func()
21+
teardown func()
22+
}{
23+
{
24+
name: "env variable exists and is valid",
25+
key: "TEST_FLOAT",
26+
value: "123.456",
27+
defaultVal: 0.0,
28+
expected: 123.456,
29+
setup: func() {
30+
os.Setenv("TEST_FLOAT", "123.456")
31+
},
32+
teardown: func() {
33+
os.Unsetenv("TEST_FLOAT")
34+
},
35+
},
36+
{
37+
name: "env variable exists but is invalid",
38+
key: "TEST_FLOAT",
39+
value: "invalid",
40+
defaultVal: 99.9,
41+
expected: 99.9,
42+
setup: func() {
43+
os.Setenv("TEST_FLOAT", "invalid")
44+
},
45+
teardown: func() {
46+
os.Unsetenv("TEST_FLOAT")
47+
},
48+
},
49+
{
50+
name: "env variable does not exist",
51+
key: "TEST_FLOAT_MISSING",
52+
defaultVal: 42.42,
53+
expected: 42.42,
54+
setup: func() {},
55+
teardown: func() {},
56+
},
57+
}
58+
59+
for _, tc := range tests {
60+
t.Run(tc.name, func(t *testing.T) {
61+
tc.setup()
62+
defer tc.teardown()
63+
64+
result := GetEnvFloat(tc.key, tc.defaultVal, logger.V(logutil.VERBOSE))
65+
if result != tc.expected {
66+
t.Errorf("GetEnvFloat(%s, %f) = %f, expected %f", tc.key, tc.defaultVal, result, tc.expected)
67+
}
68+
})
69+
}
70+
}
71+
72+
func TestGetEnvInt(t *testing.T) {
73+
logger := testr.New(t)
74+
75+
tests := []struct {
76+
name string
77+
key string
78+
value string
79+
defaultVal int
80+
expected int
81+
setup func()
82+
teardown func()
83+
}{
84+
{
85+
name: "env variable exists and is valid",
86+
key: "TEST_INT",
87+
value: "123",
88+
defaultVal: 0,
89+
expected: 123,
90+
setup: func() {
91+
os.Setenv("TEST_INT", "123")
92+
},
93+
teardown: func() {
94+
os.Unsetenv("TEST_INT")
95+
},
96+
},
97+
{
98+
name: "env variable exists but is invalid",
99+
key: "TEST_INT",
100+
value: "invalid",
101+
defaultVal: 99,
102+
expected: 99,
103+
setup: func() {
104+
os.Setenv("TEST_INT", "invalid")
105+
},
106+
teardown: func() {
107+
os.Unsetenv("TEST_INT")
108+
},
109+
},
110+
{
111+
name: "env variable does not exist",
112+
key: "TEST_INT_MISSING",
113+
defaultVal: 42,
114+
expected: 42,
115+
setup: func() {},
116+
teardown: func() {},
117+
},
118+
{
119+
name: "env variable is empty string",
120+
key: "TEST_INT_EMPTY",
121+
value: "",
122+
defaultVal: 77,
123+
expected: 77,
124+
setup: func() {
125+
os.Setenv("TEST_INT_EMPTY", "")
126+
},
127+
teardown: func() {
128+
os.Unsetenv("TEST_INT_EMPTY")
129+
},
130+
},
131+
}
132+
133+
for _, tc := range tests {
134+
t.Run(tc.name, func(t *testing.T) {
135+
tc.setup()
136+
defer tc.teardown()
137+
138+
result := GetEnvInt(tc.key, tc.defaultVal, logger.V(logutil.VERBOSE))
139+
if result != tc.expected {
140+
t.Errorf("GetEnvInt(%s, %d) = %d, expected %d", tc.key, tc.defaultVal, result, tc.expected)
141+
}
142+
})
143+
}
144+
}

0 commit comments

Comments
 (0)