Keine Beschreibung

webui_test.go 5.7KB

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