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.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 
27 /** Class that can receive and provide options to a {@link ISandbox}. */
28 @OptionClass(alias = "sandbox", global_namespace = true)
29 public final class SandboxOptions {
30 
31     public static final String TF_LOCATION = "tf-location";
32     public static final String SANDBOX_BUILD_ID = "sandbox-build-id";
33     public static final String USE_PROTO_REPORTER = "use-proto-reporter";
34     public static final String CHILD_GLOBAL_CONFIG = "sub-global-config";
35     public static final String PARENT_PREPARER_CONFIG = "parent-preparer-config";
36     public static final String WAIT_FOR_EVENTS_TIMEOUT = "wait-for-events";
37     public static final String ENABLE_DEBUG_THREAD = "sandbox-debug-thread";
38     public static final String EXTRA_BRANCH_TARGET = "extra-branch-target";
39     public static final String EXTRA_BUILD_ID_TARGET = "extra-build-id-target";
40     private static final String SANDBOX_JAVA_OPTIONS = "sandbox-java-options";
41     private static final String SANDBOX_ENV_VARIABLE_OPTIONS = "sandbox-env-variable";
42 
43     @Option(
44         name = TF_LOCATION,
45         description = "The path to the Tradefed binary of the version to use for the sandbox."
46     )
47     private File mTfVersion = null;
48 
49     @Option(
50         name = SANDBOX_BUILD_ID,
51         description =
52                 "Provide the build-id to force the sandbox version of Tradefed to be."
53                         + "Mutually exclusive with the tf-location option."
54     )
55     private String mBuildId = null;
56 
57     @Option(
58         name = USE_PROTO_REPORTER,
59         description = "Whether or not to use protobuf format reporting between processes."
60     )
61     private boolean mUseProtoReporter = true;
62 
63     @Option(
64             name = CHILD_GLOBAL_CONFIG,
65             description =
66                     "Force a particular configuration to be used as global configuration for the"
67                             + " sandbox.")
68     private String mChildGlobalConfig = null;
69 
70     @Option(
71         name = PARENT_PREPARER_CONFIG,
72         description =
73                 "A configuration which target_preparers will be run in the parent of the sandbox."
74     )
75     private String mParentPreparerConfig = null;
76 
77     @Option(
78         name = WAIT_FOR_EVENTS_TIMEOUT,
79         isTimeVal = true,
80         description =
81                 "The time we should wait for all events to complete after the "
82                         + "sandbox is done running."
83     )
84     private long mWaitForEventsTimeoutMs = 30000L;
85 
86     @Option(
87             name = ENABLE_DEBUG_THREAD,
88             description = "Whether or not to enable a debug thread for sandbox.")
89     private boolean mEnableDebugThread = false;
90 
91     @Option(
92             name = EXTRA_BRANCH_TARGET,
93             description =
94                     "Which branch to target to download the sandbox extras. Default will be "
95                             + "current target branch.")
96     private String mExtraBranchTarget = null;
97 
98     @Option(
99             name = EXTRA_BUILD_ID_TARGET,
100             description =
101                     "Which build-id to target to download the sandbox extras. Default will be "
102                             + "current target build-id.")
103     private String mExtraBuildIdTarget = null;
104 
105     @Option(
106             name = SANDBOX_JAVA_OPTIONS,
107             description = "Pass options for the java process of the sandbox.")
108     private List<String> mSandboxJavaOptions = new ArrayList<>();
109 
110     @Option(
111             name = SANDBOX_ENV_VARIABLE_OPTIONS,
112             description = "Pass environment variable and its value to the sandbox process.")
113     private Map<String, String> mSandboxEnvVariable = new LinkedHashMap<>();
114 
115     /**
116      * Returns the provided directories containing the Trade Federation version to use for
117      * sandboxing the run.
118      */
getSandboxTfDirectory()119     public File getSandboxTfDirectory() {
120         return mTfVersion;
121     }
122 
123     /** Returns the build-id forced for the sandbox to be used during the run. */
getSandboxBuildId()124     public String getSandboxBuildId() {
125         return mBuildId;
126     }
127 
128     /** Returns whether or not protobuf reporting should be used. */
shouldUseProtoReporter()129     public boolean shouldUseProtoReporter() {
130         return mUseProtoReporter;
131     }
132 
133     /**
134      * Returns the configuration to be used for the child sandbox. Or null if the parent one should
135      * be used.
136      */
getChildGlobalConfig()137     public String getChildGlobalConfig() {
138         return mChildGlobalConfig;
139     }
140 
141     /** Returns the configuration which preparer should run in the parent process of the sandbox. */
getParentPreparerConfig()142     public String getParentPreparerConfig() {
143         return mParentPreparerConfig;
144     }
145 
146     /**
147      * Returns the time we should wait for events to be processed after the sandbox is done running.
148      */
getWaitForEventsTimeout()149     public long getWaitForEventsTimeout() {
150         return mWaitForEventsTimeoutMs;
151     }
152 
153     /** Enable a debug thread. */
shouldEnableDebugThread()154     public boolean shouldEnableDebugThread() {
155         return mEnableDebugThread;
156     }
157 
158     /**
159      * Returns the branch from which to download the sandbox extras. If null, extras will be
160      * downloaded from the branch under tests.
161      */
getExtraBranchTarget()162     public String getExtraBranchTarget() {
163         return mExtraBranchTarget;
164     }
165 
166     /**
167      * Returns the build-id from which to download the sandbox extras. If null, extras will be
168      * downloaded from the build-id under tests.
169      */
getExtraBuildIdTarget()170     public String getExtraBuildIdTarget() {
171         return mExtraBuildIdTarget;
172     }
173 
174     /** The list of options to pass the java process of the sandbox. */
getJavaOptions()175     public List<String> getJavaOptions() {
176         return mSandboxJavaOptions;
177     }
178 
179     /** The map of environment variable to pass to the java process of the sandbox. */
getEnvVariables()180     public Map<String, String> getEnvVariables() {
181         return mSandboxEnvVariable;
182     }
183 }
184