Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up deprecated field command_attempt_details. #2865

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.eventbus.Subscribe;
import com.google.common.flogger.FluentLogger;
import com.google.devtools.mobileharness.api.model.error.MobileHarnessException;
import com.google.devtools.mobileharness.infra.ats.common.XtsPropertyName;
import com.google.devtools.mobileharness.infra.ats.server.proto.ServiceProto.AtsServerSessionNotification;
import com.google.devtools.mobileharness.infra.ats.server.proto.ServiceProto.AtsServerSessionNotification.NotificationCase;
import com.google.devtools.mobileharness.infra.ats.server.proto.ServiceProto.CancelReason;
Expand Down Expand Up @@ -53,6 +54,7 @@
import com.google.devtools.mobileharness.infra.client.longrunningservice.proto.SessionServiceProto.CreateSessionRequest;
import com.google.devtools.mobileharness.infra.client.longrunningservice.rpc.service.LocalSessionStub;
import com.google.devtools.mobileharness.platform.android.xts.message.proto.TestMessageProto.XtsTradefedRunCancellation;
import com.google.devtools.mobileharness.shared.util.time.TimeUtils;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
Expand All @@ -64,9 +66,11 @@
import com.google.wireless.qa.mobileharness.shared.model.job.TestInfo;
import com.google.wireless.qa.mobileharness.shared.proto.Job.TestStatus;
import java.time.Clock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
import javax.inject.Inject;

/** Session Plugin to serve test requests coming from ATS server. */
Expand Down Expand Up @@ -257,6 +261,33 @@ public void onJobEnded(JobEndEvent jobEndEvent) throws InterruptedException {
if (!tradefedJobs.isEmpty()) {
sessionInfo.addJob(tradefedJobs.remove(0));
}

String commandId = getCommandIdOfJob(jobInfo);
if (requestDetail.containsCommandDetails(commandId)) {
// ATS server requests use device UUIDs to schedule tests.
ImmutableList<String> deviceUuids =
jobInfo.subDeviceSpecs().getAllSubDevices().stream()
.flatMap(
subDeviceSpec -> Stream.ofNullable(subDeviceSpec.dimensions().get("uuid")))
.collect(toImmutableList());
TestInfo testInfo = jobInfo.tests().getAll().values().iterator().next();

CommandDetail updatedCommandDetail =
requestDetail
.getCommandDetailsOrDefault(commandId, CommandDetail.getDefaultInstance())
.toBuilder()
.addAllDeviceSerials(deviceUuids)
.setCommandAttemptId(testInfo.locator().getId())
.build();

requestDetail
.putCommandDetails(commandId, updatedCommandDetail)
.setUpdateTime(TimeUtils.toProtoTimestamp(Instant.now()));
} else {
logger.atWarning().log(
"Tradefed job %s not found in requestDetail", jobInfo.locator().getId());
}

if (requestDetail.getState() == RequestState.CANCELED) {
return;
}
Expand Down Expand Up @@ -504,6 +535,10 @@ && hasSessionCompleted(commandDetailsMap)
.anyMatch(commandDetail -> commandDetail.getFailedTestCount() > 0);
}

private static String getCommandIdOfJob(JobInfo jobInfo) {
return jobInfo.properties().getOptional(XtsPropertyName.Job.XTS_COMMAND_ID).orElse("");
}

private static void appendErrorMessage(RequestDetail.Builder requestDetail, String newMessage) {
requestDetail.setErrorMessage(appendErrorMessage(requestDetail.getErrorMessage(), newMessage));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ java_library(
"//src/devtools/mobileharness/infra/client/longrunningservice/proto:session_service_java_proto",
"//src/devtools/mobileharness/platform/android/xts/proto:test_message_java_proto",
"//src/java/com/google/devtools/mobileharness/api/model/error",
"//src/java/com/google/devtools/mobileharness/infra/ats/common:xts_property_name",
"//src/java/com/google/devtools/mobileharness/infra/client/longrunningservice/model:session_event",
"//src/java/com/google/devtools/mobileharness/infra/client/longrunningservice/model:session_info",
"//src/java/com/google/devtools/mobileharness/infra/client/longrunningservice/model:with_proto",
"//src/java/com/google/devtools/mobileharness/infra/client/longrunningservice/rpc/service:local_session_stub",
"//src/java/com/google/devtools/mobileharness/shared/util/base:proto_text_format",
"//src/java/com/google/devtools/mobileharness/shared/util/logging:google_logger",
"//src/java/com/google/devtools/mobileharness/shared/util/time:time_utils",
"//src/java/com/google/wireless/qa/mobileharness/client/api/event:job",
"//src/java/com/google/wireless/qa/mobileharness/shared/comm/message",
"//src/java/com/google/wireless/qa/mobileharness/shared/controller/event:client_test_events",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,40 @@ public void onJobEnded_manuallyCancelSession_skipCreatingNonTradefedJob() throws
assertThat(requestDetail.getState()).isEqualTo(RequestState.CANCELED);
}

@Test
public void onJobEnded_tradefedJobEnded_addCommandAttemptIdAndDeviceSerials() throws Exception {
when(sessionInfo.getSessionPluginExecutionConfig())
.thenReturn(
SessionPluginExecutionConfig.newBuilder()
.setConfig(
Any.pack(
SessionRequest.newBuilder().setNewMultiCommandRequest(request).build()))
.build());
plugin.onSessionStarting(new SessionStartingEvent(sessionInfo));
verify(sessionInfo).addJob(jobInfo);
when(jobInfo.timing()).thenReturn(timing);
when(jobInfo.status()).thenReturn(new Status(timing).set(TestStatus.DONE));
Result result = new Result(timing, new Params(timing)).set(TestResult.PASS);
when(jobInfo.result()).thenReturn(result);
JobType jobType = JobType.newBuilder().setDriver("XtsTradefedTest").build();
when(jobInfo.type()).thenReturn(jobType);

plugin.onJobEnded(new JobEndEvent(jobInfo, null));

// Set 2 times when: session is started and job is ended.
verify(sessionInfo, times(2))
.setSessionPluginOutput(unaryOperatorCaptor.capture(), eq(RequestDetail.class));
RequestDetail requestDetail = Iterables.getLast(unaryOperatorCaptor.getAllValues()).apply(null);
String commandId =
UUID.nameUUIDFromBytes(commandInfo.getCommandLine().getBytes(UTF_8)).toString();
assertThat(requestDetail.getCommandDetailsCount()).isEqualTo(1);
CommandDetail commandDetail = requestDetail.getCommandDetailsMap().get(commandId);
assertThat(commandDetail.getOriginalCommandInfo()).isEqualTo(commandInfo);
assertThat(commandDetail.getState()).isEqualTo(CommandState.RUNNING);
assertThat(commandDetail.getCommandAttemptId()).isEqualTo("test_id");
assertThat(commandDetail.getDeviceSerialsList()).containsExactly("device_id_1", "device_id_2");
}

@Test
public void onJobEnded_multipleTradefedJobsEnded_lastOneTriggerNonTradefedJob() throws Exception {
request = request.toBuilder().addCommands(commandInfo).build();
Expand Down
Loading