• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.tradefed.targetprep.suite;
17 
18 import com.android.tradefed.build.BuildInfoKey.BuildInfoFileKey;
19 import com.android.tradefed.build.IBuildInfo;
20 import com.android.tradefed.build.IDeviceBuildInfo;
21 import com.android.tradefed.config.OptionClass;
22 import com.android.tradefed.device.ITestDevice;
23 import com.android.tradefed.targetprep.TargetSetupError;
24 import com.android.tradefed.targetprep.TestAppInstallSetup;
25 import com.android.tradefed.util.FileUtil;
26 
27 import com.google.common.annotations.VisibleForTesting;
28 
29 import java.io.File;
30 import java.io.FileNotFoundException;
31 
32 /**
33  * Installs specified APKs for Suite configuration: either from $ANDROID_TARGET_OUT_TESTCASES
34  * variable or the ROOT_DIR in build info.
35  */
36 @OptionClass(alias = "apk-installer")
37 public class SuiteApkInstaller extends TestAppInstallSetup {
38 
39     private static final String ANDROID_TARGET_TESTCASES = "ANDROID_TARGET_OUT_TESTCASES";
40     private static final String ROOT_DIR = "ROOT_DIR";
41 
42     @VisibleForTesting
getEnvVariable()43     String getEnvVariable() {
44         return System.getenv(ANDROID_TARGET_TESTCASES);
45     }
46 
47     /**
48      * Try to find a path for the base root tests directory.
49      *
50      * @param buildInfo the {@link IBuildInfo} describing the build.
51      * @return a {@link File} pointing to the directory of the root tests dir.
52      * @throws FileNotFoundException if no root dir is defined.
53      */
54     @VisibleForTesting
getRootDir(IBuildInfo buildInfo)55     protected File getRootDir(IBuildInfo buildInfo) throws FileNotFoundException {
56         if (buildInfo.getBuildAttributes().get(ROOT_DIR) != null) {
57             return new File(buildInfo.getBuildAttributes().get(ROOT_DIR));
58         }
59         throw new FileNotFoundException(String.format("%s was found.", ROOT_DIR));
60     }
61 
62     /** Check within $ANDROID_TARGET_OUT_TESTCASES if the apk exists. */
getApkFromVariableTestsDir(String apkFileName)63     private File getApkFromVariableTestsDir(String apkFileName) {
64         String testcasesPath = getEnvVariable();
65         if (testcasesPath != null) {
66             File testCasesFile = new File(testcasesPath);
67             // Only return the variable directory if it exists
68             if (testCasesFile.isDirectory()) {
69                 return FileUtil.findFile(testCasesFile, apkFileName);
70             }
71         }
72         return null;
73     }
74 
75     /** Check within {@link IDeviceBuildInfo#getTestsDir()} if the apk exists. */
getApkFromBuildTestsDir(IBuildInfo buildInfo, String apkFileName)76     private File getApkFromBuildTestsDir(IBuildInfo buildInfo, String apkFileName) {
77         if (buildInfo instanceof IDeviceBuildInfo) {
78             IDeviceBuildInfo deviceBuildInfo = (IDeviceBuildInfo) buildInfo;
79             File testDir = deviceBuildInfo.getTestsDir();
80             if (testDir != null && testDir.isDirectory()) {
81                 return FileUtil.findFile(testDir, apkFileName);
82             }
83         }
84         return null;
85     }
86 
87     /** Check within the shared resouces directory if the apk can be found. */
getApkFromBuildSharedDir(IBuildInfo buildInfo, String apkFileName)88     private File getApkFromBuildSharedDir(IBuildInfo buildInfo, String apkFileName) {
89         File sharedDir = buildInfo.getFile(BuildInfoFileKey.SHARED_RESOURCE_DIR);
90         if (sharedDir != null && sharedDir.isDirectory()) {
91             return FileUtil.findFile(sharedDir, apkFileName);
92         }
93         return null;
94     }
95 
96     /** {@inheritDoc} */
97     @Override
getLocalPathForFilename( IBuildInfo buildInfo, String apkFileName, ITestDevice device)98     protected File getLocalPathForFilename(
99             IBuildInfo buildInfo, String apkFileName, ITestDevice device) throws TargetSetupError {
100         File apkFile = null;
101         try {
102             // check in ANDROID_TARGET_OUT_TESTCASES first.
103             apkFile = getApkFromVariableTestsDir(apkFileName);
104             if (apkFile != null && apkFile.isFile()) {
105                 return apkFile;
106             }
107 
108             // check from IDeviceBuildInfo.
109             apkFile = getApkFromBuildTestsDir(buildInfo, apkFileName);
110             if (apkFile != null && apkFile.isFile()) {
111                 return apkFile;
112             }
113 
114             // Check build info directly
115             apkFile = buildInfo.getFile(apkFileName);
116             if (apkFile != null && apkFile.isFile()) {
117                 return apkFile;
118             }
119 
120             // Check shared resources
121             apkFile = getApkFromBuildSharedDir(buildInfo, apkFileName);
122             if (apkFile != null && apkFile.isFile()) {
123                 return apkFile;
124             }
125 
126             // check ROOT_DIR
127             apkFile = FileUtil.findFile(getRootDir(buildInfo), apkFileName);
128             if (apkFile == null || !apkFile.isFile()) {
129                 throw new FileNotFoundException();
130             }
131         } catch (FileNotFoundException e) {
132             throw new TargetSetupError(
133                     String.format("%s not found", apkFileName), e, device.getDeviceDescriptor());
134         }
135         return apkFile;
136     }
137 }
138