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.tradefed.testtype.suite; 17 18 import com.android.tradefed.config.Option; 19 import com.android.tradefed.config.OptionCopier; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.invoker.TestInformation; 22 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 23 import com.android.tradefed.result.ByteArrayInputStreamSource; 24 import com.android.tradefed.result.ITestInvocationListener; 25 import com.android.tradefed.result.LogDataType; 26 import com.android.tradefed.result.TestDescription; 27 import com.android.tradefed.testtype.IAbi; 28 import com.android.tradefed.testtype.IAbiReceiver; 29 import com.android.tradefed.testtype.IRemoteTest; 30 import com.android.tradefed.testtype.IRuntimeHintProvider; 31 import com.android.tradefed.testtype.IShardableTest; 32 import com.android.tradefed.testtype.ITestAnnotationFilterReceiver; 33 import com.android.tradefed.testtype.ITestCollector; 34 import com.android.tradefed.testtype.ITestFilterReceiver; 35 36 import java.util.ArrayList; 37 import java.util.Collection; 38 import java.util.HashMap; 39 import java.util.HashSet; 40 import java.util.LinkedHashSet; 41 import java.util.List; 42 import java.util.Set; 43 44 /** A test Stub that can be used to fake some runs for suite's testing. */ 45 public class TestSuiteStub 46 implements IRemoteTest, 47 IAbiReceiver, 48 IRuntimeHintProvider, 49 ITestCollector, 50 ITestFilterReceiver, 51 IShardableTest, 52 ITestAnnotationFilterReceiver { 53 54 @Option(name = "module") 55 private String mModule; 56 57 @Option(name = "foo") 58 protected String mFoo; 59 60 @Option(name = "blah") 61 protected String mBlah; 62 63 @Option(name = "report-test") 64 protected boolean mReportTest = false; 65 66 @Option(name = "run-complete") 67 protected boolean mIsComplete = true; 68 69 @Option(name = "test-fail") 70 protected boolean mDoesOneTestFail = true; 71 72 @Option(name = "internal-retry") 73 protected boolean mRetry = false; 74 75 @Option(name = "throw-device-not-available") 76 protected boolean mThrow = false; 77 78 @Option(name = "log-fake-files") 79 protected boolean mLogFiles = false; 80 81 protected List<TestDescription> mShardedTestToRun; 82 protected Integer mShardIndex = null; 83 84 private Set<String> mIncludeAnnotationFilter = new LinkedHashSet<>(); 85 86 @Option( 87 name = "exclude-annotation", 88 description = "The notAnnotation class name of the test name to run, can be repeated") 89 private Set<String> mExcludeAnnotationFilter = new LinkedHashSet<>(); 90 91 private Set<String> mExcludeFilters = new LinkedHashSet<>(); 92 93 /** Tests attempt. */ testAttempt(ITestInvocationListener listener)94 private void testAttempt(ITestInvocationListener listener) throws DeviceNotAvailableException { 95 listener.testRunStarted(mModule, 3); 96 TestDescription tid = new TestDescription("TestStub", "test1"); 97 listener.testStarted(tid); 98 if (mLogFiles) { 99 listener.testLog( 100 tid.toString() + "-file", 101 LogDataType.LOGCAT, 102 new ByteArrayInputStreamSource("test".getBytes())); 103 } 104 listener.testEnded(tid, new HashMap<String, Metric>()); 105 106 if (mIsComplete) { 107 // possibly skip this one to create some not_executed case. 108 TestDescription tid2 = new TestDescription("TestStub", "test2"); 109 listener.testStarted(tid2); 110 if (mThrow) { 111 throw new DeviceNotAvailableException("test", "serial"); 112 } 113 if (mLogFiles) { 114 listener.testLog( 115 tid2.toString() + "-file", 116 LogDataType.BUGREPORT, 117 new ByteArrayInputStreamSource("test".getBytes())); 118 } 119 listener.testEnded(tid2, new HashMap<String, Metric>()); 120 } 121 122 TestDescription tid3 = new TestDescription("TestStub", "test3"); 123 listener.testStarted(tid3); 124 if (mDoesOneTestFail) { 125 listener.testFailed(tid3, "ouch this is bad."); 126 } 127 if (mLogFiles) { 128 listener.testLog( 129 tid3.toString() + "-file", 130 LogDataType.BUGREPORT, 131 new ByteArrayInputStreamSource("test".getBytes())); 132 } 133 listener.testEnded(tid3, new HashMap<String, Metric>()); 134 135 if (mLogFiles) { 136 // One file logged at run level 137 listener.testLog( 138 mModule + "-file", 139 LogDataType.EAR, 140 new ByteArrayInputStreamSource("test".getBytes())); 141 } 142 listener.testRunEnded(0, new HashMap<String, Metric>()); 143 } 144 145 /** {@inheritDoc} */ 146 @Override run(TestInformation testInfo, ITestInvocationListener listener)147 public void run(TestInformation testInfo, ITestInvocationListener listener) 148 throws DeviceNotAvailableException { 149 if (mReportTest) { 150 if (mShardedTestToRun == null) { 151 if (!mRetry) { 152 testAttempt(listener); 153 } else { 154 // We fake an internal retry by calling testRunStart/Ended again. 155 listener.testRunStarted(mModule, 3); 156 listener.testRunEnded(0, new HashMap<String, Metric>()); 157 testAttempt(listener); 158 } 159 } else { 160 // Run the shard 161 if (mDoesOneTestFail) { 162 listener.testRunStarted(mModule, mShardedTestToRun.size() + 1); 163 } else { 164 listener.testRunStarted(mModule, mShardedTestToRun.size()); 165 } 166 167 if (mIsComplete) { 168 for (TestDescription tid : mShardedTestToRun) { 169 listener.testStarted(tid); 170 listener.testEnded(tid, new HashMap<String, Metric>()); 171 } 172 } else { 173 TestDescription tid = mShardedTestToRun.get(0); 174 listener.testStarted(tid); 175 listener.testEnded(tid, new HashMap<String, Metric>()); 176 } 177 178 if (mDoesOneTestFail) { 179 TestDescription tid = new TestDescription("TestStub", "failed" + mShardIndex); 180 listener.testStarted(tid); 181 listener.testFailed(tid, "shard failed this one."); 182 listener.testEnded(tid, new HashMap<String, Metric>()); 183 } 184 listener.testRunEnded(0, new HashMap<String, Metric>()); 185 } 186 } 187 } 188 189 @Override split(int shardCountHint)190 public Collection<IRemoteTest> split(int shardCountHint) { 191 if (mShardedTestToRun == null) { 192 return null; 193 } 194 Collection<IRemoteTest> listTest = new ArrayList<>(); 195 for (TestDescription id : mShardedTestToRun) { 196 TestSuiteStub stub = new TestSuiteStub(); 197 OptionCopier.copyOptionsNoThrow(this, stub); 198 stub.mShardedTestToRun = new ArrayList<>(); 199 stub.mShardedTestToRun.add(id); 200 listTest.add(stub); 201 } 202 return listTest; 203 } 204 205 @Override setAbi(IAbi abi)206 public void setAbi(IAbi abi) { 207 // Do nothing 208 } 209 210 @Override getAbi()211 public IAbi getAbi() { 212 return null; 213 } 214 215 @Override getRuntimeHint()216 public long getRuntimeHint() { 217 return 1L; 218 } 219 220 @Override setCollectTestsOnly(boolean shouldCollectTest)221 public void setCollectTestsOnly(boolean shouldCollectTest) { 222 // Do nothing 223 } 224 225 @Override addIncludeFilter(String filter)226 public void addIncludeFilter(String filter) {} 227 228 @Override addAllIncludeFilters(Set<String> filters)229 public void addAllIncludeFilters(Set<String> filters) {} 230 231 @Override addExcludeFilter(String filter)232 public void addExcludeFilter(String filter) { 233 mExcludeFilters.add(filter); 234 } 235 236 @Override addAllExcludeFilters(Set<String> filters)237 public void addAllExcludeFilters(Set<String> filters) { 238 mExcludeFilters.addAll(filters); 239 } 240 241 @Override clearIncludeFilters()242 public void clearIncludeFilters() {} 243 244 @Override getIncludeFilters()245 public Set<String> getIncludeFilters() { 246 return new HashSet<>(); 247 } 248 249 @Override getExcludeFilters()250 public Set<String> getExcludeFilters() { 251 return mExcludeFilters; 252 } 253 254 @Override clearExcludeFilters()255 public void clearExcludeFilters() { 256 mExcludeFilters.clear(); 257 } 258 259 @Override addIncludeAnnotation(String annotation)260 public void addIncludeAnnotation(String annotation) { 261 mIncludeAnnotationFilter.add(annotation); 262 } 263 264 @Override addExcludeAnnotation(String notAnnotation)265 public void addExcludeAnnotation(String notAnnotation) { 266 mExcludeAnnotationFilter.add(notAnnotation); 267 } 268 269 @Override addAllIncludeAnnotation(Set<String> annotations)270 public void addAllIncludeAnnotation(Set<String> annotations) { 271 mIncludeAnnotationFilter.addAll(annotations); 272 } 273 274 @Override addAllExcludeAnnotation(Set<String> notAnnotations)275 public void addAllExcludeAnnotation(Set<String> notAnnotations) { 276 mExcludeAnnotationFilter.addAll(notAnnotations); 277 } 278 279 @Override getIncludeAnnotations()280 public Set<String> getIncludeAnnotations() { 281 return mIncludeAnnotationFilter; 282 } 283 284 @Override getExcludeAnnotations()285 public Set<String> getExcludeAnnotations() { 286 return mExcludeAnnotationFilter; 287 } 288 289 @Override clearIncludeAnnotations()290 public void clearIncludeAnnotations() { 291 mIncludeAnnotationFilter.clear(); 292 } 293 294 @Override clearExcludeAnnotations()295 public void clearExcludeAnnotations() { 296 mExcludeAnnotationFilter.clear(); 297 } 298 } 299