1 /* 2 * Copyright (C) 2024 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 17 package com.android.tradefed.observatory; 18 19 import com.android.tradefed.config.Configuration; 20 import com.android.tradefed.config.ConfigurationException; 21 import com.android.tradefed.config.ConfigurationFactory; 22 import com.android.tradefed.config.IConfiguration; 23 import com.android.tradefed.config.IConfigurationFactory; 24 import com.android.tradefed.invoker.tracing.CloseableTraceScope; 25 import com.android.tradefed.util.keystore.DryRunKeyStore; 26 27 import java.util.Set; 28 29 /** A utility class for test discovery. */ 30 public class TestDiscoveryUtil { getConfigurationFactory()31 public IConfigurationFactory getConfigurationFactory() { 32 return ConfigurationFactory.getInstance(); 33 } 34 hasOutputResultFile()35 public boolean hasOutputResultFile() { 36 return System.getenv(TestDiscoveryInvoker.OUTPUT_FILE) != null; 37 } 38 getTestMappingFilePath()39 public String getTestMappingFilePath() { 40 return System.getenv(TestDiscoveryInvoker.TEST_MAPPING_ZIP_FILE); 41 } 42 getEnvironment(String var)43 protected String getEnvironment(String var) { 44 return System.getenv(var); 45 } 46 47 /** 48 * Retrieve configuration base on command line args. 49 * 50 * @param args the command line args of the test. 51 * @return A {@link IConfiguration} which constructed based on command line args. 52 */ getConfiguration(String[] args)53 public IConfiguration getConfiguration(String[] args) throws ConfigurationException { 54 try (CloseableTraceScope ignored = new CloseableTraceScope("create_configuration")) { 55 IConfigurationFactory configurationFactory = getConfigurationFactory(); 56 return configurationFactory.createPartialConfigurationFromArgs( 57 args, 58 new DryRunKeyStore(), 59 Set.of( 60 Configuration.BUILD_PROVIDER_TYPE_NAME, 61 Configuration.TEST_TYPE_NAME, 62 Configuration.TARGET_PREPARER_TYPE_NAME, 63 Configuration.GLOBAL_FILTERS_TYPE_NAME), 64 null); 65 } 66 } 67 68 /** Returns true if the command is a Tradefed command */ isTradefedConfiguration(String[] args)69 public boolean isTradefedConfiguration(String[] args) { 70 if (args[0].equals("testing/mobileharness/gateway")) { 71 return false; 72 } 73 if (args[0].equals("unused")) { 74 return false; 75 } 76 return true; 77 } 78 } 79