Skip to content

Commit 211e57b

Browse files
authored
Fix unexpected behaviour in dapr list (#1244)
* Fix args parse. Signed-off-by: zhangchao <[email protected]> * fix lint Signed-off-by: zhangchao <[email protected]> * add e2e test for list Signed-off-by: zhangchao <[email protected]> * fix e2e exit to 0 Signed-off-by: zhangchao <[email protected]> * run dapr run in goroutine Signed-off-by: zhangchao <[email protected]> --------- Signed-off-by: zhangchao <[email protected]>
1 parent a6f8c76 commit 211e57b

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

pkg/standalone/list.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import (
2121
ps "github.com/mitchellh/go-ps"
2222
process "github.com/shirou/gopsutil/process"
2323

24+
"github.com/dapr/dapr/pkg/runtime"
25+
2426
"github.com/dapr/cli/pkg/age"
2527
"github.com/dapr/cli/pkg/metadata"
2628
"github.com/dapr/cli/utils"
27-
"github.com/dapr/dapr/pkg/runtime"
2829
)
2930

3031
// ListOutput represents the application ID, application port and creation time.
@@ -77,9 +78,16 @@ func List() ([]ListOutput, error) {
7778
continue
7879
}
7980

81+
// Parse command line arguments, example format for cmdLine `daprd --flag1 value1 --enable-flag2 --flag3 value3`.
8082
argumentsMap := make(map[string]string)
81-
for i := 1; i < len(cmdLineItems)-1; i += 2 {
82-
argumentsMap[cmdLineItems[i]] = cmdLineItems[i+1]
83+
for i := 1; i < len(cmdLineItems)-1; {
84+
if !strings.HasPrefix(cmdLineItems[i+1], "--") {
85+
argumentsMap[cmdLineItems[i]] = cmdLineItems[i+1]
86+
i += 2
87+
} else {
88+
argumentsMap[cmdLineItems[i]] = ""
89+
i++
90+
}
8391
}
8492

8593
httpPort := getIntArg(argumentsMap, "--dapr-http-port", runtime.DefaultDaprHTTPPort)

tests/e2e/standalone/list_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"runtime"
2626
"strings"
2727
"testing"
28+
"time"
2829

2930
"github.com/stretchr/testify/assert"
3031
"github.com/stretchr/testify/require"
@@ -97,6 +98,27 @@ func TestStandaloneList(t *testing.T) {
9798
cmd.Process.Kill()
9899
})
99100

101+
t.Run("daprd instance started by run in list", func(t *testing.T) {
102+
go func() {
103+
// starts dapr run in a goroutine
104+
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")
105+
t.Log(runoutput)
106+
require.NoError(t, err, "run failed")
107+
// daprd starts and sleep for 50s, this ensures daprd started by `dapr run ...` is stopped
108+
time.Sleep(15 * time.Second)
109+
assert.Contains(t, runoutput, "Exited Dapr successfully")
110+
}()
111+
112+
// wait for daprd to start
113+
time.Sleep(time.Second)
114+
output, err := cmdList("")
115+
t.Log(output)
116+
require.NoError(t, err, "dapr list failed with dapr run instance")
117+
listOutputCheck(t, output, true)
118+
// sleep to wait dapr run exit, in case have effect on other tests
119+
time.Sleep(15 * time.Second)
120+
})
121+
100122
t.Run("dashboard instance should not be listed", func(t *testing.T) {
101123
// TODO: remove this after figuring out the fix.
102124
// The issue is that the dashboard instance does not gets killed when the app is stopped.

0 commit comments

Comments
 (0)