|
| 1 | +package observability |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strconv" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "github.com/supabase/auth/internal/conf" |
| 14 | +) |
| 15 | + |
| 16 | +// freeLocalPort returns the string form of a free TCP port on 127.0.0.1. |
| 17 | +// Used by tests that need to start a real listener (profiler, prometheus) |
| 18 | +// without colliding with other services or other tests running in parallel. |
| 19 | +func freeLocalPort(t *testing.T) string { |
| 20 | + t.Helper() |
| 21 | + l, err := net.Listen("tcp", "127.0.0.1:0") |
| 22 | + require.NoError(t, err) |
| 23 | + port := l.Addr().(*net.TCPAddr).Port |
| 24 | + require.NoError(t, l.Close()) |
| 25 | + return strconv.Itoa(port) |
| 26 | +} |
| 27 | + |
| 28 | +func TestConfigureProfilerDisabled(t *testing.T) { |
| 29 | + require.NoError(t, ConfigureProfiler(context.Background(), &conf.ProfilerConfig{Enabled: false})) |
| 30 | +} |
| 31 | + |
| 32 | +func TestConfigureProfilerStartsAndShutsDown(t *testing.T) { |
| 33 | + // Start the profiler on a free localhost port and immediately cancel the |
| 34 | + // context; the cleanup goroutine should run server.Shutdown without |
| 35 | + // hanging the test. The function returns nil regardless of whether the |
| 36 | + // underlying listener bound successfully, so coverage focuses on the |
| 37 | + // synchronous setup path. |
| 38 | + ctx, cancel := context.WithCancel(context.Background()) |
| 39 | + port := freeLocalPort(t) |
| 40 | + require.NoError(t, ConfigureProfiler(ctx, &conf.ProfilerConfig{ |
| 41 | + Enabled: true, |
| 42 | + Host: "127.0.0.1", |
| 43 | + Port: port, |
| 44 | + })) |
| 45 | + cancel() |
| 46 | + // Give the cleanup goroutine a brief moment to run. |
| 47 | + time.Sleep(150 * time.Millisecond) |
| 48 | +} |
| 49 | + |
| 50 | +func TestProfilerHandlerServeHTTP(t *testing.T) { |
| 51 | + tests := []struct { |
| 52 | + name string |
| 53 | + path string |
| 54 | + expectedStatus int |
| 55 | + }{ |
| 56 | + {name: "pprof index", path: "/debug/pprof/", expectedStatus: http.StatusOK}, |
| 57 | + {name: "pprof cmdline", path: "/debug/pprof/cmdline", expectedStatus: http.StatusOK}, |
| 58 | + {name: "pprof symbol", path: "/debug/pprof/symbol", expectedStatus: http.StatusOK}, |
| 59 | + {name: "pprof goroutine", path: "/debug/pprof/goroutine", expectedStatus: http.StatusOK}, |
| 60 | + {name: "pprof heap", path: "/debug/pprof/heap", expectedStatus: http.StatusOK}, |
| 61 | + {name: "pprof allocs", path: "/debug/pprof/allocs", expectedStatus: http.StatusOK}, |
| 62 | + {name: "pprof threadcreate", path: "/debug/pprof/threadcreate", expectedStatus: http.StatusOK}, |
| 63 | + {name: "pprof block", path: "/debug/pprof/block", expectedStatus: http.StatusOK}, |
| 64 | + {name: "pprof mutex", path: "/debug/pprof/mutex", expectedStatus: http.StatusOK}, |
| 65 | + {name: "unknown path returns 404", path: "/debug/pprof/unknown", expectedStatus: http.StatusNotFound}, |
| 66 | + {name: "non-pprof path returns 404", path: "/something/else", expectedStatus: http.StatusNotFound}, |
| 67 | + } |
| 68 | + |
| 69 | + handler := &ProfilerHandler{} |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + req := httptest.NewRequest(http.MethodGet, tt.path, nil) |
| 73 | + w := httptest.NewRecorder() |
| 74 | + handler.ServeHTTP(w, req) |
| 75 | + require.Equal(t, tt.expectedStatus, w.Code) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments