Aucune description

webui_test.go 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. "runtime"
  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. ui := makeWebInterface(prof, &plugin.Options{
  32. Obj: fakeObjTool{},
  33. UI: &stdUI{},
  34. })
  35. // Start test server.
  36. server := httptest.NewServer(http.HandlerFunc(
  37. func(w http.ResponseWriter, r *http.Request) {
  38. switch r.URL.Path {
  39. case "/":
  40. ui.dot(w, r)
  41. case "/top":
  42. ui.top(w, r)
  43. case "/disasm":
  44. ui.disasm(w, r)
  45. case "/peek":
  46. ui.peek(w, r)
  47. case "/source":
  48. ui.source(w, r)
  49. }
  50. }))
  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. }
  96. // Implement fake object file support.
  97. const addrBase = 0x1000
  98. const fakeSource = "testdata/file1000.src"
  99. type fakeObj struct{}
  100. func (f fakeObj) Close() error { return nil }
  101. func (f fakeObj) Name() string { return "testbin" }
  102. func (f fakeObj) Base() uint64 { return 0 }
  103. func (f fakeObj) BuildID() string { return "" }
  104. func (f fakeObj) SourceLine(addr uint64) ([]plugin.Frame, error) {
  105. return nil, fmt.Errorf("SourceLine unimplemented")
  106. }
  107. func (f fakeObj) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
  108. return []*plugin.Sym{
  109. {[]string{"F1"}, fakeSource, addrBase, addrBase + 10},
  110. {[]string{"F2"}, fakeSource, addrBase + 10, addrBase + 20},
  111. {[]string{"F3"}, fakeSource, addrBase + 20, addrBase + 30},
  112. }, nil
  113. }
  114. type fakeObjTool struct{}
  115. func (obj fakeObjTool) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
  116. return fakeObj{}, nil
  117. }
  118. func (obj fakeObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
  119. return []plugin.Inst{
  120. {Addr: addrBase + 0, Text: "f1:asm", Function: "F1"},
  121. {Addr: addrBase + 10, Text: "f2:asm", Function: "F2"},
  122. {Addr: addrBase + 20, Text: "d3:asm", Function: "F3"},
  123. }, nil
  124. }
  125. func makeFakeProfile() *profile.Profile {
  126. // Three functions: F1, F2, F3 with three lines, 11, 22, 33.
  127. funcs := []*profile.Function{
  128. {ID: 1, Name: "F1", Filename: fakeSource, StartLine: 3},
  129. {ID: 2, Name: "F2", Filename: fakeSource, StartLine: 5},
  130. {ID: 3, Name: "F3", Filename: fakeSource, StartLine: 7},
  131. }
  132. lines := []profile.Line{
  133. {Function: funcs[0], Line: 11},
  134. {Function: funcs[1], Line: 22},
  135. {Function: funcs[2], Line: 33},
  136. }
  137. mapping := []*profile.Mapping{
  138. {
  139. ID: 1,
  140. Start: addrBase,
  141. Limit: addrBase + 10,
  142. Offset: 0,
  143. File: "testbin",
  144. HasFunctions: true,
  145. HasFilenames: true,
  146. HasLineNumbers: true,
  147. },
  148. }
  149. // Three interesting addresses: base+{10,20,30}
  150. locs := []*profile.Location{
  151. {ID: 1, Address: addrBase + 10, Line: lines[0:1], Mapping: mapping[0]},
  152. {ID: 2, Address: addrBase + 20, Line: lines[1:2], Mapping: mapping[0]},
  153. {ID: 3, Address: addrBase + 30, Line: lines[2:3], Mapping: mapping[0]},
  154. }
  155. // Two stack traces.
  156. return &profile.Profile{
  157. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  158. Period: 1,
  159. DurationNanos: 10e9,
  160. SampleType: []*profile.ValueType{
  161. {Type: "cpu", Unit: "milliseconds"},
  162. },
  163. Sample: []*profile.Sample{
  164. {
  165. Location: []*profile.Location{locs[2], locs[1], locs[0]},
  166. Value: []int64{100},
  167. },
  168. {
  169. Location: []*profile.Location{locs[1], locs[0]},
  170. Value: []int64{200},
  171. },
  172. },
  173. Location: locs,
  174. Function: funcs,
  175. Mapping: mapping,
  176. }
  177. }
  178. func TestNewListenerAndURL(t *testing.T) {
  179. if runtime.GOOS == "nacl" {
  180. t.Skip("test assumes tcp available")
  181. }
  182. tests := []struct {
  183. hostport string
  184. wantErr bool
  185. wantURLRe *regexp.Regexp
  186. wantLocal bool
  187. }{
  188. {
  189. hostport: ":",
  190. wantURLRe: regexp.MustCompile(`http://localhost:\d+`),
  191. wantLocal: true,
  192. },
  193. {
  194. hostport: "localhost:",
  195. wantURLRe: regexp.MustCompile(`http://localhost:\d+`),
  196. wantLocal: true,
  197. },
  198. {
  199. hostport: "http://localhost:12345",
  200. wantErr: true,
  201. },
  202. }
  203. for _, tt := range tests {
  204. t.Run(tt.hostport, func(t *testing.T) {
  205. _, url, isLocal, err := newListenerAndURL(tt.hostport)
  206. if tt.wantErr {
  207. if err == nil {
  208. t.Fatalf("newListenerAndURL(%v) want error; got none", tt.hostport)
  209. }
  210. return
  211. }
  212. if err != nil {
  213. t.Fatalf("newListenerAndURL(%v) = %v", tt.hostport, err)
  214. }
  215. if !tt.wantURLRe.MatchString(url) {
  216. t.Errorf("newListenerAndURL(%v) URL = %v; want URL matching %v", tt.hostport, url, tt.wantURLRe)
  217. }
  218. if got, want := isLocal, tt.wantLocal; got != want {
  219. t.Errorf("newListenerAndURL(%v) isLocal = %v; want %v", tt.hostport, got, want)
  220. }
  221. })
  222. }
  223. }
  224. func TestIsLocalHost(t *testing.T) {
  225. for _, s := range []string{"localhost:10000", "[::1]:10000", "127.0.0.1:10000"} {
  226. host, _, err := net.SplitHostPort(s)
  227. if err != nil {
  228. t.Error("unexpected error when splitting", s)
  229. continue
  230. }
  231. if !isLocalhost(host) {
  232. t.Errorf("host %s from %s not considered local", host, s)
  233. }
  234. }
  235. }