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 ( |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame^] | 18 | "errors" |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "strconv" |
| 23 | "strings" |
| 24 | "testing" |
| 25 | "time" |
| 26 | |
| 27 | "android/soong/ui/logger" |
| 28 | ) |
| 29 | |
| 30 | func TestUploadMetrics(t *testing.T) { |
| 31 | ctx := testContext() |
| 32 | tests := []struct { |
| 33 | description string |
| 34 | uploader string |
| 35 | createFiles bool |
| 36 | files []string |
| 37 | }{{ |
| 38 | description: "ANDROID_ENABLE_METRICS_UPLOAD not set", |
| 39 | }, { |
| 40 | description: "no metrics files to upload", |
| 41 | uploader: "fake", |
| 42 | }, { |
| 43 | description: "non-existent metrics files no upload", |
| 44 | uploader: "fake", |
| 45 | files: []string{"metrics_file_1", "metrics_file_2", "metrics_file_3"}, |
| 46 | }, { |
| 47 | description: "trigger upload", |
| 48 | uploader: "echo", |
| 49 | createFiles: true, |
| 50 | files: []string{"metrics_file_1", "metrics_file_2"}, |
| 51 | }} |
| 52 | |
| 53 | for _, tt := range tests { |
| 54 | t.Run(tt.description, func(t *testing.T) { |
| 55 | defer logger.Recover(func(err error) { |
| 56 | t.Fatalf("got unexpected error: %v", err) |
| 57 | }) |
| 58 | |
| 59 | outDir, err := ioutil.TempDir("", "") |
| 60 | if err != nil { |
| 61 | t.Fatalf("failed to create out directory: %v", outDir) |
| 62 | } |
| 63 | defer os.RemoveAll(outDir) |
| 64 | |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame^] | 65 | // Supply our own getTmpDir to delete the temp dir once the test is done. |
| 66 | orgGetTmpDir := getTmpDir |
| 67 | getTmpDir = func(string, string) (string, error) { |
| 68 | retDir := filepath.Join(outDir, "tmp_upload_dir") |
| 69 | if err := os.Mkdir(retDir, 0755); err != nil { |
| 70 | t.Fatalf("failed to create temporary directory %q: %v", retDir, err) |
| 71 | } |
| 72 | return retDir, nil |
| 73 | } |
| 74 | defer func() { getTmpDir = orgGetTmpDir }() |
| 75 | |
| 76 | metricsUploadDir := filepath.Join(outDir, ".metrics_uploader") |
| 77 | if err := os.Mkdir(metricsUploadDir, 0755); err != nil { |
| 78 | t.Fatalf("failed to create %q directory for oauth valid check: %v", metricsUploadDir, err) |
| 79 | } |
| 80 | |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 81 | var metricsFiles []string |
| 82 | if tt.createFiles { |
| 83 | for _, f := range tt.files { |
| 84 | filename := filepath.Join(outDir, f) |
| 85 | metricsFiles = append(metricsFiles, filename) |
| 86 | if err := ioutil.WriteFile(filename, []byte("test file"), 0644); err != nil { |
| 87 | t.Fatalf("failed to create a fake metrics file %q for uploading: %v", filename, err) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | config := Config{&configImpl{ |
| 93 | environ: &Environment{ |
| 94 | "OUT_DIR=" + outDir, |
| 95 | "ANDROID_ENABLE_METRICS_UPLOAD=" + tt.uploader, |
| 96 | }, |
| 97 | buildDateTime: strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10), |
| 98 | }} |
| 99 | |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame^] | 100 | UploadMetrics(ctx, config, false, 1591031903, metricsFiles...) |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestUploadMetricsErrors(t *testing.T) { |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame^] | 106 | ctx := testContext() |
| 107 | tests := []struct { |
| 108 | description string |
| 109 | tmpDir string |
| 110 | tmpDirErr error |
| 111 | expectedErr string |
| 112 | }{{ |
| 113 | description: "getTmpDir returned error", |
| 114 | tmpDirErr: errors.New("getTmpDir failed"), |
| 115 | expectedErr: "getTmpDir failed", |
| 116 | }, { |
| 117 | description: "copyFile operation error", |
| 118 | tmpDir: "/fake_dir", |
| 119 | expectedErr: "failed to copy", |
| 120 | }} |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 121 | |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame^] | 122 | for _, tt := range tests { |
| 123 | t.Run(tt.description, func(t *testing.T) { |
| 124 | defer logger.Recover(func(err error) { |
| 125 | got := err.Error() |
| 126 | if !strings.Contains(got, tt.expectedErr) { |
| 127 | t.Errorf("got %q, want %q to be contained in error", got, tt.expectedErr) |
| 128 | } |
| 129 | }) |
| 130 | |
| 131 | outDir, err := ioutil.TempDir("", "") |
| 132 | if err != nil { |
| 133 | t.Fatalf("failed to create out directory: %v", outDir) |
| 134 | } |
| 135 | defer os.RemoveAll(outDir) |
| 136 | |
| 137 | orgGetTmpDir := getTmpDir |
| 138 | getTmpDir = func(string, string) (string, error) { |
| 139 | return tt.tmpDir, tt.tmpDirErr |
| 140 | } |
| 141 | defer func() { getTmpDir = orgGetTmpDir }() |
| 142 | |
| 143 | metricsFile := filepath.Join(outDir, "metrics_file_1") |
| 144 | if err := ioutil.WriteFile(metricsFile, []byte("test file"), 0644); err != nil { |
| 145 | t.Fatalf("failed to create a fake metrics file %q for uploading: %v", metricsFile, err) |
| 146 | } |
| 147 | |
| 148 | config := Config{&configImpl{ |
| 149 | environ: &Environment{ |
| 150 | "ANDROID_ENABLE_METRICS_UPLOAD=fake", |
| 151 | "OUT_DIR=/bad", |
| 152 | }}} |
| 153 | |
| 154 | UploadMetrics(ctx, config, true, 1591031903, metricsFile) |
| 155 | t.Errorf("got nil, expecting %q as a failure", tt.expectedErr) |
| 156 | }) |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 157 | } |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 158 | } |