1 /* 2 * Copyright (C) 2020 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.android.tradefed.config.proxy; 17 18 import com.android.annotations.VisibleForTesting; 19 import com.android.tradefed.config.IConfiguration; 20 import com.android.tradefed.log.LogUtil.CLog; 21 import com.android.tradefed.result.ITestInvocationListener; 22 import com.android.tradefed.result.proto.FileProtoResultReporter; 23 import com.android.tradefed.result.proto.StreamProtoResultReporter; 24 25 import com.google.common.collect.ImmutableSet; 26 27 import java.io.File; 28 import java.util.List; 29 30 /** 31 * Class that defines the mapping from Tradefed automated reporters. 32 * 33 * <p>TODO: Formalize how to expose the list of supported automation. 34 */ 35 public class AutomatedReporters { 36 37 public static final String PROTO_REPORTING_PORT = "PROTO_REPORTING_PORT"; 38 public static final String PROTO_REPORTING_FILE = "PROTO_REPORTING_FILE"; 39 public static final String PROTO_REPORTING_FILE_GRANULAR = "PROTO_REPORTING_FILE_GRANULAR"; 40 public static final ImmutableSet<String> REPORTER_MAPPING = 41 ImmutableSet.of( 42 PROTO_REPORTING_PORT, PROTO_REPORTING_FILE, PROTO_REPORTING_FILE_GRANULAR); 43 44 /** 45 * Complete the listeners based on the environment. 46 * 47 * @param configuration The configuration to complete 48 */ applyAutomatedReporters(IConfiguration configuration)49 public void applyAutomatedReporters(IConfiguration configuration) { 50 for (String key : REPORTER_MAPPING) { 51 String envValue = getEnv(key); 52 if (envValue == null) { 53 continue; 54 } 55 switch (key) { 56 case PROTO_REPORTING_PORT: 57 StreamProtoResultReporter streamProto = new StreamProtoResultReporter(); 58 try { 59 streamProto.setProtoReportPort(Integer.parseInt(envValue)); 60 List<ITestInvocationListener> listeners = 61 configuration.getTestInvocationListeners(); 62 listeners.add(streamProto); 63 configuration.setTestInvocationListeners(listeners); 64 } catch (NumberFormatException e) { 65 CLog.e(e); 66 } 67 break; 68 case PROTO_REPORTING_FILE: 69 FileProtoResultReporter fileReporter = new FileProtoResultReporter(); 70 fileReporter.setOutputFile(new File(envValue)); 71 fileReporter.setDelimitedOutput(false); 72 // Aggregate results for this option 73 fileReporter.setGranularResults(false); 74 addToReporters(configuration, fileReporter); 75 break; 76 case PROTO_REPORTING_FILE_GRANULAR: 77 FileProtoResultReporter fileReporterGranular = new FileProtoResultReporter(); 78 fileReporterGranular.setOutputFile(new File(envValue)); 79 fileReporterGranular.setDelimitedOutput(false); 80 fileReporterGranular.setGranularResults(true); 81 addToReporters(configuration, fileReporterGranular); 82 break; 83 default: 84 break; 85 } 86 } 87 } 88 89 @VisibleForTesting getEnv(String key)90 protected String getEnv(String key) { 91 return System.getenv(key); 92 } 93 addToReporters(IConfiguration configuration, ITestInvocationListener reporter)94 private void addToReporters(IConfiguration configuration, ITestInvocationListener reporter) { 95 List<ITestInvocationListener> listeners = configuration.getTestInvocationListeners(); 96 listeners.add(reporter); 97 configuration.setTestInvocationListeners(listeners); 98 } 99 } 100