1 /*
2  * Copyright (C) 2018 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.sandbox;
17 
18 import com.android.tradefed.config.Option;
19 import com.android.tradefed.config.OptionClass;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.HashSet;
24 import java.util.LinkedHashMap;
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 
30 /** Class that can receive and provide options to a {@link ISandbox}. */
31 @OptionClass(alias = "sandbox", global_namespace = true)
32 public final class SandboxOptions {
33 
34     public static final String TF_LOCATION = "tf-location";
35     public static final String SANDBOX_BUILD_ID = "sandbox-build-id";
36     public static final String USE_PROTO_REPORTER = "use-proto-reporter";
37     public static final String CHILD_GLOBAL_CONFIG = "sub-global-config";
38     public static final String PARENT_PREPARER_CONFIG = "parent-preparer-config";
39     public static final String WAIT_FOR_EVENTS_TIMEOUT = "wait-for-events";
40     public static final String ENABLE_DEBUG_THREAD = "sandbox-debug-thread";
41     private static final String SANDBOX_JAVA_OPTIONS = "sandbox-java-options";
42     private static final String SANDBOX_ENV_VARIABLE_OPTIONS = "sandbox-env-variable";
43     private static final String SANDBOX_TESTS_ZIPS_OPTIONS = "sandbox-tests-zips";
44     private static final String ENABLE_DEFAULT_TESTS_ZIPS_OPTIONS = "sandbox-default-zips";
45     private static final String DUMP_TEST_TEMPLATE = "dump-test-template";
46     private static final String START_AVD_IN_PARENT = "avd-in-parent";
47     private static final String PARALLEL_SANDBOX_SETUP = "parallel-sandbox-setup";
48     private static final String UPDATED_FLAG_ORDER = "update-flag-orders";
49     private static final String SANDBOX_USE_TEST_DISCOVERY = "sandbox-use-test-discovery";
50     private static final String SANDBOX_FORCE_PARTIAL_DOWNLOAD_FILE_REGEX =
51             "sandbox-force-partial-download-file-regex";
52     private static final String SANDBOX_PARTIAL_DOWNLOAD_CACHE =
53             "sandbox-use-partial-download-cache";
54     private static final String SANDBOX_SPLIT_DISCOVERY = "sandbox-split-discovery";
55     private static final String SANDBOX_PARALLEL_DOWNLOAD = "sandbox-parallel-download";
56     private static final String DELAY_DOWNLOAD_AFTER_SHARDING = "delay-download-after-sharding";
57 
58     @Option(
59         name = TF_LOCATION,
60         description = "The path to the Tradefed binary of the version to use for the sandbox."
61     )
62     private File mTfVersion = null;
63 
64     @Option(
65         name = SANDBOX_BUILD_ID,
66         description =
67                 "Provide the build-id to force the sandbox version of Tradefed to be."
68                         + "Mutually exclusive with the tf-location option."
69     )
70     private String mBuildId = null;
71 
72     @Option(
73         name = USE_PROTO_REPORTER,
74         description = "Whether or not to use protobuf format reporting between processes."
75     )
76     private boolean mUseProtoReporter = true;
77 
78     @Option(
79             name = CHILD_GLOBAL_CONFIG,
80             description =
81                     "Force a particular configuration to be used as global configuration for the"
82                             + " sandbox.")
83     private String mChildGlobalConfig = null;
84 
85     @Option(
86         name = PARENT_PREPARER_CONFIG,
87         description =
88                 "A configuration which target_preparers will be run in the parent of the sandbox."
89     )
90     private String mParentPreparerConfig = null;
91 
92     @Option(
93         name = WAIT_FOR_EVENTS_TIMEOUT,
94         isTimeVal = true,
95         description =
96                 "The time we should wait for all events to complete after the "
97                         + "sandbox is done running."
98     )
99     private long mWaitForEventsTimeoutMs = 30000L;
100 
101     @Option(
102             name = ENABLE_DEBUG_THREAD,
103             description = "Whether or not to enable a debug thread for sandbox.")
104     private boolean mEnableDebugThread = false;
105 
106     @Option(
107             name = SANDBOX_JAVA_OPTIONS,
108             description = "Pass options for the java process of the sandbox.")
109     private List<String> mSandboxJavaOptions = new ArrayList<>();
110 
111     @Option(
112             name = SANDBOX_ENV_VARIABLE_OPTIONS,
113             description = "Pass environment variable and its value to the sandbox process.")
114     private Map<String, String> mSandboxEnvVariable = new LinkedHashMap<>();
115 
116     @Option(
117             name = SANDBOX_TESTS_ZIPS_OPTIONS,
118             description = "The set of tests zips to stage during sandboxing.")
119     private Set<String> mSandboxTestsZips = new LinkedHashSet<>();
120 
121     @Option(
122             name = ENABLE_DEFAULT_TESTS_ZIPS_OPTIONS,
123             description =
124                     "Whether or not to download the default tests zip when no sandbox-tests-zips "
125                             + "has been specified")
126     private boolean mEnableDefaultZips = true;
127 
128     @Option(
129             name = DUMP_TEST_TEMPLATE,
130             description =
131                     "Whether or not to use the test template from sandbox version in fallback.")
132     private boolean mDumpTestTemplate = false;
133 
134     @Option(
135             name = START_AVD_IN_PARENT,
136             description =
137                     "Whether or not to start the avd device in the parent sandbox")
138     private boolean mStartAvdInParent = true;
139 
140     @Option(
141             name = PARALLEL_SANDBOX_SETUP,
142             description = "Execute the sandbox setup step in parallel")
143     private boolean mParallelSandboxSetup = true;
144 
145     /** Deprecated */
146     @Option(name = UPDATED_FLAG_ORDER, description = "Feature flag to test safely new flags order")
147     private boolean mNewFlagOrder = true;
148 
149     @Option(
150             name = SANDBOX_USE_TEST_DISCOVERY,
151             description =
152                     "Feature flag to use observatory to discovery test modules for staging jars")
153     private boolean mUseTestDiscovery = false;
154 
155     @Option(
156             name = SANDBOX_FORCE_PARTIAL_DOWNLOAD_FILE_REGEX,
157             description =
158                     "The set of regex to force sandbox partial download always stage the files"
159                             + " that match any of the regex in the list")
160     private Set<String> mSandboxForcePartialDownloadFileRegexList = new HashSet<>();
161 
162     @Option(
163             name = SANDBOX_PARTIAL_DOWNLOAD_CACHE,
164             description = "Feature flag to use partial download cache")
165     private boolean mUsePartialDownloadCache = true;
166 
167     @Option(
168             name = SANDBOX_SPLIT_DISCOVERY,
169             description = "Enable setup where discovery is done independently.")
170     private boolean mUseSandboxSplitDiscovery = true;
171 
172     @Option(
173             name = SANDBOX_PARALLEL_DOWNLOAD,
174             description = "Enable parallel download during sandbox setup.")
175     private boolean mUseSandboxParallelDownload = true;
176 
177     @Option(
178             name = DELAY_DOWNLOAD_AFTER_SHARDING,
179             description =
180                     "Feature to delegate most of the heavy download after sharding to reduce"
181                             + " downloaded size.")
182     private boolean mDelayDownloadAfterSharding = true;
183 
184     /**
185      * Returns the provided directories containing the Trade Federation version to use for
186      * sandboxing the run.
187      */
getSandboxTfDirectory()188     public File getSandboxTfDirectory() {
189         return mTfVersion;
190     }
191 
192     /** Returns the build-id forced for the sandbox to be used during the run. */
getSandboxBuildId()193     public String getSandboxBuildId() {
194         return mBuildId;
195     }
196 
197     /** Returns whether or not protobuf reporting should be used. */
shouldUseProtoReporter()198     public boolean shouldUseProtoReporter() {
199         return mUseProtoReporter;
200     }
201 
202     /**
203      * Returns the configuration to be used for the child sandbox. Or null if the parent one should
204      * be used.
205      */
getChildGlobalConfig()206     public String getChildGlobalConfig() {
207         return mChildGlobalConfig;
208     }
209 
210     /** Returns the configuration which preparer should run in the parent process of the sandbox. */
getParentPreparerConfig()211     public String getParentPreparerConfig() {
212         return mParentPreparerConfig;
213     }
214 
215     /**
216      * Returns the time we should wait for events to be processed after the sandbox is done running.
217      */
getWaitForEventsTimeout()218     public long getWaitForEventsTimeout() {
219         return mWaitForEventsTimeoutMs;
220     }
221 
222     /** Enable a debug thread. */
shouldEnableDebugThread()223     public boolean shouldEnableDebugThread() {
224         return mEnableDebugThread;
225     }
226 
227     /** The list of options to pass the java process of the sandbox. */
getJavaOptions()228     public List<String> getJavaOptions() {
229         return mSandboxJavaOptions;
230     }
231 
232     /** The map of environment variable to pass to the java process of the sandbox. */
getEnvVariables()233     public Map<String, String> getEnvVariables() {
234         return mSandboxEnvVariable;
235     }
236 
237     /** Returns the set of tests zips to stage for the sandbox. */
getTestsZips()238     public Set<String> getTestsZips() {
239         return mSandboxTestsZips;
240     }
241 
242     /** Returns whether or not to download the default tests zips. */
downloadDefaultZips()243     public boolean downloadDefaultZips() {
244         return mEnableDefaultZips;
245     }
246 
247     /** Returns whether or not to dump the test template in fallback mode. */
dumpTestTemplate()248     public boolean dumpTestTemplate() {
249         return mDumpTestTemplate;
250     }
251 
252     /**
253      * Returns whether or not to start avd devices in parent sandbox or let it be in child.
254      */
startAvdInParent()255     public boolean startAvdInParent() {
256         return mStartAvdInParent;
257     }
258 
259     /** Returns whether or not to execute the sandbox setup in parallel. */
shouldParallelSetup()260     public boolean shouldParallelSetup() {
261         return mParallelSandboxSetup;
262     }
263 
264     /** Returns whether or not to use tradefed observatory to optimize jar staging */
shouldUseTestDiscovery()265     public boolean shouldUseTestDiscovery() {
266         return mUseTestDiscovery;
267     }
268 
269     /** Returns whether or not to use partial download caching */
shouldUsePartialDownload()270     public boolean shouldUsePartialDownload() {
271         return mUsePartialDownloadCache;
272     }
273 
274     /**
275      * Returns a set of regex, sandbox partial download will always download those files that match
276      * the regex
277      */
getForcePartialDownloadFileRegexList()278     public Set<String> getForcePartialDownloadFileRegexList() {
279         return mSandboxForcePartialDownloadFileRegexList;
280     }
281 
282     /** Returns whether to use setup with independent discovery. */
shouldUseSplitDiscovery()283     public boolean shouldUseSplitDiscovery() {
284         return mUseSandboxSplitDiscovery;
285     }
286 
287     /** Returns whether or not to use parallel download during setup. */
shouldUseParallelDownload()288     public boolean shouldUseParallelDownload() {
289         return mUseSandboxParallelDownload;
290     }
291 
292     /** Returns whether or not to delay download after the sharding. */
delayDownloadAfterSharding()293     public boolean delayDownloadAfterSharding() {
294         return mDelayDownloadAfterSharding;
295     }
296 }
297