platform_build_soong/ui/build/util_test.go
Patrice Arruda 7cc2074885 Run the metrics uploader in the background.
The metrics uploader was currently running on foreground where it
would copy the metrics files in a separate directory and then forked
into the background for the upload process. As a result, running the
lunch command would take a second longer to run since each metrics
uploader run had an average of half a second.

Bug: 140638454
Test: * Wrote and updated unit test cases.
      * Set ANDROID_ENABLE_METRICS_UPLOAD to point to the latest
        metrics_uploader bash script. Executed the "lunch 1" command
	and measured the running time. Executed "m nothing" command
	and checked that the metrics were uploaded.
      * Ran "lunch 1" and "m nothing" with
        ANDROID_ENABLE_METRICS_UPLOAD=""
      * Removed oauth from metrics_uploader and ran "m nothing" and
        "lunch 1". The oauth Message appeared only to "m nothing"

Change-Id: I13c61e666c8f44613dee291a704cef6a27335188
2020-06-13 00:34:37 +00:00

121 lines
3.1 KiB
Go

// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package build
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
"android/soong/ui/logger"
)
func TestEnsureEmptyDirs(t *testing.T) {
ctx := testContext()
defer logger.Recover(func(err error) {
t.Error(err)
})
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer func() {
err := os.RemoveAll(tmpDir)
if err != nil {
t.Errorf("Error removing tmpDir: %v", err)
}
}()
ensureEmptyDirectoriesExist(ctx, filepath.Join(tmpDir, "a/b"))
err = os.Chmod(filepath.Join(tmpDir, "a"), 0555)
if err != nil {
t.Fatalf("Failed to chown: %v", err)
}
ensureEmptyDirectoriesExist(ctx, filepath.Join(tmpDir, "a"))
}
func TestCopyFile(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "test_copy_file")
if err != nil {
t.Fatalf("failed to create temporary directory to hold test text files: %v", err)
}
defer os.Remove(tmpDir)
data := []byte("fake data")
src := filepath.Join(tmpDir, "src.txt")
if err := ioutil.WriteFile(src, data, 0755); err != nil {
t.Fatalf("failed to create a src file %q for copying: %v", src, err)
}
dst := filepath.Join(tmpDir, "dst.txt")
l, err := copyFile(src, dst)
if err != nil {
t.Fatalf("got %v, expecting nil error on copyFile operation", err)
}
if l != int64(len(data)) {
t.Errorf("got %d, expecting %d for copied bytes", l, len(data))
}
dstData, err := ioutil.ReadFile(dst)
if err != nil {
t.Fatalf("got %v, expecting nil error reading dst %q file", err, dst)
}
if bytes.Compare(data, dstData) != 0 {
t.Errorf("got %q, expecting data %q from dst %q text file", string(data), string(dstData), dst)
}
}
func TestCopyFileErrors(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "test_copy_file_errors")
if err != nil {
t.Fatalf("failed to create temporary directory to hold test text files: %v", err)
}
defer os.Remove(tmpDir)
srcExists := filepath.Join(tmpDir, "src_exist.txt")
if err := ioutil.WriteFile(srcExists, []byte("fake data"), 0755); err != nil {
t.Fatalf("failed to create a src file %q for copying: %v", srcExists, err)
}
tests := []struct {
description string
src string
dst string
}{{
description: "src file does not exist",
src: "/src/not/exist",
dst: "/dst/not/exist",
}, {
description: "dst directory does not exist",
src: srcExists,
dst: "/dst/not/exist",
}}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
if _, err := copyFile(tt.src, tt.dst); err == nil {
t.Errorf("got nil, expecting error")
}
})
}
}