mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 09:28:54 +00:00
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #40540 # Checklist for submitter - [ ] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - Changes present in previous PR ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Switched the application logging to Go's standard slog with context-aware logging, improving structured logs and observability across services (status, audit, result, integrations). * Replaced legacy logging implementations and updated runtime wiring to propagate contextual loggers for more consistent, searchable log output. * **Tests** * Updated test suites to use the new slog discard/logger setup. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package logging
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWebhookSubmission(t *testing.T) {
|
|
ctx := context.Background()
|
|
logger := slog.New(slog.DiscardHandler)
|
|
var body struct {
|
|
Timestamp time.Time `json:"timestamp"`
|
|
Details []json.RawMessage `json:"details"`
|
|
}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
require.Equal(t, "application/json", r.Header.Get("Content-Type"))
|
|
err := json.NewDecoder(r.Body).Decode(&body)
|
|
require.NoError(t, err)
|
|
}))
|
|
writer, err := NewWebhookLogWriter(server.URL, logger)
|
|
require.NoError(t, err)
|
|
|
|
logs := []json.RawMessage{
|
|
json.RawMessage(`{"pack":"fruit"}`),
|
|
json.RawMessage(`{"information":213}`),
|
|
json.RawMessage(`{"gordon":"freeman"}`),
|
|
}
|
|
|
|
err = writer.Write(ctx, logs)
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, body.Details, 3)
|
|
require.Equal(t, logs, body.Details)
|
|
}
|
|
|
|
func TestWebhookFailure(t *testing.T) {
|
|
ctx := context.Background()
|
|
logger := slog.New(slog.DiscardHandler)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Error(w, "bad", http.StatusBadRequest)
|
|
}))
|
|
writer, err := NewWebhookLogWriter(server.URL, logger)
|
|
require.NoError(t, err)
|
|
|
|
// Should always return no error and not stall, even if bad things happen
|
|
|
|
// Bad return code
|
|
err = writer.Write(ctx, []json.RawMessage{json.RawMessage("{}")})
|
|
require.NoError(t, err)
|
|
|
|
// No server
|
|
server.Close()
|
|
err = writer.Write(ctx, []json.RawMessage{json.RawMessage("{}")})
|
|
require.NoError(t, err)
|
|
}
|