Skip to content

Commit

Permalink
Using kelseyhightower/envconfig after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Aug 21, 2020
1 parent 2c2ec1e commit 28396b7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 45 deletions.
16 changes: 0 additions & 16 deletions test/upgrade/prober/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"strings"
"text/template"

"github.com/wavesoftware/go-ensure"
Expand Down Expand Up @@ -120,17 +118,3 @@ func (p *prober) compileTemplate(templateName string, brokerUrl *apis.URL) strin
ensure.NoError(tmpl.Execute(&buff, data))
return buff.String()
}

func envflag(name string, defaultValue bool) bool {
if value, ok := os.LookupEnv(name); ok {
valid := []string{"true", "yes", "enable"}
value = strings.ToLower(value)
for _, candidate := range valid {
if value == candidate {
return true
}
}
return false
}
return defaultValue
}
17 changes: 8 additions & 9 deletions test/upgrade/prober/prober.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ import (
"testing"
"time"

"github.com/kelseyhightower/envconfig"
"github.com/wavesoftware/go-ensure"
"go.uber.org/zap"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/resources"
)

const (
ServingUseFlag = "E2E_UPGRADE_TESTS_SERVING_USE"
ServingScaleToZeroFlag = "E2E_UPGRADE_TESTS_SERVING_SCALE_TO_ZERO"
)

var (
// FIXME: Interval is set to 200 msec, as lower values will result in errors: knative/eventing#2357
// Interval = 10 * time.Millisecond
Expand Down Expand Up @@ -68,15 +64,18 @@ type ServingConfig struct {
}

func NewConfig(namespace string) *Config {
servingConfig := ServingConfig{
Use: false,
ScaleToZero: true,
}
err := envconfig.Process("e2e_upgrade_tests_serving", &servingConfig)
ensure.NoError(err)
return &Config{
Namespace: namespace,
Interval: Interval,
FinishedSleep: 5 * time.Second,
FailOnErrors: true,
Serving: ServingConfig{
Use: envflag(ServingUseFlag, false),
ScaleToZero: envflag(ServingScaleToZeroFlag, true),
},
Serving: servingConfig,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,38 @@ import (
"github.com/stretchr/testify/assert"
)

func TestEnvflag(t *testing.T) {
envname := "TestEnvflag"
type envValue uint

const (
EnvFalse envValue = iota
EnvTrue
EnvUnset
)

func TestNewConfig(t *testing.T) {
envname := "E2E_UPGRADE_TESTS_SERVING_USE"
suite := []struct {
env string
envSet bool
defaultValue bool
out bool
env envValue
out bool
}{
{"", false, true, true},
{"", true, true, false},
{"yes", true, false, true},
{"y", true, false, false},
{"y", true, true, false},
{"true", true, false, true},
{"true", true, true, true},
{"enable", true, false, true},
{"enabled", true, false, false},
{EnvFalse, false},
{EnvTrue, true},
{EnvUnset, false},
}
for _, s := range suite {
t.Run(fmt.Sprintf("env(%t)=%q,def=%t", s.envSet, s.env, s.defaultValue), func(t *testing.T) {
if s.envSet {
assert.NoError(t, os.Setenv(envname, s.env))
t.Run(fmt.Sprintf("env=%v,out=%t", s.env, s.out), func(t *testing.T) {
if s.env != EnvUnset {
val := "false"
if s.env == EnvTrue {
val = "true"
}
assert.NoError(t, os.Setenv(envname, val))
defer func() { assert.NoError(t, os.Unsetenv(envname)) }()
}
result := envflag(envname, s.defaultValue)
config := NewConfig("test-ns")

assert.Equal(t, s.out, result)
assert.Equal(t, s.out, config.Serving.Use)
assert.True(t, config.Serving.ScaleToZero)
})
}
}

0 comments on commit 28396b7

Please sign in to comment.