暂无描述

fetch_test.go 6.9KB

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