Skip to content

Commit 4a421d5

Browse files
committed
fix tests
Signed-off-by: Pravin Pushkar <[email protected]>
1 parent 0749c5c commit 4a421d5

File tree

4 files changed

+138
-66
lines changed

4 files changed

+138
-66
lines changed

pkg/standalone/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ func DefaultConfigFilePath() string {
6868
return path_filepath.Join(defaultDaprDirPath(), defaultConfigFileName)
6969
}
7070

71-
// emptyAndCopyFiles copies files from src to dest. It deletes the existing files in dest before copying from src.
71+
// copyFilesAndCreateSymlink copies files from src to dest. It deletes the existing files in dest before copying from src.
7272
// this method also deletes the components dir and makes it as a symlink to resources directory.
7373
// please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345
7474
// TODO: Remove this function when `--components-path` flag is removed.
75-
func emptyAndCopyFiles(src, dest string) error {
75+
func copyFilesAndCreateSymlink(src, dest string) error {
7676
if _, err := os.Stat(src); err != nil {
7777
// if the src directory does not exist, create symlink and return nil, because there is nothing to copy from.
7878
if os.IsNotExist(err) {

pkg/standalone/common_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Copyright 2022 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package standalone
15+
16+
import (
17+
"os"
18+
path_filepath "path/filepath"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestCreateSymLink(t *testing.T) {
25+
// create a temp dir to hold the symlink and actual directory.
26+
tempDir := createTempDir(t, "dapr-test", "")
27+
defer cleanupTempDir(t, tempDir)
28+
rsrcDir := createTempDir(t, "resources", tempDir)
29+
existingSymLinkDir := createTempDir(t, "components_exist", tempDir)
30+
tests := []struct {
31+
name string
32+
actualDirName string
33+
symLinkName string
34+
expectedError bool
35+
}{
36+
{
37+
name: "create symlink for resources directory",
38+
actualDirName: rsrcDir,
39+
symLinkName: path_filepath.Join(tempDir, "components"),
40+
expectedError: false,
41+
},
42+
{
43+
name: "create symlink when resources directory does not exist",
44+
actualDirName: "invalid-dir",
45+
symLinkName: "components",
46+
expectedError: true,
47+
},
48+
{
49+
name: "create symlink when symlink named directory already exists",
50+
actualDirName: rsrcDir,
51+
symLinkName: existingSymLinkDir,
52+
expectedError: true,
53+
},
54+
}
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
err := createSymLink(tt.actualDirName, tt.symLinkName)
58+
assert.Equal(t, tt.expectedError, err != nil)
59+
})
60+
}
61+
}
62+
63+
func TestCopyFilesAndCreateSymlink(t *testing.T) {
64+
// create a temp dir to hold the symlink and actual directory.
65+
tempDir := createTempDir(t, "dapr-test", "")
66+
defer cleanupTempDir(t, tempDir)
67+
rsrcDir := createTempDir(t, "resources", tempDir)
68+
cmptDir := createTempDir(t, "components", tempDir)
69+
cmptFile := createTempFile(t, cmptDir, "pubsub.yaml")
70+
rsrcFile := createTempFile(t, cmptDir, "pubsub-rsrc.yaml")
71+
tests := []struct {
72+
name string
73+
actualDirName string
74+
symLinkName string
75+
expectedError bool
76+
presentFileName string
77+
}{
78+
{
79+
name: "copy files and create symlink for resources directory when components dir exists",
80+
actualDirName: rsrcDir,
81+
symLinkName: cmptDir,
82+
expectedError: false,
83+
presentFileName: cmptFile,
84+
},
85+
{
86+
name: "copy files and create symlink for resources directory when components dir does not exists",
87+
actualDirName: rsrcDir,
88+
symLinkName: path_filepath.Join(tempDir, "components-not-exist"),
89+
expectedError: false,
90+
presentFileName: rsrcFile,
91+
},
92+
}
93+
for _, tt := range tests {
94+
t.Run(tt.name, func(t *testing.T) {
95+
err := copyFilesAndCreateSymlink(tt.symLinkName, tt.actualDirName)
96+
assert.Equal(t, tt.expectedError, err != nil)
97+
// check if the files are copied.
98+
assert.FileExists(t, path_filepath.Join(tt.symLinkName, path_filepath.Base(tt.presentFileName)))
99+
})
100+
}
101+
}
102+
103+
func createTempDir(t *testing.T, tempDirName, containerDir string) string {
104+
dirName, err := os.MkdirTemp(containerDir, tempDirName)
105+
assert.NoError(t, err)
106+
return dirName
107+
}
108+
109+
func createTempFile(t *testing.T, tempDirName, fileName string) string {
110+
file, err := os.CreateTemp(tempDirName, fileName)
111+
assert.NoError(t, err)
112+
defer file.Close()
113+
return file.Name()
114+
}
115+
116+
func cleanupTempDir(t *testing.T, dirName string) {
117+
err := os.RemoveAll(dirName)
118+
assert.NoError(t, err)
119+
}

pkg/standalone/standalone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod
309309
print.InfoStatusEvent(os.Stdout, "Use `%s ps` to check running containers.", runtimeCmd)
310310
}
311311
// TODO: remove below method when usages of components-path flag and components directory removed completely.
312-
err = emptyAndCopyFiles(DefaultComponentsDirPath(), DefaultResourcesDirPath())
312+
err = copyFilesAndCreateSymlink(DefaultComponentsDirPath(), DefaultResourcesDirPath())
313313
if err != nil {
314314
return err
315315
}

tests/e2e/standalone/upgrade_test.go

Lines changed: 16 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@ limitations under the License.
1919
package standalone_test
2020

2121
import (
22-
"fmt"
2322
"os"
2423
"path/filepath"
2524
"testing"
2625

27-
"github.com/dapr/cli/tests/e2e/common"
28-
"github.com/dapr/cli/tests/e2e/spawn"
2926
"github.com/dapr/cli/utils"
3027
"github.com/stretchr/testify/assert"
3128
"github.com/stretchr/testify/require"
@@ -46,28 +43,23 @@ func TestCompToResrcDirMig(t *testing.T) {
4643
// Ensure a clean environment.
4744
must(t, cmdUninstall, "failed to uninstall Dapr")
4845

49-
// install dapr.
46+
// install dapr -> installs dapr, creates resources dir and symlink components dir.
5047
ensureDaprInstallation(t)
5148

52-
// rename resources to components dir.
53-
renameResourcesDir(t)
54-
55-
// dapr run should work with only components dir.
56-
// 2nd parameter is true to indicate that only components dir is present.
57-
checkDaprRunPrecedenceTest(t, true)
58-
59-
// dapr uninstall without --all flag should work.
60-
uninstallWithoutAllFlag(t)
61-
62-
// dapr init should duplicate files from components dir to resources dir.
63-
initTest(t)
49+
// check dapr run -> should not load in-memory component.
50+
checkDaprRunPrecedenceTest(t, false)
6451

6552
// copy a in memomy state store component to resources dir.
6653
copyInMemStateStore(t)
6754

68-
// check dapr run precedence order, resources dir 1st then components dir.
69-
// 2nd parameter is false to indicate that both components and resources dir are present.
70-
checkDaprRunPrecedenceTest(t, false)
55+
// check dapr run -> should load in-memory component.
56+
checkDaprRunPrecedenceTest(t, true)
57+
58+
// dapr run with --components-path flag -> should load in-memory component.
59+
checkDaprRunPrecedenceTest(t, true, "--components-path", defaultComponentsDirPath)
60+
61+
// dapr run with --resources-path flag -> should load in-memory component.
62+
checkDaprRunPrecedenceTest(t, true, "--resources-path", defaultResourcesDirPath)
7163
}
7264

7365
func copyInMemStateStore(t *testing.T) {
@@ -78,59 +70,20 @@ func copyInMemStateStore(t *testing.T) {
7870
assert.NoError(t, err, "cannot write testdata/resources/test-statestore.yaml file to resources directory")
7971
}
8072

81-
func initTest(t *testing.T) {
82-
daprRuntimeVersion, _ := common.GetVersionsFromEnv(t, false)
83-
84-
output, err := cmdInit(daprRuntimeVersion)
85-
t.Log(output)
86-
require.NoError(t, err, "init failed")
87-
assert.Contains(t, output, "Success! Dapr is up and running.")
88-
89-
homeDir, err := os.UserHomeDir()
90-
require.NoError(t, err, "failed to get user home directory")
91-
92-
daprPath := filepath.Join(homeDir, ".dapr")
93-
require.DirExists(t, daprPath, "Directory %s does not exist", daprPath)
94-
require.DirExists(t, defaultComponentsDirPath, "Components dir does not exist")
95-
require.DirExists(t, defaultResourcesDirPath, "Resources dir does not exist")
96-
}
97-
98-
func checkDaprRunPrecedenceTest(t *testing.T, onlyCompDirPresent bool) {
73+
func checkDaprRunPrecedenceTest(t *testing.T, inMemoryCompPresent bool, flags ...string) {
9974
args := []string{
10075
"--app-id", "testapp",
10176
"--", "bash", "-c", "echo 'test'",
10277
}
78+
args = append(args, flags...)
10379
output, err := cmdRun("", args...)
10480
t.Log(output)
10581
require.NoError(t, err, "run failed")
106-
if onlyCompDirPresent {
107-
assert.NotContains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1")
108-
} else {
82+
if inMemoryCompPresent {
10983
assert.Contains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1")
84+
} else {
85+
assert.NotContains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1")
11086
}
11187
assert.Contains(t, output, "Exited App successfully")
11288
assert.Contains(t, output, "Exited Dapr successfully")
11389
}
114-
115-
func uninstallWithoutAllFlag(t *testing.T) {
116-
uninstallArgs := []string{"uninstall"}
117-
daprContainerRuntime := containerRuntime()
118-
119-
// Add --container-runtime flag only if daprContainerRuntime is not empty, or overridden via args.
120-
// This is only valid for non-slim mode.
121-
if !isSlimMode() && daprContainerRuntime != "" {
122-
uninstallArgs = append(uninstallArgs, "--container-runtime", daprContainerRuntime)
123-
}
124-
_, error := spawn.Command(common.GetDaprPath(), uninstallArgs...)
125-
if error != nil {
126-
assert.NoError(t, error, "failed to uninstall Dapr")
127-
}
128-
}
129-
130-
func renameResourcesDir(t *testing.T) {
131-
err := os.Rename(defaultResourcesDirPath, defaultComponentsDirPath)
132-
if err != nil {
133-
mesg := fmt.Sprintf("pre-req to TestCompToResrcDirMig failed. error renaming components dir to resources dir: %s", err)
134-
assert.NoError(t, err, mesg)
135-
}
136-
}

0 commit comments

Comments
 (0)