|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/aptly-dev/aptly/aptly" |
| 10 | + ctx "github.com/aptly-dev/aptly/context" |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + "github.com/smira/flag" |
| 13 | + . "gopkg.in/check.v1" |
| 14 | +) |
| 15 | + |
| 16 | +// newPprofContext builds an aptly context and router from the given config file |
| 17 | +// path. It returns an error if context initialisation fails. The caller is |
| 18 | +// responsible for calling aptlyCtx.Shutdown() when done. |
| 19 | +func newPprofContext(configPath string) (*ctx.AptlyContext, http.Handler, error) { |
| 20 | + flags := flag.NewFlagSet("fakeFlags", flag.ContinueOnError) |
| 21 | + flags.Bool("no-lock", false, "dummy") |
| 22 | + flags.Int("db-open-attempts", 3, "dummy") |
| 23 | + flags.String("config", configPath, "dummy") |
| 24 | + flags.String("architectures", "", "dummy") |
| 25 | + |
| 26 | + aptlyCtx, err := ctx.NewContext(flags) |
| 27 | + if err != nil { |
| 28 | + return nil, nil, err |
| 29 | + } |
| 30 | + return aptlyCtx, Router(aptlyCtx), nil |
| 31 | +} |
| 32 | + |
| 33 | +// PprofEnabledSuite tests pprof endpoints when EnablePprofEndpoint is true. |
| 34 | +// It uses a dedicated router built from a config that enables only the pprof |
| 35 | +// endpoint, ensuring no interaction with other optional features. |
| 36 | +type PprofEnabledSuite struct { |
| 37 | + configFile *os.File |
| 38 | + aptlyCtx *ctx.AptlyContext |
| 39 | + router http.Handler |
| 40 | +} |
| 41 | + |
| 42 | +var _ = Suite(&PprofEnabledSuite{}) |
| 43 | + |
| 44 | +func (s *PprofEnabledSuite) SetUpSuite(c *C) { |
| 45 | + aptly.Version = "testVersion" |
| 46 | + |
| 47 | + file, err := os.CreateTemp("", "aptly-pprof") |
| 48 | + c.Assert(err, IsNil) |
| 49 | + s.configFile = file |
| 50 | + |
| 51 | + jsonString, err := json.Marshal(gin.H{ |
| 52 | + "architectures": []string{}, |
| 53 | + "enablePprofEndpoint": true, |
| 54 | + }) |
| 55 | + c.Assert(err, IsNil) |
| 56 | + _, err = file.Write(jsonString) |
| 57 | + c.Assert(err, IsNil) |
| 58 | + _ = file.Close() |
| 59 | + |
| 60 | + aptlyCtx, router, err := newPprofContext(file.Name()) |
| 61 | + c.Assert(err, IsNil) |
| 62 | + s.aptlyCtx = aptlyCtx |
| 63 | + s.router = router |
| 64 | +} |
| 65 | + |
| 66 | +func (s *PprofEnabledSuite) TearDownSuite(c *C) { |
| 67 | + if s.configFile != nil { |
| 68 | + _ = os.Remove(s.configFile.Name()) |
| 69 | + } |
| 70 | + if s.aptlyCtx != nil { |
| 71 | + s.aptlyCtx.Shutdown() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (s *PprofEnabledSuite) request(method, url string) *httptest.ResponseRecorder { |
| 76 | + w := httptest.NewRecorder() |
| 77 | + req, err := http.NewRequest(method, url, nil) |
| 78 | + if err != nil { |
| 79 | + panic(err) |
| 80 | + } |
| 81 | + s.router.ServeHTTP(w, req) |
| 82 | + return w |
| 83 | +} |
| 84 | + |
| 85 | +// TestPprofIndexReturns200 verifies the pprof index page is served when the |
| 86 | +// endpoint is enabled. |
| 87 | +func (s *PprofEnabledSuite) TestPprofIndexReturns200(c *C) { |
| 88 | + w := s.request("GET", "/api/debug/pprof/") |
| 89 | + c.Check(w.Code, Equals, 200) |
| 90 | + c.Check(w.Header().Get("Content-Type"), Matches, "text/html.*") |
| 91 | +} |
| 92 | + |
| 93 | +// TestPprofGoroutineReturns200 verifies the goroutine sub-profile is served. |
| 94 | +func (s *PprofEnabledSuite) TestPprofGoroutineReturns200(c *C) { |
| 95 | + w := s.request("GET", "/api/debug/pprof/goroutine?debug=1") |
| 96 | + c.Check(w.Code, Equals, 200) |
| 97 | +} |
| 98 | + |
| 99 | +// TestPprofHeapReturns200 verifies the heap sub-profile is served. |
| 100 | +func (s *PprofEnabledSuite) TestPprofHeapReturns200(c *C) { |
| 101 | + w := s.request("GET", "/api/debug/pprof/heap?debug=1") |
| 102 | + c.Check(w.Code, Equals, 200) |
| 103 | +} |
| 104 | + |
| 105 | +// TestPprofCmdlineReturns200 verifies the cmdline handler is served. |
| 106 | +func (s *PprofEnabledSuite) TestPprofCmdlineReturns200(c *C) { |
| 107 | + w := s.request("GET", "/api/debug/pprof/cmdline") |
| 108 | + c.Check(w.Code, Equals, 200) |
| 109 | +} |
| 110 | + |
| 111 | +// TestPprofSymbolReturns200 verifies the symbol lookup handler responds (GET |
| 112 | +// with no addresses returns 200 with a count of 0 symbols found). |
| 113 | +func (s *PprofEnabledSuite) TestPprofSymbolReturns200(c *C) { |
| 114 | + w := s.request("GET", "/api/debug/pprof/symbol") |
| 115 | + c.Check(w.Code, Equals, 200) |
| 116 | +} |
| 117 | + |
| 118 | +// TestPprofTraceReturns200 verifies the execution trace handler is wired |
| 119 | +// correctly. The minimum trace duration is 1 second, so this test blocks |
| 120 | +// briefly but confirms end-to-end handler registration. |
| 121 | +func (s *PprofEnabledSuite) TestPprofTraceReturns200(c *C) { |
| 122 | + w := s.request("GET", "/api/debug/pprof/trace?seconds=1") |
| 123 | + c.Check(w.Code, Equals, 200) |
| 124 | + c.Check(w.Header().Get("Content-Type"), Equals, "application/octet-stream") |
| 125 | +} |
| 126 | + |
| 127 | +// TestNonPprofRoutesUnaffectedWhenEnabled verifies that existing API routes |
| 128 | +// continue to work correctly when the pprof endpoint is enabled. |
| 129 | +func (s *PprofEnabledSuite) TestNonPprofRoutesUnaffectedWhenEnabled(c *C) { |
| 130 | + w := s.request("GET", "/api/version") |
| 131 | + c.Check(w.Code, Equals, 200) |
| 132 | +} |
| 133 | + |
| 134 | +// PprofDisabledSuite tests that pprof endpoints are NOT exposed when the config |
| 135 | +// option is off. It embeds APISuite, whose config does not set |
| 136 | +// enablePprofEndpoint, confirming the default state is safe. The inherited |
| 137 | +// APISuite tests also run here, verifying that normal API operation is |
| 138 | +// unaffected when pprof is disabled. |
| 139 | +type PprofDisabledSuite struct { |
| 140 | + APISuite |
| 141 | +} |
| 142 | + |
| 143 | +var _ = Suite(&PprofDisabledSuite{}) |
| 144 | + |
| 145 | +// TestPprofIndexNotExposed verifies the pprof index is not reachable when |
| 146 | +// enablePprofEndpoint is false (the default). |
| 147 | +func (s *PprofDisabledSuite) TestPprofIndexNotExposed(c *C) { |
| 148 | + response, err := s.HTTPRequest("GET", "/api/debug/pprof/", nil) |
| 149 | + c.Assert(err, IsNil) |
| 150 | + c.Check(response.Code, Equals, 404) |
| 151 | +} |
| 152 | + |
| 153 | +// TestPprofGoroutineNotExposed verifies the goroutine profile is not reachable |
| 154 | +// when disabled. |
| 155 | +func (s *PprofDisabledSuite) TestPprofGoroutineNotExposed(c *C) { |
| 156 | + response, err := s.HTTPRequest("GET", "/api/debug/pprof/goroutine", nil) |
| 157 | + c.Assert(err, IsNil) |
| 158 | + c.Check(response.Code, Equals, 404) |
| 159 | +} |
| 160 | + |
| 161 | +// TestPprofHeapNotExposed verifies the heap profile is not reachable when |
| 162 | +// disabled. |
| 163 | +func (s *PprofDisabledSuite) TestPprofHeapNotExposed(c *C) { |
| 164 | + response, err := s.HTTPRequest("GET", "/api/debug/pprof/heap", nil) |
| 165 | + c.Assert(err, IsNil) |
| 166 | + c.Check(response.Code, Equals, 404) |
| 167 | +} |
| 168 | + |
| 169 | +// TestPprofCmdlineNotExposed verifies the cmdline endpoint is not reachable |
| 170 | +// when disabled. |
| 171 | +func (s *PprofDisabledSuite) TestPprofCmdlineNotExposed(c *C) { |
| 172 | + response, err := s.HTTPRequest("GET", "/api/debug/pprof/cmdline", nil) |
| 173 | + c.Assert(err, IsNil) |
| 174 | + c.Check(response.Code, Equals, 404) |
| 175 | +} |
| 176 | + |
| 177 | +// TestPprofTraceNotExposed verifies the trace endpoint is not reachable when |
| 178 | +// disabled. |
| 179 | +func (s *PprofDisabledSuite) TestPprofTraceNotExposed(c *C) { |
| 180 | + response, err := s.HTTPRequest("GET", "/api/debug/pprof/trace", nil) |
| 181 | + c.Assert(err, IsNil) |
| 182 | + c.Check(response.Code, Equals, 404) |
| 183 | +} |
0 commit comments