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
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      * Add build attributes
145      *
146      * @param buildAttributes Map of attributes to be added
147      */
addBuildAttributes(Map<String, String> buildAttributes)148     public default void addBuildAttributes(Map<String, String> buildAttributes) {}
149 
150     /**
151      * Set the {@link BuildInfoProperties} for the {@link IBuildInfo} instance. Override any
152      * existing properties set before.
153      *
154      * @param properties The list of properties to add.
155      */
setProperties(BuildInfoProperties... properties)156     public void setProperties(BuildInfoProperties... properties);
157 
158     /** Returns a copy of the properties currently set on the {@link IBuildInfo}. */
getProperties()159     public Set<BuildInfoProperties> getProperties();
160 
161     /**
162      * Helper method to retrieve a file with given a {@link BuildInfoFileKey}.
163      *
164      * @param key the {@link BuildInfoFileKey} that is requested.
165      * @return the image file or <code>null</code> if not found
166      */
getFile(BuildInfoFileKey key)167     public default File getFile(BuildInfoFileKey key) {
168         // Default implementation for projects that don't extend BuildInfo class.
169         return null;
170     }
171 
172     /** Returns the set of keys available to query {@link VersionedFile} via {@link #getFile}. */
getVersionedFileKeys()173     public default Set<String> getVersionedFileKeys() {
174         return null;
175     }
176 
177     /**
178      * Helper method to retrieve a file with given name.
179      * @param name
180      * @return the image file or <code>null</code> if not found
181      */
getFile(String name)182     public File getFile(String name);
183 
184     /**
185      * Helper method to retrieve a {@link VersionedFile} with a given name.
186      *
187      * @param name
188      * @return The versioned file or <code>null</code> if not found
189      */
getVersionedFile(String name)190     public default VersionedFile getVersionedFile(String name) {
191         // Default implementation for projects that don't extend BuildInfo class.
192         return null;
193     }
194 
195     /**
196      * Helper method to retrieve a {@link VersionedFile} with a given {@link BuildInfoFileKey}.
197      *
198      * @param key The {@link BuildInfoFileKey} requested.
199      * @return The versioned file or <code>null</code> if not found
200      */
getVersionedFile(BuildInfoFileKey key)201     public default VersionedFile getVersionedFile(BuildInfoFileKey key) {
202         // Default implementation for projects that don't extend BuildInfo class.
203         return null;
204     }
205 
206     /**
207      * Helper method to retrieve a list of {@link VersionedFile}s associated with a given {@link
208      * BuildInfoFileKey}. If the key allows to store a list.
209      *
210      * @param key The {@link BuildInfoFileKey} requested.
211      * @return The versioned file or <code>null</code> if not found
212      */
getVersionedFiles(BuildInfoFileKey key)213     public default List<VersionedFile> getVersionedFiles(BuildInfoFileKey key) {
214         // Default implementation for projects that don't extend BuildInfo class.
215         return null;
216     }
217 
218     /**
219      * Returns all {@link VersionedFile}s stored in this {@link BuildInfo}.
220      */
getFiles()221     public Collection<VersionedFile> getFiles();
222 
223     /**
224      * Helper method to retrieve a file version with given name.
225      * @param name
226      * @return the image version or <code>null</code> if not found
227      */
getVersion(String name)228     public String getVersion(String name);
229 
230     /**
231      * Helper method to retrieve a file version with given a {@link BuildInfoFileKey}.
232      *
233      * @param key The {@link BuildInfoFileKey} requested.
234      * @return the image version or <code>null</code> if not found
235      */
getVersion(BuildInfoFileKey key)236     public default String getVersion(BuildInfoFileKey key) {
237         // Default implementation for project that don't extends BuildInfo class.
238         return null;
239     }
240 
241     /**
242      * Stores an file with given name in this build info.
243      *
244      * @param name the unique name of the file
245      * @param file the local {@link File}
246      * @param version the file version
247      */
setFile(String name, File file, String version)248     public void setFile(String name, File file, String version);
249 
250     /**
251      * Stores an file given a {@link BuildInfoFileKey} in this build info.
252      *
253      * @param key the unique name of the file based on {@link BuildInfoFileKey}.
254      * @param file the local {@link File}
255      * @param version the file version
256      */
setFile(BuildInfoFileKey key, File file, String version)257     public default void setFile(BuildInfoFileKey key, File file, String version) {
258         // Default implementation for projects that don't extend BuildInfo class.
259     }
260 
261     /**
262      * Gets a copy of the set of local app apk file(s) and their versions. The returned order
263      * matches the order in which the apks were added to the {@code IAppBuildInfo}.
264      */
getAppPackageFiles()265     public default List<VersionedFile> getAppPackageFiles() {
266         return new ArrayList<>();
267     }
268 
269     /**
270      * Adds the local apk file and its associated version. Note that apks will be returned from
271      * {@link #getAppPackageFiles()} in the order in which they were added by this method.
272      */
addAppPackageFile(File appPackageFile, String version)273     public default void addAppPackageFile(File appPackageFile, String version) {
274         // Default implementation for projects that don't extend BuildInfo class.
275     }
276 
277     /**
278      * Clean up any temporary build files
279      */
cleanUp()280     public void cleanUp();
281 
282     /** Version of {@link #cleanUp()} where some files are not deleted. */
cleanUp(List<File> doNotDelete)283     public void cleanUp(List<File> doNotDelete);
284 
285     /**
286      * Clones the {@link IBuildInfo} object.
287      */
clone()288     public IBuildInfo clone();
289 
290     /** Serialize a the BuildInfo instance into a protobuf. */
toProto()291     public default BuildInformation.BuildInfo toProto() {
292         // Default implementation for project that don't extends BuildInfo class.
293         return null;
294     }
295 
296     /** Get the paths for build artifacts that are delayed download. */
getRemoteFiles()297     public default Set<File> getRemoteFiles() {
298         return null;
299     }
300 
301     /**
302      * Stage a file that's part of remote files in the build info's root dir.
303      *
304      * <p>TODO(b/138416078): Remove this interface and its caller when modules required by a test
305      * can be properly built output to the test module's directory itself.
306      *
307      * @param fileName Name of the file to be located in remote files.
308      * @param workingDir a {@link File} object of the directory to stage the file.
309      * @return the {@link File} object of the file staged in local workingDir.
310      */
stageRemoteFile(String fileName, File workingDir)311     public default File stageRemoteFile(String fileName, File workingDir) {
312         return null;
313     }
314 }
315