Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame^] | 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
| 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "path/filepath" |
| 21 | "strconv" |
| 22 | "strings" |
| 23 | "testing" |
| 24 | "time" |
| 25 | |
| 26 | "android/soong/ui/logger" |
| 27 | ) |
| 28 | |
| 29 | func TestUploadMetrics(t *testing.T) { |
| 30 | ctx := testContext() |
| 31 | tests := []struct { |
| 32 | description string |
| 33 | uploader string |
| 34 | createFiles bool |
| 35 | files []string |
| 36 | }{{ |
| 37 | description: "ANDROID_ENABLE_METRICS_UPLOAD not set", |
| 38 | }, { |
| 39 | description: "no metrics files to upload", |
| 40 | uploader: "fake", |
| 41 | }, { |
| 42 | description: "non-existent metrics files no upload", |
| 43 | uploader: "fake", |
| 44 | files: []string{"metrics_file_1", "metrics_file_2", "metrics_file_3"}, |
| 45 | }, { |
| 46 | description: "trigger upload", |
| 47 | uploader: "echo", |
| 48 | createFiles: true, |
| 49 | files: []string{"metrics_file_1", "metrics_file_2"}, |
| 50 | }} |
| 51 | |
| 52 | for _, tt := range tests { |
| 53 | t.Run(tt.description, func(t *testing.T) { |
| 54 | defer logger.Recover(func(err error) { |
| 55 | t.Fatalf("got unexpected error: %v", err) |
| 56 | }) |
| 57 | |
| 58 | outDir, err := ioutil.TempDir("", "") |
| 59 | if err != nil { |
| 60 | t.Fatalf("failed to create out directory: %v", outDir) |
| 61 | } |
| 62 | defer os.RemoveAll(outDir) |
| 63 | |
| 64 | var metricsFiles []string |
| 65 | if tt.createFiles { |
| 66 | for _, f := range tt.files { |
| 67 | filename := filepath.Join(outDir, f) |
| 68 | metricsFiles = append(metricsFiles, filename) |
| 69 | if err := ioutil.WriteFile(filename, []byte("test file"), 0644); err != nil { |
| 70 | t.Fatalf("failed to create a fake metrics file %q for uploading: %v", filename, err) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | config := Config{&configImpl{ |
| 76 | environ: &Environment{ |
| 77 | "OUT_DIR=" + outDir, |
| 78 | "ANDROID_ENABLE_METRICS_UPLOAD=" + tt.uploader, |
| 79 | }, |
| 80 | buildDateTime: strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10), |
| 81 | }} |
| 82 | |
| 83 | UploadMetrics(ctx, config, 1591031903, metricsFiles...) |
| 84 | |
| 85 | if _, err := os.Stat(filepath.Join(outDir, uploadPbFilename)); err == nil { |
| 86 | t.Error("got true, want false for upload protobuf file to exist") |
| 87 | } |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestUploadMetricsErrors(t *testing.T) { |
| 93 | expectedErr := "failed to write the marshaled" |
| 94 | defer logger.Recover(func(err error) { |
| 95 | got := err.Error() |
| 96 | if !strings.Contains(got, expectedErr) { |
| 97 | t.Errorf("got %q, want %q to be contained in error", got, expectedErr) |
| 98 | } |
| 99 | }) |
| 100 | |
| 101 | outDir, err := ioutil.TempDir("", "") |
| 102 | if err != nil { |
| 103 | t.Fatalf("failed to create out directory: %v", outDir) |
| 104 | } |
| 105 | defer os.RemoveAll(outDir) |
| 106 | |
| 107 | metricsFile := filepath.Join(outDir, "metrics_file_1") |
| 108 | if err := ioutil.WriteFile(metricsFile, []byte("test file"), 0644); err != nil { |
| 109 | t.Fatalf("failed to create a fake metrics file %q for uploading: %v", metricsFile, err) |
| 110 | } |
| 111 | |
| 112 | config := Config{&configImpl{ |
| 113 | environ: &Environment{ |
| 114 | "ANDROID_ENABLE_METRICS_UPLOAD=fake", |
| 115 | "OUT_DIR=/bad", |
| 116 | }}} |
| 117 | |
| 118 | UploadMetrics(testContext(), config, 1591031903, metricsFile) |
| 119 | t.Errorf("got nil, expecting %q as a failure", expectedErr) |
| 120 | } |