Repository navigation
Implements the user errors feature for RCV2 #90
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
Open
Joseph Calev (jscalev)
wants to merge
7
commits into
main
Choose a base branch
from
dev/jcalev/ImplementUserErrors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2dabdee
Implements the user errors feature for RCV2
jscalev 20fa6aa
PR feedback
jscalev 46f3f1f
PR comments
jscalev 1300217
Merge branch 'main' into dev/jcalev/ImplementUserErrors
jscalev b585c1b
Fixes build breaks
jscalev b3b6417
Renames function
jscalev c7ece9b
PR review
jscalev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/Azure/run-command-handler-linux/pkg/errorutil" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCommandLineClarifications(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| args []string | ||
| code int | ||
| }{ | ||
| {nil, errorutil.CustomerInput_invalidCommandLineArguments}, | ||
| {[]string{"handler"}, errorutil.CustomerInput_invalidCommandLineArguments}, | ||
| {[]string{"handler", "enable", "extra"}, errorutil.CustomerInput_invalidCommandLineArguments}, | ||
| {[]string{"handler", "invalid"}, errorutil.CustomerInput_unknownCommandLineOperation}, | ||
| } { | ||
| _, err := parseCommand(tc.args) | ||
| code, ok := errorutil.FindClarification(err) | ||
| require.True(t, ok) | ||
| require.Equal(t, tc.code, code) | ||
| } | ||
| command, err := parseCommand([]string{"handler", "enable"}) | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, command.Name) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package cleanup | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/Azure/run-command-handler-linux/internal/types" | ||
| "github.com/Azure/run-command-handler-linux/pkg/errorutil" | ||
| "github.com/go-kit/kit/log" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type cleanupErrorLogger struct{ err error } | ||
|
|
||
| type cleanupDirectory struct{} | ||
|
|
||
| func (cleanupDirectory) Close() error { return nil } | ||
|
|
||
| func (l *cleanupErrorLogger) Log(values ...interface{}) error { | ||
| for _, value := range values { | ||
| if err, ok := value.(error); ok { | ||
| l.err = err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func TestCleanupClarifications(t *testing.T) { | ||
| oldClear, oldDelete, oldOpen := clearExceptMostRecent, deleteDirectoriesExcept, openDownloadDirectory | ||
| t.Cleanup(func() { | ||
| clearExceptMostRecent, deleteDirectoriesExcept, openDownloadDirectory = oldClear, oldDelete, oldOpen | ||
| }) | ||
| cause := errors.New("machine failure") | ||
| for _, want := range []int{errorutil.Os_FailedToCleanOldScriptsAndSettings, errorutil.Os_FailedToOpenDownloadDirectory, errorutil.Os_FailedToCleanOldRunAsDirectories} { | ||
| clearExceptMostRecent = func(*log.Context, string, string, string, uint64, string, string) error { | ||
| if want == errorutil.Os_FailedToCleanOldScriptsAndSettings { | ||
| return cause | ||
| } | ||
| return nil | ||
| } | ||
| deleteDirectoriesExcept = func(*log.Context, string, string) error { return cause } | ||
| openDownloadDirectory = func(string) (io.Closer, error) { | ||
| if want == errorutil.Os_FailedToOpenDownloadDirectory { | ||
| return nil, cause | ||
| } | ||
| return cleanupDirectory{}, nil | ||
| } | ||
| logger := &cleanupErrorLogger{} | ||
| user := "" | ||
| if want == errorutil.Os_FailedToCleanOldRunAsDirectories { | ||
| user = "test" | ||
| } | ||
| RunCommandCleanup(log.NewContext(logger), types.RCMetadata{DownloadPath: "."}, types.HandlerEnvironment{}, user) | ||
| code, ok := errorutil.FindClarification(logger.err) | ||
| require.True(t, ok) | ||
| require.Equal(t, want, code) | ||
| require.ErrorIs(t, logger.err, cause) | ||
| } | ||
| } | ||
|
|
||
| func TestCleanupPreservesLowerClarification(t *testing.T) { | ||
| oldClear, oldDelete, oldOpen := clearExceptMostRecent, deleteDirectoriesExcept, openDownloadDirectory | ||
| t.Cleanup(func() { | ||
| clearExceptMostRecent, deleteDirectoriesExcept, openDownloadDirectory = oldClear, oldDelete, oldOpen | ||
| }) | ||
| cause := errors.New("directory read failed") | ||
| classified := errorutil.EnsureClarification(errorutil.Os_FailedToReadCleanupParentDirectory, cause) | ||
| openDownloadDirectory = func(string) (io.Closer, error) { return cleanupDirectory{}, nil } | ||
| for _, user := range []string{"", "test"} { | ||
| t.Run("runAsUser="+user, func(t *testing.T) { | ||
| clearExceptMostRecent = func(*log.Context, string, string, string, uint64, string, string) error { | ||
| if user == "" { | ||
| return classified | ||
| } | ||
| return nil | ||
| } | ||
| deleteDirectoriesExcept = func(*log.Context, string, string) error { return classified } | ||
| logger := &cleanupErrorLogger{} | ||
| RunCommandCleanup(log.NewContext(logger), types.RCMetadata{DownloadPath: "."}, types.HandlerEnvironment{}, user) | ||
| code, ok := errorutil.FindClarification(logger.err) | ||
| require.True(t, ok) | ||
| require.Equal(t, errorutil.Os_FailedToReadCleanupParentDirectory, code) | ||
| require.ErrorIs(t, logger.err, cause) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.