Skip to content

Commit 1b421b0

Browse files
committed
add tests
Signed-off-by: Pravin Pushkar <[email protected]>
1 parent 6affa75 commit 1b421b0

File tree

3 files changed

+156
-1
lines changed

3 files changed

+156
-1
lines changed

pkg/standalone/common.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,17 @@ func DefaultConfigFilePath() string {
7272
// TODO: Remove this function when `--components-path` flag is removed.
7373
func moveFilesFromComponentsToResourcesDir(componentsDirPath, resourcesDirPath string) error {
7474
if _, err := os.Stat(componentsDirPath); err == nil {
75-
files, err := os.ReadDir(componentsDirPath)
75+
files, err := os.ReadDir(resourcesDirPath)
76+
if err != nil {
77+
return err
78+
}
79+
for _, file := range files {
80+
err = os.Remove(resourcesDirPath + "/" + file.Name())
81+
if err != nil {
82+
return err
83+
}
84+
}
85+
files, err = os.ReadDir(componentsDirPath)
7686
if err != nil {
7787
return err
7888
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//go:build e2e
2+
// +build e2e
3+
4+
/*
5+
Copyright 2022 The Dapr Authors
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package standalone_test
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
"testing"
24+
25+
"github.com/dapr/cli/tests/e2e/common"
26+
"github.com/dapr/cli/tests/e2e/spawn"
27+
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
29+
)
30+
31+
var (
32+
// DefaultComponentsDirPath is the default components directory path.
33+
defaultComponentsDirPath = ""
34+
defaultResourcesDirPath = ""
35+
)
36+
37+
func TestCompToResrcDirMig(t *testing.T) {
38+
homeDir, err := os.UserHomeDir()
39+
assert.NoError(t, err, "cannot get user home directory")
40+
defaultComponentsDirPath = filepath.Join(homeDir, ".dapr", "components")
41+
defaultResourcesDirPath = filepath.Join(homeDir, ".dapr", "resources")
42+
// Ensure a clean environment.
43+
must(t, cmdUninstall, "failed to uninstall Dapr")
44+
45+
// install dapr.
46+
ensureDaprInstallation(t)
47+
48+
// rename resources to components dir.
49+
renameResourcesDir(t)
50+
51+
// dapr run should work with only components dir.
52+
checkDaprRunPrecedenceTest(t, true)
53+
54+
// dapr uninstall without --all flag should work.
55+
uninstallWithoutAllFlag(t)
56+
57+
// dapr init should duplicate files from components dir to resources dir.
58+
initTest(t)
59+
60+
// copy a in memomy state store component to resources dir.
61+
copyInMemStateStore(t)
62+
63+
// check dapr run precedence order, resources dir 1st then components dir.
64+
checkDaprRunPrecedenceTest(t, false)
65+
}
66+
67+
func copyInMemStateStore(t *testing.T) {
68+
filePath := filepath.Join("../testdata/resources", "test-statestore.yaml")
69+
content, err := os.ReadFile(filePath)
70+
assert.NoError(t, err, "cannot read testdata/resources/test-statestore.yaml file")
71+
err = os.WriteFile(filepath.Join(defaultResourcesDirPath, "test-statestore.yaml"), content, 0644)
72+
assert.NoError(t, err, "cannot write testdata/resources/test-statestore.yaml file to resources directory")
73+
}
74+
75+
func initTest(t *testing.T) {
76+
daprRuntimeVersion, _ := common.GetVersionsFromEnv(t)
77+
78+
output, err := cmdInit(daprRuntimeVersion)
79+
t.Log(output)
80+
require.NoError(t, err, "init failed")
81+
assert.Contains(t, output, "Success! Dapr is up and running.")
82+
83+
homeDir, err := os.UserHomeDir()
84+
require.NoError(t, err, "failed to get user home directory")
85+
86+
daprPath := filepath.Join(homeDir, ".dapr")
87+
require.DirExists(t, daprPath, "Directory %s does not exist", daprPath)
88+
require.DirExists(t, defaultComponentsDirPath, "Components dir does not exist")
89+
require.DirExists(t, defaultResourcesDirPath, "Resources dir does not exist")
90+
}
91+
92+
func checkDaprRunPrecedenceTest(t *testing.T, onlyCompDirPresent bool) {
93+
args := []string{
94+
"--app-id", "testapp",
95+
"--", "bash", "-c", "echo 'test'",
96+
}
97+
output, err := cmdRun("", args...)
98+
t.Log(output)
99+
require.NoError(t, err, "run failed")
100+
if onlyCompDirPresent {
101+
assert.NotContains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1")
102+
} else {
103+
assert.Contains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1")
104+
}
105+
assert.Contains(t, output, "Exited App successfully")
106+
assert.Contains(t, output, "Exited Dapr successfully")
107+
}
108+
109+
func uninstallWithoutAllFlag(t *testing.T) {
110+
uninstallArgs := []string{"uninstall"}
111+
daprContainerRuntime := containerRuntime()
112+
113+
// Add --container-runtime flag only if daprContainerRuntime is not empty, or overridden via args.
114+
// This is only valid for non-slim mode.
115+
if !isSlimMode() && daprContainerRuntime != "" {
116+
uninstallArgs = append(uninstallArgs, "--container-runtime", daprContainerRuntime)
117+
}
118+
_, error := spawn.Command(common.GetDaprPath(), uninstallArgs...)
119+
if error != nil {
120+
assert.NoError(t, error, "failed to uninstall Dapr")
121+
}
122+
}
123+
124+
func renameResourcesDir(t *testing.T) {
125+
err := os.Rename(defaultResourcesDirPath, defaultComponentsDirPath)
126+
if err != nil {
127+
mesg := fmt.Sprintf("pre-req to TestCompToResrcDirMig failed. error renaming components dir to resources dir: %s", err)
128+
assert.NoError(t, err, mesg)
129+
}
130+
}

tests/e2e/standalone/run_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ func TestStandaloneRun(t *testing.T) {
147147
assert.Contains(t, output, "Exited Dapr successfully")
148148
})
149149

150+
// TODO: Remove this test when the deprecated --components-path flag is removed.
151+
t.Run(fmt.Sprintf("check run with components-path flag"), func(t *testing.T) {
152+
args := []string{
153+
"--app-id", "testapp",
154+
"--components-path", "../testdata/resources",
155+
"--", "bash", "-c", "echo 'test'",
156+
}
157+
output, err := cmdRun("", args...)
158+
t.Log(output)
159+
require.NoError(t, err, "run failed")
160+
assert.Contains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1")
161+
assert.Contains(t, output, "Exited App successfully")
162+
assert.Contains(t, output, "Exited Dapr successfully")
163+
})
164+
150165
t.Run("run with unknown flags", func(t *testing.T) {
151166
output, err := cmdRun("", "--flag")
152167
require.Error(t, err, "expected error on run unknown flag")

0 commit comments

Comments
 (0)