From 328a6f99f1fe92b65b8ade9dc14ade5838d3e21e Mon Sep 17 00:00:00 2001 From: Charlie Stanley Date: Fri, 2 Apr 2021 14:41:27 -0700 Subject: [PATCH] CLI run should detect that either daprd or the app has exited and clean up --- cmd/run.go | 37 +++++++++++++++++++++---- tests/e2e/standalone/standalone_test.go | 26 +++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 8ea8b8e1..7dce1196 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -111,6 +111,18 @@ var RunCmd = &cobra.Command{ os.Exit(1) } + go func() { + daprdErr := output.DaprCMD.Wait() + + if daprdErr != nil { + print.FailureStatusEvent(os.Stdout, "The daprd process exited with error code: %s", daprdErr.Error()) + + } else { + print.SuccessStatusEvent(os.Stdout, "Exited Dapr successfully") + } + sigCh <- os.Interrupt + }() + if appPort <= 0 { // If app does not listen to port, we can check for Dapr's sidecar health before starting the app. // Otherwise, it creates a deadlock. @@ -182,6 +194,17 @@ var RunCmd = &cobra.Command{ return } + go func() { + appErr := output.AppCMD.Wait() + + if appErr != nil { + print.FailureStatusEvent(os.Stdout, "The App process exited with error code: %s", appErr.Error()) + } else { + print.SuccessStatusEvent(os.Stdout, "Exited App successfully") + } + sigCh <- os.Interrupt + }() + appRunning <- true }() @@ -219,14 +242,16 @@ var RunCmd = &cobra.Command{ <-sigCh print.InfoStatusEvent(os.Stdout, "\nterminated signal received: shutting down") - err = output.DaprCMD.Process.Kill() - if err != nil { - print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error exiting Dapr: %s", err)) - } else { - print.SuccessStatusEvent(os.Stdout, "Exited Dapr successfully") + if output.DaprCMD.ProcessState == nil || !output.DaprCMD.ProcessState.Exited() { + err = output.DaprCMD.Process.Kill() + if err != nil { + print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error exiting Dapr: %s", err)) + } else { + print.SuccessStatusEvent(os.Stdout, "Exited Dapr successfully") + } } - if output.AppCMD != nil { + if output.AppCMD != nil && (output.AppCMD.ProcessState == nil || !output.AppCMD.ProcessState.Exited()) { err = output.AppCMD.Process.Kill() if err != nil { print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error exiting App: %s", err)) diff --git a/tests/e2e/standalone/standalone_test.go b/tests/e2e/standalone/standalone_test.go index da55ff2d..17f58ee7 100644 --- a/tests/e2e/standalone/standalone_test.go +++ b/tests/e2e/standalone/standalone_test.go @@ -41,6 +41,7 @@ func TestStandaloneInstall(t *testing.T) { phase func(*testing.T) }{ {"test install", testInstall}, + {"test run", testRun}, {"test uninstall", testUninstall}, } @@ -269,3 +270,28 @@ func testInstall(t *testing.T) { assert.Empty(t, configs) } + +func testRun(t *testing.T) { + daprPath := getDaprPath() + + output, err := spawn.Command(daprPath, "run", "--", "bash", "-c", "echo test") + t.Log(output) + require.NoError(t, err, "run failed") + assert.Contains(t, output, "Exited App successfully") + assert.Contains(t, output, "Exited Dapr successfully") + + output, err = spawn.Command(daprPath, "run", "--", "bash", "-c", "exit 1") + t.Log(output) + require.NoError(t, err, "run failed") + assert.Contains(t, output, "The App process exited with error code: exit status 1") + assert.Contains(t, output, "Exited Dapr successfully") + + + // Test that the CLI exits on a daprd shutdown. + output, err = spawn.Command(daprPath, "run", "--dapr-http-port", "9999", "--", "bash", "-c", "curl -v http://localhost:9999/v1.0/shutdown; sleep 10; exit 1") + t.Log(output) + require.NoError(t, err, "run failed") + assert.Contains(t, output, "Exited App successfully", "App should be shutdown before it has a chance to return non-zero") + assert.Contains(t, output, "Exited Dapr successfully") + +}