Skip to content

Commit 571f606

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

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

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)