暫無描述

webui_test.go 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. )
  29. func TestWebInterface(t *testing.T) {
  30. prof := makeFakeProfile()
  31. // Custom http server creator
  32. var server *httptest.Server
  33. serverCreated := make(chan bool)
  34. creator := func(a *plugin.HTTPServerArgs) error {
  35. server = httptest.NewServer(http.HandlerFunc(
  36. func(w http.ResponseWriter, r *http.Request) {
  37. if h := a.Handlers[r.URL.Path]; h != nil {
  38. h.ServeHTTP(w, r)
  39. }
  40. }))
  41. serverCreated <- true
  42. return nil
  43. }
  44. // Start server and wait for it to be initialized
  45. go serveWebInterface("unused:1234", prof, &plugin.Options{
  46. Obj: fakeObjTool{},
  47. UI: &stdUI{},
  48. HTTPServer: creator,
  49. })
  50. <-serverCreated
  51. defer server.Close()
  52. haveDot := false
  53. if _, err := exec.LookPath("dot"); err == nil {
  54. haveDot = true
  55. }
  56. type testCase struct {
  57. path string
  58. want []string
  59. needDot bool
  60. }
  61. testcases := []testCase{
  62. {"/", []string{"F1", "F2", "F3", "testbin", "cpu"}, true},
  63. {"/top", []string{`"Name":"F2","InlineLabel":"","Flat":200,"Cum":300,"FlatFormat":"200ms","CumFormat":"300ms"}`}, false},
  64. {"/source?f=" + url.QueryEscape("F[12]"),
  65. []string{"F1", "F2", "300ms +line1"}, false},
  66. {"/peek?f=" + url.QueryEscape("F[12]"),
  67. []string{"300ms.*F1", "200ms.*300ms.*F2"}, false},
  68. {"/disasm?f=" + url.QueryEscape("F[12]"),
  69. []string{"f1:asm", "f2:asm"}, false},
  70. }
  71. for _, c := range testcases {
  72. if c.needDot && !haveDot {
  73. t.Log("skipping", c.path, "since dot (graphviz) does not seem to be installed")
  74. continue
  75. }
  76. res, err := http.Get(server.URL + c.path)
  77. if err != nil {
  78. t.Error("could not fetch", c.path, err)
  79. continue
  80. }
  81. data, err := ioutil.ReadAll(res.Body)
  82. if err != nil {
  83. t.Error("could not read response", c.path, err)
  84. continue
  85. }
  86. result := string(data)
  87. for _, w := range c.want {
  88. if match, _ := regexp.MatchString(w, result); !match {
  89. t.Errorf("response for %s does not match "+
  90. "expected pattern '%s'; "+
  91. "actual result:\n%s", c.path, w, result)
  92. }
  93. }
  94. }
  95. // Also fetch all the test case URLs in parallel to test thread
  96. // safety when run under the race detector.
  97. var wg sync.WaitGroup
  98. for _, c := range testcases {
  99. if c.needDot && !haveDot {
  100. continue
  101. }
  102. path := server.URL + c.path
  103. for count := 0; count < 2; count++ {
  104. wg.Add(1)
  105. go func() {
  106. http.Get(path)
  107. wg.Done()
  108. }()
  109. }
  110. }
  111. wg.Wait()
  112. }
  113. // Implement fake object file support.
  114. const addrBase = 0x1000
  115. const fakeSource = "testdata/file1000.src"
  116. type fakeObj struct{}
  117. func (f fakeObj) Close() error { return nil }
  118. func (f fakeObj) Name() string { return "testbin" }
  119. func (f fakeObj) Base() uint64 { return 0 }
  120. func (f fakeObj) BuildID() string { return "" }
  121. func (f fakeObj) SourceLine(addr uint64) ([]plugin.Frame, error) {
  122. return nil, fmt.Errorf("SourceLine unimplemented")
  123. }
  124. func (f fakeObj) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
  125. return []*plugin.Sym{
  126. {[]string{"F1"}, fakeSource, addrBase, addrBase + 10},
  127. {[]string{"F2"}, fakeSource, addrBase + 10, addrBase + 20},
  128. {[]string{"F3"}, fakeSource, addrBase + 20, addrBase + 30},
  129. }, nil
  130. }
  131. type fakeObjTool struct{}
  132. func (obj fakeObjTool) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
  133. return fakeObj{}, nil
  134. }
  135. func (obj fakeObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
  136. return []plugin.Inst{
  137. {Addr: addrBase + 0, Text: "f1:asm", Function: "F1"},
  138. {Addr: addrBase + 10, Text: "f2:asm", Function: "F2"},
  139. {Addr: addrBase + 20, Text: "d3:asm", Function: "F3"},
  140. }, nil
  141. }
  142. func makeFakeProfile() *profile.Profile {
  143. // Three functions: F1, F2, F3 with three lines, 11, 22, 33.
  144. funcs := []*profile.Function{
  145. {ID: 1, Name: "F1", Filename: fakeSource, StartLine: 3},
  146. {ID: 2, Name: "F2", Filename: fakeSource, StartLine: 5},
  147. {ID: 3, Name: "F3", Filename: fakeSource, StartLine: 7},
  148. }
  149. lines := []profile.Line{
  150. {Function: funcs[0], Line: 11},
  151. {Function: funcs[1], Line: 22},
  152. {Function: funcs[2], Line: 33},
  153. }
  154. mapping := []*profile.Mapping{
  155. {
  156. ID: 1,
  157. Start: addrBase,
  158. Limit: addrBase + 10,
  159. Offset: 0,
  160. File: "testbin",
  161. HasFunctions: true,
  162. HasFilenames: true,
  163. HasLineNumbers: true,
  164. },
  165. }
  166. // Three interesting addresses: base+{10,20,30}
  167. locs := []*profile.Location{
  168. {ID: 1, Address: addrBase + 10, Line: lines[0:1], Mapping: mapping[0]},
  169. {ID: 2, Address: addrBase + 20, Line: lines[1:2], Mapping: mapping[0]},
  170. {ID: 3, Address: addrBase + 30, Line: lines[2:3], Mapping: mapping[0]},
  171. }
  172. // Two stack traces.
  173. return &profile.Profile{
  174. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  175. Period: 1,
  176. DurationNanos: 10e9,
  177. SampleType: []*profile.ValueType{
  178. {Type: "cpu", Unit: "milliseconds"},
  179. },
  180. Sample: []*profile.Sample{
  181. {
  182. Location: []*profile.Location{locs[2], locs[1], locs[0]},
  183. Value: []int64{100},
  184. },
  185. {
  186. Location: []*profile.Location{locs[1], locs[0]},
  187. Value: []int64{200},
  188. },
  189. },
  190. Location: locs,
  191. Function: funcs,
  192. Mapping: mapping,
  193. }
  194. }
  195. func TestIsLocalHost(t *testing.T) {
  196. for _, s := range []string{"localhost:10000", "[::1]:10000", "127.0.0.1:10000"} {
  197. host, _, err := net.SplitHostPort(s)
  198. if err != nil {
  199. t.Error("unexpected error when splitting", s)
  200. continue
  201. }
  202. if !isLocalhost(host) {
  203. t.Errorf("host %s from %s not considered local", host, s)
  204. }
  205. }
  206. }