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