Skip to content

Commit a54c51b

Browse files
authored
added GetEnvBool function and unit-tests (#916)
Signed-off-by: Nir Rozenbaum <[email protected]>
1 parent ede62e6 commit a54c51b

File tree

3 files changed

+79
-18
lines changed

3 files changed

+79
-18
lines changed

cmd/epp/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ var (
111111
setupLog = ctrl.Log.WithName("setup")
112112

113113
// Environment variables
114-
schedulerV2 = envutil.GetEnvString("EXPERIMENTAL_USE_SCHEDULER_V2", "false", setupLog)
115-
prefixCacheScheduling = envutil.GetEnvString("ENABLE_PREFIX_CACHE_SCHEDULING", "false", setupLog)
114+
schedulerV2 = envutil.GetEnvBool("EXPERIMENTAL_USE_SCHEDULER_V2", false, setupLog)
115+
prefixCacheScheduling = envutil.GetEnvBool("ENABLE_PREFIX_CACHE_SCHEDULING", false, setupLog)
116116
)
117117

118118
func loadPrefixCacheConfig() prefix.Config {
@@ -203,7 +203,7 @@ func run() error {
203203

204204
// --- Initialize Core EPP Components ---
205205
scheduler := scheduling.NewScheduler(datastore)
206-
if schedulerV2 == "true" {
206+
if schedulerV2 {
207207
queueScorerWeight := envutil.GetEnvInt("QUEUE_SCORE_WEIGHT", scorer.DefaultQueueScorerWeight, setupLog)
208208
kvCacheScorerWeight := envutil.GetEnvInt("KV_CACHE_SCORE_WEIGHT", scorer.DefaultKVCacheScorerWeight, setupLog)
209209

@@ -213,7 +213,7 @@ func run() error {
213213
framework.NewWeightedScorer(&scorer.KVCacheScorer{}, kvCacheScorerWeight)).
214214
WithPicker(picker.NewMaxScorePicker())
215215

216-
if prefixCacheScheduling == "true" {
216+
if prefixCacheScheduling {
217217
prefixScorerWeight := envutil.GetEnvInt("PREFIX_CACHE_SCORE_WEIGHT", prefix.DefaultScorerWeight, setupLog)
218218
if err := schedulerProfile.AddPlugins(framework.NewWeightedScorer(prefix.New(loadPrefixCacheConfig()), prefixScorerWeight)); err != nil {
219219
setupLog.Error(err, "Failed to register scheduler plugins")

pkg/epp/util/env/env.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ func GetEnvDuration(key string, defaultVal time.Duration, logger logr.Logger) ti
6262
return getEnvWithParser(key, defaultVal, time.ParseDuration, logger)
6363
}
6464

65+
// GetEnvBool gets a boolean from an environment variable with a default value.
66+
func GetEnvBool(key string, defaultVal bool, logger logr.Logger) bool {
67+
return getEnvWithParser(key, defaultVal, strconv.ParseBool, logger)
68+
}
69+
6570
// GetEnvString gets a string from an environment variable with a default value.
6671
func GetEnvString(key string, defaultVal string, logger logr.Logger) string {
6772
parser := func(s string) (string, error) { return s, nil }

pkg/epp/util/env/env_test.go

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ func TestGetEnvFloat(t *testing.T) {
1515
tests := []struct {
1616
name string
1717
key string
18-
value string
1918
defaultVal float64
2019
expected float64
2120
setup func()
@@ -24,7 +23,6 @@ func TestGetEnvFloat(t *testing.T) {
2423
{
2524
name: "env variable exists and is valid",
2625
key: "TEST_FLOAT",
27-
value: "123.456",
2826
defaultVal: 0.0,
2927
expected: 123.456,
3028
setup: func() {
@@ -37,7 +35,6 @@ func TestGetEnvFloat(t *testing.T) {
3735
{
3836
name: "env variable exists but is invalid",
3937
key: "TEST_FLOAT",
40-
value: "invalid",
4138
defaultVal: 99.9,
4239
expected: 99.9,
4340
setup: func() {
@@ -76,7 +73,6 @@ func TestGetEnvDuration(t *testing.T) {
7673
tests := []struct {
7774
name string
7875
key string
79-
value string
8076
defaultVal time.Duration
8177
expected time.Duration
8278
setup func()
@@ -85,7 +81,6 @@ func TestGetEnvDuration(t *testing.T) {
8581
{
8682
name: "env variable exists and is valid",
8783
key: "TEST_DURATION",
88-
value: "1h30m",
8984
defaultVal: 0,
9085
expected: 1*time.Hour + 30*time.Minute,
9186
setup: func() {
@@ -98,7 +93,6 @@ func TestGetEnvDuration(t *testing.T) {
9893
{
9994
name: "env variable exists but is invalid",
10095
key: "TEST_DURATION",
101-
value: "invalid-duration",
10296
defaultVal: 5 * time.Minute,
10397
expected: 5 * time.Minute,
10498
setup: func() {
@@ -119,7 +113,6 @@ func TestGetEnvDuration(t *testing.T) {
119113
{
120114
name: "env variable is empty string",
121115
key: "TEST_DURATION_EMPTY",
122-
value: "",
123116
defaultVal: 1 * time.Millisecond,
124117
expected: 1 * time.Millisecond,
125118
setup: func() {
@@ -150,7 +143,6 @@ func TestGetEnvInt(t *testing.T) {
150143
tests := []struct {
151144
name string
152145
key string
153-
value string
154146
defaultVal int
155147
expected int
156148
setup func()
@@ -159,7 +151,6 @@ func TestGetEnvInt(t *testing.T) {
159151
{
160152
name: "env variable exists and is valid",
161153
key: "TEST_INT",
162-
value: "123",
163154
defaultVal: 0,
164155
expected: 123,
165156
setup: func() {
@@ -172,7 +163,6 @@ func TestGetEnvInt(t *testing.T) {
172163
{
173164
name: "env variable exists but is invalid",
174165
key: "TEST_INT",
175-
value: "invalid",
176166
defaultVal: 99,
177167
expected: 99,
178168
setup: func() {
@@ -193,7 +183,6 @@ func TestGetEnvInt(t *testing.T) {
193183
{
194184
name: "env variable is empty string",
195185
key: "TEST_INT_EMPTY",
196-
value: "",
197186
defaultVal: 77,
198187
expected: 77,
199188
setup: func() {
@@ -218,13 +207,82 @@ func TestGetEnvInt(t *testing.T) {
218207
}
219208
}
220209

210+
func TestGetEnvBool(t *testing.T) {
211+
logger := testr.New(t)
212+
213+
tests := []struct {
214+
name string
215+
key string
216+
defaultVal bool
217+
expected bool
218+
setup func()
219+
teardown func()
220+
}{
221+
{
222+
name: "env variable exists and is valid",
223+
key: "TEST_BOOL",
224+
defaultVal: false,
225+
expected: true,
226+
setup: func() {
227+
os.Setenv("TEST_BOOL", "true")
228+
},
229+
teardown: func() {
230+
os.Unsetenv("TEST_BOOL")
231+
},
232+
},
233+
{
234+
name: "env variable exists but is invalid",
235+
key: "TEST_BOOL",
236+
defaultVal: false,
237+
expected: false,
238+
setup: func() {
239+
os.Setenv("TEST_BOOL", "invalid")
240+
},
241+
teardown: func() {
242+
os.Unsetenv("TEST_BOOL")
243+
},
244+
},
245+
{
246+
name: "env variable does not exist",
247+
key: "TEST_BOOL_MISSING",
248+
defaultVal: false,
249+
expected: false,
250+
setup: func() {},
251+
teardown: func() {},
252+
},
253+
{
254+
name: "env variable is empty string",
255+
key: "TEST_BOOL_EMPTY",
256+
defaultVal: false,
257+
expected: false,
258+
setup: func() {
259+
os.Setenv("TEST_BOOL_EMPTY", "")
260+
},
261+
teardown: func() {
262+
os.Unsetenv("TEST_BOOL_EMPTY")
263+
},
264+
},
265+
}
266+
267+
for _, tc := range tests {
268+
t.Run(tc.name, func(t *testing.T) {
269+
tc.setup()
270+
defer tc.teardown()
271+
272+
result := GetEnvBool(tc.key, tc.defaultVal, logger.V(logutil.VERBOSE))
273+
if result != tc.expected {
274+
t.Errorf("GetEnvBool(%s, %v) = %v, expected %v", tc.key, tc.defaultVal, result, tc.expected)
275+
}
276+
})
277+
}
278+
}
279+
221280
func TestGetEnvString(t *testing.T) {
222281
logger := testr.New(t)
223282

224283
tests := []struct {
225284
name string
226285
key string
227-
value string
228286
defaultVal string
229287
expected string
230288
setup func()
@@ -233,7 +291,6 @@ func TestGetEnvString(t *testing.T) {
233291
{
234292
name: "env variable exists and is valid",
235293
key: "TEST_STR",
236-
value: "123",
237294
defaultVal: "default",
238295
expected: "123",
239296
setup: func() {
@@ -254,7 +311,6 @@ func TestGetEnvString(t *testing.T) {
254311
{
255312
name: "env variable is empty string",
256313
key: "TEST_STR_EMPTY",
257-
value: "",
258314
defaultVal: "default",
259315
expected: "",
260316
setup: func() {

0 commit comments

Comments
 (0)