-
Notifications
You must be signed in to change notification settings - Fork 202
/
stop_with_run_template_test.go
132 lines (113 loc) · 4.61 KB
/
stop_with_run_template_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//go:build !windows && (e2e || template)
// +build !windows
// +build e2e template
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package standalone_test
import (
"encoding/json"
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStopAppsStartedWithRunTemplate(t *testing.T) {
ensureDaprInstallation(t)
t.Cleanup(func() {
// remove dapr installation after all tests in this function.
tearDownTestSetup(t)
})
t.Run("stop apps by passing run template file", func(t *testing.T) {
go ensureAllAppsStartedWithRunTemplate(t)
time.Sleep(10 * time.Second)
cliPID := getCLIPID(t)
// Assert dapr list contains template name
assertTemplateListOutput(t, "test_dapr_template")
output, err := cmdStopWithRunTemplate("../testdata/run-template-files/dapr.yaml")
assert.NoError(t, err, "failed to stop apps started with run template")
assert.Contains(t, output, "Dapr and app processes stopped successfully")
verifyCLIPIDNotExist(t, cliPID)
})
t.Run("stop apps by passing a directory containing dapr.yaml", func(t *testing.T) {
go ensureAllAppsStartedWithRunTemplate(t)
time.Sleep(10 * time.Second)
cliPID := getCLIPID(t)
output, err := cmdStopWithRunTemplate("../testdata/run-template-files")
assert.NoError(t, err, "failed to stop apps started with run template")
assert.Contains(t, output, "Dapr and app processes stopped successfully")
verifyCLIPIDNotExist(t, cliPID)
})
t.Run("stop apps by passing an invalid directory", func(t *testing.T) {
go ensureAllAppsStartedWithRunTemplate(t)
time.Sleep(10 * time.Second)
output, err := cmdStopWithRunTemplate("../testdata/invalid-dir")
assert.Contains(t, output, "Failed to get run file path")
assert.Error(t, err, "failed to stop apps started with run template")
// cleanup started apps
output, err = cmdStopWithRunTemplate("../testdata/run-template-files")
assert.NoError(t, err, "failed to stop apps started with run template")
assert.Contains(t, output, "Dapr and app processes stopped successfully")
})
t.Run("stop apps started with run template", func(t *testing.T) {
go ensureAllAppsStartedWithRunTemplate(t)
time.Sleep(10 * time.Second)
cliPID := getCLIPID(t)
output, err := cmdStopWithAppID("emit-metrics", "processor")
assert.NoError(t, err, "failed to stop apps started with run template")
assert.Contains(t, output, "app stopped successfully: emit-metrics")
assert.Contains(t, output, "app stopped successfully: processor")
assert.NotContains(t, output, "Dapr and app processes stopped successfully")
verifyCLIPIDNotExist(t, cliPID)
})
}
func ensureAllAppsStartedWithRunTemplate(t *testing.T) {
args := []string{
"-f", "../testdata/run-template-files/dapr.yaml",
}
_, err := cmdRun("", args...)
require.NoError(t, err, "run failed")
}
func tearDownTestSetup(t *testing.T) {
// remove dapr installation after all tests in this function.
must(t, cmdUninstall, "failed to uninstall Dapr")
os.RemoveAll("../../apps/emit-metrics/.dapr/logs")
os.RemoveAll("../../apps/processor/.dapr/logs")
}
func getCLIPID(t *testing.T) string {
output, err := cmdList("json")
require.NoError(t, err, "failed to list apps")
result := []map[string]interface{}{}
err = json.Unmarshal([]byte(output), &result)
assert.Equal(t, 2, len(result))
return fmt.Sprintf("%v", result[0]["cliPid"])
}
func verifyCLIPIDNotExist(t *testing.T, pid string) {
time.Sleep(5 * time.Second)
output, err := cmdList("")
require.NoError(t, err, "failed to list apps")
assert.NotContains(t, output, pid)
}
func assertTemplateListOutput(t *testing.T, name string) {
output, err := cmdList("json")
t.Log(output)
require.NoError(t, err, "dapr list failed")
var result []map[string]interface{}
err = json.Unmarshal([]byte(output), &result)
assert.NoError(t, err, "output was not valid JSON")
assert.Len(t, result, 2, "expected two apps to be running")
assert.Equal(t, name, result[0]["runTemplateName"], "expected run template name to be %s", name)
assert.NotEmpty(t, result[0]["appLogPath"], "expected appLogPath to be non-empty")
assert.NotEmpty(t, result[0]["daprdLogPath"], "expected daprdLogPath to be non-empty")
}