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