1 /*
2  * Copyright (C) 2017 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.compatibility.common.tradefed.testtype.suite;
17 
18 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
19 import com.android.compatibility.common.tradefed.testtype.ISubPlan;
20 import com.android.compatibility.common.tradefed.testtype.SubPlan;
21 import com.android.compatibility.common.tradefed.testtype.retry.RetryFactoryTest;
22 import com.android.ddmlib.Log.LogLevel;
23 import com.android.tradefed.build.IBuildInfo;
24 import com.android.tradefed.config.ConfigurationException;
25 import com.android.tradefed.config.IConfiguration;
26 import com.android.tradefed.config.Option;
27 import com.android.tradefed.config.Option.Importance;
28 import com.android.tradefed.config.OptionClass;
29 import com.android.tradefed.config.OptionSetter;
30 import com.android.tradefed.log.LogUtil.CLog;
31 import com.android.tradefed.testtype.IAbi;
32 import com.android.tradefed.testtype.suite.BaseTestSuite;
33 import com.android.tradefed.testtype.suite.SuiteModuleLoader;
34 import com.android.tradefed.testtype.suite.SuiteTestFilter;
35 import com.android.tradefed.testtype.suite.TestSuiteInfo;
36 import com.android.tradefed.util.xml.AbstractXmlParser.ParseException;
37 
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.FileNotFoundException;
41 import java.io.InputStream;
42 import java.util.LinkedHashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Set;
46 
47 /**
48  * A Test for running Compatibility Test Suite with new suite system.
49  */
50 @OptionClass(alias = "compatibility")
51 public class CompatibilityTestSuite extends BaseTestSuite {
52 
53     public static final String SUBPLAN_OPTION = "subplan";
54 
55     // TODO: remove this option when CompatibilityTest goes away
56     @Option(name = RetryFactoryTest.RETRY_OPTION,
57             shortName = 'r',
58             description = "Copy of --retry from CompatibilityTest to prevent using it.")
59     private Integer mRetrySessionId = null;
60 
61     @Option(name = SUBPLAN_OPTION,
62             description = "the subplan to run",
63             importance = Importance.IF_UNSET)
64     private String mSubPlan;
65 
66     private CompatibilityBuildHelper mBuildHelper;
67 
68     /**
69      * Ctor that sets some default for Compatibility runs.
70      */
CompatibilityTestSuite()71     public CompatibilityTestSuite() {
72         try {
73             OptionSetter setter = new OptionSetter(this);
74             setter.setOptionValue("skip-loading-config-jar", "true");
75         } catch (ConfigurationException e) {
76             // Should not happen
77             throw new RuntimeException(e);
78         }
79     }
80 
81     @Override
setBuild(IBuildInfo buildInfo)82     public void setBuild(IBuildInfo buildInfo) {
83         super.setBuild(buildInfo);
84         mBuildHelper = new CompatibilityBuildHelper(buildInfo);
85     }
86 
87     @Override
getTestsDir()88     public File getTestsDir() throws FileNotFoundException {
89         return mBuildHelper.getTestsDir();
90     }
91 
92     @Override
createModuleLoader( Map<String, List<SuiteTestFilter>> includeFiltersFormatted, Map<String, List<SuiteTestFilter>> excludeFiltersFormatted, List<String> testArgs, List<String> moduleArgs)93     public SuiteModuleLoader createModuleLoader(
94             Map<String, List<SuiteTestFilter>> includeFiltersFormatted,
95             Map<String, List<SuiteTestFilter>> excludeFiltersFormatted,
96             List<String> testArgs,
97             List<String> moduleArgs) {
98         return new CompatibilitySuiteModuleLoader(includeFiltersFormatted,
99                 excludeFiltersFormatted, testArgs, moduleArgs);
100     }
101 
102     @Override
loadTests()103     public LinkedHashMap<String, IConfiguration> loadTests() {
104         if (mRetrySessionId != null) {
105             throw new IllegalArgumentException(
106                     String.format("--retry cannot be specified with %s[*].xml. "
107                             + "Use 'run retry --retry <session id>' instead.",
108                             TestSuiteInfo.getInstance().getName().toLowerCase()));
109         }
110         return super.loadTests();
111     }
112 
113     /**
114      * Sets the include/exclude filters up based on if a module name was given or whether this is a
115      * retry run.
116      */
117     @Override
setupFilters(File testDir)118     final protected void setupFilters(File testDir) throws FileNotFoundException {
119         if (mSubPlan != null) {
120             try {
121                 File subPlanFile = new File(mBuildHelper.getSubPlansDir(), mSubPlan + ".xml");
122                 if (!subPlanFile.exists()) {
123                     throw new IllegalArgumentException(
124                             String.format("Could not retrieve subplan \"%s\"", mSubPlan));
125                 }
126                 InputStream subPlanInputStream = new FileInputStream(subPlanFile);
127                 ISubPlan subPlan = new SubPlan();
128                 subPlan.parse(subPlanInputStream);
129                 // Set include/exclude filter is additive
130                 setIncludeFilter(subPlan.getIncludeFilters());
131                 setExcludeFilter(subPlan.getExcludeFilters());
132             } catch (ParseException e) {
133                 throw new RuntimeException(
134                         String.format("Unable to find or parse subplan %s", mSubPlan), e);
135             }
136         }
137         super.setupFilters(testDir);
138     }
139 
140     /**
141      * Allow to reset the requested session id for retry.
142      */
resetRetryId()143     public final void resetRetryId() {
144         mRetrySessionId = null;
145     }
146 
147     /**
148      * {@inheritDoc}
149      */
150     @Override
loadingStrategy( Set<IAbi> abis, List<File> testsDirs, String suitePrefix, String suiteTag)151     public LinkedHashMap<String, IConfiguration> loadingStrategy(
152             Set<IAbi> abis, List<File> testsDirs, String suitePrefix, String suiteTag) {
153         LinkedHashMap<String, IConfiguration> loadedConfigs =
154                 super.loadingStrategy(abis, testsDirs, suitePrefix, suiteTag);
155         // Add an extra check in CTS since we never expect the config folder to be empty.
156         if (loadedConfigs.size() == 0) {
157             // Only log if nothing to run.
158             CLog.logAndDisplay(LogLevel.DEBUG,
159                     "No module that needed to run were found. nothing to do.");
160         }
161         return loadedConfigs;
162     }
163 }
164