Skip to content

Commit 19a77f3

Browse files
DeviceInfracopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 746041484
1 parent 2b2c194 commit 19a77f3

File tree

13 files changed

+393
-6
lines changed

13 files changed

+393
-6
lines changed

src/java/com/google/devtools/mobileharness/infra/ats/common/jobcreator/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ java_library(
103103
"//src/java/com/google/devtools/mobileharness/infra/ats/common:session_request_info",
104104
"//src/java/com/google/devtools/mobileharness/infra/ats/common:xts_property_name",
105105
"//src/java/com/google/devtools/mobileharness/infra/ats/common/plan:test_plan_parser",
106+
"//src/java/com/google/devtools/mobileharness/infra/ats/server/util:ats_server_session_util",
106107
"//src/java/com/google/devtools/mobileharness/platform/android/xts/suite:suite_test_filter",
107108
"//src/java/com/google/devtools/mobileharness/platform/android/xts/suite/retry:previous_result_loader",
108109
"//src/java/com/google/devtools/mobileharness/platform/android/xts/suite/retry:retry_args",

src/java/com/google/devtools/mobileharness/infra/ats/common/jobcreator/ServerJobCreator.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.devtools.mobileharness.infra.ats.common.SessionRequestInfo;
2929
import com.google.devtools.mobileharness.infra.ats.common.XtsPropertyName;
3030
import com.google.devtools.mobileharness.infra.ats.common.plan.TestPlanParser;
31+
import com.google.devtools.mobileharness.infra.ats.server.util.AtsServerSessionUtil;
3132
import com.google.devtools.mobileharness.platform.android.xts.suite.SuiteTestFilter;
3233
import com.google.devtools.mobileharness.platform.android.xts.suite.retry.PreviousResultLoader;
3334
import com.google.devtools.mobileharness.platform.android.xts.suite.retry.RetryArgs;
@@ -47,6 +48,7 @@
4748
public class ServerJobCreator extends XtsJobCreator {
4849

4950
private final PreviousResultLoader previousResultLoader;
51+
private final AtsServerSessionUtil atsServerSessionUtil;
5052

5153
@Inject
5254
ServerJobCreator(
@@ -55,7 +57,8 @@ public class ServerJobCreator extends XtsJobCreator {
5557
TestPlanParser testPlanParser,
5658
PreviousResultLoader previousResultLoader,
5759
RetryGenerator retryGenerator,
58-
ModuleShardingArgsGenerator moduleShardingArgsGenerator) {
60+
ModuleShardingArgsGenerator moduleShardingArgsGenerator,
61+
AtsServerSessionUtil atsServerSessionUtil) {
5962
super(
6063
sessionRequestHandlerUtil,
6164
localFileUtil,
@@ -64,12 +67,18 @@ public class ServerJobCreator extends XtsJobCreator {
6467
moduleShardingArgsGenerator);
6568

6669
this.previousResultLoader = previousResultLoader;
70+
this.atsServerSessionUtil = atsServerSessionUtil;
6771
}
6872

6973
@Override
7074
protected void injectEnvSpecificProperties(
71-
SessionRequestInfo sessionRequestInfo, Map<String, String> driverParams) {
72-
driverParams.put("android_xts_zip", sessionRequestInfo.androidXtsZip().get());
75+
SessionRequestInfo sessionRequestInfo, Map<String, String> driverParams)
76+
throws InterruptedException {
77+
if (atsServerSessionUtil.isLocalMode()) {
78+
driverParams.put("xts_root_dir", sessionRequestInfo.xtsRootDir());
79+
} else {
80+
driverParams.put("android_xts_zip", sessionRequestInfo.androidXtsZip().get());
81+
}
7382
if (sessionRequestInfo.remoteRunnerFilePathPrefix().isPresent()
7483
&& driverParams.containsKey("subplan_xml")) {
7584
driverParams.put(

src/java/com/google/devtools/mobileharness/infra/ats/common/jobcreator/XtsJobCreator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ protected abstract Optional<Path> getPrevSessionTestReportProperties(
630630
SessionRequestInfo sessionRequestInfo) throws MobileHarnessException;
631631

632632
protected abstract void injectEnvSpecificProperties(
633-
SessionRequestInfo sessionRequestInfo, Map<String, String> driverParams);
633+
SessionRequestInfo sessionRequestInfo, Map<String, String> driverParams)
634+
throws InterruptedException;
634635

635636
protected abstract Path prepareRunRetryTfSubPlanXmlFile(
636637
SessionRequestInfo sessionRequestInfo, SubPlan subPlan) throws MobileHarnessException;

src/java/com/google/devtools/mobileharness/infra/ats/server/sessionplugin/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ java_library(
8080
"//src/java/com/google/devtools/mobileharness/infra/ats/common:xts_type_loader",
8181
"//src/java/com/google/devtools/mobileharness/infra/ats/common/jobcreator:xts_job_creator",
8282
"//src/java/com/google/devtools/mobileharness/infra/ats/console/command/parser:command_line_parser",
83+
"//src/java/com/google/devtools/mobileharness/infra/ats/server/util:ats_server_session_util",
8384
"//src/java/com/google/devtools/mobileharness/infra/client/longrunningservice/constant:session_properties",
8485
"//src/java/com/google/devtools/mobileharness/infra/client/longrunningservice/model:session_info",
8586
"//src/java/com/google/devtools/mobileharness/infra/lab/common/dir",

src/java/com/google/devtools/mobileharness/infra/ats/server/sessionplugin/NewMultiCommandRequestHandler.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.google.devtools.mobileharness.infra.ats.server.proto.ServiceProto.RequestDetail.RequestState;
6161
import com.google.devtools.mobileharness.infra.ats.server.proto.ServiceProto.TestContext;
6262
import com.google.devtools.mobileharness.infra.ats.server.proto.ServiceProto.TestResource;
63+
import com.google.devtools.mobileharness.infra.ats.server.util.AtsServerSessionUtil;
6364
import com.google.devtools.mobileharness.infra.client.longrunningservice.constant.SessionProperties;
6465
import com.google.devtools.mobileharness.infra.client.longrunningservice.model.SessionInfo;
6566
import com.google.devtools.mobileharness.infra.lab.common.dir.DirUtil;
@@ -114,6 +115,8 @@ final class NewMultiCommandRequestHandler {
114115
/** Timeout setting for slow commands. */
115116
private static final Duration SLOW_CMD_TIMEOUT = Duration.ofMinutes(10);
116117

118+
private static final Duration UNZIP_TIMEOUT = Duration.ofHours(1);
119+
117120
private static final DateTimeFormatter TIMESTAMP_DIR_NAME_FORMATTER =
118121
DateTimeFormatter.ofPattern("uuuu.MM.dd_HH.mm.ss.SSS").withZone(ZoneId.systemDefault());
119122
private static final String OUTPUT_MANIFEST_FILE_NAME = "FILES";
@@ -141,6 +144,7 @@ final class NewMultiCommandRequestHandler {
141144
private final XtsTypeLoader xtsTypeLoader;
142145
private final XtsJobCreator xtsJobCreator;
143146
private final Sleeper sleeper;
147+
private final AtsServerSessionUtil atsServerSessionUtil;
144148

145149
// Cache for storing validated sessionRequestInfo for each command generated by addTradefedJobs(),
146150
// and mainly used by addNonTradefedJobs() to reduce duplicate sessionRequestInfo generation that
@@ -160,7 +164,8 @@ final class NewMultiCommandRequestHandler {
160164
Clock clock,
161165
XtsTypeLoader xtsTypeLoader,
162166
XtsJobCreator xtsJobCreator,
163-
Sleeper sleeper) {
167+
Sleeper sleeper,
168+
AtsServerSessionUtil atsServerSessionUtil) {
164169
this.sessionRequestHandlerUtil = sessionRequestHandlerUtil;
165170
this.sessionResultHandlerUtil = sessionResultHandlerUtil;
166171
this.localFileUtil = localFileUtil;
@@ -170,6 +175,7 @@ final class NewMultiCommandRequestHandler {
170175
this.xtsTypeLoader = xtsTypeLoader;
171176
this.xtsJobCreator = xtsJobCreator;
172177
this.sleeper = sleeper;
178+
this.atsServerSessionUtil = atsServerSessionUtil;
173179
}
174180

175181
CreateJobsResult createTradefedJobs(NewMultiCommandRequest request, SessionInfo sessionInfo)
@@ -980,6 +986,9 @@ private boolean hasCommandFailed(CommandDetail commandDetail) {
980986
@CanIgnoreReturnValue
981987
private String mountOrUnzipXtsZip(String zipFilePath, String targetDirPath)
982988
throws MobileHarnessException, InterruptedException {
989+
if (atsServerSessionUtil.isLocalMode()) {
990+
return localFileUtil.unzipFile(zipFilePath, targetDirPath, UNZIP_TIMEOUT);
991+
}
983992
Command command =
984993
Command.of("fuse-zip", "-r", zipFilePath, targetDirPath).timeout(SLOW_CMD_TIMEOUT);
985994
try {
@@ -996,13 +1005,17 @@ private String mountOrUnzipXtsZip(String zipFilePath, String targetDirPath)
9961005
logger.atWarning().withCause(e).log(
9971006
"Failed to mount XTS zip file %s to %s. Trying to unzip it instead.",
9981007
zipFilePath, targetDirPath);
999-
return localFileUtil.unzipFile(zipFilePath, targetDirPath, SLOW_CMD_TIMEOUT);
1008+
return localFileUtil.unzipFile(zipFilePath, targetDirPath, UNZIP_TIMEOUT);
10001009
}
10011010
}
10021011
}
10031012

10041013
private void unmountOrRemoveZipDir(String targetDirPath)
10051014
throws MobileHarnessException, InterruptedException {
1015+
if (atsServerSessionUtil.isLocalMode()) {
1016+
localFileUtil.removeFileOrDir(targetDirPath);
1017+
return;
1018+
}
10061019
Command command = Command.of("fusermount", "-u", targetDirPath).timeout(SLOW_CMD_TIMEOUT);
10071020
try {
10081021
// Add a 5 seconds delay before unmounting the zip file to avoid race condition and unmount
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
* https://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+
17+
package com.google.devtools.mobileharness.infra.ats.server.util;
18+
19+
import com.google.common.base.Ascii;
20+
import com.google.common.flogger.FluentLogger;
21+
import com.google.devtools.mobileharness.api.model.error.MobileHarnessException;
22+
import com.google.devtools.mobileharness.infra.client.api.controller.device.DeviceQuerier;
23+
import com.google.devtools.mobileharness.shared.util.network.NetworkUtil;
24+
import com.google.wireless.qa.mobileharness.shared.constant.Dimension;
25+
import com.google.wireless.qa.mobileharness.shared.proto.query.DeviceQuery;
26+
import com.google.wireless.qa.mobileharness.shared.proto.query.DeviceQuery.DeviceInfo;
27+
import com.google.wireless.qa.mobileharness.shared.proto.query.DeviceQuery.DeviceQueryFilter;
28+
import com.google.wireless.qa.mobileharness.shared.proto.query.DeviceQuery.DeviceQueryResult;
29+
import javax.inject.Inject;
30+
31+
/** Utility class for Ats Server Session. */
32+
public class AtsServerSessionUtil {
33+
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
34+
private final DeviceQuerier deviceQuerier;
35+
private final NetworkUtil networkUtil;
36+
37+
@Inject
38+
AtsServerSessionUtil(DeviceQuerier deviceQuerier, NetworkUtil networkUtil) {
39+
this.deviceQuerier = deviceQuerier;
40+
this.networkUtil = networkUtil;
41+
}
42+
43+
/**
44+
* Checks if the current OLCS instance is running on the same host as the lab.
45+
*
46+
* @return true if the current OLCS instance is running on the same host as the lab.
47+
*/
48+
public boolean isLocalMode() throws InterruptedException {
49+
DeviceQueryResult queryResult;
50+
try {
51+
queryResult = deviceQuerier.queryDevice(DeviceQueryFilter.getDefaultInstance());
52+
} catch (MobileHarnessException e) {
53+
logger.atWarning().withCause(e).log("Failed to query device");
54+
return false;
55+
}
56+
String olcsHostName = "";
57+
try {
58+
olcsHostName = networkUtil.getLocalHostName();
59+
} catch (MobileHarnessException ignored) {
60+
return false;
61+
}
62+
if (olcsHostName.isEmpty()) {
63+
return false;
64+
}
65+
for (DeviceInfo deviceInfo : queryResult.getDeviceInfoList()) {
66+
String labHostName =
67+
deviceInfo.getDimensionList().stream()
68+
.filter(
69+
dimension ->
70+
dimension
71+
.getName()
72+
.equals(Ascii.toLowerCase(Dimension.Name.HOST_NAME.name())))
73+
.findFirst()
74+
.map(DeviceQuery.Dimension::getValue)
75+
.orElse("");
76+
if (!labHostName.equals(olcsHostName)) {
77+
return false;
78+
}
79+
}
80+
return true;
81+
}
82+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
load("//third_party/bazel_rules/rules_java/java:java_library.bzl", "java_library")
17+
18+
package(
19+
default_applicable_licenses = ["//:license"],
20+
default_visibility = [
21+
"//src/java/com/google/devtools/mobileharness/infra/ats:__subpackages__",
22+
"//src/java/com/google/devtools/mobileharness/platform/android/xts/suite/retry:__pkg__",
23+
"//src/javatests/com/google/devtools/mobileharness/infra/ats:__subpackages__",
24+
],
25+
)
26+
27+
java_library(
28+
name = "ats_server_session_util",
29+
srcs = ["AtsServerSessionUtil.java"],
30+
deps = [
31+
"//src/devtools/mobileharness/api/query/proto:device_query_java_proto",
32+
"//src/java/com/google/devtools/mobileharness/api/model/error",
33+
"//src/java/com/google/devtools/mobileharness/infra/client/api/controller/device:querier",
34+
"//src/java/com/google/devtools/mobileharness/shared/util/logging:google_logger",
35+
"//src/java/com/google/devtools/mobileharness/shared/util/network",
36+
"//src/java/com/google/wireless/qa/mobileharness/shared/constant:dimension",
37+
"@maven//:com_google_guava_guava",
38+
"@maven//:javax_inject_jsr330_api",
39+
],
40+
)

src/javatests/com/google/devtools/mobileharness/infra/ats/common/jobcreator/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ java_library(
3737
"//src/java/com/google/devtools/mobileharness/infra/ats/common/jobcreator:module_sharding_args_generator",
3838
"//src/java/com/google/devtools/mobileharness/infra/ats/common/jobcreator:server_job_creator",
3939
"//src/java/com/google/devtools/mobileharness/infra/ats/common/plan:test_plan_parser",
40+
"//src/java/com/google/devtools/mobileharness/infra/ats/server/util:ats_server_session_util",
4041
"//src/java/com/google/devtools/mobileharness/platform/android/xts/suite:suite_test_filter",
4142
"//src/java/com/google/devtools/mobileharness/platform/android/xts/suite/retry:previous_result_loader",
4243
"//src/java/com/google/devtools/mobileharness/platform/android/xts/suite/retry:retry_args",

src/javatests/com/google/devtools/mobileharness/infra/ats/common/jobcreator/ServerJobCreatorTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.google.devtools.mobileharness.infra.ats.common.XtsPropertyName.Job;
3838
import com.google.devtools.mobileharness.infra.ats.common.plan.TestPlanParser;
3939
import com.google.devtools.mobileharness.infra.ats.common.proto.XtsCommonProto.ShardingMode;
40+
import com.google.devtools.mobileharness.infra.ats.server.util.AtsServerSessionUtil;
4041
import com.google.devtools.mobileharness.platform.android.xts.suite.retry.PreviousResultLoader;
4142
import com.google.devtools.mobileharness.platform.android.xts.suite.retry.RetryArgs;
4243
import com.google.devtools.mobileharness.platform.android.xts.suite.retry.RetryGenerator;
@@ -88,6 +89,7 @@ public final class ServerJobCreatorTest {
8889
@Rule public final SetFlagsOss flags = new SetFlagsOss();
8990

9091
@Bind @Mock private SessionRequestHandlerUtil sessionRequestHandlerUtil;
92+
@Bind @Mock private AtsServerSessionUtil atsServerSessionUtil;
9193
@Bind @Mock private LocalFileUtil localFileUtil;
9294
@Bind @Mock private TestPlanParser testPlanParser;
9395
@Bind @Mock private PreviousResultLoader previousResultLoader;
@@ -148,6 +150,45 @@ public void createXtsTradefedTestJob() throws Exception {
148150
.containsExactly(Job.XTS_TEST_PLAN, "cts");
149151
}
150152

153+
@SuppressWarnings("unchecked")
154+
@Test
155+
public void createXtsTradefedTestJob_localMode() throws Exception {
156+
when(atsServerSessionUtil.isLocalMode()).thenReturn(true);
157+
SessionRequestInfo sessionRequestInfo =
158+
SessionRequestInfo.builder()
159+
.setTestPlan("cts")
160+
.setCommandLineArgs("cts")
161+
.setXtsType("cts")
162+
.setXtsRootDir(XTS_ROOT_DIR_PATH)
163+
.setAndroidXtsZip(ANDROID_XTS_ZIP_PATH)
164+
.setModuleNames(ImmutableList.of("mock_module"))
165+
.build();
166+
ArgumentCaptor<Map<String, String>> driverParamsCaptor = ArgumentCaptor.forClass(Map.class);
167+
168+
when(sessionRequestHandlerUtil.initializeJobConfig(eq(sessionRequestInfo), any()))
169+
.thenReturn(JobConfig.getDefaultInstance());
170+
171+
ImmutableList<TradefedJobInfo> tradefedJobInfoList =
172+
jobCreator.createXtsTradefedTestJobInfo(
173+
sessionRequestInfo, ImmutableList.of("mock_module"));
174+
175+
assertThat(tradefedJobInfoList).hasSize(1);
176+
verify(sessionRequestHandlerUtil)
177+
.initializeJobConfig(eq(sessionRequestInfo), driverParamsCaptor.capture());
178+
assertThat(driverParamsCaptor.getValue())
179+
.containsExactly(
180+
"run_command_args",
181+
"-m mock_module",
182+
"xts_type",
183+
"cts",
184+
"xts_root_dir",
185+
XTS_ROOT_DIR_PATH,
186+
"xts_test_plan",
187+
"cts");
188+
assertThat(tradefedJobInfoList.get(0).extraJobProperties())
189+
.containsExactly(Job.XTS_TEST_PLAN, "cts");
190+
}
191+
151192
@Test
152193
public void createXtsTradefedTestJob_moduleSharding() throws Exception {
153194
SessionRequestInfo sessionRequestInfo =

src/javatests/com/google/devtools/mobileharness/infra/ats/server/sessionplugin/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ java_library(
5353
"//src/java/com/google/devtools/mobileharness/infra/ats/server/sessionplugin:ats_server_session_plugin_lib",
5454
"//src/java/com/google/devtools/mobileharness/infra/ats/server/sessionplugin:new_multi_command_request_handler",
5555
"//src/java/com/google/devtools/mobileharness/infra/ats/server/sessionplugin:tradefed_config_generator",
56+
"//src/java/com/google/devtools/mobileharness/infra/ats/server/util:ats_server_session_util",
5657
"//src/java/com/google/devtools/mobileharness/infra/client/api/controller/device:querier",
5758
"//src/java/com/google/devtools/mobileharness/infra/client/longrunningservice/constant:session_properties",
5859
"//src/java/com/google/devtools/mobileharness/infra/client/longrunningservice/model:session_event",

0 commit comments

Comments
 (0)