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 package com.android.cts.tradefed.result; 17 18 import com.android.cts.tradefed.build.CtsBuildHelper; 19 import com.android.cts.tradefed.testtype.CtsTest; 20 import com.android.cts.tradefed.testtype.ITestPackageDef; 21 import com.android.cts.tradefed.testtype.ITestPackageRepo; 22 import com.android.cts.tradefed.testtype.ITestPlan; 23 import com.android.cts.tradefed.testtype.TestPackageRepo; 24 import com.android.cts.tradefed.testtype.TestPlan; 25 import com.android.ddmlib.Log; 26 import com.android.ddmlib.Log.LogLevel; 27 import com.android.ddmlib.testrunner.TestIdentifier; 28 import com.android.tradefed.config.ConfigurationException; 29 import com.android.tradefed.config.Option; 30 import com.android.tradefed.config.Option.Importance; 31 import com.android.tradefed.log.LogUtil.CLog; 32 33 import java.io.BufferedOutputStream; 34 import java.io.File; 35 import java.io.FileNotFoundException; 36 import java.io.FileOutputStream; 37 import java.io.IOException; 38 import java.util.Collection; 39 import java.util.LinkedHashSet; 40 import java.util.Set; 41 42 /** 43 * Class for creating test plans from CTS result XML. 44 */ 45 public class PlanCreator { 46 47 @Option (name = "plan", shortName = 'p', description = "the name of the plan to create", 48 importance=Importance.IF_UNSET) 49 private String mPlanName = null; 50 51 @Option (name = "session", shortName = 's', description = "the session id to derive from", 52 importance=Importance.IF_UNSET) 53 private Integer mSessionId = null; 54 55 @Option (name = "result", shortName = 'r', 56 description = "the result type to filter. One of pass, fail, notExecuted.", 57 importance=Importance.IF_UNSET) 58 private String mResultFilterString = null; 59 60 @Option(name = CtsTest.RUN_KNOWN_FAILURES_OPTION) 61 private boolean mIncludeKnownFailures = false; 62 63 private CtsTestStatus mResultFilter = null; 64 private TestResults mResult = null; 65 66 private File mPlanFile; 67 68 /** 69 * Create an empty {@link PlanCreator}. 70 * <p/> 71 * All {@link Option} fields must be populated via 72 * {@link com.android.tradefed.config.ArgsOptionParser} 73 */ PlanCreator()74 public PlanCreator() { 75 } 76 77 /** 78 * Create a {@link PlanCreator} using the specified option values. 79 */ PlanCreator(String planName, int session, CtsTestStatus result)80 public PlanCreator(String planName, int session, CtsTestStatus result) { 81 mPlanName = planName; 82 mSessionId = session; 83 mResultFilterString = result.getValue(); 84 } 85 86 /** 87 * Create and serialize a test plan derived from a result. 88 * <p/> 89 * {@link Option} values must all be set before this is called. 90 * @throws ConfigurationException 91 */ createAndSerializeDerivedPlan(CtsBuildHelper build, Set<String> abis)92 public void createAndSerializeDerivedPlan(CtsBuildHelper build, Set<String> abis) 93 throws ConfigurationException { 94 ITestPlan derivedPlan = createDerivedPlan(build, abis); 95 if (derivedPlan != null) { 96 try { 97 derivedPlan.serialize(new BufferedOutputStream(new FileOutputStream(mPlanFile))); 98 } catch (IOException e) { 99 Log.logAndDisplay(LogLevel.ERROR, "", String.format("Failed to create plan file %s", 100 mPlanName)); 101 CLog.e(e); 102 } 103 } 104 } 105 106 /** 107 * Create a test plan derived from a result. 108 * <p/> 109 * {@link Option} values must all be set before this is called. 110 * 111 * @param build 112 * @return test plan 113 * @throws ConfigurationException 114 */ createDerivedPlan(CtsBuildHelper build, Set<String> abis)115 public ITestPlan createDerivedPlan(CtsBuildHelper build, Set<String> abis) 116 throws ConfigurationException { 117 checkFields(build); 118 ITestPackageRepo pkgDefRepo = 119 new TestPackageRepo(build.getTestCasesDir(), mIncludeKnownFailures); 120 ITestPlan derivedPlan = new TestPlan(mPlanName, abis); 121 for (TestPackageResult pkg : mResult.getPackages()) { 122 Collection<TestIdentifier> filteredTests = pkg.getTestsWithStatus(mResultFilter); 123 String pkgId = pkg.getId(); 124 ITestPackageDef pkgDef = pkgDefRepo.getTestPackage(pkgId); 125 if (pkgDef != null) { 126 Collection<TestIdentifier> excludedTests = 127 new LinkedHashSet<TestIdentifier>(pkgDef.getTests()); 128 excludedTests.removeAll(filteredTests); 129 derivedPlan.addPackage(pkgId); 130 derivedPlan.addExcludedTests(pkgId, excludedTests); 131 } else { 132 CLog.e("Could not find package %s in repository", pkgId); 133 } 134 } 135 return derivedPlan; 136 } 137 138 /** 139 * Check that all {@Option}s have been populated with valid values. 140 * @param build 141 * @throws ConfigurationException if any option has an invalid value 142 */ checkFields(CtsBuildHelper build)143 private void checkFields(CtsBuildHelper build) throws ConfigurationException { 144 if (mSessionId == null) { 145 throw new ConfigurationException("Missing --session argument"); 146 } 147 ITestResultRepo repo = new TestResultRepo(build.getResultsDir()); 148 mResult = repo.getResult(mSessionId); 149 if (mResult == null) { 150 throw new ConfigurationException(String.format("Could not find session with id %d", 151 mSessionId)); 152 } 153 if (mResultFilterString == null) { 154 throw new ConfigurationException("Missing --result argument"); 155 } 156 mResultFilter = CtsTestStatus.getStatus(mResultFilterString); 157 if (mResultFilter == null) { 158 throw new ConfigurationException( 159 "Invalid result argument. Expected one of pass,fail,notExecuted"); 160 } 161 if (mPlanName == null) { 162 throw new ConfigurationException("Missing --plan argument"); 163 } 164 try { 165 mPlanFile = build.getTestPlanFile(mPlanName); 166 if (mPlanFile.exists()) { 167 throw new ConfigurationException(String.format("Test plan %s already exists", 168 mPlanName)); 169 } 170 } catch (FileNotFoundException e) { 171 throw new ConfigurationException("Could not find plans directory"); 172 } 173 } 174 } 175