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