暫無描述

fetch_test.go 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2014 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/http"
  19. "net/url"
  20. "os"
  21. "path/filepath"
  22. "reflect"
  23. "regexp"
  24. "runtime"
  25. "testing"
  26. "time"
  27. "github.com/google/pprof/internal/plugin"
  28. "github.com/google/pprof/internal/proftest"
  29. "github.com/google/pprof/profile"
  30. )
  31. func TestSymbolizationPath(t *testing.T) {
  32. if runtime.GOOS == "windows" {
  33. t.Skip("test assumes Unix paths")
  34. }
  35. // Save environment variables to restore after test
  36. saveHome := os.Getenv("HOME")
  37. savePath := os.Getenv("PPROF_BINARY_PATH")
  38. tempdir, err := ioutil.TempDir("", "home")
  39. if err != nil {
  40. t.Fatal("creating temp dir: ", err)
  41. }
  42. defer os.RemoveAll(tempdir)
  43. os.MkdirAll(filepath.Join(tempdir, "pprof", "binaries", "abcde10001"), 0700)
  44. os.Create(filepath.Join(tempdir, "pprof", "binaries", "abcde10001", "binary"))
  45. obj := testObj{tempdir}
  46. os.Setenv("HOME", tempdir)
  47. for _, tc := range []struct {
  48. env, file, buildID, want string
  49. msgCount int
  50. }{
  51. {"", "/usr/bin/binary", "", "/usr/bin/binary", 0},
  52. {"", "/usr/bin/binary", "fedcb10000", "/usr/bin/binary", 0},
  53. {"", "/prod/path/binary", "abcde10001", filepath.Join(tempdir, "pprof/binaries/abcde10001/binary"), 0},
  54. {"/alternate/architecture", "/usr/bin/binary", "", "/alternate/architecture/binary", 0},
  55. {"/alternate/architecture", "/usr/bin/binary", "abcde10001", "/alternate/architecture/binary", 0},
  56. {"/nowhere:/alternate/architecture", "/usr/bin/binary", "fedcb10000", "/usr/bin/binary", 1},
  57. {"/nowhere:/alternate/architecture", "/usr/bin/binary", "abcde10002", "/usr/bin/binary", 1},
  58. } {
  59. os.Setenv("PPROF_BINARY_PATH", tc.env)
  60. p := &profile.Profile{
  61. Mapping: []*profile.Mapping{
  62. {
  63. File: tc.file,
  64. BuildID: tc.buildID,
  65. },
  66. },
  67. }
  68. s := &source{}
  69. locateBinaries(p, s, obj, &proftest.TestUI{t, tc.msgCount})
  70. if file := p.Mapping[0].File; file != tc.want {
  71. t.Errorf("%s:%s:%s, want %s, got %s", tc.env, tc.file, tc.buildID, tc.want, file)
  72. }
  73. }
  74. os.Setenv("HOME", saveHome)
  75. os.Setenv("PPROF_BINARY_PATH", savePath)
  76. }
  77. func TestCollectMappingSources(t *testing.T) {
  78. const startAddress uint64 = 0x40000
  79. const url = "http://example.com"
  80. for _, tc := range []struct {
  81. file, buildID string
  82. want plugin.MappingSources
  83. }{
  84. {"/usr/bin/binary", "buildId", mappingSources("buildId", url, startAddress)},
  85. {"/usr/bin/binary", "", mappingSources("/usr/bin/binary", url, startAddress)},
  86. {"", "", mappingSources(url, url, startAddress)},
  87. } {
  88. p := &profile.Profile{
  89. Mapping: []*profile.Mapping{
  90. {
  91. File: tc.file,
  92. BuildID: tc.buildID,
  93. Start: startAddress,
  94. },
  95. },
  96. }
  97. got := collectMappingSources(p, url)
  98. if !reflect.DeepEqual(got, tc.want) {
  99. t.Errorf("%s:%s, want %s, got %s", tc.file, tc.buildID, tc.want, got)
  100. }
  101. }
  102. }
  103. func TestUnsourceMappings(t *testing.T) {
  104. for _, tc := range []struct {
  105. file, buildID, want string
  106. }{
  107. {"/usr/bin/binary", "buildId", "/usr/bin/binary"},
  108. {"http://example.com", "", ""},
  109. } {
  110. p := &profile.Profile{
  111. Mapping: []*profile.Mapping{
  112. {
  113. File: tc.file,
  114. BuildID: tc.buildID,
  115. },
  116. },
  117. }
  118. unsourceMappings(p)
  119. if got := p.Mapping[0].File; got != tc.want {
  120. t.Errorf("%s:%s, want %s, got %s", tc.file, tc.buildID, tc.want, got)
  121. }
  122. }
  123. }
  124. type testObj struct {
  125. home string
  126. }
  127. func (o testObj) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
  128. switch file {
  129. case "/alternate/architecture/binary":
  130. return testFile{file, "abcde10001"}, nil
  131. case "/usr/bin/binary":
  132. return testFile{file, "fedcb10000"}, nil
  133. case filepath.Join(o.home, "pprof/binaries/abcde10001/binary"):
  134. return testFile{file, "abcde10001"}, nil
  135. }
  136. return nil, fmt.Errorf("not found: %s", file)
  137. }
  138. func (testObj) Demangler(_ string) func(names []string) (map[string]string, error) {
  139. return func(names []string) (map[string]string, error) { return nil, nil }
  140. }
  141. func (testObj) Disasm(file string, start, end uint64) ([]plugin.Inst, error) { return nil, nil }
  142. type testFile struct{ name, buildID string }
  143. func (f testFile) Name() string { return f.name }
  144. func (testFile) Base() uint64 { return 0 }
  145. func (f testFile) BuildID() string { return f.buildID }
  146. func (testFile) SourceLine(addr uint64) ([]plugin.Frame, error) { return nil, nil }
  147. func (testFile) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) { return nil, nil }
  148. func (testFile) Close() error { return nil }
  149. func TestFetch(t *testing.T) {
  150. const path = "testdata/"
  151. // Intercept http.Get calls from HTTPFetcher.
  152. httpGet = stubHTTPGet
  153. type testcase struct {
  154. source, execName string
  155. }
  156. for _, tc := range []testcase{
  157. {path + "go.crc32.cpu", ""},
  158. {path + "go.nomappings.crash", "/bin/gotest.exe"},
  159. {"http://localhost/profile?file=cppbench.cpu", ""},
  160. } {
  161. p, _, _, err := grabProfile(&source{ExecName: tc.execName}, tc.source, 0, nil, testObj{}, &proftest.TestUI{t, 0})
  162. if err != nil {
  163. t.Fatalf("%s: %s", tc.source, err)
  164. }
  165. if len(p.Sample) == 0 {
  166. t.Errorf("%s: want non-zero samples", tc.source)
  167. }
  168. if e := tc.execName; e != "" {
  169. switch {
  170. case len(p.Mapping) == 0 || p.Mapping[0] == nil:
  171. t.Errorf("%s: want mapping[0].execName == %s, got no mappings", tc.source, e)
  172. case p.Mapping[0].File != e:
  173. t.Errorf("%s: want mapping[0].execName == %s, got %s", tc.source, e, p.Mapping[0].File)
  174. }
  175. }
  176. }
  177. }
  178. // mappingSources creates MappingSources map with a single item.
  179. func mappingSources(key, source string, start uint64) plugin.MappingSources {
  180. return plugin.MappingSources{
  181. key: []struct {
  182. Source string
  183. Start uint64
  184. }{
  185. {Source: source, Start: start},
  186. },
  187. }
  188. }
  189. // stubHTTPGet intercepts a call to http.Get and rewrites it to use
  190. // "file://" to get the profile directly from a file.
  191. func stubHTTPGet(source string, _ time.Duration) (*http.Response, error) {
  192. url, err := url.Parse(source)
  193. if err != nil {
  194. return nil, err
  195. }
  196. values := url.Query()
  197. file := values.Get("file")
  198. if file == "" {
  199. return nil, fmt.Errorf("want .../file?profile, got %s", source)
  200. }
  201. t := &http.Transport{}
  202. t.RegisterProtocol("file", http.NewFileTransport(http.Dir("testdata/")))
  203. c := &http.Client{Transport: t}
  204. return c.Get("file:///" + file)
  205. }