Repository navigation
fix(tunnel): preserve agent errors on handler cancellation #1808
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
Merged
Merged
Changes from all commits
Commits
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,21 @@ | ||
| package sshtunnel | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| type streamerSafetySuite struct{ suite.Suite } | ||
|
|
||
| func TestStreamerSafetySuite(t *testing.T) { | ||
| suite.Run(t, new(streamerSafetySuite)) | ||
| } | ||
|
|
||
| func (s *streamerSafetySuite) TestAgentOutputIsNotRetainedForErrors() { | ||
| streamer := newSSHTunnelJSONLogStreamer() | ||
| _, err := streamer.Write([]byte("synthetic-workspace-credential\n")) | ||
| s.Require().NoError(err) | ||
| s.Require().NoError(streamer.Close()) | ||
| s.Empty(streamer.ErrorOutput()) | ||
| } |
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,197 @@ | ||
| package tunnel | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| config2 "github.com/devsy-org/devsy/pkg/devcontainer/config" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| type pipeBridgeClassificationSuite struct{ suite.Suite } | ||
|
|
||
| func TestPipeBridgeClassificationSuite(t *testing.T) { | ||
| suite.Run(t, new(pipeBridgeClassificationSuite)) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestTunnelErrorWinsOverInternalCancellation() { | ||
| agentErr := errors.New("agent command failed") | ||
| err := s.runPair( | ||
| context.Background(), | ||
| func(context.Context, *os.File, *os.File) error { return agentErr }, | ||
| func(ctx context.Context, _, _ *os.File) error { <-ctx.Done(); return ctx.Err() }, | ||
| ) | ||
| s.ErrorIs(err, agentErr) | ||
|
skevetter marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestTunnelErrorAfterTeardownDoesNotOverrideHandler() { | ||
| err := s.runPair( | ||
| context.Background(), | ||
| func(ctx context.Context, _, _ *os.File) error { <-ctx.Done(); return errors.New("teardown failed") }, | ||
| func(context.Context, *os.File, *os.File) error { return context.Canceled }, | ||
| ) | ||
| s.ErrorIs(err, context.Canceled) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestEarlierTunnelFailureWinsOverHandlerFailure() { | ||
| agentErr := errors.New("agent command failed") | ||
| handlerErr := errors.New("handler failed") | ||
| err := s.runPair( | ||
| context.Background(), | ||
| func(context.Context, *os.File, *os.File) error { return agentErr }, | ||
| func(ctx context.Context, _, _ *os.File) error { <-ctx.Done(); return handlerErr }, | ||
| ) | ||
| s.ErrorIs(err, agentErr) | ||
| s.NotErrorIs(err, handlerErr) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestTunnelErrorWinsOverClosedHandlerPipe() { | ||
| agentErr := errors.New("agent command failed") | ||
| err := s.runPair(context.Background(), | ||
| func(context.Context, *os.File, *os.File) error { return agentErr }, | ||
| func(ctx context.Context, _, _ *os.File) error { | ||
| <-ctx.Done() | ||
| return fmt.Errorf("write: %w", os.ErrClosed) | ||
| }, | ||
| ) | ||
| s.ErrorIs(err, agentErr) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestCompletedHandlerResultSurvivesTunnelExit() { | ||
| var result *config2.Result | ||
| resultSent := make(chan struct{}) | ||
| err := s.runPair( | ||
| context.Background(), | ||
| func(context.Context, *os.File, *os.File) error { | ||
| <-resultSent | ||
| return errors.New("SSH command failed with exit code 1") | ||
| }, | ||
| func(ctx context.Context, _, _ *os.File) error { | ||
| result = &config2.Result{Error: "workspace changed; use --recreate"} | ||
| close(resultSent) | ||
| <-ctx.Done() | ||
| return nil | ||
| }, | ||
| ) | ||
| s.NoError(err) | ||
| s.Require().NotNil(result) | ||
| s.EqualError(result.Err(), "workspace changed; use --recreate") | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestHandlerFailureRecordedBeforeTunnelError() { | ||
| tunnelStarted := make(chan struct{}) | ||
| handlerErr := errors.New("handler failed") | ||
| err := s.runPair(context.Background(), | ||
| func(ctx context.Context, _, _ *os.File) error { | ||
| close(tunnelStarted) | ||
| <-ctx.Done() | ||
| return errors.New("closed resource") | ||
| }, | ||
| func(context.Context, *os.File, *os.File) error { | ||
| <-tunnelStarted | ||
| return handlerErr | ||
| }, | ||
| ) | ||
| s.ErrorIs(err, handlerErr) | ||
| s.Equal("tunnel to container: handler failed", err.Error()) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestSuccessfulTunnelBeforeHandlerReportsIncompleteOperation() { | ||
| err := s.runPair(context.Background(), | ||
| func(context.Context, *os.File, *os.File) error { return nil }, | ||
| func(ctx context.Context, _, _ *os.File) error { <-ctx.Done(); return ctx.Err() }, | ||
| ) | ||
| s.EqualError(err, "tunnel closed before handler completed") | ||
| s.NotErrorIs(err, context.Canceled) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestHandlerDeadlineAfterSuccessfulTunnel() { | ||
| err := s.runPair( | ||
| context.Background(), | ||
| func(context.Context, *os.File, *os.File) error { return nil }, | ||
| func(ctx context.Context, _, _ *os.File) error { <-ctx.Done(); return context.DeadlineExceeded }, | ||
| ) | ||
| s.ErrorIs(err, context.DeadlineExceeded) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestParentCancellationWins() { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| agentErr := errors.New("agent command failed") | ||
| err := s.runPair( | ||
| ctx, | ||
| func(context.Context, *os.File, *os.File) error { cancel(); return agentErr }, | ||
| func(ctx context.Context, _, _ *os.File) error { <-ctx.Done(); return ctx.Err() }, | ||
| ) | ||
| s.ErrorIs(err, context.Canceled) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestHandlerDeadlinePreservesTunnelError() { | ||
| agentErr := errors.New("agent command failed") | ||
| err := s.runPair( | ||
| context.Background(), | ||
| func(context.Context, *os.File, *os.File) error { return agentErr }, | ||
| func(ctx context.Context, _, _ *os.File) error { | ||
| <-ctx.Done() | ||
| return fmt.Errorf("handler: %w", context.DeadlineExceeded) | ||
| }, | ||
| ) | ||
| s.ErrorIs(err, agentErr) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestParentDeadlineWins() { | ||
| ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second)) | ||
| defer cancel() | ||
| err := s.runPair( | ||
| ctx, | ||
| func(context.Context, *os.File, *os.File) error { return errors.New("agent failed") }, | ||
| func(ctx context.Context, _, _ *os.File) error { <-ctx.Done(); return ctx.Err() }, | ||
| ) | ||
| s.ErrorIs(err, context.DeadlineExceeded) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestWrappedTunnelCancellationDoesNotReplaceHandlerError() { | ||
| for _, tunnelErr := range []error{context.Canceled, context.DeadlineExceeded} { | ||
| err := s.runPair( | ||
| context.Background(), | ||
| func(context.Context, *os.File, *os.File) error { return fmt.Errorf("tunnel: %w", tunnelErr) }, | ||
| func(ctx context.Context, _, _ *os.File) error { <-ctx.Done(); return context.Canceled }, | ||
| ) | ||
| s.Equal("tunnel to container: context canceled", err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestBothNil() { | ||
| err := s.runPair( | ||
| context.Background(), | ||
| func(context.Context, *os.File, *os.File) error { return nil }, | ||
| func(context.Context, *os.File, *os.File) error { return nil }, | ||
| ) | ||
| s.NoError(err) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) TestConnectionErrorClassification() { | ||
| agentErr := errors.New("dial failed") | ||
| err := s.runPair( | ||
| context.Background(), | ||
| func(context.Context, *os.File, *os.File) error { return agentErr }, | ||
| func(context.Context, *os.File, *os.File) error { return io.EOF }, | ||
| ) | ||
| s.ErrorIs(err, agentErr) | ||
| s.Equal("connect to server: dial failed", err.Error()) | ||
| } | ||
|
|
||
| func (s *pipeBridgeClassificationSuite) runPair( | ||
| ctx context.Context, | ||
| tunnelFn TunnelFunc, | ||
| handlerFn HandlerFunc, | ||
| ) error { | ||
| bridge, err := NewPipeBridge() | ||
| s.Require().NoError(err) | ||
| defer bridge.Close() | ||
| return bridge.RunPair(ctx, tunnelFn, handlerFn) | ||
| } | ||
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,67 @@ | ||
| package tunnel_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| agenttunnel "github.com/devsy-org/devsy/pkg/agent/tunnel" | ||
| "github.com/devsy-org/devsy/pkg/agent/tunnelserver" | ||
| config2 "github.com/devsy-org/devsy/pkg/devcontainer/config" | ||
| "github.com/devsy-org/devsy/pkg/stdio" | ||
| "github.com/devsy-org/devsy/pkg/tunnel" | ||
| "github.com/stretchr/testify/suite" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| ) | ||
|
|
||
| type bridgeResultSuite struct{ suite.Suite } | ||
|
|
||
| func TestBridgeResultSuite(t *testing.T) { suite.Run(t, new(bridgeResultSuite)) } | ||
|
|
||
| func (s *bridgeResultSuite) TestForwardedAgentErrorSurvivesSSHExit() { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) | ||
| defer cancel() | ||
| bridge, err := tunnel.NewPipeBridge() | ||
| s.Require().NoError(err) | ||
| defer bridge.Close() | ||
| var result *config2.Result | ||
| err = bridge.RunPair(ctx, | ||
| func(ctx context.Context, stdin, stdout *os.File) error { | ||
| conn, err := grpc.NewClient( | ||
| "passthrough:///agent", | ||
| grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
| grpc.WithContextDialer( | ||
| func(context.Context, string) (net.Conn, error) { return stdio.NewStdioStream(stdin, stdout), nil }, | ||
| ), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { _ = conn.Close() }() | ||
| _, err = tunnelserver.ReportResult( | ||
| ctx, | ||
| agenttunnel.NewTunnelClient(conn), | ||
| func(context.Context) (*config2.Result, error) { | ||
| return nil, errors.New("workspace changed; use --recreate") | ||
| }, | ||
| ) | ||
| if err == nil { | ||
| return errors.New("expected agent failure") | ||
| } | ||
| return errors.New("SSH command failed with exit code 1") | ||
| }, | ||
| func(ctx context.Context, stdout, stdin *os.File) error { | ||
| var handlerErr error | ||
| result, handlerErr = tunnelserver.New().RunWithResult(ctx, stdout, stdin) | ||
| <-ctx.Done() | ||
| return handlerErr | ||
| }, | ||
| ) | ||
| s.Require().NoError(err) | ||
| s.Require().NotNil(result) | ||
| s.EqualError(result.Err(), "workspace changed; use --recreate") | ||
| } |
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.