Nenhuma descrição

webui_test.go 6.6KB

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