From bd3657fc498118c52475404b6a460d93fa52a431 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Fri, 9 Dec 2022 14:57:48 +0530 Subject: [PATCH 01/30] set dapr run defaults precedence Signed-off-by: Pravin Pushkar --- cmd/run.go | 2 +- pkg/standalone/common.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/run.go b/cmd/run.go index c2e2739ad..63a0b052b 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -377,7 +377,7 @@ func init() { RunCmd.Flags().StringVarP(&logLevel, "log-level", "", "info", "The log verbosity. Valid values are: debug, info, warn, error, fatal, or panic") RunCmd.Flags().IntVarP(&maxConcurrency, "app-max-concurrency", "", -1, "The concurrency level of the application, otherwise is unlimited") RunCmd.Flags().StringVarP(&protocol, "app-protocol", "P", "http", "The protocol (gRPC or HTTP) Dapr uses to talk to the application") - RunCmd.Flags().StringVarP(&componentsPath, "components-path", "d", standalone.DefaultComponentsDirPath(), "The path for components directory") + RunCmd.Flags().StringVarP(&componentsPath, "components-path", "d", standalone.DefaultResourcesDirPrecedence(), "The path for components directory") RunCmd.Flags().StringVarP(&resourcesPath, "resources-path", "", "", "The path for resources directory") // TODO: Remove below line once the flag is removed in the future releases. // By marking this as deprecated, the flag will be hidden from the help menu, but will continue to work. It will show a warning message when used. diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index e81e1f6f7..3417e0746 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -23,6 +23,7 @@ const ( defaultDaprDirName = ".dapr" defaultDaprBinDirName = "bin" defaultComponentsDirName = "components" + defaultResourcesDirName = "resources" defaultConfigFileName = "config.yaml" ) @@ -47,6 +48,18 @@ func DefaultComponentsDirPath() string { return path_filepath.Join(defaultDaprDirPath(), defaultComponentsDirName) } +func DefaultResourcesDirPath() string { + return path_filepath.Join(defaultDaprDirPath(), defaultResourcesDirName) +} + +func DefaultResourcesDirPrecedence() string { + defaultResourcesDirPath := DefaultResourcesDirPath() + if _, err := os.Stat(defaultResourcesDirPath); os.IsNotExist(err) { + return DefaultComponentsDirPath() + } + return defaultResourcesDirPath +} + func DefaultConfigFilePath() string { return path_filepath.Join(defaultDaprDirPath(), defaultConfigFileName) } From 4e6d8b59f2ca6d75040115c37c54a5a78a3fb405 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Fri, 9 Dec 2022 16:46:13 +0530 Subject: [PATCH 02/30] dapr init create resources dir and moves existing components content to it. Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 30 ++++++++++++++++++++++++++++++ pkg/standalone/standalone.go | 26 ++++++++++++++++---------- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index 3417e0746..bdfc60855 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -17,6 +17,8 @@ import ( "os" path_filepath "path/filepath" "runtime" + + "github.com/dapr/cli/pkg/print" ) const ( @@ -52,6 +54,8 @@ func DefaultResourcesDirPath() string { return path_filepath.Join(defaultDaprDirPath(), defaultResourcesDirName) } +// when either `components-path` or `resources-path` flags are not present then preference is given to resources dir and then components dir. +// TODO: Remove this function and use `DefaultResourcesDirPath` when `--components-path` flag is removed. func DefaultResourcesDirPrecedence() string { defaultResourcesDirPath := DefaultResourcesDirPath() if _, err := os.Stat(defaultResourcesDirPath); os.IsNotExist(err) { @@ -63,3 +67,29 @@ func DefaultResourcesDirPrecedence() string { func DefaultConfigFilePath() string { return path_filepath.Join(defaultDaprDirPath(), defaultConfigFileName) } + +// It used to copy existing resources from components dir to resources dir. +// TODO: Remove this function when `--components-path` flag is removed. +func moveFilesFromComponentsToResourcesDir(componentsDirPath, resourcesDirPath string) error { + if _, err := os.Stat(componentsDirPath); err == nil { + files, err := os.ReadDir(componentsDirPath) + if err != nil { + return err + } + if len(files) > 0 { + print.InfoStatusEvent(os.Stdout, "Moving files from %q to %q", componentsDirPath, resourcesDirPath) + for _, file := range files { + content, err := os.ReadFile(componentsDirPath + "/" + file.Name()) + if err != nil { + return err + } + // #nosec G306 + err = os.WriteFile(resourcesDirPath+"/"+file.Name(), content, 0o644) + if err != nil { + return err + } + } + } + } + return nil +} diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index e7ddd4833..91476bfcf 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -244,7 +244,7 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod defer stopSpinning(print.Failure) // Make default components directory. - err = makeDefaultComponentsDir() + err = makeDefaultResourcesDir() if err != nil { return err } @@ -297,7 +297,8 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod } for _, container := range dockerContainerNames { containerName := utils.CreateContainerName(container, dockerNetwork) - ok, err := confirmContainerIsRunningOrExists(containerName, true, runtimeCmd) + var ok bool + ok, err = confirmContainerIsRunningOrExists(containerName, true, runtimeCmd) if err != nil { return err } @@ -307,6 +308,11 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod } print.InfoStatusEvent(os.Stdout, "Use `%s ps` to check running containers.", runtimeCmd) } + // TODO: remove below method when components-path flag is removed. + err = moveFilesFromComponentsToResourcesDir(DefaultComponentsDirPath(), DefaultResourcesDirPath()) + if err != nil { + return err + } return nil } @@ -659,14 +665,14 @@ func createComponentsAndConfiguration(wg *sync.WaitGroup, errorChan chan<- error var err error // Make default components directory. - componentsDir := DefaultComponentsDirPath() + resourcesDir := DefaultResourcesDirPath() - err = createRedisPubSub(redisHost, componentsDir) + err = createRedisPubSub(redisHost, resourcesDir) if err != nil { errorChan <- fmt.Errorf("error creating redis pubsub component file: %w", err) return } - err = createRedisStateStore(redisHost, componentsDir) + err = createRedisStateStore(redisHost, resourcesDir) if err != nil { errorChan <- fmt.Errorf("error creating redis statestore component file: %w", err) return @@ -693,19 +699,19 @@ func createSlimConfiguration(wg *sync.WaitGroup, errorChan chan<- error, info in } } -func makeDefaultComponentsDir() error { +func makeDefaultResourcesDir() error { // Make default components directory. - componentsDir := DefaultComponentsDirPath() + resourcesDir := DefaultResourcesDirPath() //nolint - _, err := os.Stat(componentsDir) + _, err := os.Stat(resourcesDir) if os.IsNotExist(err) { - errDir := os.MkdirAll(componentsDir, 0o755) + errDir := os.MkdirAll(resourcesDir, 0o755) if errDir != nil { return fmt.Errorf("error creating default components folder: %w", errDir) } } - os.Chmod(componentsDir, 0o777) + os.Chmod(resourcesDir, 0o777) return nil } From 8358a21bb1ff04f4c522e25c92861af0bcfa2d4a Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Fri, 9 Dec 2022 17:11:18 +0530 Subject: [PATCH 03/30] modify tests Signed-off-by: Pravin Pushkar --- pkg/standalone/run_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/standalone/run_test.go b/pkg/standalone/run_test.go index 7bc89d658..e0148c4d6 100644 --- a/pkg/standalone/run_test.go +++ b/pkg/standalone/run_test.go @@ -56,7 +56,7 @@ func assertArgumentNotEqual(t *testing.T, key string, expectedValue string, args } func setupRun(t *testing.T) { - componentsDir := DefaultComponentsDirPath() + componentsDir := DefaultResourcesDirPath() configFile := DefaultConfigFilePath() err := os.MkdirAll(componentsDir, 0o700) assert.Equal(t, nil, err, "Unable to setup components dir before running test") @@ -66,7 +66,7 @@ func setupRun(t *testing.T) { } func tearDownRun(t *testing.T) { - err := os.RemoveAll(DefaultComponentsDirPath()) + err := os.RemoveAll(DefaultResourcesDirPath()) assert.Equal(t, nil, err, "Unable to delete default components dir after running test") err = os.Remove(DefaultConfigFilePath()) assert.Equal(t, nil, err, "Unable to delete default config file after running test") @@ -87,7 +87,7 @@ func assertCommonArgs(t *testing.T, basicConfig *RunConfig, output *RunOutput) { assertArgumentEqual(t, "app-max-concurrency", "-1", output.DaprCMD.Args) assertArgumentEqual(t, "app-protocol", "http", output.DaprCMD.Args) assertArgumentEqual(t, "app-port", "3000", output.DaprCMD.Args) - assertArgumentEqual(t, "components-path", DefaultComponentsDirPath(), output.DaprCMD.Args) + assertArgumentEqual(t, "components-path", DefaultResourcesDirPrecedence(), output.DaprCMD.Args) assertArgumentEqual(t, "app-ssl", "", output.DaprCMD.Args) assertArgumentEqual(t, "metrics-port", "9001", output.DaprCMD.Args) assertArgumentEqual(t, "dapr-http-max-request-size", "-1", output.DaprCMD.Args) @@ -148,7 +148,7 @@ func TestRun(t *testing.T) { EnableProfiling: false, ProfilePort: 9090, Protocol: "http", - ComponentsPath: DefaultComponentsDirPath(), + ComponentsPath: DefaultResourcesDirPrecedence(), AppSSL: true, MetricsPort: 9001, MaxRequestBodySize: -1, From c3f41e9259fd9ea1a6bce9789737f4b7d49e8633 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Fri, 9 Dec 2022 22:19:12 +0530 Subject: [PATCH 04/30] fix test Signed-off-by: Pravin Pushkar --- tests/e2e/standalone/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/standalone/utils.go b/tests/e2e/standalone/utils.go index f16a32b69..a4680204d 100644 --- a/tests/e2e/standalone/utils.go +++ b/tests/e2e/standalone/utils.go @@ -142,7 +142,7 @@ func ensureDaprInstallation(t *testing.T) { // Slim mode does not have any components by default. // Install the components required by the tests. if isSlimMode() { - err = createSlimComponents(filepath.Join(daprPath, "components")) + err = createSlimComponents(filepath.Join(daprPath, "resources")) require.NoError(t, err, "failed to create components") } } From 6affa75b6b1d83f076154eaf10771a28e37978b5 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Mon, 12 Dec 2022 10:47:22 +0530 Subject: [PATCH 05/30] fix tests Signed-off-by: Pravin Pushkar --- tests/e2e/standalone/init_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/e2e/standalone/init_test.go b/tests/e2e/standalone/init_test.go index f73157a34..857d2a513 100644 --- a/tests/e2e/standalone/init_test.go +++ b/tests/e2e/standalone/init_test.go @@ -191,7 +191,7 @@ func verifyBinaries(t *testing.T, daprPath, runtimeVersion, dashboardVersion str } } -// verifyConfigs ensures that the Dapr configuration and component YAMLs +// verifyConfigs ensures that the Dapr configuration and resources YAMLs // are present in the correct path and have the correct values. func verifyConfigs(t *testing.T, daprPath string) { configSpec := map[interface{}]interface{}{} @@ -218,9 +218,9 @@ func verifyConfigs(t *testing.T, daprPath string) { }, } - // The default components are not installed in slim mode. + // The default resources are not installed in slim mode. if !isSlimMode() { - configs[filepath.Join("components", "statestore.yaml")] = map[string]interface{}{ + configs[filepath.Join("resources", "statestore.yaml")] = map[string]interface{}{ "apiVersion": "dapr.io/v1alpha1", "kind": "Component", "metadata": map[interface{}]interface{}{ @@ -245,7 +245,7 @@ func verifyConfigs(t *testing.T, daprPath string) { }, }, } - configs[filepath.Join("components", "pubsub.yaml")] = map[string]interface{}{ + configs[filepath.Join("resources", "pubsub.yaml")] = map[string]interface{}{ "apiVersion": "dapr.io/v1alpha1", "kind": "Component", "metadata": map[interface{}]interface{}{ From 1b421b07f39c1b20edd401f04b63c37207b7507c Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Mon, 12 Dec 2022 17:19:41 +0530 Subject: [PATCH 06/30] add tests Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 12 +- .../componentsDir_to_resourcesDir_mig_test.go | 130 ++++++++++++++++++ tests/e2e/standalone/run_test.go | 15 ++ 3 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index bdfc60855..d285b5ac0 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -72,7 +72,17 @@ func DefaultConfigFilePath() string { // TODO: Remove this function when `--components-path` flag is removed. func moveFilesFromComponentsToResourcesDir(componentsDirPath, resourcesDirPath string) error { if _, err := os.Stat(componentsDirPath); err == nil { - files, err := os.ReadDir(componentsDirPath) + files, err := os.ReadDir(resourcesDirPath) + if err != nil { + return err + } + for _, file := range files { + err = os.Remove(resourcesDirPath + "/" + file.Name()) + if err != nil { + return err + } + } + files, err = os.ReadDir(componentsDirPath) if err != nil { return err } diff --git a/tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go b/tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go new file mode 100644 index 000000000..31ea7c9c0 --- /dev/null +++ b/tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go @@ -0,0 +1,130 @@ +//go:build e2e +// +build e2e + +/* +Copyright 2022 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 ( + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/dapr/cli/tests/e2e/common" + "github.com/dapr/cli/tests/e2e/spawn" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var ( + // DefaultComponentsDirPath is the default components directory path. + defaultComponentsDirPath = "" + defaultResourcesDirPath = "" +) + +func TestCompToResrcDirMig(t *testing.T) { + homeDir, err := os.UserHomeDir() + assert.NoError(t, err, "cannot get user home directory") + defaultComponentsDirPath = filepath.Join(homeDir, ".dapr", "components") + defaultResourcesDirPath = filepath.Join(homeDir, ".dapr", "resources") + // Ensure a clean environment. + must(t, cmdUninstall, "failed to uninstall Dapr") + + // install dapr. + ensureDaprInstallation(t) + + // rename resources to components dir. + renameResourcesDir(t) + + // dapr run should work with only components dir. + checkDaprRunPrecedenceTest(t, true) + + // dapr uninstall without --all flag should work. + uninstallWithoutAllFlag(t) + + // dapr init should duplicate files from components dir to resources dir. + initTest(t) + + // copy a in memomy state store component to resources dir. + copyInMemStateStore(t) + + // check dapr run precedence order, resources dir 1st then components dir. + checkDaprRunPrecedenceTest(t, false) +} + +func copyInMemStateStore(t *testing.T) { + filePath := filepath.Join("../testdata/resources", "test-statestore.yaml") + content, err := os.ReadFile(filePath) + assert.NoError(t, err, "cannot read testdata/resources/test-statestore.yaml file") + err = os.WriteFile(filepath.Join(defaultResourcesDirPath, "test-statestore.yaml"), content, 0644) + assert.NoError(t, err, "cannot write testdata/resources/test-statestore.yaml file to resources directory") +} + +func initTest(t *testing.T) { + daprRuntimeVersion, _ := common.GetVersionsFromEnv(t) + + output, err := cmdInit(daprRuntimeVersion) + t.Log(output) + require.NoError(t, err, "init failed") + assert.Contains(t, output, "Success! Dapr is up and running.") + + homeDir, err := os.UserHomeDir() + require.NoError(t, err, "failed to get user home directory") + + daprPath := filepath.Join(homeDir, ".dapr") + require.DirExists(t, daprPath, "Directory %s does not exist", daprPath) + require.DirExists(t, defaultComponentsDirPath, "Components dir does not exist") + require.DirExists(t, defaultResourcesDirPath, "Resources dir does not exist") +} + +func checkDaprRunPrecedenceTest(t *testing.T, onlyCompDirPresent bool) { + args := []string{ + "--app-id", "testapp", + "--", "bash", "-c", "echo 'test'", + } + output, err := cmdRun("", args...) + t.Log(output) + require.NoError(t, err, "run failed") + if onlyCompDirPresent { + assert.NotContains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1") + } else { + assert.Contains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1") + } + assert.Contains(t, output, "Exited App successfully") + assert.Contains(t, output, "Exited Dapr successfully") +} + +func uninstallWithoutAllFlag(t *testing.T) { + uninstallArgs := []string{"uninstall"} + daprContainerRuntime := containerRuntime() + + // Add --container-runtime flag only if daprContainerRuntime is not empty, or overridden via args. + // This is only valid for non-slim mode. + if !isSlimMode() && daprContainerRuntime != "" { + uninstallArgs = append(uninstallArgs, "--container-runtime", daprContainerRuntime) + } + _, error := spawn.Command(common.GetDaprPath(), uninstallArgs...) + if error != nil { + assert.NoError(t, error, "failed to uninstall Dapr") + } +} + +func renameResourcesDir(t *testing.T) { + err := os.Rename(defaultResourcesDirPath, defaultComponentsDirPath) + if err != nil { + mesg := fmt.Sprintf("pre-req to TestCompToResrcDirMig failed. error renaming components dir to resources dir: %s", err) + assert.NoError(t, err, mesg) + } +} diff --git a/tests/e2e/standalone/run_test.go b/tests/e2e/standalone/run_test.go index 29156e340..bf801689e 100644 --- a/tests/e2e/standalone/run_test.go +++ b/tests/e2e/standalone/run_test.go @@ -147,6 +147,21 @@ func TestStandaloneRun(t *testing.T) { assert.Contains(t, output, "Exited Dapr successfully") }) + // TODO: Remove this test when the deprecated --components-path flag is removed. + t.Run(fmt.Sprintf("check run with components-path flag"), func(t *testing.T) { + args := []string{ + "--app-id", "testapp", + "--components-path", "../testdata/resources", + "--", "bash", "-c", "echo 'test'", + } + output, err := cmdRun("", args...) + t.Log(output) + require.NoError(t, err, "run failed") + assert.Contains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1") + assert.Contains(t, output, "Exited App successfully") + assert.Contains(t, output, "Exited Dapr successfully") + }) + t.Run("run with unknown flags", func(t *testing.T) { output, err := cmdRun("", "--flag") require.Error(t, err, "expected error on run unknown flag") From 03633651499556a38af12b76c0f1cf617114adc2 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Mon, 12 Dec 2022 18:22:31 +0530 Subject: [PATCH 07/30] trigger pr checks Signed-off-by: Pravin Pushkar From d9caa1590c067874b29d30b3d24ce9ed5a640a17 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 13 Dec 2022 12:07:29 +0530 Subject: [PATCH 08/30] Update pkg/standalone/common.go Co-authored-by: Shubham Sharma Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index d285b5ac0..cc397bc05 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -54,7 +54,7 @@ func DefaultResourcesDirPath() string { return path_filepath.Join(defaultDaprDirPath(), defaultResourcesDirName) } -// when either `components-path` or `resources-path` flags are not present then preference is given to resources dir and then components dir. +// DefaultResourcesDirPrecedence returns the path to the resources directory if it exists, or otherwise the components directory. // TODO: Remove this function and use `DefaultResourcesDirPath` when `--components-path` flag is removed. func DefaultResourcesDirPrecedence() string { defaultResourcesDirPath := DefaultResourcesDirPath() From 2007ed574738eda460497a8736e8dc52f88522ea Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 13 Dec 2022 13:17:07 +0530 Subject: [PATCH 09/30] Apply suggestions from code review Co-authored-by: Shubham Sharma Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 2 +- pkg/standalone/standalone.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index cc397bc05..7342b5350 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -68,7 +68,7 @@ func DefaultConfigFilePath() string { return path_filepath.Join(defaultDaprDirPath(), defaultConfigFileName) } -// It used to copy existing resources from components dir to resources dir. +// emptyAndCopyFiles copies files from src to dest. It deletes the existing files in dest before copying from src. // TODO: Remove this function when `--components-path` flag is removed. func moveFilesFromComponentsToResourcesDir(componentsDirPath, resourcesDirPath string) error { if _, err := os.Stat(componentsDirPath); err == nil { diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 91476bfcf..284a33fcd 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -243,7 +243,7 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod stopSpinning := print.Spinner(os.Stdout, msg) defer stopSpinning(print.Failure) - // Make default components directory. + // Make default resources directory. err = makeDefaultResourcesDir() if err != nil { return err From bf0e7ff2d9861df236e146761eed78d49a73d28b Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 13 Dec 2022 13:24:44 +0530 Subject: [PATCH 10/30] review comments Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 43 ++++++++++--------- pkg/standalone/standalone.go | 6 +-- .../componentsDir_to_resourcesDir_mig_test.go | 1 + 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index 7342b5350..125b7c94b 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -70,34 +70,35 @@ func DefaultConfigFilePath() string { // emptyAndCopyFiles copies files from src to dest. It deletes the existing files in dest before copying from src. // TODO: Remove this function when `--components-path` flag is removed. -func moveFilesFromComponentsToResourcesDir(componentsDirPath, resourcesDirPath string) error { - if _, err := os.Stat(componentsDirPath); err == nil { - files, err := os.ReadDir(resourcesDirPath) +func emptyAndCopyFiles(src, dest string) error { + if _, err := os.Stat(src); err != nil { + return err + } + files, err := os.ReadDir(dest) + if err != nil { + return err + } + for _, file := range files { + err = os.Remove(dest + "/" + file.Name()) if err != nil { return err } + } + files, err = os.ReadDir(src) + if err != nil { + return err + } + if len(files) > 0 { + print.InfoStatusEvent(os.Stdout, "Moving files from %q to %q", src, dest) for _, file := range files { - err = os.Remove(resourcesDirPath + "/" + file.Name()) + content, err := os.ReadFile(src + "/" + file.Name()) if err != nil { return err } - } - files, err = os.ReadDir(componentsDirPath) - if err != nil { - return err - } - if len(files) > 0 { - print.InfoStatusEvent(os.Stdout, "Moving files from %q to %q", componentsDirPath, resourcesDirPath) - for _, file := range files { - content, err := os.ReadFile(componentsDirPath + "/" + file.Name()) - if err != nil { - return err - } - // #nosec G306 - err = os.WriteFile(resourcesDirPath+"/"+file.Name(), content, 0o644) - if err != nil { - return err - } + // #nosec G306 + err = os.WriteFile(dest+"/"+file.Name(), content, 0o644) + if err != nil { + return err } } } diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 284a33fcd..386bc50d5 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -295,9 +295,9 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod if isAirGapInit { dockerContainerNames = []string{DaprPlacementContainerName} } + var ok bool for _, container := range dockerContainerNames { containerName := utils.CreateContainerName(container, dockerNetwork) - var ok bool ok, err = confirmContainerIsRunningOrExists(containerName, true, runtimeCmd) if err != nil { return err @@ -309,7 +309,7 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod print.InfoStatusEvent(os.Stdout, "Use `%s ps` to check running containers.", runtimeCmd) } // TODO: remove below method when components-path flag is removed. - err = moveFilesFromComponentsToResourcesDir(DefaultComponentsDirPath(), DefaultResourcesDirPath()) + err = emptyAndCopyFiles(DefaultComponentsDirPath(), DefaultResourcesDirPath()) if err != nil { return err } @@ -707,7 +707,7 @@ func makeDefaultResourcesDir() error { if os.IsNotExist(err) { errDir := os.MkdirAll(resourcesDir, 0o755) if errDir != nil { - return fmt.Errorf("error creating default components folder: %w", errDir) + return fmt.Errorf("error creating default resources folder: %w", errDir) } } diff --git a/tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go b/tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go index 31ea7c9c0..48e85fb13 100644 --- a/tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go +++ b/tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +// TODO: Remove the test when `--components-path` flag is removed. package standalone_test import ( From d8422a75fabb8359e747629850a219fb9b5a9d5d Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 13 Dec 2022 16:16:09 +0530 Subject: [PATCH 11/30] review comments Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 18 ++++++++++-------- ...esourcesDir_mig_test.go => upgrade_test.go} | 5 ++++- tests/e2e/standalone/utils.go | 9 +++++---- 3 files changed, 19 insertions(+), 13 deletions(-) rename tests/e2e/standalone/{componentsDir_to_resourcesDir_mig_test.go => upgrade_test.go} (93%) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index 125b7c94b..b5c1de3fb 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -22,11 +22,12 @@ import ( ) const ( - defaultDaprDirName = ".dapr" - defaultDaprBinDirName = "bin" - defaultComponentsDirName = "components" - defaultResourcesDirName = "resources" - defaultConfigFileName = "config.yaml" + DefaultComponentsDirName = "components" + DefaultResourcesDirName = "resources" + + defaultDaprDirName = ".dapr" + defaultDaprBinDirName = "bin" + defaultConfigFileName = "config.yaml" ) func defaultDaprDirPath() string { @@ -47,11 +48,11 @@ func binaryFilePath(binaryDir string, binaryFilePrefix string) string { } func DefaultComponentsDirPath() string { - return path_filepath.Join(defaultDaprDirPath(), defaultComponentsDirName) + return path_filepath.Join(defaultDaprDirPath(), DefaultComponentsDirName) } func DefaultResourcesDirPath() string { - return path_filepath.Join(defaultDaprDirPath(), defaultResourcesDirName) + return path_filepath.Join(defaultDaprDirPath(), DefaultResourcesDirName) } // DefaultResourcesDirPrecedence returns the path to the resources directory if it exists, or otherwise the components directory. @@ -72,7 +73,8 @@ func DefaultConfigFilePath() string { // TODO: Remove this function when `--components-path` flag is removed. func emptyAndCopyFiles(src, dest string) error { if _, err := os.Stat(src); err != nil { - return err + // if the src directory does not exist, return nil, because there is nothing to copy from. + return nil } files, err := os.ReadDir(dest) if err != nil { diff --git a/tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go b/tests/e2e/standalone/upgrade_test.go similarity index 93% rename from tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go rename to tests/e2e/standalone/upgrade_test.go index 48e85fb13..d46480086 100644 --- a/tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go +++ b/tests/e2e/standalone/upgrade_test.go @@ -14,7 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. */ -// TODO: Remove the test when `--components-path` flag is removed. +// TODO: Remove the test file when `--components-path` flag is removed. + +// This file contains tests for the migration of components directory to resources directory. +// It covers the test flow for scenario when user does: (1) dapr uninstall (2) upgrades dapr cli (3) dapr init (4) dapr run. package standalone_test import ( diff --git a/tests/e2e/standalone/utils.go b/tests/e2e/standalone/utils.go index a4680204d..4255b9443 100644 --- a/tests/e2e/standalone/utils.go +++ b/tests/e2e/standalone/utils.go @@ -28,6 +28,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/dapr/cli/pkg/standalone" "github.com/dapr/cli/tests/e2e/common" ) @@ -139,11 +140,11 @@ func ensureDaprInstallation(t *testing.T) { require.NoError(t, err, "failed to stat dapr installation") } - // Slim mode does not have any components by default. - // Install the components required by the tests. + // Slim mode does not have any resources by default. + // Install the resources required by the tests. if isSlimMode() { - err = createSlimComponents(filepath.Join(daprPath, "resources")) - require.NoError(t, err, "failed to create components") + err = createSlimComponents(filepath.Join(daprPath, standalone.DefaultResourcesDirName)) + require.NoError(t, err, "failed to create resources directory for slim mode") } } From e73ec7df6ce3e968594fd2391098cbe8f7ccf663 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 13 Dec 2022 16:27:11 +0530 Subject: [PATCH 12/30] fix error handle on path not present Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index b5c1de3fb..f3f2759f2 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -74,7 +74,10 @@ func DefaultConfigFilePath() string { func emptyAndCopyFiles(src, dest string) error { if _, err := os.Stat(src); err != nil { // if the src directory does not exist, return nil, because there is nothing to copy from. - return nil + if os.IsNotExist(err) { + return nil + } + return err } files, err := os.ReadDir(dest) if err != nil { From 093239bdb53f90a4e65709a3361aa9aa9ddb530a Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 13 Dec 2022 17:08:14 +0530 Subject: [PATCH 13/30] fix tests Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 8 +++----- tests/e2e/standalone/utils.go | 4 ++-- utils/utils.go | 3 +++ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index f3f2759f2..8ae872720 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -19,12 +19,10 @@ import ( "runtime" "github.com/dapr/cli/pkg/print" + "github.com/dapr/cli/utils" ) const ( - DefaultComponentsDirName = "components" - DefaultResourcesDirName = "resources" - defaultDaprDirName = ".dapr" defaultDaprBinDirName = "bin" defaultConfigFileName = "config.yaml" @@ -48,11 +46,11 @@ func binaryFilePath(binaryDir string, binaryFilePrefix string) string { } func DefaultComponentsDirPath() string { - return path_filepath.Join(defaultDaprDirPath(), DefaultComponentsDirName) + return path_filepath.Join(defaultDaprDirPath(), utils.DefaultComponentsDirName) } func DefaultResourcesDirPath() string { - return path_filepath.Join(defaultDaprDirPath(), DefaultResourcesDirName) + return path_filepath.Join(defaultDaprDirPath(), utils.DefaultResourcesDirName) } // DefaultResourcesDirPrecedence returns the path to the resources directory if it exists, or otherwise the components directory. diff --git a/tests/e2e/standalone/utils.go b/tests/e2e/standalone/utils.go index 4255b9443..b20d391f1 100644 --- a/tests/e2e/standalone/utils.go +++ b/tests/e2e/standalone/utils.go @@ -28,8 +28,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/dapr/cli/pkg/standalone" "github.com/dapr/cli/tests/e2e/common" + "github.com/dapr/cli/utils" ) // getSocketCases return different unix socket paths for testing across Dapr commands. @@ -143,7 +143,7 @@ func ensureDaprInstallation(t *testing.T) { // Slim mode does not have any resources by default. // Install the resources required by the tests. if isSlimMode() { - err = createSlimComponents(filepath.Join(daprPath, standalone.DefaultResourcesDirName)) + err = createSlimComponents(filepath.Join(daprPath, utils.DefaultResourcesDirName)) require.NoError(t, err, "failed to create resources directory for slim mode") } } diff --git a/utils/utils.go b/utils/utils.go index dc094dfbe..f5b7d6b64 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -41,6 +41,9 @@ const ( DOCKER ContainerRuntime = "docker" PODMAN ContainerRuntime = "podman" + DefaultComponentsDirName = "components" + DefaultResourcesDirName = "resources" + marinerImageVariantName = "mariner" socketFormat = "%s/dapr-%s-%s.socket" From 5df0795723c39c8c66bcb5251b340fa4cdeb1652 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 13 Dec 2022 17:25:01 +0530 Subject: [PATCH 14/30] trigger pr checks Signed-off-by: Pravin Pushkar From 125ebc8919ed74ef88877a4575517a7965ebf125 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Wed, 14 Dec 2022 10:46:49 +0530 Subject: [PATCH 15/30] readme changes and some more messages today Signed-off-by: Pravin Pushkar --- README.md | 18 +++++++++++------- pkg/standalone/standalone.go | 4 ++-- tests/e2e/standalone/upgrade_test.go | 2 ++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 07985f858..41bd88c47 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ Output should look like so: ``` ⌛ Making the jump to hyperspace... -✅ Downloaded binaries and completed components set up. +✅ Downloaded binaries and completed resources set up. ℹ️ daprd binary has been installed to $HOME/.dapr/bin. ℹ️ dapr_placement container is running. ℹ️ dapr_redis container is running. @@ -99,10 +99,12 @@ Output should look like so: This step creates the following defaults: -1. components folder which is later used during `dapr run` unless the `--resources-path` (`--components-path` is deprecated and will be removed in future releases) option is provided. For Linux/MacOS, the default components folder path is `$HOME/.dapr/components` and for Windows it is `%USERPROFILE%\.dapr\components`. -2. component files in the components folder called `pubsub.yaml` and `statestore.yaml`. +1. resources folder which is later used during `dapr run` unless the `--resources-path` (`--components-path` is deprecated and will be removed in future releases) option is provided. For Linux/MacOS, the default resources folder path is `$HOME/.dapr/resources` and for Windows it is `%USERPROFILE%\.dapr\resources`. +2. resoources files in the resources folder called `pubsub.yaml` and `statestore.yaml`. 3. default config file `$HOME/.dapr/config.yaml` for Linux/MacOS or for Windows at `%USERPROFILE%\.dapr\config.yaml` to enable tracing on `dapr init` call. Can be overridden with the `--config` flag on `dapr run`. +> Note: If there is a `components` directory present in the default dapr installation path `$HOME/.dapr/resources` (for Linux/MacOS) or `%USERPROFILE%\.dapr\resources` (for windows) then `dapr init` will copy the `resources` from the `components` to `resources` directory. This is to ensure safe migration from using `components` to `resources` directory. + #### Slim Init Alternatively to the above, to have the CLI not install any default configuration files or run Docker containers, use the `--slim` flag with the init command. Only Dapr binaries will be installed. @@ -115,13 +117,15 @@ Output should look like so: ```bash ⌛ Making the jump to hyperspace... -✅ Downloaded binaries and completed components set up. +✅ Downloaded binaries and completed resources set up. ℹ️ daprd binary has been installed to $HOME/.dapr/bin. ℹ️ placement binary has been installed. ✅ Success! Dapr is up and running. To get started, go here: https://aka.ms/dapr-getting-started ``` ->Note: When initializing Dapr with the `--slim` flag only the Dapr runtime binary and the placement service binary are installed. An empty default components folder is created with no default configuration files. During `dapr run` user should use `--resources-path` (`--components-path` is deprecated and will be removed in future releases) to point to a components directory with custom configurations files or alternatively place these files in the default directory. For Linux/MacOS, the default components directory path is `$HOME/.dapr/components` and for Windows it is `%USERPROFILE%\.dapr\components`. +>Note: When initializing Dapr with the `--slim` flag only the Dapr runtime binary and the placement service binary are installed. An empty default `resources` folder is created with no default configuration files. During `dapr run` user should use `--resources-path` (`--components-path` is deprecated and will be removed in future releases) to point to a `resources` directory with custom configurations files or alternatively place these files in the default directory. For Linux/MacOS, the default `resources` directory path is `$HOME/.dapr/resources` and for Windows it is `%USERPROFILE%\.dapr\resources`. + +> Note: If there is a `components` directory present in the default dapr installation path `$HOME/.dapr/resources` (for Linux/MacOS) or `%USERPROFILE%\.dapr\resources` (for windows) then `dapr init` will copy the `resources` from the `components` to `resources` directory. This is to ensure safe migration from using `components` to `resources` directory. #### Install a specific runtime version @@ -325,7 +329,7 @@ To remove all Dapr Custom Resource Definitions: dapr uninstall -k --all ``` -*Warning: this will remove any components, subscriptions or configurations that are applied in the cluster at the time of deletion.* +*Warning: this will remove any resources(and components also), subscriptions or configurations that are applied in the cluster at the time of deletion.* ### Upgrade Dapr on Kubernetes @@ -444,7 +448,7 @@ Launch Dapr and your app: dapr run --app-id nodeapp --app-port 3000 node app.js ``` -Note: To choose a non-default components folder, use the --resources-path(--components-path is deprecated) option. +Note: To choose a non-default resources folder, use the --resources-path(--components-path is deprecated) option. Invoke your app: diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 386bc50d5..17e9bb4b8 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -279,9 +279,9 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod stopSpinning(print.Success) - msg = "Downloaded binaries and completed components set up." + msg = "Downloaded binaries and completed resources set up." if isAirGapInit { - msg = "Extracted binaries and completed components set up." + msg = "Extracted binaries and completed resources set up." } print.SuccessStatusEvent(os.Stdout, msg) print.InfoStatusEvent(os.Stdout, "%s binary has been installed to %s.", daprRuntimeFilePrefix, daprBinDir) diff --git a/tests/e2e/standalone/upgrade_test.go b/tests/e2e/standalone/upgrade_test.go index d46480086..1b18020f2 100644 --- a/tests/e2e/standalone/upgrade_test.go +++ b/tests/e2e/standalone/upgrade_test.go @@ -53,6 +53,7 @@ func TestCompToResrcDirMig(t *testing.T) { renameResourcesDir(t) // dapr run should work with only components dir. + // 2nd paramter is true to indicate that only components dir is present. checkDaprRunPrecedenceTest(t, true) // dapr uninstall without --all flag should work. @@ -65,6 +66,7 @@ func TestCompToResrcDirMig(t *testing.T) { copyInMemStateStore(t) // check dapr run precedence order, resources dir 1st then components dir. + // 2nd paramter is false to indicate that both components and resources dir are present. checkDaprRunPrecedenceTest(t, false) } From 3b55cd5d5f96d4c13229dff719577e6c437c4e67 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Wed, 14 Dec 2022 12:08:09 +0530 Subject: [PATCH 16/30] trigger pr checks Signed-off-by: Pravin Pushkar From 3c8020b5c03bd0fdb1d7becbbd91b1cbdbcaa4fd Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Wed, 14 Dec 2022 13:50:53 +0530 Subject: [PATCH 17/30] Update README.md Co-authored-by: Shubham Sharma Signed-off-by: Pravin Pushkar --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 41bd88c47..677509541 100644 --- a/README.md +++ b/README.md @@ -329,7 +329,7 @@ To remove all Dapr Custom Resource Definitions: dapr uninstall -k --all ``` -*Warning: this will remove any resources(and components also), subscriptions or configurations that are applied in the cluster at the time of deletion.* +*Warning: this will remove any components, subscriptions or configurations that are applied in the cluster at the time of deletion.* ### Upgrade Dapr on Kubernetes From ff6ce5d8b621efe9d898c153bb84d64ed71da312 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Wed, 14 Dec 2022 14:59:52 +0530 Subject: [PATCH 18/30] update readme Signed-off-by: Pravin Pushkar --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 677509541..dd058ff1a 100644 --- a/README.md +++ b/README.md @@ -448,7 +448,7 @@ Launch Dapr and your app: dapr run --app-id nodeapp --app-port 3000 node app.js ``` -Note: To choose a non-default resources folder, use the --resources-path(--components-path is deprecated) option. +Note: To choose a non-default resources folder, use the --resources-path option (--components-path is deprecated, see https://github.com/dapr/cli/issues/953 for more information). Invoke your app: @@ -566,8 +566,8 @@ To use a custom path for component definitions ```bash dapr run --resources-path [custom path] -> Note: --components-path flag is deprecated. It will continue to work until it is removed completely. ``` +> Note: --components-path flag is deprecated. It will continue to work until it is removed completely. See https://github.com/dapr/cli/issues/953 for more information. ### List Configurations From 5334d15e12822355f779379f53cce9889c464e48 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Wed, 14 Dec 2022 22:53:03 +0530 Subject: [PATCH 19/30] Typo Signed-off-by: Shubham Sharma --- tests/e2e/standalone/upgrade_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/standalone/upgrade_test.go b/tests/e2e/standalone/upgrade_test.go index 1b18020f2..f4ff5e8d4 100644 --- a/tests/e2e/standalone/upgrade_test.go +++ b/tests/e2e/standalone/upgrade_test.go @@ -53,7 +53,7 @@ func TestCompToResrcDirMig(t *testing.T) { renameResourcesDir(t) // dapr run should work with only components dir. - // 2nd paramter is true to indicate that only components dir is present. + // 2nd parameter is true to indicate that only components dir is present. checkDaprRunPrecedenceTest(t, true) // dapr uninstall without --all flag should work. From 57cfa8ad8cb1458fb58916180f03a13113663af2 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Wed, 14 Dec 2022 22:53:42 +0530 Subject: [PATCH 20/30] Type (2) Signed-off-by: Shubham Sharma --- tests/e2e/standalone/upgrade_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/standalone/upgrade_test.go b/tests/e2e/standalone/upgrade_test.go index f4ff5e8d4..0df6f8497 100644 --- a/tests/e2e/standalone/upgrade_test.go +++ b/tests/e2e/standalone/upgrade_test.go @@ -66,7 +66,7 @@ func TestCompToResrcDirMig(t *testing.T) { copyInMemStateStore(t) // check dapr run precedence order, resources dir 1st then components dir. - // 2nd paramter is false to indicate that both components and resources dir are present. + // 2nd parameter is false to indicate that both components and resources dir are present. checkDaprRunPrecedenceTest(t, false) } From a4c5f818bf0b5918378872ad272ef3565ee25867 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Thu, 15 Dec 2022 13:50:29 +0530 Subject: [PATCH 21/30] few more refactoring Signed-off-by: Pravin Pushkar --- cmd/run.go | 2 +- pkg/standalone/common.go | 6 +++--- pkg/standalone/run_test.go | 8 ++++---- pkg/standalone/standalone.go | 2 +- tests/e2e/standalone/upgrade_test.go | 8 ++++---- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 63a0b052b..aa122e238 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -377,7 +377,7 @@ func init() { RunCmd.Flags().StringVarP(&logLevel, "log-level", "", "info", "The log verbosity. Valid values are: debug, info, warn, error, fatal, or panic") RunCmd.Flags().IntVarP(&maxConcurrency, "app-max-concurrency", "", -1, "The concurrency level of the application, otherwise is unlimited") RunCmd.Flags().StringVarP(&protocol, "app-protocol", "P", "http", "The protocol (gRPC or HTTP) Dapr uses to talk to the application") - RunCmd.Flags().StringVarP(&componentsPath, "components-path", "d", standalone.DefaultResourcesDirPrecedence(), "The path for components directory") + RunCmd.Flags().StringVarP(&componentsPath, "components-path", "d", standalone.GetResourcesDir(), "The path for resources directory") RunCmd.Flags().StringVarP(&resourcesPath, "resources-path", "", "", "The path for resources directory") // TODO: Remove below line once the flag is removed in the future releases. // By marking this as deprecated, the flag will be hidden from the help menu, but will continue to work. It will show a warning message when used. diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index 8ae872720..78b241ba0 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -53,9 +53,9 @@ func DefaultResourcesDirPath() string { return path_filepath.Join(defaultDaprDirPath(), utils.DefaultResourcesDirName) } -// DefaultResourcesDirPrecedence returns the path to the resources directory if it exists, or otherwise the components directory. -// TODO: Remove this function and use `DefaultResourcesDirPath` when `--components-path` flag is removed. -func DefaultResourcesDirPrecedence() string { +// GetResourcesDir returns the path to the resources directory if it exists, otherwise it returns the path of components directory. +// TODO: Remove this function and replace all its usage with above defined `DefaultResourcesDirPath` when `--components-path` flag is removed. +func GetResourcesDir() string { defaultResourcesDirPath := DefaultResourcesDirPath() if _, err := os.Stat(defaultResourcesDirPath); os.IsNotExist(err) { return DefaultComponentsDirPath() diff --git a/pkg/standalone/run_test.go b/pkg/standalone/run_test.go index e0148c4d6..78b44daea 100644 --- a/pkg/standalone/run_test.go +++ b/pkg/standalone/run_test.go @@ -56,9 +56,9 @@ func assertArgumentNotEqual(t *testing.T, key string, expectedValue string, args } func setupRun(t *testing.T) { - componentsDir := DefaultResourcesDirPath() + resourcesDir := DefaultResourcesDirPath() configFile := DefaultConfigFilePath() - err := os.MkdirAll(componentsDir, 0o700) + err := os.MkdirAll(resourcesDir, 0o700) assert.Equal(t, nil, err, "Unable to setup components dir before running test") file, err := os.Create(configFile) file.Close() @@ -87,7 +87,7 @@ func assertCommonArgs(t *testing.T, basicConfig *RunConfig, output *RunOutput) { assertArgumentEqual(t, "app-max-concurrency", "-1", output.DaprCMD.Args) assertArgumentEqual(t, "app-protocol", "http", output.DaprCMD.Args) assertArgumentEqual(t, "app-port", "3000", output.DaprCMD.Args) - assertArgumentEqual(t, "components-path", DefaultResourcesDirPrecedence(), output.DaprCMD.Args) + assertArgumentEqual(t, "components-path", GetResourcesDir(), output.DaprCMD.Args) assertArgumentEqual(t, "app-ssl", "", output.DaprCMD.Args) assertArgumentEqual(t, "metrics-port", "9001", output.DaprCMD.Args) assertArgumentEqual(t, "dapr-http-max-request-size", "-1", output.DaprCMD.Args) @@ -148,7 +148,7 @@ func TestRun(t *testing.T) { EnableProfiling: false, ProfilePort: 9090, Protocol: "http", - ComponentsPath: DefaultResourcesDirPrecedence(), + ComponentsPath: GetResourcesDir(), AppSSL: true, MetricsPort: 9001, MaxRequestBodySize: -1, diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 17e9bb4b8..60ddfdcb2 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -664,7 +664,7 @@ func createComponentsAndConfiguration(wg *sync.WaitGroup, errorChan chan<- error } var err error - // Make default components directory. + // Make default resources directory. resourcesDir := DefaultResourcesDirPath() err = createRedisPubSub(redisHost, resourcesDir) diff --git a/tests/e2e/standalone/upgrade_test.go b/tests/e2e/standalone/upgrade_test.go index 0df6f8497..bc0c6f9e6 100644 --- a/tests/e2e/standalone/upgrade_test.go +++ b/tests/e2e/standalone/upgrade_test.go @@ -15,9 +15,7 @@ limitations under the License. */ // TODO: Remove the test file when `--components-path` flag is removed. - // This file contains tests for the migration of components directory to resources directory. -// It covers the test flow for scenario when user does: (1) dapr uninstall (2) upgrades dapr cli (3) dapr init (4) dapr run. package standalone_test import ( @@ -28,6 +26,7 @@ import ( "github.com/dapr/cli/tests/e2e/common" "github.com/dapr/cli/tests/e2e/spawn" + "github.com/dapr/cli/utils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -38,11 +37,12 @@ var ( defaultResourcesDirPath = "" ) +// It covers the test flow for scenario when user does: (1) dapr uninstall (2) upgrades dapr cli (3) dapr init (4) dapr run. func TestCompToResrcDirMig(t *testing.T) { homeDir, err := os.UserHomeDir() assert.NoError(t, err, "cannot get user home directory") - defaultComponentsDirPath = filepath.Join(homeDir, ".dapr", "components") - defaultResourcesDirPath = filepath.Join(homeDir, ".dapr", "resources") + defaultComponentsDirPath = filepath.Join(homeDir, ".dapr", utils.DefaultComponentsDirName) + defaultResourcesDirPath = filepath.Join(homeDir, ".dapr", utils.DefaultResourcesDirName) // Ensure a clean environment. must(t, cmdUninstall, "failed to uninstall Dapr") From 5a624af435171828ea19dfda217b080d856336c5 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 20 Dec 2022 12:00:42 +0530 Subject: [PATCH 22/30] fix review comment and failing merge Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 6 +++--- tests/e2e/standalone/upgrade_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index 78b241ba0..1596d4f3b 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -82,7 +82,7 @@ func emptyAndCopyFiles(src, dest string) error { return err } for _, file := range files { - err = os.Remove(dest + "/" + file.Name()) + err = os.Remove(path_filepath.Join(dest, file.Name())) if err != nil { return err } @@ -94,12 +94,12 @@ func emptyAndCopyFiles(src, dest string) error { if len(files) > 0 { print.InfoStatusEvent(os.Stdout, "Moving files from %q to %q", src, dest) for _, file := range files { - content, err := os.ReadFile(src + "/" + file.Name()) + content, err := os.ReadFile(path_filepath.Join(src, file.Name())) if err != nil { return err } // #nosec G306 - err = os.WriteFile(dest+"/"+file.Name(), content, 0o644) + err = os.WriteFile(path_filepath.Join(dest, file.Name()), content, 0o644) if err != nil { return err } diff --git a/tests/e2e/standalone/upgrade_test.go b/tests/e2e/standalone/upgrade_test.go index bc0c6f9e6..087cb05ec 100644 --- a/tests/e2e/standalone/upgrade_test.go +++ b/tests/e2e/standalone/upgrade_test.go @@ -79,7 +79,7 @@ func copyInMemStateStore(t *testing.T) { } func initTest(t *testing.T) { - daprRuntimeVersion, _ := common.GetVersionsFromEnv(t) + daprRuntimeVersion, _ := common.GetVersionsFromEnv(t, false) output, err := cmdInit(daprRuntimeVersion) t.Log(output) From 3989aadde509875606ec0578f3ece67d6a005314 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Sat, 24 Dec 2022 20:31:07 +0530 Subject: [PATCH 23/30] make symlink Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 47 ++++++++++++++++++++++++++++++------ pkg/standalone/standalone.go | 2 +- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index 1596d4f3b..6f4ffa592 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -14,6 +14,7 @@ limitations under the License. package standalone import ( + "fmt" "os" path_filepath "path/filepath" "runtime" @@ -68,42 +69,72 @@ func DefaultConfigFilePath() string { } // emptyAndCopyFiles copies files from src to dest. It deletes the existing files in dest before copying from src. +// this method also deletes the components dir and makes it as a symlink to resources directory. +// please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345 // TODO: Remove this function when `--components-path` flag is removed. func emptyAndCopyFiles(src, dest string) error { if _, err := os.Stat(src); err != nil { - // if the src directory does not exist, return nil, because there is nothing to copy from. + // if the src directory does not exist, create symlink and return nil, because there is nothing to copy from. if os.IsNotExist(err) { + err = createSymLink(dest, src) + if err != nil { + return err + } return nil } - return err + return fmt.Errorf("error reading directory %s: %w", src, err) } files, err := os.ReadDir(dest) if err != nil { - return err + return fmt.Errorf("error reading files from %s: %w", dest, err) } for _, file := range files { err = os.Remove(path_filepath.Join(dest, file.Name())) if err != nil { - return err + return fmt.Errorf("error removing file %s: %w", file.Name(), err) } } files, err = os.ReadDir(src) if err != nil { - return err + return fmt.Errorf("error reading files from %s: %w", src, err) } if len(files) > 0 { print.InfoStatusEvent(os.Stdout, "Moving files from %q to %q", src, dest) + var content []byte for _, file := range files { - content, err := os.ReadFile(path_filepath.Join(src, file.Name())) + content, err = os.ReadFile(path_filepath.Join(src, file.Name())) if err != nil { - return err + return fmt.Errorf("error reading file %s: %w", file.Name(), err) } // #nosec G306 err = os.WriteFile(path_filepath.Join(dest, file.Name()), content, 0o644) if err != nil { - return err + return fmt.Errorf("error writing file %s: %w", file.Name(), err) } } } + // delete the components dir and make it as a symlink to resources directory. + err = os.RemoveAll(src) + if err != nil { + return fmt.Errorf("error removing directory %s: %w", src, err) + } + err = createSymLink(dest, src) + if err != nil { + return err + } + return nil +} + +func createSymLink(dirName, symLinkName string) error { + if _, err := os.Stat(dirName); err != nil { + if os.IsNotExist(err) { + return fmt.Errorf("directory %s does not exist", dirName) + } + return fmt.Errorf("error reading directory %s: %w", dirName, err) + } + err := os.Symlink(dirName, symLinkName) + if err != nil { + return fmt.Errorf("error creating symlink from %s to %s: %w", dirName, symLinkName, err) + } return nil } diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 60ddfdcb2..7eec040d1 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -308,7 +308,7 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod } print.InfoStatusEvent(os.Stdout, "Use `%s ps` to check running containers.", runtimeCmd) } - // TODO: remove below method when components-path flag is removed. + // TODO: remove below method when usages of components-path flag and components directory removed completely. err = emptyAndCopyFiles(DefaultComponentsDirPath(), DefaultResourcesDirPath()) if err != nil { return err From 4a421d57a5db9c1d78d3f242264bb9dd6f834705 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Mon, 26 Dec 2022 23:19:52 +0530 Subject: [PATCH 24/30] fix tests Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 4 +- pkg/standalone/common_test.go | 119 +++++++++++++++++++++++++++ pkg/standalone/standalone.go | 2 +- tests/e2e/standalone/upgrade_test.go | 79 ++++-------------- 4 files changed, 138 insertions(+), 66 deletions(-) create mode 100644 pkg/standalone/common_test.go diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index 6f4ffa592..93023cf8c 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -68,11 +68,11 @@ func DefaultConfigFilePath() string { return path_filepath.Join(defaultDaprDirPath(), defaultConfigFileName) } -// emptyAndCopyFiles copies files from src to dest. It deletes the existing files in dest before copying from src. +// copyFilesAndCreateSymlink copies files from src to dest. It deletes the existing files in dest before copying from src. // this method also deletes the components dir and makes it as a symlink to resources directory. // please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345 // TODO: Remove this function when `--components-path` flag is removed. -func emptyAndCopyFiles(src, dest string) error { +func copyFilesAndCreateSymlink(src, dest string) error { if _, err := os.Stat(src); err != nil { // if the src directory does not exist, create symlink and return nil, because there is nothing to copy from. if os.IsNotExist(err) { diff --git a/pkg/standalone/common_test.go b/pkg/standalone/common_test.go new file mode 100644 index 000000000..edc8936b0 --- /dev/null +++ b/pkg/standalone/common_test.go @@ -0,0 +1,119 @@ +/* +Copyright 2022 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 + +import ( + "os" + path_filepath "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCreateSymLink(t *testing.T) { + // create a temp dir to hold the symlink and actual directory. + tempDir := createTempDir(t, "dapr-test", "") + defer cleanupTempDir(t, tempDir) + rsrcDir := createTempDir(t, "resources", tempDir) + existingSymLinkDir := createTempDir(t, "components_exist", tempDir) + tests := []struct { + name string + actualDirName string + symLinkName string + expectedError bool + }{ + { + name: "create symlink for resources directory", + actualDirName: rsrcDir, + symLinkName: path_filepath.Join(tempDir, "components"), + expectedError: false, + }, + { + name: "create symlink when resources directory does not exist", + actualDirName: "invalid-dir", + symLinkName: "components", + expectedError: true, + }, + { + name: "create symlink when symlink named directory already exists", + actualDirName: rsrcDir, + symLinkName: existingSymLinkDir, + expectedError: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := createSymLink(tt.actualDirName, tt.symLinkName) + assert.Equal(t, tt.expectedError, err != nil) + }) + } +} + +func TestCopyFilesAndCreateSymlink(t *testing.T) { + // create a temp dir to hold the symlink and actual directory. + tempDir := createTempDir(t, "dapr-test", "") + defer cleanupTempDir(t, tempDir) + rsrcDir := createTempDir(t, "resources", tempDir) + cmptDir := createTempDir(t, "components", tempDir) + cmptFile := createTempFile(t, cmptDir, "pubsub.yaml") + rsrcFile := createTempFile(t, cmptDir, "pubsub-rsrc.yaml") + tests := []struct { + name string + actualDirName string + symLinkName string + expectedError bool + presentFileName string + }{ + { + name: "copy files and create symlink for resources directory when components dir exists", + actualDirName: rsrcDir, + symLinkName: cmptDir, + expectedError: false, + presentFileName: cmptFile, + }, + { + name: "copy files and create symlink for resources directory when components dir does not exists", + actualDirName: rsrcDir, + symLinkName: path_filepath.Join(tempDir, "components-not-exist"), + expectedError: false, + presentFileName: rsrcFile, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := copyFilesAndCreateSymlink(tt.symLinkName, tt.actualDirName) + assert.Equal(t, tt.expectedError, err != nil) + // check if the files are copied. + assert.FileExists(t, path_filepath.Join(tt.symLinkName, path_filepath.Base(tt.presentFileName))) + }) + } +} + +func createTempDir(t *testing.T, tempDirName, containerDir string) string { + dirName, err := os.MkdirTemp(containerDir, tempDirName) + assert.NoError(t, err) + return dirName +} + +func createTempFile(t *testing.T, tempDirName, fileName string) string { + file, err := os.CreateTemp(tempDirName, fileName) + assert.NoError(t, err) + defer file.Close() + return file.Name() +} + +func cleanupTempDir(t *testing.T, dirName string) { + err := os.RemoveAll(dirName) + assert.NoError(t, err) +} diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 7eec040d1..f44f45c1c 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -309,7 +309,7 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod print.InfoStatusEvent(os.Stdout, "Use `%s ps` to check running containers.", runtimeCmd) } // TODO: remove below method when usages of components-path flag and components directory removed completely. - err = emptyAndCopyFiles(DefaultComponentsDirPath(), DefaultResourcesDirPath()) + err = copyFilesAndCreateSymlink(DefaultComponentsDirPath(), DefaultResourcesDirPath()) if err != nil { return err } diff --git a/tests/e2e/standalone/upgrade_test.go b/tests/e2e/standalone/upgrade_test.go index 087cb05ec..110b6bbe5 100644 --- a/tests/e2e/standalone/upgrade_test.go +++ b/tests/e2e/standalone/upgrade_test.go @@ -19,13 +19,10 @@ limitations under the License. package standalone_test import ( - "fmt" "os" "path/filepath" "testing" - "github.com/dapr/cli/tests/e2e/common" - "github.com/dapr/cli/tests/e2e/spawn" "github.com/dapr/cli/utils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -46,28 +43,23 @@ func TestCompToResrcDirMig(t *testing.T) { // Ensure a clean environment. must(t, cmdUninstall, "failed to uninstall Dapr") - // install dapr. + // install dapr -> installs dapr, creates resources dir and symlink components dir. ensureDaprInstallation(t) - // rename resources to components dir. - renameResourcesDir(t) - - // dapr run should work with only components dir. - // 2nd parameter is true to indicate that only components dir is present. - checkDaprRunPrecedenceTest(t, true) - - // dapr uninstall without --all flag should work. - uninstallWithoutAllFlag(t) - - // dapr init should duplicate files from components dir to resources dir. - initTest(t) + // check dapr run -> should not load in-memory component. + checkDaprRunPrecedenceTest(t, false) // copy a in memomy state store component to resources dir. copyInMemStateStore(t) - // check dapr run precedence order, resources dir 1st then components dir. - // 2nd parameter is false to indicate that both components and resources dir are present. - checkDaprRunPrecedenceTest(t, false) + // check dapr run -> should load in-memory component. + checkDaprRunPrecedenceTest(t, true) + + // dapr run with --components-path flag -> should load in-memory component. + checkDaprRunPrecedenceTest(t, true, "--components-path", defaultComponentsDirPath) + + // dapr run with --resources-path flag -> should load in-memory component. + checkDaprRunPrecedenceTest(t, true, "--resources-path", defaultResourcesDirPath) } func copyInMemStateStore(t *testing.T) { @@ -78,59 +70,20 @@ func copyInMemStateStore(t *testing.T) { assert.NoError(t, err, "cannot write testdata/resources/test-statestore.yaml file to resources directory") } -func initTest(t *testing.T) { - daprRuntimeVersion, _ := common.GetVersionsFromEnv(t, false) - - output, err := cmdInit(daprRuntimeVersion) - t.Log(output) - require.NoError(t, err, "init failed") - assert.Contains(t, output, "Success! Dapr is up and running.") - - homeDir, err := os.UserHomeDir() - require.NoError(t, err, "failed to get user home directory") - - daprPath := filepath.Join(homeDir, ".dapr") - require.DirExists(t, daprPath, "Directory %s does not exist", daprPath) - require.DirExists(t, defaultComponentsDirPath, "Components dir does not exist") - require.DirExists(t, defaultResourcesDirPath, "Resources dir does not exist") -} - -func checkDaprRunPrecedenceTest(t *testing.T, onlyCompDirPresent bool) { +func checkDaprRunPrecedenceTest(t *testing.T, inMemoryCompPresent bool, flags ...string) { args := []string{ "--app-id", "testapp", "--", "bash", "-c", "echo 'test'", } + args = append(args, flags...) output, err := cmdRun("", args...) t.Log(output) require.NoError(t, err, "run failed") - if onlyCompDirPresent { - assert.NotContains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1") - } else { + if inMemoryCompPresent { assert.Contains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1") + } else { + assert.NotContains(t, output, "component loaded. name: test-statestore, type: state.in-memory/v1") } assert.Contains(t, output, "Exited App successfully") assert.Contains(t, output, "Exited Dapr successfully") } - -func uninstallWithoutAllFlag(t *testing.T) { - uninstallArgs := []string{"uninstall"} - daprContainerRuntime := containerRuntime() - - // Add --container-runtime flag only if daprContainerRuntime is not empty, or overridden via args. - // This is only valid for non-slim mode. - if !isSlimMode() && daprContainerRuntime != "" { - uninstallArgs = append(uninstallArgs, "--container-runtime", daprContainerRuntime) - } - _, error := spawn.Command(common.GetDaprPath(), uninstallArgs...) - if error != nil { - assert.NoError(t, error, "failed to uninstall Dapr") - } -} - -func renameResourcesDir(t *testing.T) { - err := os.Rename(defaultResourcesDirPath, defaultComponentsDirPath) - if err != nil { - mesg := fmt.Sprintf("pre-req to TestCompToResrcDirMig failed. error renaming components dir to resources dir: %s", err) - assert.NoError(t, err, mesg) - } -} From f73af111f78b35cf84b41bfad06c3ab33d8daf59 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Fri, 30 Dec 2022 00:27:41 +0530 Subject: [PATCH 25/30] review comments Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 46 +++++---- pkg/standalone/common_test.go | 98 ++++++++++++++---- tests/e2e/standalone/commands.go | 21 ++-- tests/e2e/standalone/init_negative_test.go | 4 +- tests/e2e/standalone/init_test.go | 12 +-- tests/e2e/standalone/uninstall_test.go | 4 +- tests/e2e/standalone/upgrade_test.go | 114 +++++++++++++++++---- tests/e2e/standalone/utils.go | 12 +++ 8 files changed, 229 insertions(+), 82 deletions(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index 93023cf8c..4ed6028ed 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -73,7 +73,8 @@ func DefaultConfigFilePath() string { // please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345 // TODO: Remove this function when `--components-path` flag is removed. func copyFilesAndCreateSymlink(src, dest string) error { - if _, err := os.Stat(src); err != nil { + var err error + if _, err = os.Stat(src); err != nil { // if the src directory does not exist, create symlink and return nil, because there is nothing to copy from. if os.IsNotExist(err) { err = createSymLink(dest, src) @@ -84,24 +85,36 @@ func copyFilesAndCreateSymlink(src, dest string) error { } return fmt.Errorf("error reading directory %s: %w", src, err) } - files, err := os.ReadDir(dest) + if err = moveDir(src, dest); err != nil { + return err + } + if err = createSymLink(dest, src); err != nil { + return err + } + return nil +} + +// moveDir moves files from src to dest. If there are files in src, it deletes the existing files in dest before copying from src. +func moveDir(src, dest string) error { + destFiles, err := os.ReadDir(dest) if err != nil { return fmt.Errorf("error reading files from %s: %w", dest, err) } - for _, file := range files { - err = os.Remove(path_filepath.Join(dest, file.Name())) - if err != nil { - return fmt.Errorf("error removing file %s: %w", file.Name(), err) - } - } - files, err = os.ReadDir(src) + srcFiles, err := os.ReadDir(src) if err != nil { return fmt.Errorf("error reading files from %s: %w", src, err) } - if len(files) > 0 { + if len(srcFiles) > 0 { + // delete the existing files in dest before copying from src iff there are files in src. + for _, file := range destFiles { + err = os.Remove(path_filepath.Join(dest, file.Name())) + if err != nil { + return fmt.Errorf("error removing file %s: %w", file.Name(), err) + } + } print.InfoStatusEvent(os.Stdout, "Moving files from %q to %q", src, dest) var content []byte - for _, file := range files { + for _, file := range srcFiles { content, err = os.ReadFile(path_filepath.Join(src, file.Name())) if err != nil { return fmt.Errorf("error reading file %s: %w", file.Name(), err) @@ -118,23 +131,20 @@ func copyFilesAndCreateSymlink(src, dest string) error { if err != nil { return fmt.Errorf("error removing directory %s: %w", src, err) } - err = createSymLink(dest, src) - if err != nil { - return err - } return nil } -func createSymLink(dirName, symLinkName string) error { +// createSymLink creates a symlink from dirName to symLink. +func createSymLink(dirName, symLink string) error { if _, err := os.Stat(dirName); err != nil { if os.IsNotExist(err) { return fmt.Errorf("directory %s does not exist", dirName) } return fmt.Errorf("error reading directory %s: %w", dirName, err) } - err := os.Symlink(dirName, symLinkName) + err := os.Symlink(dirName, symLink) if err != nil { - return fmt.Errorf("error creating symlink from %s to %s: %w", dirName, symLinkName, err) + return fmt.Errorf("error creating symlink from %s to %s: %w", dirName, symLink, err) } return nil } diff --git a/pkg/standalone/common_test.go b/pkg/standalone/common_test.go index edc8936b0..2183dc89d 100644 --- a/pkg/standalone/common_test.go +++ b/pkg/standalone/common_test.go @@ -25,8 +25,8 @@ func TestCreateSymLink(t *testing.T) { // create a temp dir to hold the symlink and actual directory. tempDir := createTempDir(t, "dapr-test", "") defer cleanupTempDir(t, tempDir) - rsrcDir := createTempDir(t, "resources", tempDir) - existingSymLinkDir := createTempDir(t, "components_exist", tempDir) + originalDir := createTempDir(t, "original_dir", tempDir) + existingSymLinkDir := createTempDir(t, "new_name_exist", tempDir) tests := []struct { name string actualDirName string @@ -35,19 +35,19 @@ func TestCreateSymLink(t *testing.T) { }{ { name: "create symlink for resources directory", - actualDirName: rsrcDir, - symLinkName: path_filepath.Join(tempDir, "components"), + actualDirName: originalDir, + symLinkName: path_filepath.Join(tempDir, "new_name"), expectedError: false, }, { name: "create symlink when resources directory does not exist", actualDirName: "invalid-dir", - symLinkName: "components", + symLinkName: "new_name", expectedError: true, }, { name: "create symlink when symlink named directory already exists", - actualDirName: rsrcDir, + actualDirName: originalDir, symLinkName: existingSymLinkDir, expectedError: true, }, @@ -64,38 +64,92 @@ func TestCopyFilesAndCreateSymlink(t *testing.T) { // create a temp dir to hold the symlink and actual directory. tempDir := createTempDir(t, "dapr-test", "") defer cleanupTempDir(t, tempDir) - rsrcDir := createTempDir(t, "resources", tempDir) - cmptDir := createTempDir(t, "components", tempDir) - cmptFile := createTempFile(t, cmptDir, "pubsub.yaml") - rsrcFile := createTempFile(t, cmptDir, "pubsub-rsrc.yaml") + destDir := createTempDir(t, "dest", tempDir) + srcDir := createTempDir(t, "src", tempDir) + srcFile := createTempFile(t, srcDir, "pubsub.yaml") tests := []struct { name string - actualDirName string - symLinkName string + destDirName string + srcDirName string expectedError bool presentFileName string }{ { - name: "copy files and create symlink for resources directory when components dir exists", - actualDirName: rsrcDir, - symLinkName: cmptDir, + name: "copy files and create symlink for destination directory when source dir exists", + destDirName: destDir, + srcDirName: srcDir, expectedError: false, - presentFileName: cmptFile, + presentFileName: srcFile, }, { - name: "copy files and create symlink for resources directory when components dir does not exists", - actualDirName: rsrcDir, - symLinkName: path_filepath.Join(tempDir, "components-not-exist"), + name: "copy files and create symlink for destination directory when source dir does not exists", + destDirName: destDir, + srcDirName: path_filepath.Join(tempDir, "non-existent-source-dir"), expectedError: false, - presentFileName: rsrcFile, + presentFileName: path_filepath.Base(srcFile), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := copyFilesAndCreateSymlink(tt.symLinkName, tt.actualDirName) + err := copyFilesAndCreateSymlink(tt.srcDirName, tt.destDirName) assert.Equal(t, tt.expectedError, err != nil) // check if the files are copied. - assert.FileExists(t, path_filepath.Join(tt.symLinkName, path_filepath.Base(tt.presentFileName))) + assert.FileExists(t, path_filepath.Join(tt.srcDirName, path_filepath.Base(tt.presentFileName))) + }) + } +} + +func TestMoveDir(t *testing.T) { + // create a temp dir to hold the source and destination directory. + tempDir := createTempDir(t, "dapr-test", "") + defer cleanupTempDir(t, tempDir) + // create a file in the source and destination directory. + src1 := createTempDir(t, "src1", tempDir) + dest2 := createTempDir(t, "dest2", tempDir) + srcFile := createTempFile(t, src1, "pubsub.yaml") + destFile := createTempFile(t, dest2, "pubsub-dest.yaml") + tests := []struct { + name string + srcDirName string + destDirName string + expectedError bool + presentFileName string + }{ + { + name: "move directory when source directory contains files", + srcDirName: src1, + destDirName: createTempDir(t, "dest1", tempDir), + expectedError: false, + presentFileName: path_filepath.Base(srcFile), + }, + { + name: "move directory when source directory does not contain files", + srcDirName: createTempDir(t, "src2", tempDir), + destDirName: dest2, + expectedError: false, + presentFileName: path_filepath.Base(destFile), + }, + { + name: "move directory when source directory does not exists", + srcDirName: path_filepath.Join(tempDir, "non-existent-source-dir"), + destDirName: createTempDir(t, "dest3", tempDir), + expectedError: true, + }, + { + name: "move directory when destination directory does not exists", + srcDirName: createTempDir(t, "src4", tempDir), + destDirName: path_filepath.Join(tempDir, "non-existent-dir-dir"), + expectedError: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := moveDir(tt.srcDirName, tt.destDirName) + assert.Equal(t, tt.expectedError, err != nil) + if tt.presentFileName != "" { + // check if the files are moved correctly. + assert.FileExists(t, path_filepath.Join(tt.destDirName, tt.presentFileName)) + } }) } } diff --git a/tests/e2e/standalone/commands.go b/tests/e2e/standalone/commands.go index 3518bd9aa..35aad9545 100644 --- a/tests/e2e/standalone/commands.go +++ b/tests/e2e/standalone/commands.go @@ -23,7 +23,6 @@ import ( "github.com/dapr/cli/tests/e2e/common" "github.com/dapr/cli/tests/e2e/spawn" - "github.com/dapr/cli/utils" ) // cmdDashboard runs the Dapr dashboard and blocks until it is started. @@ -119,20 +118,18 @@ func cmdStop(appId string, args ...string) (string, error) { return spawn.Command(common.GetDaprPath(), stopArgs...) } -// cmdUninstall uninstalls Dapr with --all flag and returns the command output and error. -func cmdUninstall(args ...string) (string, error) { +// cmdUninstallAll uninstalls Dapr with --all flag and returns the command output and error. +func cmdUninstallAll(args ...string) (string, error) { uninstallArgs := []string{"uninstall", "--all"} - - daprContainerRuntime := containerRuntime() - - // Add --container-runtime flag only if daprContainerRuntime is not empty, or overridden via args. - // This is only valid for non-slim mode. - if !isSlimMode() && daprContainerRuntime != "" && !utils.Contains(args, "--container-runtime") { - uninstallArgs = append(uninstallArgs, "--container-runtime", daprContainerRuntime) - } uninstallArgs = append(uninstallArgs, args...) + return uninstallDapr(uninstallArgs) +} - return spawn.Command(common.GetDaprPath(), uninstallArgs...) +// cmdUninstall uninstalls Dapr without --all flag and returns the command output and error. +func cmdUninstall(args ...string) (string, error) { + uninstallArgs := []string{"uninstall"} + uninstallArgs = append(uninstallArgs, args...) + return uninstallDapr(uninstallArgs) } // cmdVersion checks the version of Dapr and returns the command output and error. diff --git a/tests/e2e/standalone/init_negative_test.go b/tests/e2e/standalone/init_negative_test.go index 562c9a6d1..78c736bb0 100644 --- a/tests/e2e/standalone/init_negative_test.go +++ b/tests/e2e/standalone/init_negative_test.go @@ -27,7 +27,7 @@ import ( func TestStandaloneInitNegatives(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") homeDir, err := os.UserHomeDir() require.NoError(t, err, "expected no error on querying for os home dir") @@ -56,7 +56,7 @@ func TestStandaloneInitNegatives(t *testing.T) { }) t.Run("uninstall without install", func(t *testing.T) { - output, err := cmdUninstall() + output, err := cmdUninstallAll() require.NoError(t, err, "expected no error on uninstall without install") require.Contains(t, output, "Removing Dapr from your machine...", "expected output to contain message") path := filepath.Join(homeDir, ".dapr", "bin") diff --git a/tests/e2e/standalone/init_test.go b/tests/e2e/standalone/init_test.go index dbd64651e..b2d10eb34 100644 --- a/tests/e2e/standalone/init_test.go +++ b/tests/e2e/standalone/init_test.go @@ -42,7 +42,7 @@ func TestStandaloneInit(t *testing.T) { } // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") args := []string{ "--runtime-version", daprRuntimeVersion, "--image-registry", "smplregistry.io/owner", @@ -58,7 +58,7 @@ func TestStandaloneInit(t *testing.T) { } // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") args := []string{ "--runtime-version", daprRuntimeVersion, "--image-registry", "localhost:5000", @@ -71,7 +71,7 @@ func TestStandaloneInit(t *testing.T) { t.Run("init should error out if container runtime is not valid", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") args := []string{ "--runtime-version", daprRuntimeVersion, "--container-runtime", "invalid", @@ -83,7 +83,7 @@ func TestStandaloneInit(t *testing.T) { t.Run("init", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") args := []string{ "--runtime-version", daprRuntimeVersion, @@ -106,7 +106,7 @@ func TestStandaloneInit(t *testing.T) { t.Run("init with mariner images", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") args := []string{ "--runtime-version", daprRuntimeVersion, @@ -130,7 +130,7 @@ func TestStandaloneInit(t *testing.T) { t.Run("init without runtime-version flag", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") output, err := cmdInit() t.Log(output) diff --git a/tests/e2e/standalone/uninstall_test.go b/tests/e2e/standalone/uninstall_test.go index 4ba272b3a..d54aaa130 100644 --- a/tests/e2e/standalone/uninstall_test.go +++ b/tests/e2e/standalone/uninstall_test.go @@ -31,7 +31,7 @@ import ( func TestStandaloneUninstall(t *testing.T) { t.Run("uninstall should error out if container runtime is not valid", func(t *testing.T) { - output, err := cmdUninstall("--container-runtime", "invalid") + output, err := cmdUninstallAll("--container-runtime", "invalid") require.Error(t, err, "expected error if container runtime is invalid") require.Contains(t, output, "Invalid container runtime") }) @@ -39,7 +39,7 @@ func TestStandaloneUninstall(t *testing.T) { t.Run("uninstall", func(t *testing.T) { ensureDaprInstallation(t) - output, err := cmdUninstall() + output, err := cmdUninstallAll() t.Log(output) require.NoError(t, err, "dapr uninstall failed") assert.Contains(t, output, "Dapr has been removed successfully") diff --git a/tests/e2e/standalone/upgrade_test.go b/tests/e2e/standalone/upgrade_test.go index 110b6bbe5..6f8b683cf 100644 --- a/tests/e2e/standalone/upgrade_test.go +++ b/tests/e2e/standalone/upgrade_test.go @@ -34,43 +34,117 @@ var ( defaultResourcesDirPath = "" ) -// It covers the test flow for scenario when user does: (1) dapr uninstall (2) upgrades dapr cli (3) dapr init (4) dapr run. -func TestCompToResrcDirMig(t *testing.T) { +// Tests precedence for --components-path and --resources-path flags. +func TestResourcesLoadPrecedence(t *testing.T) { homeDir, err := os.UserHomeDir() assert.NoError(t, err, "cannot get user home directory") defaultComponentsDirPath = filepath.Join(homeDir, ".dapr", utils.DefaultComponentsDirName) defaultResourcesDirPath = filepath.Join(homeDir, ".dapr", utils.DefaultResourcesDirName) - // Ensure a clean environment. - must(t, cmdUninstall, "failed to uninstall Dapr") - // install dapr -> installs dapr, creates resources dir and symlink components dir. - ensureDaprInstallation(t) + t.Run("without pre-existing components directory", func(t *testing.T) { + // Ensure a clean environment. + must(t, cmdUninstallAll, "failed to uninstall Dapr") - // check dapr run -> should not load in-memory component. - checkDaprRunPrecedenceTest(t, false) + // install dapr -> installs dapr, creates resources dir and symlink components dir. + ensureDaprInstallation(t) - // copy a in memomy state store component to resources dir. - copyInMemStateStore(t) + // check dapr run -> should not load a in-memory statestore component "test-statestore". + testDaprRunOutput(t, false) - // check dapr run -> should load in-memory component. - checkDaprRunPrecedenceTest(t, true) + // copy an in-memomy state store component to the resources directory. + copyInMemStateStore(t, defaultResourcesDirPath) - // dapr run with --components-path flag -> should load in-memory component. - checkDaprRunPrecedenceTest(t, true, "--components-path", defaultComponentsDirPath) + // check dapr run -> should load in-memory component "test-statestore" from resources directory. + testDaprRunOutput(t, true) - // dapr run with --resources-path flag -> should load in-memory component. - checkDaprRunPrecedenceTest(t, true, "--resources-path", defaultResourcesDirPath) + // dapr run with --components-path flag -> should also load the in-memory component because "components" directory is symlinked to "resources" directory. + testDaprRunOutput(t, true, "--components-path", defaultComponentsDirPath) + + // dapr run with --resources-path flag -> should also load the in-memory component. + testDaprRunOutput(t, true, "--resources-path", defaultResourcesDirPath) + + // dapr run with both flags --resources-path and --components-path. + args := []string{ + "--components-path", defaultComponentsDirPath, + "--resources-path", defaultResourcesDirPath + } + testDaprRunOutput(t, true, args...) + } + + t.Run("with pre-existing components directory", func(t *testing.T) { + // Ensure a clean environment. + must(t, cmdUninstallAll, "failed to uninstall Dapr") + + // install dapr -> installs dapr, creates resources dir and symlink components dir. + ensureDaprInstallation(t) + + // test setup -> remove created symlink and rename resources directory to components. + prepareComponentsDir(t) + + // copy an in-memomy state store component to the components directory. + copyInMemStateStore(t, defaultComponentsDirPath) + + // uninstall without removing the components directory. + must(t, cmdUninstall, "failed to uninstall Dapr") + + // install dapr -> installs dapr. It does following - + // 1) creates resources directory. 2)copy resources from components to resources directory. + // 3) delete components directory. 4) creates symlink components for resources directory. + ensureDaprInstallation(t) + + // check dapr run -> should load the in-memory statestore component "test-statestore". + testDaprRunOutput(t, true) + + // dapr run with --components-path flag -> should also load the in-memory component because "components" directory is symlinked to "resources" directory. + testDaprRunOutput(t, true, "--components-path", defaultComponentsDirPath) + + // dapr run with --resources-path flag -> should also load the in-memory component. + testDaprRunOutput(t, true, "--resources-path", defaultResourcesDirPath) + } + + t.Run("add resources to components directory post dapr install", func(t *testing.T) { + // Ensure a clean environment. + must(t, cmdUninstallAll, "failed to uninstall Dapr") + + // install dapr -> installs dapr, creates resources dir and symlink components dir. + ensureDaprInstallation(t) + + // check dapr run -> should not load a in-memory statestore component "test-statestore". + testDaprRunOutput(t, false) + + // copy an in-memomy state store component to the components directory. + copyInMemStateStore(t, defaultComponentsDirPath) + + // check dapr run -> should load in-memory component "test-statestore" from resources directory. + testDaprRunOutput(t, true) + + // dapr run with --components-path flag -> should also load the in-memory component because "components" directory is symlinked to "resources" directory. + testDaprRunOutput(t, true, "--components-path", defaultComponentsDirPath) + + // dapr run with --resources-path flag -> should also load the in-memory component. + testDaprRunOutput(t, true, "--resources-path", defaultResourcesDirPath) + } +} + +func prepareComponentsDir(t *testing.T) { + // remove symlink. + err := os.Remove(defaultComponentsDirPath) + assert.NoError(t, err, fmt.Sprintf("cannot remove symlink %q", defaultComponentsDirPath)) + + // rename resources directory to components. + err = os.Rename(defaultResourcesDirPath, defaultComponentsDirPath) + assert.NoError(t, err, fmt.Sprintf("cannot rename %q to %q", defaultResourcesDirPath, defaultComponentsDirPath)) } -func copyInMemStateStore(t *testing.T) { +func copyInMemStateStore(t *testing.T, targetDirPath string) { filePath := filepath.Join("../testdata/resources", "test-statestore.yaml") content, err := os.ReadFile(filePath) assert.NoError(t, err, "cannot read testdata/resources/test-statestore.yaml file") - err = os.WriteFile(filepath.Join(defaultResourcesDirPath, "test-statestore.yaml"), content, 0644) - assert.NoError(t, err, "cannot write testdata/resources/test-statestore.yaml file to resources directory") + err = os.WriteFile(filepath.Join(targetDirPath, "test-statestore.yaml"), content, 0644) + assert.NoError(t, err, fmt.Sprintf("cannot write testdata/resources/test-statestore.yaml file to %q directory", targetDirPath)) } -func checkDaprRunPrecedenceTest(t *testing.T, inMemoryCompPresent bool, flags ...string) { +func testDaprRunOutput(t *testing.T, inMemoryCompPresent bool, flags ...string) { args := []string{ "--app-id", "testapp", "--", "bash", "-c", "echo 'test'", diff --git a/tests/e2e/standalone/utils.go b/tests/e2e/standalone/utils.go index 43726fdf9..775be1eb0 100644 --- a/tests/e2e/standalone/utils.go +++ b/tests/e2e/standalone/utils.go @@ -29,6 +29,7 @@ import ( "github.com/stretchr/testify/require" "github.com/dapr/cli/tests/e2e/common" + "github.com/dapr/cli/tests/e2e/spawn" "github.com/dapr/cli/utils" ) @@ -157,3 +158,14 @@ func containerRuntime() string { } return "" } + +func uninstallDapr(uninstallArgs ...string) (string, error) { + daprContainerRuntime := containerRuntime() + + // Add --container-runtime flag only if daprContainerRuntime is not empty, or overridden via args. + // This is only valid for non-slim mode. + if !isSlimMode() && daprContainerRuntime != "" && !utils.Contains(uninstallArgs, "--container-runtime") { + uninstallArgs = append(uninstallArgs, "--container-runtime", daprContainerRuntime) + } + return spawn.Command(common.GetDaprPath(), uninstallArgs...) +} From e7ccf04b6decdea4692193c102528870b92689a9 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Fri, 30 Dec 2022 00:56:20 +0530 Subject: [PATCH 26/30] fix tests Signed-off-by: Pravin Pushkar --- tests/e2e/standalone/commands.go | 4 ++-- tests/e2e/standalone/upgrade_test.go | 13 ++++++----- tests/e2e/standalone/utils.go | 35 +++++++++++++++++++--------- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/tests/e2e/standalone/commands.go b/tests/e2e/standalone/commands.go index 35aad9545..5d79c77fd 100644 --- a/tests/e2e/standalone/commands.go +++ b/tests/e2e/standalone/commands.go @@ -122,14 +122,14 @@ func cmdStop(appId string, args ...string) (string, error) { func cmdUninstallAll(args ...string) (string, error) { uninstallArgs := []string{"uninstall", "--all"} uninstallArgs = append(uninstallArgs, args...) - return uninstallDapr(uninstallArgs) + return uninstallDapr(uninstallArgs...) } // cmdUninstall uninstalls Dapr without --all flag and returns the command output and error. func cmdUninstall(args ...string) (string, error) { uninstallArgs := []string{"uninstall"} uninstallArgs = append(uninstallArgs, args...) - return uninstallDapr(uninstallArgs) + return uninstallDapr(uninstallArgs...) } // cmdVersion checks the version of Dapr and returns the command output and error. diff --git a/tests/e2e/standalone/upgrade_test.go b/tests/e2e/standalone/upgrade_test.go index 6f8b683cf..37e1fea4e 100644 --- a/tests/e2e/standalone/upgrade_test.go +++ b/tests/e2e/standalone/upgrade_test.go @@ -19,6 +19,7 @@ limitations under the License. package standalone_test import ( + "fmt" "os" "path/filepath" "testing" @@ -65,11 +66,11 @@ func TestResourcesLoadPrecedence(t *testing.T) { // dapr run with both flags --resources-path and --components-path. args := []string{ - "--components-path", defaultComponentsDirPath, - "--resources-path", defaultResourcesDirPath + "--components-path", defaultComponentsDirPath, + "--resources-path", defaultResourcesDirPath, } testDaprRunOutput(t, true, args...) - } + }) t.Run("with pre-existing components directory", func(t *testing.T) { // Ensure a clean environment. @@ -87,7 +88,7 @@ func TestResourcesLoadPrecedence(t *testing.T) { // uninstall without removing the components directory. must(t, cmdUninstall, "failed to uninstall Dapr") - // install dapr -> installs dapr. It does following - + // install dapr -> installs dapr. It does following - // 1) creates resources directory. 2)copy resources from components to resources directory. // 3) delete components directory. 4) creates symlink components for resources directory. ensureDaprInstallation(t) @@ -100,7 +101,7 @@ func TestResourcesLoadPrecedence(t *testing.T) { // dapr run with --resources-path flag -> should also load the in-memory component. testDaprRunOutput(t, true, "--resources-path", defaultResourcesDirPath) - } + }) t.Run("add resources to components directory post dapr install", func(t *testing.T) { // Ensure a clean environment. @@ -123,7 +124,7 @@ func TestResourcesLoadPrecedence(t *testing.T) { // dapr run with --resources-path flag -> should also load the in-memory component. testDaprRunOutput(t, true, "--resources-path", defaultResourcesDirPath) - } + }) } func prepareComponentsDir(t *testing.T) { diff --git a/tests/e2e/standalone/utils.go b/tests/e2e/standalone/utils.go index 775be1eb0..0a624873f 100644 --- a/tests/e2e/standalone/utils.go +++ b/tests/e2e/standalone/utils.go @@ -127,23 +127,27 @@ func executeAgainstRunningDapr(t *testing.T, f func(), daprArgs ...string) { // ensureDaprInstallation ensures that Dapr is installed. // If Dapr is not installed, a new installation is attempted. func ensureDaprInstallation(t *testing.T) { - daprRuntimeVersion, _ := common.GetVersionsFromEnv(t, false) homeDir, err := os.UserHomeDir() require.NoError(t, err, "failed to get user home directory") daprPath := filepath.Join(homeDir, ".dapr") - _, err = os.Stat(daprPath) - if os.IsNotExist(err) { - args := []string{ - "--runtime-version", daprRuntimeVersion, + if _, err = os.Stat(daprPath); err != nil { + if os.IsNotExist(err) { + installDapr(t) + } else { + // Some other error occurred. + require.NoError(t, err, "failed to stat dapr installation") + } + } + daprBinPath := filepath.Join(daprPath, "bin") + if _, err = os.Stat(daprBinPath); err != nil { + if os.IsNotExist(err) { + installDapr(t) + } else { + // Some other error occurred. + require.NoError(t, err, "failed to stat dapr installation") } - _, err = cmdInit(args...) - require.NoError(t, err, "failed to install dapr") - } else if err != nil { - // Some other error occurred. - require.NoError(t, err, "failed to stat dapr installation") } - // Slim mode does not have any resources by default. // Install the resources required by the tests. if isSlimMode() { @@ -159,6 +163,15 @@ func containerRuntime() string { return "" } +func installDapr(t *testing.T) { + daprRuntimeVersion, _ := common.GetVersionsFromEnv(t, false) + args := []string{ + "--runtime-version", daprRuntimeVersion, + } + _, err := cmdInit(args...) + require.NoError(t, err, "failed to install dapr") +} + func uninstallDapr(uninstallArgs ...string) (string, error) { daprContainerRuntime := containerRuntime() From 771b25686b44b7e3b3c423fadcaaa6afcfdfbc9f Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Fri, 30 Dec 2022 13:56:02 +0530 Subject: [PATCH 27/30] some more refactor Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 31 ++---------------------- pkg/standalone/common_test.go | 39 ------------------------------ pkg/standalone/standalone.go | 26 ++++++++++++++++++++ pkg/standalone/standalone_test.go | 40 +++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 68 deletions(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index 4ed6028ed..57d32a3ea 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -68,33 +68,7 @@ func DefaultConfigFilePath() string { return path_filepath.Join(defaultDaprDirPath(), defaultConfigFileName) } -// copyFilesAndCreateSymlink copies files from src to dest. It deletes the existing files in dest before copying from src. -// this method also deletes the components dir and makes it as a symlink to resources directory. -// please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345 -// TODO: Remove this function when `--components-path` flag is removed. -func copyFilesAndCreateSymlink(src, dest string) error { - var err error - if _, err = os.Stat(src); err != nil { - // if the src directory does not exist, create symlink and return nil, because there is nothing to copy from. - if os.IsNotExist(err) { - err = createSymLink(dest, src) - if err != nil { - return err - } - return nil - } - return fmt.Errorf("error reading directory %s: %w", src, err) - } - if err = moveDir(src, dest); err != nil { - return err - } - if err = createSymLink(dest, src); err != nil { - return err - } - return nil -} - -// moveDir moves files from src to dest. If there are files in src, it deletes the existing files in dest before copying from src. +// moveDir moves files from src to dest. If there are files in src, it deletes the existing files in dest before copying from src and then deletes the src directory. func moveDir(src, dest string) error { destFiles, err := os.ReadDir(dest) if err != nil { @@ -105,7 +79,7 @@ func moveDir(src, dest string) error { return fmt.Errorf("error reading files from %s: %w", src, err) } if len(srcFiles) > 0 { - // delete the existing files in dest before copying from src iff there are files in src. + // delete the existing files in dest before copying from src if there are files in src. for _, file := range destFiles { err = os.Remove(path_filepath.Join(dest, file.Name())) if err != nil { @@ -126,7 +100,6 @@ func moveDir(src, dest string) error { } } } - // delete the components dir and make it as a symlink to resources directory. err = os.RemoveAll(src) if err != nil { return fmt.Errorf("error removing directory %s: %w", src, err) diff --git a/pkg/standalone/common_test.go b/pkg/standalone/common_test.go index 2183dc89d..b0777ec86 100644 --- a/pkg/standalone/common_test.go +++ b/pkg/standalone/common_test.go @@ -60,45 +60,6 @@ func TestCreateSymLink(t *testing.T) { } } -func TestCopyFilesAndCreateSymlink(t *testing.T) { - // create a temp dir to hold the symlink and actual directory. - tempDir := createTempDir(t, "dapr-test", "") - defer cleanupTempDir(t, tempDir) - destDir := createTempDir(t, "dest", tempDir) - srcDir := createTempDir(t, "src", tempDir) - srcFile := createTempFile(t, srcDir, "pubsub.yaml") - tests := []struct { - name string - destDirName string - srcDirName string - expectedError bool - presentFileName string - }{ - { - name: "copy files and create symlink for destination directory when source dir exists", - destDirName: destDir, - srcDirName: srcDir, - expectedError: false, - presentFileName: srcFile, - }, - { - name: "copy files and create symlink for destination directory when source dir does not exists", - destDirName: destDir, - srcDirName: path_filepath.Join(tempDir, "non-existent-source-dir"), - expectedError: false, - presentFileName: path_filepath.Base(srcFile), - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := copyFilesAndCreateSymlink(tt.srcDirName, tt.destDirName) - assert.Equal(t, tt.expectedError, err != nil) - // check if the files are copied. - assert.FileExists(t, path_filepath.Join(tt.srcDirName, path_filepath.Base(tt.presentFileName))) - }) - } -} - func TestMoveDir(t *testing.T) { // create a temp dir to hold the source and destination directory. tempDir := createTempDir(t, "dapr-test", "") diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index f44f45c1c..6e763c5ab 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -1242,3 +1242,29 @@ func setAirGapInit(fromDir string) { // mostly this is used for unit testing aprat from one use in Init() function. isAirGapInit = (len(strings.TrimSpace(fromDir)) != 0) } + +// copyFilesAndCreateSymlink copies files from src to dest. It deletes the existing files in dest before copying from src. +// this method also deletes the src dir and makes it as a symlink to resources directory. +// please see this comment for more details:https://github.com/dapr/cli/pull/1149#issuecomment-1364424345 +// TODO: Remove this function when `--components-path` flag is removed. +func copyFilesAndCreateSymlink(src, dest string) error { + var err error + if _, err = os.Stat(src); err != nil { + // if the src directory does not exist, create symlink and return nil, because there is nothing to copy from. + if os.IsNotExist(err) { + err = createSymLink(dest, src) + if err != nil { + return err + } + return nil + } + return fmt.Errorf("error reading directory %s: %w", src, err) + } + if err = moveDir(src, dest); err != nil { + return err + } + if err = createSymLink(dest, src); err != nil { + return err + } + return nil +} diff --git a/pkg/standalone/standalone_test.go b/pkg/standalone/standalone_test.go index c4eff2dd2..0af847f81 100644 --- a/pkg/standalone/standalone_test.go +++ b/pkg/standalone/standalone_test.go @@ -15,6 +15,7 @@ package standalone import ( "os" + path_filepath "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -304,3 +305,42 @@ func TestIsAirGapInit(t *testing.T) { }) } } + +func TestCopyFilesAndCreateSymlink(t *testing.T) { + // create a temp dir to hold the symlink and actual directory. + tempDir := createTempDir(t, "dapr-test", "") + defer cleanupTempDir(t, tempDir) + destDir := createTempDir(t, "dest", tempDir) + srcDir := createTempDir(t, "src", tempDir) + srcFile := createTempFile(t, srcDir, "pubsub.yaml") + tests := []struct { + name string + destDirName string + srcDirName string + expectedError bool + presentFileName string + }{ + { + name: "copy files and create symlink for destination directory when source dir exists", + destDirName: destDir, + srcDirName: srcDir, + expectedError: false, + presentFileName: srcFile, + }, + { + name: "copy files and create symlink for destination directory when source dir does not exists", + destDirName: destDir, + srcDirName: path_filepath.Join(tempDir, "non-existent-source-dir"), + expectedError: false, + presentFileName: path_filepath.Base(srcFile), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := copyFilesAndCreateSymlink(tt.srcDirName, tt.destDirName) + assert.Equal(t, tt.expectedError, err != nil) + // check if the files are copied. + assert.FileExists(t, path_filepath.Join(tt.srcDirName, path_filepath.Base(tt.presentFileName))) + }) + } +} From 968b5ca9c779616e7e64315032254358df331fd1 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 3 Jan 2023 23:47:55 +0530 Subject: [PATCH 28/30] fix tests Signed-off-by: Pravin Pushkar --- pkg/standalone/run_test.go | 10 +++++----- tests/e2e/standalone/stop_test.go | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/standalone/run_test.go b/pkg/standalone/run_test.go index d50825a7b..99d583e30 100644 --- a/pkg/standalone/run_test.go +++ b/pkg/standalone/run_test.go @@ -78,7 +78,7 @@ func setupRun(t *testing.T) { resourcesDir := GetDaprResourcesPath(myDaprPath) configFile := GetDaprConfigPath(myDaprPath) err = os.MkdirAll(resourcesDir, 0o700) - assert.Equal(t, nil, err, "Unable to setup components dir before running test") + assert.Equal(t, nil, err, "Unable to setup resources dir before running test") file, err := os.Create(configFile) file.Close() assert.Equal(t, nil, err, "Unable to create config file before running test") @@ -92,7 +92,7 @@ func tearDownRun(t *testing.T) { configFile := GetDaprConfigPath(myDaprPath) err = os.RemoveAll(componentsDir) - assert.Equal(t, nil, err, "Unable to delete default components dir after running test") + assert.Equal(t, nil, err, "Unable to delete default resources dir after running test") err = os.Remove(configFile) assert.Equal(t, nil, err, "Unable to delete default config file after running test") } @@ -115,7 +115,7 @@ func assertCommonArgs(t *testing.T, basicConfig *RunConfig, output *RunOutput) { assertArgumentEqual(t, "app-max-concurrency", "-1", output.DaprCMD.Args) assertArgumentEqual(t, "app-protocol", "http", output.DaprCMD.Args) assertArgumentEqual(t, "app-port", "3000", output.DaprCMD.Args) - assertArgumentEqual(t, "components-path", GetDaprComponentsPath(daprPath), output.DaprCMD.Args) + assertArgumentEqual(t, "components-path", GetDaprResourcesPath(daprPath), output.DaprCMD.Args) assertArgumentEqual(t, "app-ssl", "", output.DaprCMD.Args) assertArgumentEqual(t, "metrics-port", "9001", output.DaprCMD.Args) assertArgumentEqual(t, "dapr-http-max-request-size", "-1", output.DaprCMD.Args) @@ -169,14 +169,14 @@ func TestRun(t *testing.T) { myDaprPath, err := GetDaprPath("") assert.NoError(t, err) - componentsDir := GetDaprComponentsPath(myDaprPath) + resourcesDir := GetDaprResourcesPath(myDaprPath) configFile := GetDaprConfigPath(myDaprPath) sharedRunConfig := &SharedRunConfig{ LogLevel: "WARN", EnableProfiling: false, AppProtocol: "http", - ComponentsPath: componentsDir, + ComponentsPath: resourcesDir, AppSSL: true, MaxRequestBodySize: -1, HTTPReadBufferSize: -1, diff --git a/tests/e2e/standalone/stop_test.go b/tests/e2e/standalone/stop_test.go index f6d1b9212..1155b25b9 100644 --- a/tests/e2e/standalone/stop_test.go +++ b/tests/e2e/standalone/stop_test.go @@ -24,6 +24,8 @@ import ( ) func TestStandaloneStop(t *testing.T) { + // Ensure a clean environment. + must(t, cmdUninstallAll, "failed to uninstall Dapr") ensureDaprInstallation(t) executeAgainstRunningDapr(t, func() { From ed1169cb8b4b040939f2b0a3a468432ac8323ddf Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Wed, 4 Jan 2023 14:42:43 +0530 Subject: [PATCH 29/30] change few uninstall to uninstallAll and add cleanup in upgrade test Signed-off-by: Pravin Pushkar --- tests/e2e/standalone/init_test.go | 6 +++--- tests/e2e/standalone/run_test.go | 8 ++++---- tests/e2e/standalone/stop_test.go | 1 - tests/e2e/standalone/upgrade_test.go | 5 +++++ tests/e2e/standalone/version_test.go | 6 +++--- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/e2e/standalone/init_test.go b/tests/e2e/standalone/init_test.go index d68836235..13d6bca74 100644 --- a/tests/e2e/standalone/init_test.go +++ b/tests/e2e/standalone/init_test.go @@ -130,7 +130,7 @@ func TestStandaloneInit(t *testing.T) { t.Run("init with --dapr-path flag", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") daprPath, err := os.MkdirTemp("", "dapr-e2e-init-with-flag-*") assert.NoError(t, err) @@ -148,7 +148,7 @@ func TestStandaloneInit(t *testing.T) { t.Run("init with DAPR_PATH env var", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") daprPath, err := os.MkdirTemp("", "dapr-e2e-init-with-env-var-*") assert.NoError(t, err) @@ -168,7 +168,7 @@ func TestStandaloneInit(t *testing.T) { t.Run("init with --dapr-path flag and DAPR_PATH env var", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") daprPath1, err := os.MkdirTemp("", "dapr-e2e-init-with-flag-and-env-1-*") assert.NoError(t, err) diff --git a/tests/e2e/standalone/run_test.go b/tests/e2e/standalone/run_test.go index 31355ac9d..478e14ef0 100644 --- a/tests/e2e/standalone/run_test.go +++ b/tests/e2e/standalone/run_test.go @@ -178,12 +178,12 @@ func TestStandaloneRun(t *testing.T) { func TestStandaloneRunNonDefaultDaprPath(t *testing.T) { // Uninstall Dapr at the end of the test since it's being installed in a non-default location. t.Cleanup(func() { - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") }) t.Run("run with flag", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") daprPath, err := os.MkdirTemp("", "dapr-e2e-run-with-flag-*") assert.NoError(t, err) @@ -216,7 +216,7 @@ func TestStandaloneRunNonDefaultDaprPath(t *testing.T) { t.Run("run with env var", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") daprPath, err := os.MkdirTemp("", "dapr-e2e-run-with-env-*") assert.NoError(t, err) @@ -251,7 +251,7 @@ func TestStandaloneRunNonDefaultDaprPath(t *testing.T) { t.Run("run with both flag and env var", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") daprPathForEnv, err := os.MkdirTemp("", "dapr-e2e-run-with-envflag-1-*") assert.NoError(t, err) diff --git a/tests/e2e/standalone/stop_test.go b/tests/e2e/standalone/stop_test.go index 1155b25b9..57de2dfd3 100644 --- a/tests/e2e/standalone/stop_test.go +++ b/tests/e2e/standalone/stop_test.go @@ -25,7 +25,6 @@ import ( func TestStandaloneStop(t *testing.T) { // Ensure a clean environment. - must(t, cmdUninstallAll, "failed to uninstall Dapr") ensureDaprInstallation(t) executeAgainstRunningDapr(t, func() { diff --git a/tests/e2e/standalone/upgrade_test.go b/tests/e2e/standalone/upgrade_test.go index 37e1fea4e..0f5b293e9 100644 --- a/tests/e2e/standalone/upgrade_test.go +++ b/tests/e2e/standalone/upgrade_test.go @@ -37,6 +37,11 @@ var ( // Tests precedence for --components-path and --resources-path flags. func TestResourcesLoadPrecedence(t *testing.T) { + t.Cleanup(func() { + // Ensure environment is clean after tests are complete. + must(t, cmdUninstallAll, "failed to uninstall Dapr") + }) + homeDir, err := os.UserHomeDir() assert.NoError(t, err, "cannot get user home directory") defaultComponentsDirPath = filepath.Join(homeDir, ".dapr", utils.DefaultComponentsDirName) diff --git a/tests/e2e/standalone/version_test.go b/tests/e2e/standalone/version_test.go index 38ea1e0c1..99889e675 100644 --- a/tests/e2e/standalone/version_test.go +++ b/tests/e2e/standalone/version_test.go @@ -55,7 +55,7 @@ func TestStandaloneVersion(t *testing.T) { func TestStandaloneVersionNonDefaultDaprPath(t *testing.T) { t.Run("version with flag", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") daprPath, err := os.MkdirTemp("", "dapr-e2e-ver-with-flag-*") assert.NoError(t, err) @@ -84,7 +84,7 @@ func TestStandaloneVersionNonDefaultDaprPath(t *testing.T) { t.Run("version with env var", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") daprPath, err := os.MkdirTemp("", "dapr-e2e-ver-with-env-*") assert.NoError(t, err) @@ -115,7 +115,7 @@ func TestStandaloneVersionNonDefaultDaprPath(t *testing.T) { t.Run("version with both flag and env var", func(t *testing.T) { // Ensure a clean environment - must(t, cmdUninstall, "failed to uninstall Dapr") + must(t, cmdUninstallAll, "failed to uninstall Dapr") daprPath1, err := os.MkdirTemp("", "dapr-e2e-ver-with-both-flag-*") assert.NoError(t, err) From d605587f5a278bb27e2443ff1369920efda080cd Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 28 Feb 2023 22:53:12 +0530 Subject: [PATCH 30/30] lint fix Signed-off-by: Pravin Pushkar --- pkg/standalone/common.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index a60e19d7c..5c9a3016c 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -29,8 +29,7 @@ const ( DefaultConfigFileName = "config.yaml" DefaultResourcesDirName = "resources" - defaultDaprBinDirName = "bin" - defaultComponentsDirName = "components" + defaultDaprBinDirName = "bin" ) // GetDaprRuntimePath returns the dapr runtime installation path.