Skip to content

Commit 1f7444a

Browse files
baishengzcopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 751274063
1 parent 3d49b3c commit 1f7444a

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

src/java/com/google/wireless/qa/mobileharness/shared/api/validator/util/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
load("@rules_java//java:defs.bzl", "java_library")
1717

18+
package(default_applicable_licenses = ["//:license"])
19+
1820
java_library(
1921
name = "android_decorator_validator_util",
2022
srcs = [
@@ -29,3 +31,20 @@ java_library(
2931
"@maven//:com_google_guava_guava",
3032
],
3133
)
34+
35+
java_library(
36+
name = "mobly_decorator_adapter_job_validator_util",
37+
srcs = ["MoblyDecoratorAdapterJobValidatorUtil.java"],
38+
visibility = [
39+
],
40+
deps = [
41+
"//src/java/com/google/devtools/mobileharness/api/model/error",
42+
"//src/java/com/google/wireless/qa/mobileharness/shared/api/job:job_type_util",
43+
"//src/java/com/google/wireless/qa/mobileharness/shared/model/job",
44+
"//src/java/com/google/wireless/qa/mobileharness/shared/proto:job_java_proto",
45+
"//src/java/com/google/wireless/qa/mobileharness/shared/proto/spec:base_spec_java_proto",
46+
"//src/java/com/google/wireless/qa/mobileharness/shared/proto/spec:mobly_decorator_adapter_spec_java_proto",
47+
"//src/java/com/google/wireless/qa/mobileharness/shared/proto/spec:testbed_decorator_adapter_spec_java_proto",
48+
"@maven//:com_google_guava_guava",
49+
],
50+
)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.wireless.qa.mobileharness.shared.api.validator.util;
18+
19+
import com.google.common.base.Joiner;
20+
import com.google.devtools.mobileharness.api.model.error.ExtErrorId;
21+
import com.google.devtools.mobileharness.api.model.error.MobileHarnessException;
22+
import com.google.wireless.qa.mobileharness.shared.api.job.JobTypeUtil;
23+
import com.google.wireless.qa.mobileharness.shared.model.job.JobInfo;
24+
import com.google.wireless.qa.mobileharness.shared.proto.Job.JobType;
25+
import com.google.wireless.qa.mobileharness.shared.proto.Job.TestStatus;
26+
import com.google.wireless.qa.mobileharness.shared.proto.spec.Google3File;
27+
import com.google.wireless.qa.mobileharness.shared.proto.spec.decorator.DeviceSelector;
28+
import com.google.wireless.qa.mobileharness.shared.proto.spec.decorator.DeviceToJobSpec;
29+
import com.google.wireless.qa.mobileharness.shared.proto.spec.decorator.MoblyDecoratorAdapterSpec;
30+
import com.google.wireless.qa.mobileharness.shared.proto.spec.decorator.SubDeviceJobSpec;
31+
import com.google.wireless.qa.mobileharness.shared.proto.spec.decorator.SubDeviceJobSpec.FileSpec;
32+
import com.google.wireless.qa.mobileharness.shared.proto.spec.decorator.SubDeviceJobSpec.ParamSpec;
33+
34+
/** Utils for validators of decorators that extend from {@code MoblyDecoratorAdapter}. */
35+
public final class MoblyDecoratorAdapterJobValidatorUtil {
36+
private MoblyDecoratorAdapterJobValidatorUtil() {}
37+
38+
/** Validates {@code spec}. Throws exceptions if it contains any errors. */
39+
public static void validateSpec(MoblyDecoratorAdapterSpec spec) throws MobileHarnessException {
40+
if (spec.getDeviceToJobSpecCount() == 0) {
41+
throw new MobileHarnessException(
42+
ExtErrorId.MOBLY_DECORATOR_ADAPTER_DECORATOR_SPEC_ERROR,
43+
"Spec has no devices to decorate");
44+
}
45+
for (DeviceToJobSpec deviceSpec : spec.getDeviceToJobSpecList()) {
46+
if (!deviceSpec.hasDeviceSelector()) {
47+
throw new MobileHarnessException(
48+
ExtErrorId.MOBLY_DECORATOR_ADAPTER_DECORATOR_SPEC_ERROR,
49+
"Spec is missing device selector");
50+
}
51+
DeviceSelector selector = deviceSpec.getDeviceSelector();
52+
if (!selector.hasDeviceLabel()
53+
&& selector.getDimensionsCount() == 0
54+
&& !selector.hasSubDeviceId()) {
55+
throw new MobileHarnessException(
56+
ExtErrorId.MOBLY_DECORATOR_ADAPTER_DECORATOR_SPEC_ERROR, "Device selector is empty");
57+
}
58+
}
59+
}
60+
61+
/**
62+
* Creates a {@link JobInfo} from a SubDeviceJobSpec.
63+
*
64+
* @param rootJobInfo The {@link JobInfo} from the root job
65+
* @param deviceSpec The spec containing the params, files and decorators for this subdevice
66+
* @return a {@link JobInfo} with the same settings as the root ID, but with a custom type string,
67+
* param, and file list.
68+
* @throws MobileHarnessException if resolving files or parsing the job spec fails.
69+
*/
70+
public static JobInfo makeSubDeviceJobInfo(JobInfo rootJobInfo, SubDeviceJobSpec deviceSpec)
71+
throws MobileHarnessException {
72+
String jobTypeStr =
73+
"_nodevice_+_nodriver_+" + Joiner.on('+').join(deviceSpec.getDecoratorList());
74+
JobType jobType = JobTypeUtil.parseString(jobTypeStr);
75+
JobInfo subJobInfo =
76+
JobInfo.newBuilder()
77+
.setLocator(rootJobInfo.locator())
78+
.setJobUser(rootJobInfo.jobUser())
79+
.setType(jobType)
80+
.setSetting(rootJobInfo.setting())
81+
.build();
82+
for (ParamSpec param : deviceSpec.getParamsList()) {
83+
subJobInfo.params().add(param.getName(), param.getValue());
84+
}
85+
// Add files from job's root info to sub job info.
86+
subJobInfo.files().addAll(rootJobInfo.files().getAll());
87+
// Add files from scoped job spec to sub job info.
88+
for (FileSpec fileSpec : deviceSpec.getFilesList()) {
89+
for (String filePath : fileSpec.getFilesList()) {
90+
subJobInfo.files().add(fileSpec.getTag(), filePath);
91+
}
92+
for (Google3File file : fileSpec.getG3FilesList()) {
93+
for (String output : file.getOutputList()) {
94+
subJobInfo.files().add(fileSpec.getTag(), output);
95+
}
96+
}
97+
}
98+
// This subJobInfo is only used to pass Job parameters to sub tests. Job status does not mean
99+
// anything to test runner. Set the status to "RUNNING" to avoid {@link MobileHarnessException}
100+
// with {@link ErrorCode.JOB_NOT_STARTED} at {@link JobInfo#getExpireTime}, error message:
101+
// "Failed to calculate the job expire time because job is not started. Please set the
102+
// job status from NEW to any other status."
103+
// This will be set to DONE once all tests are completed.
104+
subJobInfo.status().set(TestStatus.RUNNING);
105+
return subJobInfo;
106+
}
107+
}

src/java/com/google/wireless/qa/mobileharness/shared/proto/spec/BUILD

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ package(
2020
default_visibility = ["//visibility:public"],
2121
)
2222

23-
PYTHON_API_VERSION = 2
24-
2523
# If you are adding a new spec, please add it as a dependency here. If your spec is
2624
# not in the deps list here, it may not be included in the lab server build and will
2725
# fail to be populated.
@@ -287,6 +285,11 @@ cc_proto_library(
287285
deps = [":install_apk_step_spec_proto"],
288286
)
289287

288+
java_proto_library(
289+
name = "mobly_decorator_adapter_spec_java_proto",
290+
deps = [":mobly_decorator_adapter_spec_proto"],
291+
)
292+
290293
proto_library(
291294
name = "no_op_decorator_spec_proto",
292295
srcs = ["decorator/no_op_decorator_spec.proto"],
@@ -325,6 +328,20 @@ java_proto_library(
325328
deps = [":no_op_driver_spec_proto"],
326329
)
327330

331+
proto_library(
332+
name = "testbed_decorator_adapter_spec_proto",
333+
srcs = ["decorator/testbed_decorator_adapter_spec.proto"],
334+
deps = [
335+
":base_spec_proto",
336+
"//src/java/com/google/wireless/qa/mobileharness/shared/proto:common_proto",
337+
],
338+
)
339+
340+
java_proto_library(
341+
name = "testbed_decorator_adapter_spec_java_proto",
342+
deps = [":testbed_decorator_adapter_spec_proto"],
343+
)
344+
328345
proto_library(
329346
name = "xts_tradefed_test_spec_proto",
330347
srcs = ["driver/xts_tradefed_test_spec.proto"],

0 commit comments

Comments
 (0)