Нет описания

fetch_test.go 6.3KB

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