Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tradefed.util;
     18 
     19 import com.android.tradefed.build.IBuildInfo;
     20 import com.android.tradefed.build.IDeviceBuildInfo;
     21 import com.android.tradefed.log.LogUtil.CLog;
     22 
     23 import com.google.common.annotations.VisibleForTesting;
     24 
     25 import java.io.File;
     26 import java.util.ArrayList;
     27 import java.util.Arrays;
     28 import java.util.List;
     29 
     30 
     31 /** Utility class for making system calls. */
     32 public class SystemUtil {
     33 
     34     @VisibleForTesting static SystemUtil singleton = new SystemUtil();
     35 
     36     // Environment variables for the test cases directory in target out directory and host out
     37     // directory.
     38     @VisibleForTesting
     39     static final String ENV_ANDROID_TARGET_OUT_TESTCASES = "ANDROID_TARGET_OUT_TESTCASES";
     40 
     41     @VisibleForTesting
     42     static final String ENV_ANDROID_HOST_OUT_TESTCASES = "ANDROID_HOST_OUT_TESTCASES";
     43 
     44     static final String ENV_ANDROID_PRODUCT_OUT = "ANDROID_PRODUCT_OUT";
     45 
     46     private static final String HOST_TESTCASES = "host/testcases";
     47     private static final String TARGET_TESTCASES = "target/testcases";
     48 
     49     /**
     50      * Get the value of an environment variable.
     51      *
     52      * <p>The wrapper function is created for mock in unit test.
     53      *
     54      * @param name the name of the environment variable.
     55      * @return {@link String} value of the given environment variable.
     56      */
     57     @VisibleForTesting
     58     String getEnv(String name) {
     59         return System.getenv(name);
     60     }
     61 
     62     /** Get a list of {@link File} pointing to tests directories external to Tradefed. */
     63     public static List<File> getExternalTestCasesDirs() {
     64         List<File> testCasesDirs = new ArrayList<File>();
     65         // TODO(b/36782030): Support running both HOST and TARGET tests.
     66         List<String> testCasesDirNames =
     67                 // List order matters. ConfigurationFactory caller uses first dir with test config.
     68                 Arrays.asList(
     69                         singleton.getEnv(ENV_ANDROID_TARGET_OUT_TESTCASES),
     70                         singleton.getEnv(ENV_ANDROID_HOST_OUT_TESTCASES));
     71         for (String testCasesDirName : testCasesDirNames) {
     72             if (testCasesDirName != null) {
     73                 File dir = new File(testCasesDirName);
     74                 if (dir.exists() && dir.isDirectory()) {
     75                     CLog.d("Found test case dir: %s", testCasesDirName);
     76                     testCasesDirs.add(dir);
     77                 } else {
     78                     CLog.w(
     79                             "Path %s for test cases directory does not exist or it's not a "
     80                                     + "directory.",
     81                             testCasesDirName);
     82                 }
     83             }
     84         }
     85         return testCasesDirs;
     86     }
     87 
     88     /**
     89      * Get a list of {@link File} of the test cases directories
     90      *
     91      * @param buildInfo the build artifact information. Set it to null if build info is not
     92      *     available or there is no need to get test cases directories from build info.
     93      * @return a list of {@link File} of directories of the test cases folder of build output, based
     94      *     on the value of environment variables and the given build info.
     95      */
     96     public static List<File> getTestCasesDirs(IBuildInfo buildInfo) {
     97         List<File> testCasesDirs = new ArrayList<File>();
     98         testCasesDirs.addAll(getExternalTestCasesDirs());
     99 
    100         // TODO: Remove this logic after Versioned TF V2 is implemented, in which staging build
    101         // artifact will be done by the parent process, and the test cases dirs will be set by
    102         // environment variables.
    103         // Add tests dir from build info.
    104         if (buildInfo instanceof IDeviceBuildInfo) {
    105             IDeviceBuildInfo deviceBuildInfo = (IDeviceBuildInfo) buildInfo;
    106             File testsDir = deviceBuildInfo.getTestsDir();
    107             // Add all possible paths to the testcases directory list.
    108             if (testsDir != null) {
    109                 testCasesDirs.addAll(
    110                         Arrays.asList(
    111                                 testsDir,
    112                                 FileUtil.getFileForPath(testsDir, HOST_TESTCASES),
    113                                 FileUtil.getFileForPath(testsDir, TARGET_TESTCASES)));
    114             }
    115         }
    116 
    117         return testCasesDirs;
    118     }
    119 
    120     /**
    121      * Gets the product specific output dir from an Android build tree. Typically this location
    122      * contains images for various device partitions, bootloader, radio and so on.
    123      *
    124      * <p>Note: the method does not guarantee that this path exists.
    125      *
    126      * @return the location of the output dir or <code>null</code> if the current build is not
    127      */
    128     public static File getProductOutputDir() {
    129         String path = singleton.getEnv(ENV_ANDROID_PRODUCT_OUT);
    130         if (path == null) {
    131             return null;
    132         } else {
    133             return new File(path);
    134         }
    135     }
    136 }
    137