Skip to content

Commit

Permalink
Allow Int values and parse as Seconds for Durations
Browse files Browse the repository at this point in the history
The timeout values could be accidentally set as ints by a user
rather than golang durations. This meant that "5" gets parsed
to 5ms not 5s. Now we try and parse non duration strings as
seconds.

This has been tested by creating a function with the old binary,
setting the timeouts for read, write and exec to "5" and 5.
These are interpreted as 0s.

New binary swapped in and re-deployed, this then worked as our
waits were less than the 5 seconds, when expanded over 5s (the waits)
then the fn was stopped

Signed-off-by: Alistair Hey <[email protected]>
  • Loading branch information
Waterdrips authored and alexellis committed Jan 8, 2020
1 parent 325228e commit dd56bb9
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 4 deletions.
20 changes: 16 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,26 @@ func mapEnv(env []string) map[string]string {
}

func getDuration(env map[string]string, key string, defaultValue time.Duration) time.Duration {
result := defaultValue
if val, exists := env[key]; exists {
parsed, _ := time.ParseDuration(val)
result = parsed
return parseIntOrDurationValue(val, defaultValue)
}

return defaultValue
}

func parseIntOrDurationValue(val string, fallback time.Duration) time.Duration {
if len(val) > 0 {
parsedVal, parseErr := strconv.Atoi(val)
if parseErr == nil && parsedVal >= 0 {
return time.Duration(parsedVal) * time.Second
}
}

return result
duration, durationErr := time.ParseDuration(val)
if durationErr != nil {
return fallback
}
return duration
}

func getInt(env map[string]string, key string, defaultValue int) int {
Expand Down
85 changes: 85 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,88 @@ func Test_Timeouts(t *testing.T) {
}

}

func Test_TestNonDurationValue_getDuration(t *testing.T) {
want := 10 * time.Second
env := map[string]string{"time": "10"}
got := getDuration(env, "time", 5*time.Second)

if want != got {
t.Error(fmt.Sprintf("want: %q got: %q", want, got))
}
}

func Test_TestDurationValue_getDuration(t *testing.T) {
want := 10 * time.Second
env := map[string]string{"time": "10s"}
got := getDuration(env, "time", 5*time.Second)

if want != got {
t.Error(fmt.Sprintf("want: %q got: %q", want, got))
}
}
func Test_TestNonParsableValue_getDuration(t *testing.T) {
want := 5 * time.Second
env := map[string]string{"time": "this is bad"}
got := getDuration(env, "time", 5*time.Second)

if want != got {
t.Error(fmt.Sprintf("want: %q got: %q", want, got))
}
}

func Test_TestMissingMapValue_getDuration(t *testing.T) {
want := 5 * time.Second
env := map[string]string{"time_is_missing": "10"}
got := getDuration(env, "time", 5*time.Second)

if want != got {
t.Error(fmt.Sprintf("want: %q got: %q", want, got))
}
}

func Test_IntAsString_parseIntOrDurationValue(t *testing.T) {
want := 10 * time.Second

got := parseIntOrDurationValue("10", 5*time.Second)
if want != got {
t.Error(fmt.Sprintf("want: %q got: %q", want, got))
}
}

func Test_Duration_parseIntOrDurationValue(t *testing.T) {
want := 10 * time.Second

got := parseIntOrDurationValue("10s", 5*time.Second)
if want != got {
t.Error(fmt.Sprintf("want: %q got: %q", want, got))
}

}

func Test_EmptyString_parseIntOrDurationValue(t *testing.T) {
want := 5 * time.Second

got := parseIntOrDurationValue("", 5*time.Second)
if want != got {
t.Error(fmt.Sprintf("want: %q got: %q", want, got))
}
}

func Test_ZeroAsString_parseIntOrDurationValue(t *testing.T) {
want := 0 * time.Second

got := parseIntOrDurationValue("0", 5*time.Second)
if want != got {
t.Error(fmt.Sprintf("want: %q got: %q", want, got))
}
}

func Test_NonParsableString_parseIntOrDurationValue(t *testing.T) {
want := 5 * time.Second

got := parseIntOrDurationValue("this is not good", 5*time.Second)
if want != got {
t.Error(fmt.Sprintf("want: %q got: %q", want, got))
}
}

0 comments on commit dd56bb9

Please sign in to comment.