-
Notifications
You must be signed in to change notification settings - Fork 48
[kernel-1116] browser debug: externel events #203
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
archandatta
wants to merge
14
commits into
archand/kernel-1116/cdp-monitor
Choose a base branch
from
archand/kernel-1116/externel-events
base: archand/kernel-1116/cdp-monitor
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.
+360
−20
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a9eb638
feat: add Pipeline glue type sequencing truncation, file write, and r…
archandatta dd31b84
review: fix truncateIfNeeded branch split, atomic.Pointer[string], Re…
archandatta b615377
fix: serialise Pipeline.Publish to guarantee monotonic seq delivery o…
archandatta fbb97a5
review
archandatta ab29008
refactor: rename BrowserEvent to Event, DetailDefault to DetailStandard
archandatta f0aed53
refactor: extract Envelope wrapper, move seq and capture_session_id o…
archandatta e07c024
refactor: unify seq as universal cursor, add NewReader(afterSeq)
archandatta 3945ce4
refactor: return ReadResult instead of synthetic drop events
archandatta 894e9d0
fix: guard against nil marshal data and oversized non-data envelopes
archandatta 7d38b48
fix: remove duplicate ReadResult type in ringbuffer.go
archandatta 6a67415
feat: add POST /events/publish and GET /events/stream endpoints
archandatta 9425b05
review: update naming
archandatta c2435ed
review: update tests
archandatta 27856a3
review: add mutex lock/unlock
archandatta 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
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,150 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/onkernel/kernel-images/server/lib/events" | ||
| "github.com/onkernel/kernel-images/server/lib/recorder" | ||
| "github.com/onkernel/kernel-images/server/lib/scaletozero" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func newPublishTestService(t *testing.T, logDir string) (*ApiService, *events.CaptureSession) { | ||
| t.Helper() | ||
| ring := events.NewRingBuffer(16) | ||
| fw := events.NewFileWriter(logDir) | ||
| cs := events.NewCaptureSession(ring, fw) | ||
| cs.Start("test-capture") | ||
| svc, err := New( | ||
| recorder.NewFFmpegManager(), | ||
| newMockFactory(), | ||
| newTestUpstreamManager(), | ||
| scaletozero.NewNoopController(), | ||
| newMockNekoClient(t), | ||
| cs, | ||
| 0, | ||
| ) | ||
| require.NoError(t, err) | ||
| return svc, cs | ||
| } | ||
|
|
||
| func publishEvent(t *testing.T, svc *ApiService, ev events.Event) *httptest.ResponseRecorder { | ||
| t.Helper() | ||
| b, err := json.Marshal(ev) | ||
| require.NoError(t, err) | ||
| req := httptest.NewRequest(http.MethodPost, "/events/publish", bytes.NewReader(b)) | ||
| req.Header.Set("Content-Type", "application/json") | ||
| w := httptest.NewRecorder() | ||
| svc.PublishEvent(w, req) | ||
| return w | ||
| } | ||
|
|
||
| func readEnvelope(t *testing.T, cs *events.CaptureSession) events.Envelope { | ||
| t.Helper() | ||
| reader := cs.NewReader(0) | ||
| ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
| defer cancel() | ||
| res, err := reader.Read(ctx) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, res.Envelope) | ||
| return *res.Envelope | ||
| } | ||
|
|
||
| func assertLogFileExists(t *testing.T, logDir, filename string) { | ||
| t.Helper() | ||
| info, err := os.Stat(filepath.Join(logDir, filename)) | ||
| require.NoError(t, err, "%s should exist", filename) | ||
| assert.Greater(t, info.Size(), int64(0), "%s should be non-empty", filename) | ||
| } | ||
|
|
||
| func TestPublishEvent(t *testing.T) { | ||
| t.Run("valid_event_published_to_ring", func(t *testing.T) { | ||
| logDir := t.TempDir() | ||
| svc, cs := newPublishTestService(t, logDir) | ||
|
|
||
| w := publishEvent(t, svc, events.Event{ | ||
| Type: "liveview_click", | ||
| Category: events.CategoryLiveview, | ||
| Source: events.Source{Kind: events.KindKernelAPI}, | ||
| Data: json.RawMessage(`{"x":100}`), | ||
| }) | ||
| assert.Equal(t, http.StatusOK, w.Code) | ||
|
|
||
| env := readEnvelope(t, cs) | ||
| assert.Equal(t, "liveview_click", env.Event.Type) | ||
| assert.Equal(t, events.CategoryLiveview, env.Event.Category) | ||
| }) | ||
|
|
||
| t.Run("invalid_json", func(t *testing.T) { | ||
| svc, _ := newPublishTestService(t, t.TempDir()) | ||
|
|
||
| req := httptest.NewRequest(http.MethodPost, "/events/publish", bytes.NewReader([]byte(`not-json`))) | ||
| req.Header.Set("Content-Type", "application/json") | ||
| w := httptest.NewRecorder() | ||
| svc.PublishEvent(w, req) | ||
|
|
||
| assert.Equal(t, http.StatusBadRequest, w.Code) | ||
| }) | ||
|
|
||
| t.Run("empty_type_rejected", func(t *testing.T) { | ||
| svc, _ := newPublishTestService(t, t.TempDir()) | ||
|
|
||
| w := publishEvent(t, svc, events.Event{ | ||
| Category: events.CategoryConsole, | ||
| Data: json.RawMessage(`{"x":1}`), | ||
| }) | ||
| assert.Equal(t, http.StatusBadRequest, w.Code) | ||
| }) | ||
|
|
||
| t.Run("liveview_routes_to_log", func(t *testing.T) { | ||
| logDir := t.TempDir() | ||
| svc, _ := newPublishTestService(t, logDir) | ||
|
|
||
| w := publishEvent(t, svc, events.Event{ | ||
| Type: "liveview_click", | ||
| Category: events.CategoryLiveview, | ||
| Source: events.Source{Kind: events.KindKernelAPI}, | ||
| Data: json.RawMessage(`{"x":100}`), | ||
| }) | ||
| require.Equal(t, http.StatusOK, w.Code) | ||
| assertLogFileExists(t, logDir, "liveview.log") | ||
| }) | ||
|
|
||
| t.Run("captcha_routes_to_log", func(t *testing.T) { | ||
| logDir := t.TempDir() | ||
| svc, _ := newPublishTestService(t, logDir) | ||
|
|
||
| w := publishEvent(t, svc, events.Event{ | ||
| Type: "captcha_solve", | ||
| Category: events.CategoryCaptcha, | ||
| Source: events.Source{Kind: events.KindKernelAPI}, | ||
| Data: json.RawMessage(`{"token":"abc"}`), | ||
| }) | ||
| require.Equal(t, http.StatusOK, w.Code) | ||
| assertLogFileExists(t, logDir, "captcha.log") | ||
| }) | ||
|
|
||
| t.Run("category_derived_from_type", func(t *testing.T) { | ||
| logDir := t.TempDir() | ||
| svc, cs := newPublishTestService(t, logDir) | ||
|
|
||
| w := publishEvent(t, svc, events.Event{ | ||
| Type: "liveview_click", | ||
| Data: json.RawMessage(`{"x":50}`), | ||
| }) | ||
| require.Equal(t, http.StatusOK, w.Code) | ||
|
|
||
| env := readEnvelope(t, cs) | ||
| assert.Equal(t, events.CategoryLiveview, env.Event.Category) | ||
| assertLogFileExists(t, logDir, "liveview.log") | ||
| }) | ||
| } |
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.