暂无描述

webui_test.go 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package driver
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "net"
  19. "net/http"
  20. "net/http/httptest"
  21. "net/url"
  22. "os/exec"
  23. "regexp"
  24. "sync"
  25. "testing"
  26. "github.com/google/pprof/internal/plugin"
  27. "github.com/google/pprof/profile"
  28. "runtime"
  29. )
  30. func TestWebInterface(t *testing.T) {
  31. if runtime.GOOS == "nacl" {
  32. t.Skip("test assumes tcp available")
  33. }
  34. prof := makeFakeProfile()
  35. // Custom http server creator
  36. var server *httptest.Server
  37. serverCreated := make(chan bool)
  38. creator := func(a *plugin.HTTPServerArgs) error {
  39. server = httptest.NewServer(http.HandlerFunc(
  40. func(w http.ResponseWriter, r *http.Request) {
  41. if h := a.Handlers[r.URL.Path]; h != nil {
  42. h.ServeHTTP(w, r)
  43. }
  44. }))
  45. serverCreated <- true
  46. return nil
  47. }
  48. // Start server and wait for it to be initialized
  49. go serveWebInterface("unused:1234", prof, &plugin.Options{
  50. Obj: fakeObjTool{},
  51. UI: &stdUI{},
  52. HTTPServer: creator,
  53. }, false)
  54. <-serverCreated
  55. defer server.Close()
  56. haveDot := false
  57. if _, err := exec.LookPath("dot"); err == nil {
  58. haveDot = true
  59. }
  60. type testCase struct {
  61. path string
  62. want []string
  63. needDot bool
  64. }
  65. testcases := []testCase{
  66. {"/", []string{"F1", "F2", "F3", "testbin", "cpu"}, true},
  67. {"/top", []string{`"Name":"F2","InlineLabel":"","Flat":200,"Cum":300,"FlatFormat":"200ms","CumFormat":"300ms"}`}, false},
  68. {"/source?f=" + url.QueryEscape("F[12]"),
  69. []string{"F1", "F2", "300ms +line1"}, false},
  70. {"/peek?f=" + url.QueryEscape("F[12]"),
  71. []string{"300ms.*F1", "200ms.*300ms.*F2"}, false},
  72. {"/disasm?f=" + url.QueryEscape("F[12]"),
  73. []string{"f1:asm", "f2:asm"}, false},
  74. {"/flamegraph", []string{"File: testbin", "\"n\":\"root\"", "\"n\":\"F1\"", "function tip", "function flameGraph", "function hierarchy"}, false},
  75. }
  76. for _, c := range testcases {
  77. if c.needDot && !haveDot {
  78. t.Log("skipping", c.path, "since dot (graphviz) does not seem to be installed")
  79. continue
  80. }
  81. res, err := http.Get(server.URL + c.path)
  82. if err != nil {
  83. t.Error("could not fetch", c.path, err)
  84. continue
  85. }
  86. data, err := ioutil.ReadAll(res.Body)
  87. if err != nil {
  88. t.Error("could not read response", c.path, err)
  89. continue
  90. }
  91. result := string(data)
  92. for _, w := range c.want {
  93. if match, _ := regexp.MatchString(w, result); !match {
  94. t.Errorf("response for %s does not match "+
  95. "expected pattern '%s'; "+
  96. "actual result:\n%s", c.path, w, result)
  97. }
  98. }
  99. }
  100. // Also fetch all the test case URLs in parallel to test thread
  101. // safety when run under the race detector.
  102. var wg sync.WaitGroup
  103. for _, c := range testcases {
  104. if c.needDot && !haveDot {
  105. continue
  106. }
  107. path := server.URL + c.path
  108. for count := 0; count < 2; count++ {
  109. wg.Add(1)
  110. go func() {
  111. http.Get(path)
  112. wg.Done()
  113. }()
  114. }
  115. }
  116. wg.Wait()
  117. }
  118. // Implement fake object file support.
  119. const addrBase = 0x1000
  120. const fakeSource = "testdata/file1000.src"
  121. type fakeObj struct{}
  122. func (f fakeObj) Close() error { return nil }
  123. func (f fakeObj) Name() string { return "testbin" }
  124. func (f fakeObj) Base() uint64 { return 0 }
  125. func (f fakeObj) BuildID() string { return "" }
  126. func (f fakeObj) SourceLine(addr uint64) ([]plugin.Frame, error) {
  127. return nil, fmt.Errorf("SourceLine unimplemented")
  128. }
  129. func (f fakeObj) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
  130. return []*plugin.Sym{
  131. {
  132. Name: []string{"F1"}, File: fakeSource,
  133. Start: addrBase, End: addrBase + 10,
  134. },
  135. {
  136. Name: []string{"F2"}, File: fakeSource,
  137. Start: addrBase + 10, End: addrBase + 20,
  138. },
  139. {
  140. Name: []string{"F3"}, File: fakeSource,
  141. Start: addrBase + 20, End: addrBase + 30,
  142. },
  143. }, nil
  144. }
  145. type fakeObjTool struct{}
  146. func (obj fakeObjTool) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
  147. return fakeObj{}, nil
  148. }
  149. func (obj fakeObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
  150. return []plugin.Inst{
  151. {Addr: addrBase + 0, Text: "f1:asm", Function: "F1"},
  152. {Addr: addrBase + 10, Text: "f2:asm", Function: "F2"},
  153. {Addr: addrBase + 20, Text: "d3:asm", Function: "F3"},
  154. }, nil
  155. }
  156. func makeFakeProfile() *profile.Profile {
  157. // Three functions: F1, F2, F3 with three lines, 11, 22, 33.
  158. funcs := []*profile.Function{
  159. {ID: 1, Name: "F1", Filename: fakeSource, StartLine: 3},
  160. {ID: 2, Name: "F2", Filename: fakeSource, StartLine: 5},
  161. {ID: 3, Name: "F3", Filename: fakeSource, StartLine: 7},
  162. }
  163. lines := []profile.Line{
  164. {Function: funcs[0], Line: 11},
  165. {Function: funcs[1], Line: 22},
  166. {Function: funcs[2], Line: 33},
  167. }
  168. mapping := []*profile.Mapping{
  169. {
  170. ID: 1,
  171. Start: addrBase,
  172. Limit: addrBase + 10,
  173. Offset: 0,
  174. File: "testbin",
  175. HasFunctions: true,
  176. HasFilenames: true,
  177. HasLineNumbers: true,
  178. },
  179. }
  180. // Three interesting addresses: base+{10,20,30}
  181. locs := []*profile.Location{
  182. {ID: 1, Address: addrBase + 10, Line: lines[0:1], Mapping: mapping[0]},
  183. {ID: 2, Address: addrBase + 20, Line: lines[1:2], Mapping: mapping[0]},
  184. {ID: 3, Address: addrBase + 30, Line: lines[2:3], Mapping: mapping[0]},
  185. }
  186. // Two stack traces.
  187. return &profile.Profile{
  188. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  189. Period: 1,
  190. DurationNanos: 10e9,
  191. SampleType: []*profile.ValueType{
  192. {Type: "cpu", Unit: "milliseconds"},
  193. },
  194. Sample: []*profile.Sample{
  195. {
  196. Location: []*profile.Location{locs[2], locs[1], locs[0]},
  197. Value: []int64{100},
  198. },
  199. {
  200. Location: []*profile.Location{locs[1], locs[0]},
  201. Value: []int64{200},
  202. },
  203. },
  204. Location: locs,
  205. Function: funcs,
  206. Mapping: mapping,
  207. }
  208. }
  209. func TestIsLocalHost(t *testing.T) {
  210. for _, s := range []string{"localhost:10000", "[::1]:10000", "127.0.0.1:10000"} {
  211. host, _, err := net.SplitHostPort(s)
  212. if err != nil {
  213. t.Error("unexpected error when splitting", s)
  214. continue
  215. }
  216. if !isLocalhost(host) {
  217. t.Errorf("host %s from %s not considered local", host, s)
  218. }
  219. }
  220. }