No Description

webui_test.go 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 := &webInterface{
  32. prof: prof,
  33. options: &plugin.Options{Obj: fakeObjTool{}},
  34. help: make(map[string]string),
  35. }
  36. // Start test server.
  37. server := httptest.NewServer(http.HandlerFunc(
  38. func(w http.ResponseWriter, r *http.Request) {
  39. switch r.URL.Path {
  40. case "/":
  41. ui.dot(w, r)
  42. case "/top":
  43. ui.top(w, r)
  44. case "/disasm":
  45. ui.disasm(w, r)
  46. case "/peek":
  47. ui.peek(w, r)
  48. case "/weblist":
  49. ui.weblist(w, r)
  50. }
  51. }))
  52. defer server.Close()
  53. haveDot := false
  54. if _, err := exec.LookPath("dot"); err == nil {
  55. haveDot = true
  56. }
  57. type testCase struct {
  58. path string
  59. want []string
  60. needDot bool
  61. }
  62. testcases := []testCase{
  63. {"/", []string{"F1", "F2", "F3", "testbin", "cpu"}, true},
  64. {"/top", []string{"Flat", "200ms.*100%.*F2"}, false},
  65. {"/weblist?f=" + url.QueryEscape("F[12]"),
  66. []string{"F1", "F2", "300ms line1"}, false},
  67. {"/peek?f=" + url.QueryEscape("F[12]"),
  68. []string{"300ms.*F1", "200ms.*300ms.*F2"}, false},
  69. {"/disasm?f=" + url.QueryEscape("F[12]"),
  70. []string{"f1:asm", "f2:asm"}, false},
  71. }
  72. for _, c := range testcases {
  73. if c.needDot && !haveDot {
  74. t.Log("skipping", c.path, "since dot (graphviz) does not seem to be installed")
  75. continue
  76. }
  77. res, err := http.Get(server.URL + c.path)
  78. if err != nil {
  79. t.Error("could not fetch", c.path, err)
  80. continue
  81. }
  82. data, err := ioutil.ReadAll(res.Body)
  83. if err != nil {
  84. t.Error("could not read response", c.path, err)
  85. continue
  86. }
  87. result := string(data)
  88. for _, w := range c.want {
  89. if match, _ := regexp.MatchString(w, result); !match {
  90. t.Errorf("response for %s does not match "+
  91. "expected pattern '%s'; "+
  92. "actual result:\n%s", c.path, w, result)
  93. }
  94. }
  95. }
  96. }
  97. // Implement fake object file support.
  98. const addrBase = 0x1000
  99. const fakeSource = "testdata/file1000.src"
  100. type fakeObj struct{}
  101. func (f fakeObj) Close() error { return nil }
  102. func (f fakeObj) Name() string { return "testbin" }
  103. func (f fakeObj) Base() uint64 { return 0 }
  104. func (f fakeObj) BuildID() string { return "" }
  105. func (f fakeObj) SourceLine(addr uint64) ([]plugin.Frame, error) {
  106. return nil, fmt.Errorf("SourceLine unimplemented")
  107. }
  108. func (f fakeObj) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
  109. return []*plugin.Sym{
  110. {[]string{"F1"}, fakeSource, addrBase, addrBase + 10},
  111. {[]string{"F2"}, fakeSource, addrBase + 10, addrBase + 20},
  112. {[]string{"F3"}, fakeSource, addrBase + 20, addrBase + 30},
  113. }, nil
  114. }
  115. type fakeObjTool struct{}
  116. func (obj fakeObjTool) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
  117. return fakeObj{}, nil
  118. }
  119. func (obj fakeObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
  120. return []plugin.Inst{
  121. {Addr: addrBase + 0, Text: "f1:asm", Function: "F1"},
  122. {Addr: addrBase + 10, Text: "f2:asm", Function: "F2"},
  123. {Addr: addrBase + 20, Text: "d3:asm", Function: "F3"},
  124. }, nil
  125. }
  126. func makeFakeProfile() *profile.Profile {
  127. // Three functions: F1, F2, F3 with three lines, 11, 22, 33.
  128. funcs := []*profile.Function{
  129. {ID: 1, Name: "F1", Filename: fakeSource, StartLine: 3},
  130. {ID: 2, Name: "F2", Filename: fakeSource, StartLine: 5},
  131. {ID: 3, Name: "F3", Filename: fakeSource, StartLine: 7},
  132. }
  133. lines := []profile.Line{
  134. {Function: funcs[0], Line: 11},
  135. {Function: funcs[1], Line: 22},
  136. {Function: funcs[2], Line: 33},
  137. }
  138. mapping := []*profile.Mapping{
  139. {
  140. ID: 1,
  141. Start: addrBase,
  142. Limit: addrBase + 10,
  143. Offset: 0,
  144. File: "testbin",
  145. HasFunctions: true,
  146. HasFilenames: true,
  147. HasLineNumbers: true,
  148. },
  149. }
  150. // Three interesting addresses: base+{10,20,30}
  151. locs := []*profile.Location{
  152. {ID: 1, Address: addrBase + 10, Line: lines[0:1], Mapping: mapping[0]},
  153. {ID: 2, Address: addrBase + 20, Line: lines[1:2], Mapping: mapping[0]},
  154. {ID: 3, Address: addrBase + 30, Line: lines[2:3], Mapping: mapping[0]},
  155. }
  156. // Two stack traces.
  157. return &profile.Profile{
  158. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  159. Period: 1,
  160. DurationNanos: 10e9,
  161. SampleType: []*profile.ValueType{
  162. {Type: "cpu", Unit: "milliseconds"},
  163. },
  164. Sample: []*profile.Sample{
  165. {
  166. Location: []*profile.Location{locs[2], locs[1], locs[0]},
  167. Value: []int64{100},
  168. },
  169. {
  170. Location: []*profile.Location{locs[1], locs[0]},
  171. Value: []int64{200},
  172. },
  173. },
  174. Location: locs,
  175. Function: funcs,
  176. Mapping: mapping,
  177. }
  178. }
  179. func TestNewListenerAndURL(t *testing.T) {
  180. if runtime.GOOS == "nacl" {
  181. t.Skip("test assumes tcp available")
  182. }
  183. tests := []struct {
  184. hostport string
  185. wantErr bool
  186. wantURLRe *regexp.Regexp
  187. wantLocal bool
  188. }{
  189. {
  190. hostport: ":",
  191. wantURLRe: regexp.MustCompile(`http://localhost:\d+`),
  192. wantLocal: true,
  193. },
  194. {
  195. hostport: "localhost:",
  196. wantURLRe: regexp.MustCompile(`http://localhost:\d+`),
  197. wantLocal: true,
  198. },
  199. {
  200. hostport: "http://localhost:12345",
  201. wantErr: true,
  202. },
  203. }
  204. for _, tt := range tests {
  205. t.Run(tt.hostport, func(t *testing.T) {
  206. _, url, isLocal, err := newListenerAndURL(tt.hostport)
  207. if tt.wantErr {
  208. if err == nil {
  209. t.Fatalf("newListenerAndURL(%v) want error; got none", tt.hostport)
  210. }
  211. return
  212. }
  213. if err != nil {
  214. t.Fatalf("newListenerAndURL(%v) = %v", tt.hostport, err)
  215. }
  216. if !tt.wantURLRe.MatchString(url) {
  217. t.Errorf("newListenerAndURL(%v) URL = %v; want URL matching %v", tt.hostport, url, tt.wantURLRe)
  218. }
  219. if got, want := isLocal, tt.wantLocal; got != want {
  220. t.Errorf("newListenerAndURL(%v) isLocal = %v; want %v", tt.hostport, got, want)
  221. }
  222. })
  223. }
  224. }
  225. func TestIsLocalHost(t *testing.T) {
  226. for _, s := range []string{"localhost:10000", "[::1]:10000", "127.0.0.1:10000"} {
  227. host, _, err := net.SplitHostPort(s)
  228. if err != nil {
  229. t.Error("unexpected error when splitting", s)
  230. continue
  231. }
  232. if !isLocalhost(host) {
  233. t.Errorf("host %s from %s not considered local", host, s)
  234. }
  235. }
  236. }