upload_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Copyright 2020 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 build
  15. import (
  16. "errors"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "reflect"
  21. "sort"
  22. "strconv"
  23. "strings"
  24. "testing"
  25. "time"
  26. "android/soong/ui/logger"
  27. )
  28. func writeBazelProfileFile(dir string) error {
  29. contents := `
  30. === PHASE SUMMARY INFORMATION ===
  31. Total launch phase time 1.193 s 15.77%
  32. Total init phase time 1.092 s 14.44%
  33. Total target pattern evaluation phase time 0.580 s 7.67%
  34. Total interleaved loading-and-analysis phase time 3.646 s 48.21%
  35. Total preparation phase time 0.022 s 0.30%
  36. Total execution phase time 0.993 s 13.13%
  37. Total finish phase time 0.036 s 0.48%
  38. ---------------------------------------------------------------------
  39. Total run time 7.563 s 100.00%
  40. Critical path (178 ms):
  41. Time Percentage Description
  42. 178 ms 100.00% action 'BazelWorkspaceStatusAction stable-status.txt'
  43. `
  44. file := filepath.Join(dir, "bazel_metrics.txt")
  45. return os.WriteFile(file, []byte(contents), 0666)
  46. }
  47. func TestPruneMetricsFiles(t *testing.T) {
  48. rootDir := t.TempDir()
  49. dirs := []string{
  50. filepath.Join(rootDir, "d1"),
  51. filepath.Join(rootDir, "d1", "d2"),
  52. filepath.Join(rootDir, "d1", "d2", "d3"),
  53. }
  54. files := []string{
  55. filepath.Join(rootDir, "d1", "f1"),
  56. filepath.Join(rootDir, "d1", "d2", "f1"),
  57. filepath.Join(rootDir, "d1", "d2", "d3", "f1"),
  58. }
  59. for _, d := range dirs {
  60. if err := os.MkdirAll(d, 0777); err != nil {
  61. t.Fatalf("got %v, expecting nil error for making directory %q", err, d)
  62. }
  63. }
  64. for _, f := range files {
  65. if err := ioutil.WriteFile(f, []byte{}, 0777); err != nil {
  66. t.Fatalf("got %v, expecting nil error on writing file %q", err, f)
  67. }
  68. }
  69. want := []string{
  70. filepath.Join(rootDir, "d1", "f1"),
  71. filepath.Join(rootDir, "d1", "d2", "f1"),
  72. filepath.Join(rootDir, "d1", "d2", "d3", "f1"),
  73. }
  74. got := pruneMetricsFiles([]string{rootDir})
  75. sort.Strings(got)
  76. sort.Strings(want)
  77. if !reflect.DeepEqual(got, want) {
  78. t.Errorf("got %q, want %q after pruning metrics files", got, want)
  79. }
  80. }
  81. func TestUploadMetrics(t *testing.T) {
  82. ctx := testContext()
  83. tests := []struct {
  84. description string
  85. uploader string
  86. createFiles bool
  87. files []string
  88. }{{
  89. description: "no metrics uploader",
  90. }, {
  91. description: "non-existent metrics files no upload",
  92. uploader: "echo",
  93. files: []string{"metrics_file_1", "metrics_file_2", "metrics_file_3, bazel_metrics.pb"},
  94. }, {
  95. description: "trigger upload",
  96. uploader: "echo",
  97. createFiles: true,
  98. files: []string{"metrics_file_1", "metrics_file_2, bazel_metrics.pb"},
  99. }}
  100. for _, tt := range tests {
  101. t.Run(tt.description, func(t *testing.T) {
  102. defer logger.Recover(func(err error) {
  103. t.Fatalf("got unexpected error: %v", err)
  104. })
  105. outDir, err := ioutil.TempDir("", "")
  106. if err != nil {
  107. t.Fatalf("failed to create out directory: %v", outDir)
  108. }
  109. defer os.RemoveAll(outDir)
  110. // Supply our own tmpDir to delete the temp dir once the test is done.
  111. orgTmpDir := tmpDir
  112. tmpDir = func(string, string) (string, error) {
  113. retDir := filepath.Join(outDir, "tmp_upload_dir")
  114. if err := os.Mkdir(retDir, 0755); err != nil {
  115. t.Fatalf("failed to create temporary directory %q: %v", retDir, err)
  116. }
  117. return retDir, nil
  118. }
  119. defer func() { tmpDir = orgTmpDir }()
  120. metricsUploadDir := filepath.Join(outDir, ".metrics_uploader")
  121. if err := os.Mkdir(metricsUploadDir, 0755); err != nil {
  122. t.Fatalf("failed to create %q directory for oauth valid check: %v", metricsUploadDir, err)
  123. }
  124. var metricsFiles []string
  125. if tt.createFiles {
  126. for _, f := range tt.files {
  127. filename := filepath.Join(outDir, f)
  128. metricsFiles = append(metricsFiles, filename)
  129. if err := ioutil.WriteFile(filename, []byte("test file"), 0644); err != nil {
  130. t.Fatalf("failed to create a fake metrics file %q for uploading: %v", filename, err)
  131. }
  132. }
  133. }
  134. if err := writeBazelProfileFile(outDir); err != nil {
  135. t.Fatalf("failed to create bazel profile file in dir: %v", outDir)
  136. }
  137. config := Config{&configImpl{
  138. environ: &Environment{
  139. "OUT_DIR=" + outDir,
  140. },
  141. buildDateTime: strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10),
  142. metricsUploader: tt.uploader,
  143. }}
  144. UploadMetrics(ctx, config, false, time.Now(), metricsFiles...)
  145. })
  146. }
  147. }
  148. func TestUploadMetricsErrors(t *testing.T) {
  149. ctx := testContext()
  150. tests := []struct {
  151. description string
  152. tmpDir string
  153. tmpDirErr error
  154. expectedErr string
  155. }{{
  156. description: "getTmpDir returned error",
  157. tmpDirErr: errors.New("getTmpDir failed"),
  158. expectedErr: "getTmpDir failed",
  159. }, {
  160. description: "copyFile operation error",
  161. tmpDir: "/fake_dir",
  162. expectedErr: "failed to copy",
  163. }}
  164. for _, tt := range tests {
  165. t.Run(tt.description, func(t *testing.T) {
  166. defer logger.Recover(func(err error) {
  167. got := err.Error()
  168. if !strings.Contains(got, tt.expectedErr) {
  169. t.Errorf("got %q, want %q to be contained in error", got, tt.expectedErr)
  170. }
  171. })
  172. outDir, err := ioutil.TempDir("", "")
  173. if err != nil {
  174. t.Fatalf("failed to create out directory: %v", outDir)
  175. }
  176. defer os.RemoveAll(outDir)
  177. orgTmpDir := tmpDir
  178. tmpDir = func(string, string) (string, error) {
  179. return tt.tmpDir, tt.tmpDirErr
  180. }
  181. defer func() { tmpDir = orgTmpDir }()
  182. metricsFile := filepath.Join(outDir, "metrics_file_1")
  183. if err := ioutil.WriteFile(metricsFile, []byte("test file"), 0644); err != nil {
  184. t.Fatalf("failed to create a fake metrics file %q for uploading: %v", metricsFile, err)
  185. }
  186. config := Config{&configImpl{
  187. environ: &Environment{
  188. "OUT_DIR=/bad",
  189. },
  190. metricsUploader: "echo",
  191. }}
  192. UploadMetrics(ctx, config, true, time.Now(), metricsFile)
  193. t.Errorf("got nil, expecting %q as a failure", tt.expectedErr)
  194. })
  195. }
  196. }
  197. func TestParsePercentageToTenThousandths(t *testing.T) {
  198. // 2.59% should be returned as 259 - representing 259/10000 of the build
  199. percentage := parsePercentageToTenThousandths("2.59%")
  200. if percentage != 259 {
  201. t.Errorf("Expected percentage to be returned as ten-thousandths. Expected 259, have %d\n", percentage)
  202. }
  203. // Test without a leading digit
  204. percentage = parsePercentageToTenThousandths(".52%")
  205. if percentage != 52 {
  206. t.Errorf("Expected percentage to be returned as ten-thousandths. Expected 52, have %d\n", percentage)
  207. }
  208. }
  209. func TestParseTimingToNanos(t *testing.T) {
  210. // This parses from seconds (with millis precision) and returns nanos
  211. timingNanos := parseTimingToNanos("0.111")
  212. if timingNanos != 111000000 {
  213. t.Errorf("Error parsing timing. Expected 111000, have %d\n", timingNanos)
  214. }
  215. // Test without a leading digit
  216. timingNanos = parseTimingToNanos(".112")
  217. if timingNanos != 112000000 {
  218. t.Errorf("Error parsing timing. Expected 112000, have %d\n", timingNanos)
  219. }
  220. }
  221. func TestParsePhaseTiming(t *testing.T) {
  222. // Sample lines include:
  223. // Total launch phase time 0.011 s 2.59%
  224. // Total target pattern evaluation phase time 0.012 s 4.59%
  225. line1 := "Total launch phase time 0.011 s 2.59%"
  226. timing := parsePhaseTiming(line1)
  227. if timing.GetPhaseName() != "launch" {
  228. t.Errorf("Failed to parse phase name. Expected launch, have %s\n", timing.GetPhaseName())
  229. } else if timing.GetDurationNanos() != 11000000 {
  230. t.Errorf("Failed to parse duration nanos. Expected 11000000, have %d\n", timing.GetDurationNanos())
  231. } else if timing.GetPortionOfBuildTime() != 259 {
  232. t.Errorf("Failed to parse portion of build time. Expected 259, have %d\n", timing.GetPortionOfBuildTime())
  233. }
  234. // Test with a multiword phase name
  235. line2 := "Total target pattern evaluation phase time 0.012 s 4.59%"
  236. timing = parsePhaseTiming(line2)
  237. if timing.GetPhaseName() != "target pattern evaluation" {
  238. t.Errorf("Failed to parse phase name. Expected target pattern evaluation, have %s\n", timing.GetPhaseName())
  239. } else if timing.GetDurationNanos() != 12000000 {
  240. t.Errorf("Failed to parse duration nanos. Expected 12000000, have %d\n", timing.GetDurationNanos())
  241. } else if timing.GetPortionOfBuildTime() != 459 {
  242. t.Errorf("Failed to parse portion of build time. Expected 459, have %d\n", timing.GetPortionOfBuildTime())
  243. }
  244. }
  245. func TestParseTotal(t *testing.T) {
  246. // Total line is in the form of:
  247. // Total run time 7.563 s 100.00%
  248. line := "Total run time 7.563 s 100.00%"
  249. total := parseTotal(line)
  250. // Only the seconds field is parsed, as nanos
  251. if total != 7563000000 {
  252. t.Errorf("Failed to parse total build time. Expected 7563000000, have %d\n", total)
  253. }
  254. }