1 /*
2  * Copyright (C) 2011 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 import vogar.Expectation;
18 import vogar.ExpectationStore;
19 import vogar.ModeId;
20 import vogar.Result;
21 
22 import com.android.tradefed.util.AbiUtils;
23 
24 import java.io.File;
25 import java.io.FilenameFilter;
26 import java.io.IOException;
27 import java.util.Arrays;
28 import java.util.HashSet;
29 import java.util.Set;
30 
31 public class VogarUtils {
32 
isVogarKnownFailure(ExpectationStore[] expectationStores, final String testClassName, final String testMethodName)33     public static boolean isVogarKnownFailure(ExpectationStore[] expectationStores,
34             final String testClassName,
35             final String testMethodName) {
36         for (ExpectationStore expectationStore : expectationStores) {
37             if (isVogarKnownFailure(expectationStore, testClassName, testMethodName)) {
38                 return true;
39             }
40         }
41         return false;
42     }
43 
44     /**
45      * @return true iff the class/name is found in the vogar known failure list and it is not
46      * a known failure that is a result of an unsupported abi.
47      */
isVogarKnownFailure(ExpectationStore expectationStore, final String testClassName, final String testMethodName)48     public static boolean isVogarKnownFailure(ExpectationStore expectationStore,
49             final String testClassName,
50             final String testMethodName) {
51         if (expectationStore == null) {
52             return false;
53         }
54         String fullTestName = buildFullTestName(testClassName, testMethodName);
55         Expectation expectation = expectationStore.get(fullTestName);
56         if (expectation.getResult() == Result.SUCCESS) {
57             return false;
58         }
59 
60         String description = expectation.getDescription();
61         boolean foundAbi = AbiUtils.parseAbiList(description).size() > 0;
62 
63         return expectation.getResult() != Result.SUCCESS && !foundAbi;
64     }
65 
provideExpectationStore(String dir)66     public static ExpectationStore provideExpectationStore(String dir) throws IOException {
67         if (dir == null) {
68             return null;
69         }
70         ExpectationStore result = ExpectationStore.parse(getExpectationFiles(dir), ModeId.DEVICE);
71         return result;
72     }
73 
getExpectationFiles(String dir)74     private static Set<File> getExpectationFiles(String dir) {
75         Set<File> expectSet = new HashSet<File>();
76         File[] files = new File(dir).listFiles(new FilenameFilter() {
77             // ignore obviously temporary files
78             public boolean accept(File dir, String name) {
79                 return !name.endsWith("~") && !name.startsWith(".");
80             }
81         });
82         if (files != null) {
83             expectSet.addAll(Arrays.asList(files));
84         }
85         return expectSet;
86     }
87 
88     /** @return the test name in the form of com.android.myclass.TestClass#testMyMethod */
buildFullTestName(String testClass, String testMethodName)89     public static String buildFullTestName(String testClass, String testMethodName) {
90         return String.format("%s#%s", testClass, testMethodName);
91     }
92 
93     /**
94      * This method looks in the description field of the Vogar entry for the ABI_LIST_MARKER
95      * and returns the list of abis found there.
96      *
97      * @return The Set of supported abis parsed from the {@code expectation}'s description.
98      */
extractSupportedAbis(String architecture, Expectation expectation)99     public static Set<String> extractSupportedAbis(String architecture, Expectation expectation) {
100         Set<String> supportedAbiSet = AbiUtils.getAbisForArch(architecture);
101         if (expectation == null || expectation.getDescription().isEmpty()) {
102             // Include all abis since there was no limitation found in the description
103             return supportedAbiSet;
104         }
105 
106         // Remove any abis that are not supported for the test.
107         supportedAbiSet.removeAll(AbiUtils.parseAbiList(expectation.getDescription()));
108 
109         return supportedAbiSet;
110     }
111 
112     /**
113      * Determine the correct set of ABIs for the given className/testName.
114      *
115      * @return the set of ABIs that can be expected to pass for the given combination of
116      * {@code architecture}, {@code className} and {@code testName}.
117      */
extractSupportedAbis(String architecture, ExpectationStore[] expectationStores, String className, String testName)118     public static Set<String> extractSupportedAbis(String architecture,
119                                                    ExpectationStore[] expectationStores,
120                                                    String className,
121                                                    String testName) {
122 
123         String fullTestName = buildFullTestName(className, testName);
124         Set<String> supportedAbiSet = AbiUtils.getAbisForArch(architecture);
125         for (ExpectationStore expectationStore : expectationStores) {
126             Expectation expectation = expectationStore.get(fullTestName);
127             supportedAbiSet.retainAll(extractSupportedAbis(architecture, expectation));
128         }
129 
130         return supportedAbiSet;
131     }
132 
133     /**
134      * Returns the greatest timeout in minutes for the test in all
135      * expectation stores, or 0 if no timeout was found.
136      */
timeoutInMinutes(ExpectationStore[] expectationStores, final String testClassName, final String testMethodName)137     public static int timeoutInMinutes(ExpectationStore[] expectationStores,
138             final String testClassName,
139             final String testMethodName) {
140         int timeoutInMinutes = 0;
141         for (ExpectationStore expectationStore : expectationStores) {
142             timeoutInMinutes = Math.max(timeoutInMinutes,
143                                         timeoutInMinutes(expectationStore,
144                                                          testClassName,
145                                                          testMethodName));
146         }
147         return timeoutInMinutes;
148     }
149 
150     /**
151      * Returns the timeout in minutes for the test in the expectation
152      * stores, or 0 if no timeout was found.
153      */
timeoutInMinutes(ExpectationStore expectationStore, final String testClassName, final String testMethodName)154     public static int timeoutInMinutes(ExpectationStore expectationStore,
155             final String testClassName,
156             final String testMethodName) {
157         if (expectationStore == null) {
158             return 0;
159         }
160         String fullTestName = buildFullTestName(testClassName, testMethodName);
161         return timeoutInMinutes(expectationStore.get(fullTestName));
162     }
163 
164     /**
165      * Returns the timeout in minutes for the expectation. Currently a
166      * tag of large results in a 60 minute timeout, otherwise 0 is
167      * returned to indicate a default timeout should be used.
168      */
timeoutInMinutes(Expectation expectation)169     public static int timeoutInMinutes(Expectation expectation) {
170         return expectation.getTags().contains("large") ? 60 : 0;
171     }
172 }
173