1 /* 2 * Copyright (C) 2023 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.util; 17 18 import com.android.annotations.VisibleForTesting; 19 import com.android.tradefed.log.LogUtil.CLog; 20 21 import java.io.File; 22 import java.util.ArrayList; 23 import java.util.List; 24 25 /** A utility class to help various test runners. */ 26 public class TestRunnerUtil { 27 28 @VisibleForTesting static TestRunnerUtil singleton = new TestRunnerUtil(); 29 30 /** 31 * Get the value of an environment variable. 32 * 33 * <p>The wrapper function is created for mock in unit test. 34 * 35 * @param name the name of the environment variable. 36 * @return {@link String} value of the given environment variable. 37 */ 38 @VisibleForTesting getEnv(String name)39 String getEnv(String name) { 40 return System.getenv(name); 41 } 42 43 /** 44 * Return LD_LIBRARY_PATH for hostside tests that require native library. 45 * 46 * @param testFile the {@link File} of the test module 47 * @return a string specifying the colon separated library path. 48 */ getLdLibraryPath(File testFile)49 public static String getLdLibraryPath(File testFile) { 50 List<String> paths = new ArrayList<>(); 51 52 String libs[] = {"lib", "lib64"}; 53 String androidHostOut = singleton.getEnv("ANDROID_HOST_OUT"); 54 String testcasesFolderPath = getTestcasesFolderPath(testFile); 55 for (String lib : libs) { 56 File libFile = new File(testFile.getParentFile().getAbsolutePath(), lib); 57 if (libFile.exists()) { 58 paths.add(libFile.getAbsolutePath()); 59 } 60 // Include `testcases` directory for running tests based on test zip. 61 if (testcasesFolderPath != null) { 62 libFile = new File(testcasesFolderPath, lib); 63 if (libFile.exists()) { 64 paths.add(libFile.getAbsolutePath()); 65 } 66 // Handle special case of art 67 libFile = new File(new File(testcasesFolderPath, "art_common/out/host/linux-x86/"), lib); 68 if (libFile.exists()) { 69 paths.add(libFile.getAbsolutePath()); 70 } 71 } 72 // Include ANDROID_HOST_OUT/lib to support local case. 73 if (androidHostOut != null) { 74 libFile = new File(androidHostOut, lib); 75 if (libFile.exists()) { 76 paths.add(libFile.getAbsolutePath()); 77 } 78 } 79 } 80 File moduleSharedLibs = new File(testFile.getParentFile(), "shared_libs"); 81 if (moduleSharedLibs.exists()) { 82 paths.add(moduleSharedLibs.getAbsolutePath()); 83 } 84 if (paths.isEmpty()) { 85 return null; 86 } 87 String ldLibraryPath = String.join(java.io.File.pathSeparator, paths); 88 CLog.d("Identify LD_LIBRARY_PATH to be used: %s", ldLibraryPath); 89 return ldLibraryPath; 90 } 91 92 /** Return the path to the testcases folder or null if it doesn't exist. */ getTestcasesFolderPath(File testFile)93 private static String getTestcasesFolderPath(File testFile) { 94 // Assume test binary path is like 95 // /tmp/tf-workfolder/testsdir/host/testcases/test_name/x86_64/binary_name 96 File folder = testFile.getParentFile(); 97 if (folder != null) { 98 folder = folder.getParentFile(); 99 if (folder != null) { 100 folder = folder.getParentFile(); 101 if (folder != null) return folder.getAbsolutePath(); 102 } 103 } 104 return null; 105 } 106 } 107