|
| 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 | +} |
0 commit comments