From 211e57b9152278f6a906ed5273153e51078758b1 Mon Sep 17 00:00:00 2001 From: Taction Date: Wed, 1 Mar 2023 00:59:24 +0800 Subject: [PATCH] Fix unexpected behaviour in dapr list (#1244) * Fix args parse. Signed-off-by: zhangchao * fix lint Signed-off-by: zhangchao * add e2e test for list Signed-off-by: zhangchao * fix e2e exit to 0 Signed-off-by: zhangchao * run dapr run in goroutine Signed-off-by: zhangchao --------- Signed-off-by: zhangchao --- pkg/standalone/list.go | 14 +++++++++++--- tests/e2e/standalone/list_test.go | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/pkg/standalone/list.go b/pkg/standalone/list.go index 4f657ee1..102e9b97 100644 --- a/pkg/standalone/list.go +++ b/pkg/standalone/list.go @@ -21,10 +21,11 @@ import ( ps "github.com/mitchellh/go-ps" process "github.com/shirou/gopsutil/process" + "github.com/dapr/dapr/pkg/runtime" + "github.com/dapr/cli/pkg/age" "github.com/dapr/cli/pkg/metadata" "github.com/dapr/cli/utils" - "github.com/dapr/dapr/pkg/runtime" ) // ListOutput represents the application ID, application port and creation time. @@ -77,9 +78,16 @@ func List() ([]ListOutput, error) { continue } + // Parse command line arguments, example format for cmdLine `daprd --flag1 value1 --enable-flag2 --flag3 value3`. argumentsMap := make(map[string]string) - for i := 1; i < len(cmdLineItems)-1; i += 2 { - argumentsMap[cmdLineItems[i]] = cmdLineItems[i+1] + for i := 1; i < len(cmdLineItems)-1; { + if !strings.HasPrefix(cmdLineItems[i+1], "--") { + argumentsMap[cmdLineItems[i]] = cmdLineItems[i+1] + i += 2 + } else { + argumentsMap[cmdLineItems[i]] = "" + i++ + } } httpPort := getIntArg(argumentsMap, "--dapr-http-port", runtime.DefaultDaprHTTPPort) diff --git a/tests/e2e/standalone/list_test.go b/tests/e2e/standalone/list_test.go index 86876626..2071dad1 100644 --- a/tests/e2e/standalone/list_test.go +++ b/tests/e2e/standalone/list_test.go @@ -25,6 +25,7 @@ import ( "runtime" "strings" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -97,6 +98,27 @@ func TestStandaloneList(t *testing.T) { cmd.Process.Kill() }) + t.Run("daprd instance started by run in list", func(t *testing.T) { + go func() { + // starts dapr run in a goroutine + runoutput, err := cmdRun("", "--app-id", "dapr_e2e_list", "--dapr-http-port", "3555", "--dapr-grpc-port", "4555", "--app-port", "0", "--enable-app-health-check", "--", "bash", "-c", "sleep 15; exit 0") + t.Log(runoutput) + require.NoError(t, err, "run failed") + // daprd starts and sleep for 50s, this ensures daprd started by `dapr run ...` is stopped + time.Sleep(15 * time.Second) + assert.Contains(t, runoutput, "Exited Dapr successfully") + }() + + // wait for daprd to start + time.Sleep(time.Second) + output, err := cmdList("") + t.Log(output) + require.NoError(t, err, "dapr list failed with dapr run instance") + listOutputCheck(t, output, true) + // sleep to wait dapr run exit, in case have effect on other tests + time.Sleep(15 * time.Second) + }) + t.Run("dashboard instance should not be listed", func(t *testing.T) { // TODO: remove this after figuring out the fix. // The issue is that the dashboard instance does not gets killed when the app is stopped.