From 638a0778bb3b61ab1868864cd24c9dad15467dac Mon Sep 17 00:00:00 2001 From: jonyhy96 Date: Fri, 24 Apr 2020 10:50:00 +0800 Subject: [PATCH 1/5] fix: logic of mapEnv Signed-off-by: jonyhy96 --- config/config.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index 214beae6..3be8f0f5 100644 --- a/config/config.go +++ b/config/config.go @@ -112,17 +112,13 @@ func mapEnv(env []string) map[string]string { mapped := map[string]string{} for _, val := range env { - sep := strings.Index(val, "=") - - if sep > 0 { - key := val[0:sep] - value := val[sep+1:] - mapped[key] = value - } else { - fmt.Println("Bad environment: " + val) + keyValue := strings.Split(val, "=") + if len(keyValue) == 2 { + mapped[keyValue[0]] = keyValue[1] + continue } + fmt.Println("Bad environment: " + val) } - return mapped } From dcb9c5a99db208cb35dd51dabcedf5d9205de871 Mon Sep 17 00:00:00 2001 From: jonyhy96 Date: Fri, 24 Apr 2020 10:51:45 +0800 Subject: [PATCH 2/5] fix: return nil rather than empty slice when Process called Signed-off-by: jonyhy96 --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 3be8f0f5..04e2cf44 100644 --- a/config/config.go +++ b/config/config.go @@ -45,7 +45,7 @@ func (w WatchdogConfig) Process() (string, []string) { return parts[0], parts[1:] } - return parts[0], []string{} + return parts[0], nil } // New create config based upon environmental variables. From 58eaa19562e8d581245b6bc62d6eef257f5f5d4d Mon Sep 17 00:00:00 2001 From: jonyhy96 Date: Fri, 24 Apr 2020 10:54:38 +0800 Subject: [PATCH 3/5] fix: use strings.SplitN instead to prevent env values contains '=' Signed-off-by: jonyhy96 --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 04e2cf44..4737fc7e 100644 --- a/config/config.go +++ b/config/config.go @@ -112,7 +112,7 @@ func mapEnv(env []string) map[string]string { mapped := map[string]string{} for _, val := range env { - keyValue := strings.Split(val, "=") + keyValue := strings.SplitN(val, "=", 2) if len(keyValue) == 2 { mapped[keyValue[0]] = keyValue[1] continue From 0f7c83efd29efdc6aa363aeb37898db947d4fa67 Mon Sep 17 00:00:00 2001 From: jonyhy96 Date: Fri, 24 Apr 2020 11:18:27 +0800 Subject: [PATCH 4/5] fix: ignore empty value environment Signed-off-by: jonyhy96 --- config/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 4737fc7e..4a9fece6 100644 --- a/config/config.go +++ b/config/config.go @@ -101,7 +101,7 @@ func New(env []string) WatchdogConfig { MaxInflight: getInt(envMap, "max_inflight", 0), } - if val := envMap["mode"]; len(val) > 0 { + if val, exists := envMap["mode"]; exists { config.OperationalMode = WatchdogModeConst(val) } @@ -113,7 +113,7 @@ func mapEnv(env []string) map[string]string { for _, val := range env { keyValue := strings.SplitN(val, "=", 2) - if len(keyValue) == 2 { + if len(keyValue) == 2 && keyValue[1] != "" { mapped[keyValue[0]] = keyValue[1] continue } From 0a01fefeee1bd7f8bd8131701c45ef49af6b3765 Mon Sep 17 00:00:00 2001 From: jonyhy96 Date: Fri, 24 Apr 2020 11:29:46 +0800 Subject: [PATCH 5/5] test: add some tests Signed-off-by: jonyhy96 --- config/config_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/config/config_test.go b/config/config_test.go index 3b4b3725..b6b36d07 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "reflect" "testing" "time" ) @@ -347,3 +348,56 @@ func Test_NonParsableString_parseIntOrDurationValue(t *testing.T) { t.Error(fmt.Sprintf("want: %q got: %q", want, got)) } } + +func Test_mapEnv(t *testing.T) { + type args struct { + env []string + } + tests := []struct { + name string + args args + want map[string]string + }{ + { + name: "change env to map", + args: args{ + env: []string{ + "FOO=BAR", + }, + }, + want: map[string]string{ + "FOO": "BAR", + }, + }, + { + name: "keep '=' of environment value when contains '='", + args: args{ + env: []string{ + "FOO=BAR=BAZ", + }, + }, + want: map[string]string{ + "FOO": "BAR=BAZ", + }, + }, + { + name: "ignore empty value environment", + args: args{ + env: []string{ + "FOO=", + "BAR=BAZ", + }, + }, + want: map[string]string{ + "BAR": "BAZ", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := mapEnv(tt.args.env); !reflect.DeepEqual(got, tt.want) { + t.Errorf("mapEnv() = %v, want %v", got, tt.want) + } + }) + } +}