1 /* 2 * Copyright (C) 2019 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.google.asuite.clearcut.junit.listener; 17 18 import com.android.asuite.clearcut.Common; 19 import com.android.asuite.clearcut.Common.UserType; 20 import com.android.asuite.clearcut.ExternalUserLog.AtestLogEventExternal; 21 import com.android.asuite.clearcut.ExternalUserLog.AtestLogEventExternal.AtestExitEvent; 22 import com.android.asuite.clearcut.ExternalUserLog.AtestLogEventExternal.AtestStartEvent; 23 import com.android.asuite.clearcut.ExternalUserLog.AtestLogEventExternal.RunTestsFinishEvent; 24 import com.android.asuite.clearcut.ExternalUserLog.AtestLogEventExternal.RunnerFinishEvent; 25 import com.android.asuite.clearcut.InternalUserLog.AtestLogEventInternal; 26 27 import com.google.protobuf.ByteString; 28 29 import java.time.Duration; 30 31 /** 32 * Utility to help populate the event protos 33 * Cloned from tradefed and decoupled. 34 * Needs a_lot of work. 35 */ 36 public class ClearcutEventHelper { 37 38 private static final String TOOL_NAME = "Tradefed"; 39 40 /** 41 * Create the start event for Tradefed. 42 * 43 * @param userKey The unique id representing the user 44 * @param runId The current id for the session. 45 * @param userType The type of the user: internal or external. 46 * @param toolName The name of test tool. 47 * @param subToolName The name of test suite tool. 48 * @return a ByteString representation of the even proto. 49 */ createStartEvent( String userKey, String runId, UserType userType, String toolName, String subToolName)50 public static ByteString createStartEvent( 51 String userKey, String runId, UserType userType, String toolName, String subToolName) { 52 if (UserType.GOOGLE.equals(userType)) { 53 AtestLogEventInternal.Builder builder = 54 createBaseInternalEventBuilder(userKey, runId, userType, toolName, subToolName); 55 AtestLogEventInternal.AtestStartEvent.Builder startEventBuilder = 56 AtestLogEventInternal.AtestStartEvent.newBuilder(); 57 builder.setAtestStartEvent(startEventBuilder.build()); 58 return builder.build().toByteString(); 59 } 60 61 AtestLogEventExternal.Builder builder = 62 createBaseExternalEventBuilder(userKey, runId, userType, toolName, subToolName); 63 AtestStartEvent.Builder startBuilder = AtestStartEvent.newBuilder(); 64 builder.setAtestStartEvent(startBuilder.build()); 65 return builder.build().toByteString(); 66 } 67 68 /** 69 * Create the end event for Tradefed. 70 * 71 * @param userKey The unique id representing the user 72 * @param runId The current id for the session. 73 * @param userType The type of the user: internal or external. 74 * @param toolName The name of tool. 75 * @param subToolName The name of test suite tool. 76 * @param sessionDuration The duration of the complete session. 77 * @return a ByteString representation of the even proto. 78 */ createFinishedEvent( String userKey, String runId, UserType userType, String toolName, String subToolName, Duration sessionDuration)79 public static ByteString createFinishedEvent( 80 String userKey, 81 String runId, 82 UserType userType, 83 String toolName, 84 String subToolName, 85 Duration sessionDuration) { 86 if (UserType.GOOGLE.equals(userType)) { 87 AtestLogEventInternal.Builder builder = 88 createBaseInternalEventBuilder(userKey, runId, userType, toolName, subToolName); 89 AtestLogEventInternal.AtestExitEvent.Builder exitEventBuilder = 90 AtestLogEventInternal.AtestExitEvent.newBuilder(); 91 Common.Duration duration = 92 Common.Duration.newBuilder() 93 .setSeconds(sessionDuration.getSeconds()) 94 .setNanos(sessionDuration.getNano()) 95 .build(); 96 exitEventBuilder.setDuration(duration); 97 builder.setAtestExitEvent(exitEventBuilder.build()); 98 return builder.build().toByteString(); 99 } 100 101 AtestLogEventExternal.Builder builder = 102 createBaseExternalEventBuilder(userKey, runId, userType, toolName, subToolName); 103 AtestLogEventExternal.AtestExitEvent.Builder startBuilder = AtestExitEvent.newBuilder(); 104 builder.setAtestExitEvent(startBuilder.build()); 105 return builder.build().toByteString(); 106 } 107 108 /** 109 * Create the start invocation event for Tradefed. 110 * 111 * @param userKey The unique id representing the user 112 * @param runId The current id for the session. 113 * @param userType The type of the user: internal or external. 114 * @param toolName The name of the tool. 115 * @param subToolName The name of test suite tool. 116 * @return a ByteString representation of the even proto. 117 */ createRunStartEvent( String userKey, String runId, UserType userType, String toolName, String subToolName)118 public static ByteString createRunStartEvent( 119 String userKey, String runId, UserType userType, String toolName, String subToolName) { 120 if (UserType.GOOGLE.equals(userType)) { 121 //This is where individual test results belong. Weird. 122 AtestLogEventInternal.Builder builder = 123 createBaseInternalEventBuilder(userKey, runId, userType, toolName, subToolName); 124 AtestLogEventInternal.RunnerFinishEvent.Builder startRunEventBuilder = 125 AtestLogEventInternal.RunnerFinishEvent.newBuilder(); 126 //Why aren't we calling? startRunEventBuilder.addTest(); 127 builder.setRunnerFinishEvent(startRunEventBuilder.build()); 128 return builder.build().toByteString(); 129 } 130 131 AtestLogEventExternal.Builder builder = 132 createBaseExternalEventBuilder(userKey, runId, userType, toolName, subToolName); 133 RunnerFinishEvent.Builder startBuilder = RunnerFinishEvent.newBuilder(); 134 builder.setRunnerFinishEvent(startBuilder.build()); 135 return builder.build().toByteString(); 136 } 137 138 /** 139 * Not needed yet. 140 */ buildTest(String name, int result, String stacktrace)141 private AtestLogEventInternal.RunnerFinishEvent.Test buildTest(String name, int result, String stacktrace) { 142 AtestLogEventInternal.RunnerFinishEvent.Test.Builder testBuilder = AtestLogEventInternal.RunnerFinishEvent.Test.newBuilder(); 143 testBuilder.setName(name); 144 testBuilder.setResult(result); 145 testBuilder.setStacktrace(stacktrace); 146 return testBuilder.build(); 147 } 148 149 /** 150 * Create the run test finished event for Tradefed. 151 * 152 * @param userKey The unique id representing the user 153 * @param runId The current id for the session. 154 * @param userType The type of the user: internal or external. 155 * @param toolName The name of test suite tool. 156 * @param subToolName The name of test suite tool. 157 * @param testDuration the duration of the test session. 158 * @return a ByteString representation of the even proto. 159 */ creatRunTestFinished( String userKey, String runId, UserType userType, String toolName, String subToolName, Duration testDuration)160 public static ByteString creatRunTestFinished( 161 String userKey, 162 String runId, 163 UserType userType, 164 String toolName, 165 String subToolName, 166 Duration testDuration) { 167 if (UserType.GOOGLE.equals(userType)) { 168 AtestLogEventInternal.Builder builder = 169 createBaseInternalEventBuilder(userKey, runId, userType, toolName, subToolName); 170 AtestLogEventInternal.RunTestsFinishEvent.Builder runTestsFinished = 171 AtestLogEventInternal.RunTestsFinishEvent.newBuilder(); 172 Common.Duration duration = 173 Common.Duration.newBuilder() 174 .setSeconds(testDuration.getSeconds()) 175 .setNanos(testDuration.getNano()) 176 .build(); 177 runTestsFinished.setDuration(duration); 178 builder.setRunTestsFinishEvent(runTestsFinished.build()); 179 return builder.build().toByteString(); 180 } 181 182 AtestLogEventExternal.Builder builder = 183 createBaseExternalEventBuilder(userKey, runId, userType, toolName, subToolName); 184 RunTestsFinishEvent.Builder startBuilder = RunTestsFinishEvent.newBuilder(); 185 builder.setRunTestsFinishEvent(startBuilder.build()); 186 return builder.build().toByteString(); 187 } 188 189 /** 190 * Create the basic event builder with all the common information. 191 * 192 * @param userKey The unique id representing the user 193 * @param runId The current id for the session. 194 * @param userType The type of the user: internal or external. 195 * @param toolName The name of test suite tool. 196 * @param subToolName The name of test suite tool. 197 * @return a builder for the event. 198 */ createBaseExternalEventBuilder( String userKey, String runId, UserType userType, String toolName, String subToolName)199 private static AtestLogEventExternal.Builder createBaseExternalEventBuilder( 200 String userKey, String runId, UserType userType, String toolName, String subToolName) { 201 AtestLogEventExternal.Builder builder = AtestLogEventExternal.newBuilder(); 202 builder.setUserKey(userKey); 203 builder.setRunId(runId); 204 builder.setUserType(userType); 205 builder.setToolName(toolName); 206 builder.setSubToolName(subToolName); 207 return builder; 208 } 209 210 /** 211 * Create the basic event builder with all the common informations. 212 * 213 * @param userKey The unique id representing the user 214 * @param runId The current id for the session. 215 * @param userType The type of the user: internal or external. 216 * @param subToolName The name of test suite tool. 217 * @return a builder for the event. 218 */ createBaseInternalEventBuilder( String userKey, String runId, UserType userType, String toolName, String subToolName)219 private static AtestLogEventInternal.Builder createBaseInternalEventBuilder( 220 String userKey, String runId, UserType userType, String toolName, String subToolName) { 221 AtestLogEventInternal.Builder builder = AtestLogEventInternal.newBuilder(); 222 builder.setUserKey(userKey); 223 builder.setRunId(runId); 224 builder.setUserType(userType); 225 builder.setToolName(toolName); 226 builder.setSubToolName(subToolName); 227 return builder; 228 } 229 }