Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename components directory to resources under .dapr directory #1149

Draft
wants to merge 38 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
bd3657f
set dapr run defaults precedence
pravinpushkar Dec 9, 2022
4e6d8b5
dapr init create resources dir and moves existing components content …
pravinpushkar Dec 9, 2022
8358a21
modify tests
pravinpushkar Dec 9, 2022
c3f41e9
fix test
pravinpushkar Dec 9, 2022
6affa75
fix tests
pravinpushkar Dec 12, 2022
1b421b0
add tests
pravinpushkar Dec 12, 2022
0363365
trigger pr checks
pravinpushkar Dec 12, 2022
d9caa15
Update pkg/standalone/common.go
pravinpushkar Dec 13, 2022
2007ed5
Apply suggestions from code review
pravinpushkar Dec 13, 2022
bf0e7ff
review comments
pravinpushkar Dec 13, 2022
d8422a7
review comments
pravinpushkar Dec 13, 2022
e73ec7d
fix error handle on path not present
pravinpushkar Dec 13, 2022
093239b
fix tests
pravinpushkar Dec 13, 2022
5df0795
trigger pr checks
pravinpushkar Dec 13, 2022
125ebc8
readme changes and some more messages today
pravinpushkar Dec 14, 2022
3b55cd5
trigger pr checks
pravinpushkar Dec 14, 2022
3c8020b
Update README.md
pravinpushkar Dec 14, 2022
ff6ce5d
update readme
pravinpushkar Dec 14, 2022
5334d15
Typo
shubham1172 Dec 14, 2022
57cfa8a
Type (2)
shubham1172 Dec 14, 2022
15a894c
Merge branch 'master' into feat/resources_dir
mukundansundar Dec 15, 2022
a4c5f81
few more refactoring
pravinpushkar Dec 15, 2022
03d190a
Merge branch 'master' into feat/resources_dir
mukundansundar Dec 19, 2022
5a624af
fix review comment and failing merge
pravinpushkar Dec 20, 2022
3989aad
make symlink
pravinpushkar Dec 24, 2022
0749c5c
Merge branch 'master' into feat/resources_dir
pravinpushkar Dec 26, 2022
4a421d5
fix tests
pravinpushkar Dec 26, 2022
f73af11
review comments
pravinpushkar Dec 29, 2022
e7ccf04
fix tests
pravinpushkar Dec 29, 2022
771b256
some more refactor
pravinpushkar Dec 30, 2022
c5646d4
merge master and fix conflicts
pravinpushkar Jan 3, 2023
968b5ca
fix tests
pravinpushkar Jan 3, 2023
ed1169c
change few uninstall to uninstallAll and add cleanup in upgrade test
pravinpushkar Jan 4, 2023
5871ad4
Merge branch 'master' into feat/resources_dir
mukundansundar Jan 12, 2023
771d423
Merge branch 'master' into feat/resources_dir
shubham1172 Jan 16, 2023
e471c28
fix conflicts & merge master
pravinpushkar Feb 28, 2023
e2794b0
Merge branch 'master' into feat/resources_dir
pravinpushkar Feb 28, 2023
d605587
lint fix
pravinpushkar Feb 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
pravinpushkar marked this conversation as resolved.
Show resolved Hide resolved
// 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.
Expand Down
54 changes: 54 additions & 0 deletions pkg/standalone/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ import (
"os"
path_filepath "path/filepath"
"runtime"

"github.com/dapr/cli/pkg/print"
)

const (
defaultDaprDirName = ".dapr"
defaultDaprBinDirName = "bin"
defaultComponentsDirName = "components"
defaultResourcesDirName = "resources"
defaultConfigFileName = "config.yaml"
)

Expand All @@ -47,6 +50,57 @@ func DefaultComponentsDirPath() string {
return path_filepath.Join(defaultDaprDirPath(), defaultComponentsDirName)
}

func DefaultResourcesDirPath() string {
return path_filepath.Join(defaultDaprDirPath(), 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 {
shubham1172 marked this conversation as resolved.
Show resolved Hide resolved
defaultResourcesDirPath := DefaultResourcesDirPath()
if _, err := os.Stat(defaultResourcesDirPath); os.IsNotExist(err) {
return DefaultComponentsDirPath()
}
return defaultResourcesDirPath
}

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.
// TODO: Remove this function when `--components-path` flag is removed.
func emptyAndCopyFiles(src, dest string) error {
pravinpushkar marked this conversation as resolved.
Show resolved Hide resolved
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())
pravinpushkar marked this conversation as resolved.
Show resolved Hide resolved
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 {
content, err := os.ReadFile(src + "/" + file.Name())
pravinpushkar marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
// #nosec G306
err = os.WriteFile(dest+"/"+file.Name(), content, 0o644)
if err != nil {
return err
}
}
}
return nil
}
8 changes: 4 additions & 4 deletions pkg/standalone/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 18 additions & 12 deletions pkg/standalone/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ func Init(runtimeVersion, dashboardVersion string, dockerNetwork string, slimMod
stopSpinning := print.Spinner(os.Stdout, msg)
defer stopSpinning(print.Failure)

// Make default components directory.
err = makeDefaultComponentsDir()
// Make default resources directory.
err = makeDefaultResourcesDir()
if err != nil {
return err
}
Expand Down Expand Up @@ -295,9 +295,10 @@ 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)
ok, err := confirmContainerIsRunningOrExists(containerName, true, runtimeCmd)
ok, err = confirmContainerIsRunningOrExists(containerName, true, runtimeCmd)
if err != nil {
return err
}
Expand All @@ -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 = emptyAndCopyFiles(DefaultComponentsDirPath(), DefaultResourcesDirPath())
pravinpushkar marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
return fmt.Errorf("error creating default resources folder: %w", errDir)
}
}

os.Chmod(componentsDir, 0o777)
os.Chmod(resourcesDir, 0o777)
return nil
}

Expand Down
131 changes: 131 additions & 0 deletions tests/e2e/standalone/componentsDir_to_resourcesDir_mig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//go:build e2e
shubham1172 marked this conversation as resolved.
Show resolved Hide resolved
// +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.
*/

// TODO: Remove the test when `--components-path` flag is removed.
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)
}
}
8 changes: 4 additions & 4 deletions tests/e2e/standalone/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
Expand All @@ -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{}{
Expand All @@ -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{}{
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/standalone/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/standalone/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
pravinpushkar marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err, "failed to create components")
}
}
Expand Down