blob: 764a1e1278b0bc39c2ea6f58a7a835cdaa0adb40 [file] [log] [blame]
Patrice Arruda219eef32020-06-01 17:29:30 +00001// 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
15package build
16
17import (
Patrice Arruda7cc20742020-06-10 18:48:01 +000018 "errors"
Patrice Arruda219eef32020-06-01 17:29:30 +000019 "io/ioutil"
20 "os"
21 "path/filepath"
Patrice Arruda7d235cc2020-12-09 22:43:26 +000022 "reflect"
23 "sort"
Patrice Arruda219eef32020-06-01 17:29:30 +000024 "strconv"
25 "strings"
26 "testing"
27 "time"
28
29 "android/soong/ui/logger"
30)
31
Patrice Arruda7d235cc2020-12-09 22:43:26 +000032func TestPruneMetricsFiles(t *testing.T) {
33 rootDir := t.TempDir()
34
35 dirs := []string{
36 filepath.Join(rootDir, "d1"),
37 filepath.Join(rootDir, "d1", "d2"),
38 filepath.Join(rootDir, "d1", "d2", "d3"),
39 }
40
41 files := []string{
42 filepath.Join(rootDir, "d1", "f1"),
43 filepath.Join(rootDir, "d1", "d2", "f1"),
44 filepath.Join(rootDir, "d1", "d2", "d3", "f1"),
45 }
46
47 for _, d := range dirs {
48 if err := os.MkdirAll(d, 0777); err != nil {
49 t.Fatalf("got %v, expecting nil error for making directory %q", err, d)
50 }
51 }
52
53 for _, f := range files {
54 if err := ioutil.WriteFile(f, []byte{}, 0777); err != nil {
55 t.Fatalf("got %v, expecting nil error on writing file %q", err, f)
56 }
57 }
58
59 want := []string{
60 filepath.Join(rootDir, "d1", "f1"),
61 filepath.Join(rootDir, "d1", "d2", "f1"),
62 filepath.Join(rootDir, "d1", "d2", "d3", "f1"),
63 }
64
65 got := pruneMetricsFiles([]string{rootDir})
66
67 sort.Strings(got)
68 sort.Strings(want)
69
70 if !reflect.DeepEqual(got, want) {
71 t.Errorf("got %q, want %q after pruning metrics files", got, want)
72 }
73}
74
Patrice Arruda219eef32020-06-01 17:29:30 +000075func TestUploadMetrics(t *testing.T) {
76 ctx := testContext()
77 tests := []struct {
78 description string
79 uploader string
80 createFiles bool
81 files []string
82 }{{
Yu Liu6e13b402021-07-27 14:29:06 -070083 description: "no metrics uploader",
Patrice Arruda219eef32020-06-01 17:29:30 +000084 }, {
85 description: "non-existent metrics files no upload",
Yu Liu6e13b402021-07-27 14:29:06 -070086 uploader: "echo",
Patrice Arruda219eef32020-06-01 17:29:30 +000087 files: []string{"metrics_file_1", "metrics_file_2", "metrics_file_3"},
88 }, {
89 description: "trigger upload",
90 uploader: "echo",
91 createFiles: true,
92 files: []string{"metrics_file_1", "metrics_file_2"},
93 }}
94
95 for _, tt := range tests {
96 t.Run(tt.description, func(t *testing.T) {
97 defer logger.Recover(func(err error) {
98 t.Fatalf("got unexpected error: %v", err)
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
Patrice Arruda92dc64f2020-11-16 16:29:16 -0800107 // Supply our own tmpDir to delete the temp dir once the test is done.
108 orgTmpDir := tmpDir
109 tmpDir = func(string, string) (string, error) {
Patrice Arruda7cc20742020-06-10 18:48:01 +0000110 retDir := filepath.Join(outDir, "tmp_upload_dir")
111 if err := os.Mkdir(retDir, 0755); err != nil {
112 t.Fatalf("failed to create temporary directory %q: %v", retDir, err)
113 }
114 return retDir, nil
115 }
Patrice Arruda92dc64f2020-11-16 16:29:16 -0800116 defer func() { tmpDir = orgTmpDir }()
Patrice Arruda7cc20742020-06-10 18:48:01 +0000117
118 metricsUploadDir := filepath.Join(outDir, ".metrics_uploader")
119 if err := os.Mkdir(metricsUploadDir, 0755); err != nil {
120 t.Fatalf("failed to create %q directory for oauth valid check: %v", metricsUploadDir, err)
121 }
122
Patrice Arruda219eef32020-06-01 17:29:30 +0000123 var metricsFiles []string
124 if tt.createFiles {
125 for _, f := range tt.files {
126 filename := filepath.Join(outDir, f)
127 metricsFiles = append(metricsFiles, filename)
128 if err := ioutil.WriteFile(filename, []byte("test file"), 0644); err != nil {
129 t.Fatalf("failed to create a fake metrics file %q for uploading: %v", filename, err)
130 }
131 }
132 }
133
134 config := Config{&configImpl{
135 environ: &Environment{
136 "OUT_DIR=" + outDir,
Patrice Arruda219eef32020-06-01 17:29:30 +0000137 },
Yu Liu6e13b402021-07-27 14:29:06 -0700138 buildDateTime: strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10),
139 metricsUploader: tt.uploader,
Patrice Arruda219eef32020-06-01 17:29:30 +0000140 }}
141
Patrice Arruda73c790f2020-07-13 23:01:18 +0000142 UploadMetrics(ctx, config, false, time.Now(), metricsFiles...)
Patrice Arruda219eef32020-06-01 17:29:30 +0000143 })
144 }
145}
146
147func TestUploadMetricsErrors(t *testing.T) {
Patrice Arruda7cc20742020-06-10 18:48:01 +0000148 ctx := testContext()
149 tests := []struct {
150 description string
151 tmpDir string
152 tmpDirErr error
153 expectedErr string
154 }{{
155 description: "getTmpDir returned error",
156 tmpDirErr: errors.New("getTmpDir failed"),
157 expectedErr: "getTmpDir failed",
158 }, {
159 description: "copyFile operation error",
160 tmpDir: "/fake_dir",
161 expectedErr: "failed to copy",
162 }}
Patrice Arruda219eef32020-06-01 17:29:30 +0000163
Patrice Arruda7cc20742020-06-10 18:48:01 +0000164 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
173 outDir, err := ioutil.TempDir("", "")
174 if err != nil {
175 t.Fatalf("failed to create out directory: %v", outDir)
176 }
177 defer os.RemoveAll(outDir)
178
Patrice Arruda92dc64f2020-11-16 16:29:16 -0800179 orgTmpDir := tmpDir
180 tmpDir = func(string, string) (string, error) {
Patrice Arruda7cc20742020-06-10 18:48:01 +0000181 return tt.tmpDir, tt.tmpDirErr
182 }
Patrice Arruda92dc64f2020-11-16 16:29:16 -0800183 defer func() { tmpDir = orgTmpDir }()
Patrice Arruda7cc20742020-06-10 18:48:01 +0000184
185 metricsFile := filepath.Join(outDir, "metrics_file_1")
186 if err := ioutil.WriteFile(metricsFile, []byte("test file"), 0644); err != nil {
187 t.Fatalf("failed to create a fake metrics file %q for uploading: %v", metricsFile, err)
188 }
189
190 config := Config{&configImpl{
191 environ: &Environment{
Patrice Arruda7cc20742020-06-10 18:48:01 +0000192 "OUT_DIR=/bad",
Yu Liu6e13b402021-07-27 14:29:06 -0700193 },
194 metricsUploader: "echo",
195 }}
Patrice Arruda7cc20742020-06-10 18:48:01 +0000196
Patrice Arruda73c790f2020-07-13 23:01:18 +0000197 UploadMetrics(ctx, config, true, time.Now(), metricsFile)
Patrice Arruda7cc20742020-06-10 18:48:01 +0000198 t.Errorf("got nil, expecting %q as a failure", tt.expectedErr)
199 })
Patrice Arruda219eef32020-06-01 17:29:30 +0000200 }
Patrice Arruda219eef32020-06-01 17:29:30 +0000201}