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