Skip to content

Commit 788139d

Browse files
committed
Simpler and safer spawn of dapr comands in tests
Signed-off-by: Albert Callarisa <albert@diagrid.io>
1 parent b21c8c6 commit 788139d

File tree

8 files changed

+30
-45
lines changed

8 files changed

+30
-45
lines changed

tests/e2e/spawn/spawn.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ func Command(command string, arguments ...string) (string, error) {
8080
// CommandExecWithContext runs a command with its arguments, kills the command after context is done
8181
// and returns the combined stdout, stderr or the error.
8282
func CommandExecWithContext(ctx context.Context, command string, arguments ...string) (string, error) {
83+
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
84+
defer cancel()
8385
cmd := exec.CommandContext(ctx, command, arguments...)
8486
var b bytes.Buffer
8587
cmd.Stdout = &b
@@ -90,27 +92,21 @@ func CommandExecWithContext(ctx context.Context, command string, arguments ...st
9092
return "", fmt.Errorf("error starting command : %w", err)
9193
}
9294

93-
waitErrChan := make(chan error, 1)
94-
go func(errChan chan error) {
95-
waitErr := cmd.Wait()
96-
if waitErr != nil {
97-
fmt.Printf("error waiting for command : %s\n", waitErr)
98-
}
99-
waitErrChan <- waitErr
100-
}(waitErrChan)
101-
ctx, cancel := context.WithTimeout(ctx, time.Second*20)
102-
defer cancel()
103-
<-ctx.Done()
104-
if cmd.ProcessState == nil || !cmd.ProcessState.Exited() {
105-
cmd.Process.Signal(syscall.SIGTERM)
106-
time.Sleep(10 * time.Second)
107-
if cmd.ProcessState == nil || !cmd.ProcessState.Exited() {
108-
err = cmd.Process.Kill()
109-
if err != nil {
110-
return b.String(), fmt.Errorf("error killing command : %w", err)
95+
// Send SIGTERM after 20 seconds for graceful shutdown
96+
go func() {
97+
select {
98+
case <-ctx.Done():
99+
// Do nothing, it's already stopped
100+
case <-time.After(20 * time.Second):
101+
if cmd.ProcessState == nil || !cmd.ProcessState.Exited() {
102+
cmd.Process.Signal(syscall.SIGTERM)
111103
}
112104
}
113-
}
105+
}()
114106

115-
return b.String(), <-waitErrChan
107+
err = cmd.Wait()
108+
if err != nil {
109+
return b.String(), fmt.Errorf("error waiting for command : %w", err)
110+
}
111+
return b.String(), nil
116112
}

tests/e2e/standalone/run_template_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"path/filepath"
2424
"strings"
2525
"testing"
26-
"time"
2726

2827
"github.com/stretchr/testify/assert"
2928
"github.com/stretchr/testify/require"
@@ -52,7 +51,6 @@ func TestRunWithTemplateFile(t *testing.T) {
5251
t.Cleanup(func() {
5352
// assumption in the test is that there is only one set of app and daprd logs in the logs directory.
5453
cleanUpLogs()
55-
waitAppsToBeStopped()
5654
})
5755
args := []string{
5856
"-f", runFilePath,
@@ -99,7 +97,6 @@ func TestRunWithTemplateFile(t *testing.T) {
9997
t.Cleanup(func() {
10098
// assumption in the test is that there is only one set of app and daprd logs in the logs directory.
10199
cleanUpLogs()
102-
waitAppsToBeStopped()
103100
})
104101
args := []string{
105102
"-f", runFilePath,
@@ -153,7 +150,6 @@ func TestRunWithTemplateFile(t *testing.T) {
153150
t.Cleanup(func() {
154151
// assumption in the test is that there is only one set of app and daprd logs in the logs directory.
155152
cleanUpLogs()
156-
waitAppsToBeStopped()
157153
})
158154
args := []string{
159155
"-f", runFilePath,
@@ -201,7 +197,6 @@ func TestRunWithTemplateFile(t *testing.T) {
201197
t.Cleanup(func() {
202198
// assumption in the test is that there is only one set of app and daprd logs in the logs directory.
203199
cleanUpLogs()
204-
waitAppsToBeStopped()
205200
})
206201
args := []string{
207202
"-f", runFilePath,
@@ -224,7 +219,7 @@ func TestRunWithTemplateFile(t *testing.T) {
224219
appID: "processor",
225220
baseLogDirPath: "../../apps/processor/.dapr/logs",
226221
appLogContents: []string{
227-
"Starting server in port 9086...",
222+
"Starting server in port 9081...",
228223
"termination signal received: shutting down",
229224
},
230225
daprdLogContent: []string{
@@ -250,7 +245,6 @@ func TestRunWithTemplateFile(t *testing.T) {
250245
t.Cleanup(func() {
251246
// assumption in the test is that there is only one set of app and daprd logs in the logs directory.
252247
cleanUpLogs()
253-
waitAppsToBeStopped()
254248
})
255249
args := []string{
256250
"-f", runFilePath,
@@ -269,7 +263,7 @@ func TestRunWithTemplateFile(t *testing.T) {
269263
appID: "processor",
270264
baseLogDirPath: "../../apps/processor/.dapr/logs",
271265
appLogContents: []string{
272-
"Starting server in port 9084...",
266+
"Starting server in port 9081...",
273267
"termination signal received: shutting down",
274268
},
275269
daprdLogContent: []string{
@@ -296,7 +290,6 @@ func TestRunWithTemplateFile(t *testing.T) {
296290
t.Cleanup(func() {
297291
// assumption in the test is that there is only one set of app and daprd logs in the logs directory.
298292
cleanUpLogs()
299-
waitAppsToBeStopped()
300293
})
301294
args := []string{
302295
"-f", runFilePath,
@@ -408,7 +401,3 @@ func lookUpFileFullName(dirPath, partialFilename string) (string, error) {
408401
}
409402
return "", fmt.Errorf("failed to find file with partial name %s in directory %s", partialFilename, dirPath)
410403
}
411-
412-
func waitAppsToBeStopped() {
413-
time.Sleep(15 * time.Second)
414-
}

tests/e2e/testdata/run-template-files/app_output_to_only_console.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
version: 1
22
apps:
33
- appDirPath: ../../../apps/processor/
4-
appPort: 9082
4+
appPort: 9081
55
daprHTTPPort: 3510
66
command: ["go","run", "app.go"]
77
appLogDestination: console
88
env:
9-
APP_PORT: 9082
9+
APP_PORT: 9081
1010
- appID: emit-metrics
1111
appDirPath: ../../../apps/emit-metrics/
1212
daprHTTPPort: 3511

tests/e2e/testdata/run-template-files/dapr.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ version: 1
22
name: test_dapr_template
33
apps:
44
- appDirPath: ../../../apps/processor/
5-
appPort: 9083
5+
appPort: 9081
66
daprHTTPPort: 3510
77
command: ["go","run", "app.go"]
88
appLogDestination: file
99
env:
10-
APP_PORT: 9083
10+
APP_PORT: 9081
1111
- appID: emit-metrics
1212
appDirPath: ../../../apps/emit-metrics/
1313
daprHTTPPort: 3511

tests/e2e/testdata/run-template-files/empty_app_command.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
version: 1
22
apps:
33
- appDirPath: ../../../apps/processor/
4-
appPort: 9084
4+
appPort: 9081
55
daprHTTPPort: 3510
66
command: ["go","run", "app.go"]
77
appLogDestination: file
88
env:
9-
APP_PORT: 9084
9+
APP_PORT: 9081
1010
- appID: emit-metrics
1111
appDirPath: ../../../apps/emit-metrics/
1212
daprHTTPPort: 3511

tests/e2e/testdata/run-template-files/env_var_not_set_dapr.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
version: 1
22
apps:
33
- appDirPath: ../../../apps/processor/
4-
appPort: 9085
4+
appPort: 9081
55
daprHTTPPort: 3510
66
appLogDestination: file
77
command: ["go","run", "app.go"]
88
env:
9-
APP_PORT: 9085
9+
APP_PORT: 9081
1010
- appID: emit-metrics
1111
appDirPath: ../../../apps/emit-metrics/
1212
daprHTTPPort: 3511 # required env var DAPR_HOST_ADD not set in the yaml file

tests/e2e/testdata/run-template-files/no_app_command.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ common:
66
configFilePath: appconfig.yaml
77
apps:
88
- appDirPath: ../../../apps/processor/
9-
appPort: 9086
9+
appPort: 9081
1010
daprHTTPPort: 3510
1111
command: ["go","run", "app.go"]
1212
appLogDestination: file
1313
env:
14-
APP_PORT: 9086
14+
APP_PORT: 9081
1515
- appID: emit-metrics
1616
appDirPath: ../../../apps/emit-metrics/
1717
daprHTTPPort: 3511

tests/e2e/testdata/run-template-files/wrong_emit_metrics_app_dapr.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
version: 1
22
apps:
33
- appDirPath: ../../../apps/processor/
4-
appPort: 9087
4+
appPort: 9081
55
daprHTTPPort: 3510
66
command: ["go","run", "app.go"]
77
appLogDestination: file
88
env:
9-
APP_PORT: 9087
9+
APP_PORT: 9081
1010
- appID: emit-metrics
1111
appDirPath: ../../../apps/emit-metrics/
1212
daprHTTPPort: 3511

0 commit comments

Comments
 (0)