1 /*
2  * Copyright (C) 2010 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.build;
17 
18 import com.android.tradefed.build.BuildInfoKey.BuildInfoFileKey;
19 import com.android.tradefed.build.proto.BuildInformation;
20 import com.android.tradefed.device.ITestDevice;
21 
22 import java.io.File;
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 
30 /** Holds information about the build under test. */
31 public interface IBuildInfo extends Serializable {
32 
33     /** Some properties that a {@link IBuildInfo} can have to tweak some handling of it. */
34     public enum BuildInfoProperties {
35         DO_NOT_COPY_ON_SHARDING,
36         DO_NOT_LINK_TESTS_DIR,
37         /**
38          * If a copy of the build is requested, do not copy the device image file. Represented by
39          * {@link BuildInfoFileKey#DEVICE_IMAGE} key.
40          */
41         DO_NOT_COPY_IMAGE_FILE,
42     }
43 
44     /** Default value when build ID is unknown. */
45     public static final String UNKNOWN_BUILD_ID = "-1";
46 
47     /** Prefix used in name to indicate the file is set to be delayed download. */
48     public static final String REMOTE_FILE_PREFIX = "remote_file:";
49 
50     /** Remote file is not versioned. */
51     public static final String REMOTE_FILE_VERSION = "";
52 
53     /**
54      * Returns the unique identifier of build under test. Should never be null. Defaults to {@link
55      * #UNKNOWN_BUILD_ID}.
56      */
getBuildId()57     public String getBuildId();
58 
59     /**
60      * Sets the unique identifier of build under test. Should never be null.
61      */
setBuildId(String buildId)62     public void setBuildId(String buildId);
63 
64     /**
65      * Return a unique name for the tests being run.
66      */
getTestTag()67     public String getTestTag();
68 
69     /**
70      * Sets the unique name for the tests being run.
71      */
setTestTag(String testTag)72     public void setTestTag(String testTag);
73 
74     /**
75      * Return complete name for the build being tested.
76      * <p/>
77      * A common implementation is to construct the build target name from a combination of
78      * the build flavor and branch name. [ie (branch name)-(build flavor)]
79      */
getBuildTargetName()80     public String getBuildTargetName();
81 
82     /**
83      * Optional method to return the type of build being tested.
84      * <p/>
85      * A common implementation for Android platform builds is to return
86      * (build product)-(build os)-(build variant).
87      * ie generic-linux-userdebug
88      *
89      * @return the build flavor or <code>null</code> if unset/not applicable
90      */
getBuildFlavor()91     public String getBuildFlavor();
92 
93     /**
94      * @return the {@link ITestDevice} serial that this build was executed on. Returns <code>null
95      * </code> if no device is associated with this build.
96      */
getDeviceSerial()97     public String getDeviceSerial();
98 
99     /**
100      * Set the build flavor.
101      *
102      * @param buildFlavor
103      */
setBuildFlavor(String buildFlavor)104     public void setBuildFlavor(String buildFlavor);
105 
106     /**
107      * Optional method to return the source control branch that the build being tested was
108      * produced from.
109      *
110      * @return the build branch or <code>null</code> if unset/not applicable
111      */
getBuildBranch()112     public String getBuildBranch();
113 
114     /**
115      * Set the build branch
116      *
117      * @param branch the branch name
118      */
setBuildBranch(String branch)119     public void setBuildBranch(String branch);
120 
121     /**
122      * Set the {@link ITestDevice} serial associated with this build.
123      *
124      * @param serial the serial number of the {@link ITestDevice} that this build was executed with.
125      */
setDeviceSerial(String serial)126     public void setDeviceSerial(String serial);
127 
128     /**
129      * Get a set of name-value pairs of additional attributes describing the build.
130      *
131      * @return a {@link Map} of build attributes. Will not be <code>null</code>, but may be empty.
132      */
getBuildAttributes()133     public Map<String, String> getBuildAttributes();
134 
135     /**
136      * Add a build attribute. This doesn't allow overriding an existing attributeName.
137      *
138      * @param attributeName the unique attribute name
139      * @param attributeValue the attribute value
140      */
addBuildAttribute(String attributeName, String attributeValue)141     public void addBuildAttribute(String attributeName, String attributeValue);
142 
143     /**
144      * Remove a given attributeName from tracking.
145      *
146      * @param attributeName the attribute to stop tracking
147      */
removeBuildAttribute(String attributeName)148     public default void removeBuildAttribute(String attributeName) {
149         // Empty by default.
150     }
151 
152     /**
153      * Add build attributes
154      *
155      * @param buildAttributes Map of attributes to be added
156      */
addBuildAttributes(Map<String, String> buildAttributes)157     public default void addBuildAttributes(Map<String, String> buildAttributes) {}
158 
159     /**
160      * Set the {@link BuildInfoProperties} for the {@link IBuildInfo} instance. Override any
161      * existing properties set before.
162      *
163      * @param properties The list of properties to add.
164      */
setProperties(BuildInfoProperties... properties)165     public void setProperties(BuildInfoProperties... properties);
166 
167     /** Returns a copy of the properties currently set on the {@link IBuildInfo}. */
getProperties()168     public Set<BuildInfoProperties> getProperties();
169 
170     /**
171      * Helper method to retrieve a file with given a {@link BuildInfoFileKey}.
172      *
173      * @param key the {@link BuildInfoFileKey} that is requested.
174      * @return the image file or <code>null</code> if not found
175      */
getFile(BuildInfoFileKey key)176     public default File getFile(BuildInfoFileKey key) {
177         // Default implementation for projects that don't extend BuildInfo class.
178         return null;
179     }
180 
181     /**
182      * Helper method to retrieve a file with given name.
183      * @param name
184      * @return the image file or <code>null</code> if not found
185      */
getFile(String name)186     public File getFile(String name);
187 
188     /** Returns the set of keys available to query {@link VersionedFile} via {@link #getFile}. */
getVersionedFileKeys()189     public default Set<String> getVersionedFileKeys() {
190         return null;
191     }
192 
193     /**
194      * Helper method to retrieve a {@link VersionedFile} with a given name.
195      *
196      * @param name
197      * @return The versioned file or <code>null</code> if not found
198      */
getVersionedFile(String name)199     public default VersionedFile getVersionedFile(String name) {
200         // Default implementation for projects that don't extend BuildInfo class.
201         return null;
202     }
203 
204     /**
205      * Helper method to retrieve a {@link VersionedFile} with a given {@link BuildInfoFileKey}.
206      *
207      * @param key The {@link BuildInfoFileKey} requested.
208      * @return The versioned file or <code>null</code> if not found
209      */
getVersionedFile(BuildInfoFileKey key)210     public default VersionedFile getVersionedFile(BuildInfoFileKey key) {
211         // Default implementation for projects that don't extend BuildInfo class.
212         return null;
213     }
214 
215     /**
216      * Helper method to retrieve a list of {@link VersionedFile}s associated with a given {@link
217      * BuildInfoFileKey}. If the key allows to store a list.
218      *
219      * @param key The {@link BuildInfoFileKey} requested.
220      * @return The versioned file or <code>null</code> if not found
221      */
getVersionedFiles(BuildInfoFileKey key)222     public default List<VersionedFile> getVersionedFiles(BuildInfoFileKey key) {
223         // Default implementation for projects that don't extend BuildInfo class.
224         return null;
225     }
226 
227     /**
228      * Returns all {@link VersionedFile}s stored in this {@link BuildInfo}.
229      */
getFiles()230     public Collection<VersionedFile> getFiles();
231 
232     /**
233      * Helper method to retrieve a file version with given name.
234      * @param name
235      * @return the image version or <code>null</code> if not found
236      */
getVersion(String name)237     public String getVersion(String name);
238 
239     /**
240      * Helper method to retrieve a file version with given a {@link BuildInfoFileKey}.
241      *
242      * @param key The {@link BuildInfoFileKey} requested.
243      * @return the image version or <code>null</code> if not found
244      */
getVersion(BuildInfoFileKey key)245     public default String getVersion(BuildInfoFileKey key) {
246         // Default implementation for project that don't extends BuildInfo class.
247         return null;
248     }
249 
250     /**
251      * Stores an file with given name in this build info.
252      *
253      * @param name the unique name of the file
254      * @param file the local {@link File}
255      * @param version the file version
256      */
setFile(String name, File file, String version)257     public void setFile(String name, File file, String version);
258 
259     /**
260      * Stores an file given a {@link BuildInfoFileKey} in this build info.
261      *
262      * @param key the unique name of the file based on {@link BuildInfoFileKey}.
263      * @param file the local {@link File}
264      * @param version the file version
265      */
setFile(BuildInfoFileKey key, File file, String version)266     public default void setFile(BuildInfoFileKey key, File file, String version) {
267         // Default implementation for projects that don't extend BuildInfo class.
268     }
269 
270     /**
271      * Gets a copy of the set of local app apk file(s) and their versions. The returned order
272      * matches the order in which the apks were added to the {@code IAppBuildInfo}.
273      */
getAppPackageFiles()274     public default List<VersionedFile> getAppPackageFiles() {
275         return new ArrayList<>();
276     }
277 
278     /**
279      * Adds the local apk file and its associated version. Note that apks will be returned from
280      * {@link #getAppPackageFiles()} in the order in which they were added by this method.
281      */
addAppPackageFile(File appPackageFile, String version)282     public default void addAppPackageFile(File appPackageFile, String version) {
283         // Default implementation for projects that don't extend BuildInfo class.
284     }
285 
286     /**
287      * Clean up any temporary build files
288      */
cleanUp()289     public void cleanUp();
290 
291     /** Version of {@link #cleanUp()} where some files are not deleted. */
cleanUp(List<File> doNotDelete)292     public void cleanUp(List<File> doNotDelete);
293 
294     /**
295      * Clones the {@link IBuildInfo} object.
296      */
clone()297     public IBuildInfo clone();
298 
299     /** Serialize a the BuildInfo instance into a protobuf. */
toProto()300     public default BuildInformation.BuildInfo toProto() {
301         // Default implementation for project that don't extends BuildInfo class.
302         return null;
303     }
304 
305     /** Get the paths for build artifacts that are delayed download. */
getRemoteFiles()306     public default Set<File> getRemoteFiles() {
307         return null;
308     }
309 
310     /**
311      * Stage a file that's part of remote files in the build info's root dir.
312      *
313      * <p>TODO(b/138416078): Remove this interface and its caller when modules required by a test
314      * can be properly built output to the test module's directory itself.
315      *
316      * @param fileName Name of the file to be located in remote files.
317      * @param workingDir a {@link File} object of the directory to stage the file.
318      * @return the {@link File} object of the file staged in local workingDir.
319      */
stageRemoteFile(String fileName, File workingDir)320     public default File stageRemoteFile(String fileName, File workingDir) {
321         return null;
322     }
323 
324     /** Sets permission to allow staging remote files. */
allowStagingRemoteFile(boolean stageRemoteFile)325     public default void allowStagingRemoteFile(boolean stageRemoteFile) {}
326 }
327