Fix unexpected behaviour in dapr list (#1244)

* Fix args parse.

Signed-off-by: zhangchao <zchao9100@gmail.com>

* fix lint

Signed-off-by: zhangchao <zchao9100@gmail.com>

* add e2e test for list

Signed-off-by: zhangchao <zchao9100@gmail.com>

* fix e2e exit to 0

Signed-off-by: zhangchao <zchao9100@gmail.com>

* run dapr run in goroutine

Signed-off-by: zhangchao <zchao9100@gmail.com>

---------

Signed-off-by: zhangchao <zchao9100@gmail.com>
This commit is contained in:
Taction 2023-03-01 00:59:24 +08:00 committed by GitHub
parent a6f8c7694d
commit 211e57b915
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View File

@ -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)

View File

@ -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.