Repository navigation
🌱 Add unit tests for terminal error helpers #3204
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
andretorresdg
wants to merge
2
commits into
operator-framework:main
Choose a base branch
from
andretorresdg:test-terminal-error
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| package error | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| ) | ||
|
|
||
| func TestNewTerminalError(t *testing.T) { | ||
| inner := errors.New("boom") | ||
| err := NewTerminalError("SomeReason", inner) | ||
|
|
||
| require.ErrorIs(t, err, reconcile.TerminalError(nil), "error should be a terminal error") | ||
| require.ErrorIs(t, err, inner, "error should wrap the inner error") | ||
| require.Equal(t, "terminal error: boom", err.Error()) | ||
|
|
||
| reason, ok := ExtractTerminalReason(err) | ||
| require.True(t, ok) | ||
| require.Equal(t, "SomeReason", reason) | ||
| } | ||
|
|
||
| func TestExtractTerminalReason(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| err error | ||
| wantReason string | ||
| wantOK bool | ||
| }{ | ||
| { | ||
| name: "nil error", | ||
| err: nil, | ||
| }, | ||
| { | ||
| name: "plain error", | ||
| err: errors.New("boom"), | ||
| }, | ||
| { | ||
| name: "terminal error without a reason", | ||
| err: reconcile.TerminalError(errors.New("boom")), | ||
| }, | ||
| { | ||
| name: "terminal error without a reason created by WrapTerminal", | ||
| err: WrapTerminal(errors.New("boom"), true), | ||
| }, | ||
| { | ||
| name: "terminal error with a reason", | ||
| err: NewTerminalError("SomeReason", errors.New("boom")), | ||
| wantReason: "SomeReason", | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| name: "terminal error with a reason wrapped by another error", | ||
| err: fmt.Errorf("context: %w", NewTerminalError("SomeReason", errors.New("boom"))), | ||
| wantReason: "SomeReason", | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| // ExtractTerminalReason only unwraps one level, so a terminal error | ||
| // hidden behind errors.Join is not found. | ||
| name: "terminal error with a reason joined with another error", | ||
| err: errors.Join(errors.New("other"), NewTerminalError("SomeReason", errors.New("boom"))), | ||
| }, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| reason, ok := ExtractTerminalReason(tc.err) | ||
| require.Equal(t, tc.wantOK, ok) | ||
| require.Equal(t, tc.wantReason, reason) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestWrapTerminal(t *testing.T) { | ||
| inner := errors.New("boom") | ||
|
|
||
| t.Run("nil error stays nil when terminal", func(t *testing.T) { | ||
| require.NoError(t, WrapTerminal(nil, true)) | ||
| }) | ||
| t.Run("nil error stays nil when not terminal", func(t *testing.T) { | ||
| require.NoError(t, WrapTerminal(nil, false)) | ||
| }) | ||
| t.Run("non-terminal returns the original error", func(t *testing.T) { | ||
| err := WrapTerminal(inner, false) | ||
| require.Same(t, inner, err) | ||
| require.NotErrorIs(t, err, reconcile.TerminalError(nil)) | ||
| }) | ||
| t.Run("terminal wraps the original error", func(t *testing.T) { | ||
| err := WrapTerminal(inner, true) | ||
| require.ErrorIs(t, err, reconcile.TerminalError(nil)) | ||
| require.ErrorIs(t, err, inner) | ||
| }) | ||
| t.Run("terminal wraps an error that already wraps another error", func(t *testing.T) { | ||
| wrapped := fmt.Errorf("context: %w", inner) | ||
| err := WrapTerminal(wrapped, true) | ||
| require.ErrorIs(t, err, reconcile.TerminalError(nil)) | ||
| require.ErrorIs(t, err, inner) | ||
| require.Equal(t, "terminal error: context: boom", err.Error()) | ||
| }) | ||
| t.Run("non-terminal leaves a wrapped terminal error terminal", func(t *testing.T) { | ||
| wrapped := fmt.Errorf("context: %w", reconcile.TerminalError(inner)) | ||
| err := WrapTerminal(wrapped, false) | ||
| require.Same(t, wrapped, err) | ||
| require.ErrorIs(t, err, reconcile.TerminalError(nil)) | ||
| }) | ||
| } | ||
|
|
||
| func TestUnwrapTerminal(t *testing.T) { | ||
| inner := errors.New("boom") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| err error | ||
| want error | ||
| // wantTerminal is true when the returned error is expected to still be | ||
| // a terminal error. | ||
| wantTerminal bool | ||
| }{ | ||
| { | ||
| name: "nil error", | ||
| err: nil, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "plain error is returned unchanged", | ||
| err: inner, | ||
| want: inner, | ||
| }, | ||
| { | ||
| name: "terminal error returns the inner error", | ||
| err: reconcile.TerminalError(inner), | ||
| want: inner, | ||
| }, | ||
| { | ||
| name: "terminal error with a reason returns the inner error", | ||
| err: NewTerminalError("SomeReason", inner), | ||
| want: inner, | ||
| }, | ||
| { | ||
| // UnwrapTerminal sees the terminal error through the wrapper, but | ||
| // only strips one level: the wrapper is removed and the terminal | ||
| // error underneath is returned as is. | ||
| name: "error wrapping a terminal error returns the terminal error", | ||
| err: fmt.Errorf("context: %w", reconcile.TerminalError(inner)), | ||
| want: reconcile.TerminalError(inner), | ||
| wantTerminal: true, | ||
| }, | ||
| { | ||
| name: "error wrapping a terminal error with a reason returns the terminal error", | ||
| err: fmt.Errorf("context: %w", NewTerminalError("SomeReason", inner)), | ||
| want: NewTerminalError("SomeReason", inner), | ||
| wantTerminal: true, | ||
| }, | ||
| { | ||
| // errors.Join has no single underlying error, so the joined error | ||
| // is returned unchanged. | ||
| name: "joined error containing a terminal error is returned unchanged", | ||
| err: errors.Join(errors.New("other"), reconcile.TerminalError(inner)), | ||
| want: errors.Join(errors.New("other"), reconcile.TerminalError(inner)), | ||
| wantTerminal: true, | ||
| }, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got := UnwrapTerminal(tc.err) | ||
| if tc.want == nil { | ||
| require.NoError(t, got) | ||
| return | ||
| } | ||
| require.EqualError(t, got, tc.want.Error()) | ||
| if tc.wantTerminal { | ||
| require.ErrorIs(t, got, reconcile.TerminalError(nil)) | ||
| } else { | ||
| require.NotErrorIs(t, got, reconcile.TerminalError(nil)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add a test for an error that wraps a terminal error?