-
Notifications
You must be signed in to change notification settings - Fork 328
implement UFFD failure handling and unit tests #2856
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
AdaAibaby
wants to merge
2
commits into
e2b-dev:main
Choose a base branch
from
AdaAibaby:feat/uffd-failure-handling-unit-test
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
61 changes: 61 additions & 0 deletions
61
packages/orchestrator/pkg/sandbox/uffd/testutils/mock_memfile.go
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,61 @@ | ||
| //go:build linux | ||
|
|
||
| package testutils | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/storage/header" | ||
| ) | ||
|
|
||
| // MockMemfile is a mock implementation of block.ReadonlyDevice for testing. | ||
| type MockMemfile struct { | ||
| t *testing.T | ||
| } | ||
|
|
||
| // NewMockMemfile creates a new mock memfile for testing. | ||
| func NewMockMemfile(t *testing.T) *MockMemfile { | ||
| return &MockMemfile{t: t} | ||
| } | ||
|
|
||
| // ReadAt implements block.ReadonlyDevice. | ||
| func (m *MockMemfile) ReadAt(ctx context.Context, p []byte, off int64) (int, error) { | ||
| // Return zeros for testing | ||
| for i := range p { | ||
| p[i] = 0 | ||
| } | ||
| return len(p), nil | ||
| } | ||
|
|
||
| // Size implements block.ReadonlyDevice. | ||
| func (m *MockMemfile) Size(ctx context.Context) (int64, error) { | ||
| return 1024 * 1024 * 1024, nil // 1GB | ||
| } | ||
|
|
||
| // Close implements io.Closer. | ||
| func (m *MockMemfile) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| // Slice implements block.Slicer. | ||
| func (m *MockMemfile) Slice(ctx context.Context, off, length int64) ([]byte, error) { | ||
| data := make([]byte, length) | ||
| _, err := m.ReadAt(ctx, data, off) | ||
| return data, err | ||
| } | ||
|
|
||
| // BlockSize implements block.ReadonlyDevice. | ||
| func (m *MockMemfile) BlockSize() int64 { | ||
| return 4096 | ||
| } | ||
|
|
||
| // Header implements block.ReadonlyDevice. | ||
| func (m *MockMemfile) Header() *header.Header { | ||
| return &header.Header{} | ||
| } | ||
|
|
||
| // SwapHeader implements block.ReadonlyDevice. | ||
| func (m *MockMemfile) SwapHeader(h *header.Header) { | ||
| // No-op for mock | ||
| } |
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,236 @@ | ||
| //go:build linux | ||
|
|
||
| package uffd | ||
|
|
||
| import ( | ||
| "context" | ||
| "path/filepath" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/orchestrator/pkg/sandbox/uffd/testutils" | ||
| ) | ||
|
|
||
| // TestSetOnFailure verifies that the failure callback is properly set and can be retrieved. | ||
| func TestSetOnFailure(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| memfile := testutils.NewMockMemfile(t) | ||
| socketPath := filepath.Join(t.TempDir(), "test.sock") | ||
|
|
||
| u := New(memfile, socketPath) | ||
|
|
||
| assert.Nil(t, u.GetOnFailure()) | ||
|
|
||
| u.SetOnFailure(func(ctx context.Context, sandboxID string, err error) {}) | ||
|
|
||
| assert.NotNil(t, u.GetOnFailure()) | ||
| } | ||
|
|
||
| // TestOnFailureNotInvokedWhenCallbackNil verifies that a nil callback doesn't crash. | ||
| func TestOnFailureNotInvokedWhenCallbackNil(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| memfile := testutils.NewMockMemfile(t) | ||
| socketPath := filepath.Join(t.TempDir(), "test.sock") | ||
|
|
||
| u := New(memfile, socketPath) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| err := u.Start(ctx, "test-sandbox") | ||
| require.NoError(t, err) | ||
|
|
||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| stopErr := u.Stop() | ||
| assert.NoError(t, stopErr) | ||
| } | ||
|
|
||
| // TestMultipleCallbackSets verifies that SetOnFailure can be called multiple times, | ||
| // with each call replacing the previous callback. | ||
| func TestMultipleCallbackSets(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| memfile := testutils.NewMockMemfile(t) | ||
| socketPath := filepath.Join(t.TempDir(), "test.sock") | ||
|
|
||
| u := New(memfile, socketPath) | ||
|
|
||
| u.SetOnFailure(func(ctx context.Context, sandboxID string, err error) {}) | ||
| assert.NotNil(t, u.GetOnFailure()) | ||
|
|
||
| u.SetOnFailure(func(ctx context.Context, sandboxID string, err error) {}) | ||
| assert.NotNil(t, u.GetOnFailure()) | ||
|
|
||
| u.SetOnFailure(nil) | ||
| assert.Nil(t, u.GetOnFailure()) | ||
| } | ||
|
|
||
| // TestUffdStopAfterFailure verifies that UFFD can be stopped cleanly after a failure. | ||
| func TestUffdStopAfterFailure(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| memfile := testutils.NewMockMemfile(t) | ||
| socketPath := filepath.Join(t.TempDir(), "test.sock") | ||
|
|
||
| u := New(memfile, socketPath) | ||
| u.SetOnFailure(func(ctx context.Context, sandboxID string, err error) {}) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| err := u.Start(ctx, "test-sandbox") | ||
| require.NoError(t, err) | ||
|
|
||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| stopErr := u.Stop() | ||
| assert.NoError(t, stopErr) | ||
| } | ||
|
|
||
| // TestCallbackNotInvokedOnCleanStop verifies that the callback is NOT invoked | ||
| // when UFFD is stopped cleanly (no failure). | ||
| func TestCallbackNotInvokedOnCleanStop(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| memfile := testutils.NewMockMemfile(t) | ||
| socketPath := filepath.Join(t.TempDir(), "test.sock") | ||
|
|
||
| u := New(memfile, socketPath) | ||
|
|
||
| invoked := atomic.Bool{} | ||
| u.SetOnFailure(func(ctx context.Context, sandboxID string, err error) { | ||
| invoked.Store(true) | ||
| }) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| err := u.Start(ctx, "test-sandbox") | ||
| require.NoError(t, err) | ||
|
|
||
| _ = u.Stop() | ||
|
|
||
| time.Sleep(50 * time.Millisecond) | ||
|
|
||
| assert.False(t, invoked.Load(), "callback should not be invoked on clean stop") | ||
| } | ||
|
|
||
| // TestOnFailureInvokedOnHandleError verifies that the failure callback is invoked | ||
| // when handle fails (socket timeout). Long-running: waits ~10s for socket deadline. | ||
| func TestOnFailureInvokedOnHandleError(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip("skipping long-running test in short mode") | ||
| } | ||
|
|
||
| t.Parallel() | ||
|
|
||
| memfile := testutils.NewMockMemfile(t) | ||
| socketPath := filepath.Join(t.TempDir(), "test.sock") | ||
|
|
||
| u := New(memfile, socketPath) | ||
|
|
||
| var ( | ||
| wg sync.WaitGroup | ||
| callbackSandbox string | ||
| callbackErr error | ||
| mu sync.Mutex | ||
| ) | ||
|
|
||
| wg.Add(1) | ||
| sandboxID := "test-sandbox-123" | ||
| u.SetOnFailure(func(ctx context.Context, sbxID string, err error) { | ||
| defer wg.Done() | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| callbackSandbox = sbxID | ||
| callbackErr = err | ||
| }) | ||
|
|
||
| err := u.Start(context.Background(), sandboxID) | ||
| require.NoError(t, err) | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| wg.Wait() | ||
| close(done) | ||
| }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| case <-time.After(15 * time.Second): | ||
| t.Fatal("callback was not invoked within timeout") | ||
| } | ||
|
|
||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| assert.Equal(t, sandboxID, callbackSandbox) | ||
| assert.NotNil(t, callbackErr) | ||
| } | ||
|
|
||
| // TestLateCallbackRegistrationAfterFailure verifies the key correctness guarantee | ||
| // from the code review: a callback registered AFTER a failure has already occurred | ||
| // is still invoked immediately with the stored failure details. | ||
| func TestLateCallbackRegistrationAfterFailure(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip("skipping long-running test in short mode") | ||
| } | ||
|
|
||
| t.Parallel() | ||
|
|
||
| memfile := testutils.NewMockMemfile(t) | ||
| socketPath := filepath.Join(t.TempDir(), "test.sock") | ||
|
|
||
| u := New(memfile, socketPath) | ||
|
|
||
| sandboxID := "test-sandbox-late" | ||
|
|
||
| // Start without a callback - UFFD will fail after socket timeout (10 seconds) | ||
| err := u.Start(context.Background(), sandboxID) | ||
| require.NoError(t, err) | ||
|
|
||
| // Wait for the internal goroutine to finish (socket timeout = 10 seconds) | ||
| <-u.readyCh | ||
|
|
||
| // Register callback AFTER failure has already occurred. | ||
| // It must be invoked immediately in a goroutine with the stored failure details. | ||
| var ( | ||
| wg sync.WaitGroup | ||
| receivedSandbox string | ||
| receivedErr error | ||
| mu sync.Mutex | ||
| ) | ||
|
|
||
| wg.Add(1) | ||
| u.SetOnFailure(func(ctx context.Context, sbxID string, cbErr error) { | ||
| defer wg.Done() | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| receivedSandbox = sbxID | ||
| receivedErr = cbErr | ||
| }) | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| wg.Wait() | ||
| close(done) | ||
| }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| case <-time.After(2 * time.Second): | ||
| t.Fatal("late-registered callback was not invoked") | ||
| } | ||
|
|
||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| assert.Equal(t, sandboxID, receivedSandbox) | ||
| assert.NotNil(t, receivedErr) | ||
| } |
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.