1 /* 2 * Copyright (C) 2015 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 17 package com.android.compatibility.common.tradefed.build; 18 19 import com.android.tradefed.build.IBuildInfo; 20 import com.android.tradefed.config.OptionSetter; 21 import com.android.tradefed.util.FileUtil; 22 23 import junit.framework.TestCase; 24 25 import java.io.File; 26 import java.io.FileNotFoundException; 27 28 public class CompatibilityBuildHelperTest extends TestCase { 29 30 private static final String ROOT_PROPERTY = "TESTS_ROOT"; 31 private static final String BUILD_NUMBER = "2"; 32 private static final String SUITE_NAME = "TESTS"; 33 private static final String SUITE_FULL_NAME = "Compatibility Tests"; 34 private static final String SUITE_VERSION = "1"; 35 private static final String SUITE_PLAN = "cts"; 36 private static final String DYNAMIC_CONFIG_URL = ""; 37 private static final String ROOT_DIR_NAME = "root"; 38 private static final String BASE_DIR_NAME = "android-tests"; 39 private static final String TESTCASES = "testcases"; 40 private static final String COMMAND_LINE_ARGS = "cts -m CtsModuleTestCases"; 41 private static final String BUSINESS_LOGIC_HOST_FILE = "BUSINESS_LOGIC_HOST_FILE"; 42 43 private File mRoot = null; 44 private File mBase = null; 45 private File mTests = null; 46 private IBuildInfo mBuild; 47 private CompatibilityBuildHelper mHelper; 48 49 @Override setUp()50 public void setUp() throws Exception { 51 mRoot = FileUtil.createTempDir(ROOT_DIR_NAME); 52 setProperty(mRoot.getAbsolutePath()); 53 CompatibilityBuildProvider provider = new CompatibilityBuildProvider() { 54 @Override 55 protected String getSuiteInfoName() { 56 return SUITE_NAME; 57 } 58 @Override 59 protected String getSuiteInfoBuildNumber() { 60 return BUILD_NUMBER; 61 } 62 @Override 63 protected String getSuiteInfoFullname() { 64 return SUITE_FULL_NAME; 65 } 66 @Override 67 protected String getSuiteInfoVersion() { 68 return SUITE_VERSION; 69 } 70 }; 71 OptionSetter setter = new OptionSetter(provider); 72 setter.setOptionValue("plan", SUITE_PLAN); 73 setter.setOptionValue("dynamic-config-url", DYNAMIC_CONFIG_URL); 74 mBuild = provider.getBuild(); 75 mHelper = new CompatibilityBuildHelper(mBuild); 76 } 77 78 @Override tearDown()79 public void tearDown() throws Exception { 80 setProperty(null); 81 FileUtil.recursiveDelete(mRoot); 82 mRoot = null; 83 mBase = null; 84 mTests = null; 85 } 86 createDirStructure()87 private void createDirStructure() { 88 mBase = new File(mRoot, BASE_DIR_NAME); 89 mBase.mkdirs(); 90 mTests = new File(mBase, TESTCASES); 91 mTests.mkdirs(); 92 } 93 testSuiteInfoLoad()94 public void testSuiteInfoLoad() throws Exception { 95 setProperty(mRoot.getAbsolutePath()); 96 assertEquals("Incorrect suite build number", BUILD_NUMBER, mHelper.getSuiteBuild()); 97 assertEquals("Incorrect suite name", SUITE_NAME, mHelper.getSuiteName()); 98 assertEquals("Incorrect suite full name", SUITE_FULL_NAME, mHelper.getSuiteFullName()); 99 assertEquals("Incorrect suite version", SUITE_VERSION, mHelper.getSuiteVersion()); 100 } 101 102 /** 103 * Test loading of CTS_ROOT is it exists or not. 104 */ testProperty()105 public void testProperty() throws Exception { 106 setProperty(null); 107 CompatibilityBuildProvider provider = new CompatibilityBuildProvider() { 108 @Override 109 protected String getSuiteInfoName() { 110 return SUITE_NAME; 111 } 112 }; 113 IBuildInfo info = null; 114 File rootDir = null; 115 try { 116 OptionSetter setter = new OptionSetter(provider); 117 setter.setOptionValue("plan", SUITE_PLAN); 118 setter.setOptionValue("dynamic-config-url", DYNAMIC_CONFIG_URL); 119 info = provider.getBuild(); 120 rootDir = new CompatibilityBuildHelper(info).getRootDir(); 121 assertNotNull(info.getBuildAttributes().get("ROOT_DIR")); 122 } finally { 123 provider.cleanUp(info); 124 } 125 setProperty(mRoot.getAbsolutePath()); 126 // Shouldn't fail with root set 127 CompatibilityBuildHelper helper = new CompatibilityBuildHelper(provider.getBuild()); 128 // If the root dir property is set then we use it. 129 assertFalse(helper.getRootDir().equals(rootDir)); 130 } 131 testValidation()132 public void testValidation() throws Exception { 133 setProperty(mRoot.getAbsolutePath()); 134 try { 135 mHelper.getDir(); 136 fail("Build helper validation succeeded on an invalid installation"); 137 } catch (FileNotFoundException e) { 138 // Expected 139 } 140 createDirStructure(); 141 try { 142 mHelper.getTestsDir(); 143 } catch (IllegalArgumentException e) { 144 e.printStackTrace(); 145 fail("Build helper validation failed on a valid installation"); 146 } 147 } 148 testDirs()149 public void testDirs() throws Exception { 150 setProperty(mRoot.getAbsolutePath()); 151 createDirStructure(); 152 assertNotNull(mRoot); 153 assertNotNull(mBuild); 154 assertNotNull(mHelper.getRootDir()); 155 assertEquals("Incorrect root dir", mRoot.getAbsolutePath(), 156 mHelper.getRootDir().getAbsolutePath()); 157 assertEquals("Incorrect base dir", mBase.getAbsolutePath(), 158 mHelper.getDir().getAbsolutePath()); 159 assertEquals("Incorrect logs dir", new File(mBase, "logs").getAbsolutePath(), 160 mHelper.getLogsDir().getAbsolutePath()); 161 assertEquals("Incorrect tests dir", mTests.getAbsolutePath(), 162 mHelper.getTestsDir().getAbsolutePath()); 163 assertEquals("Incorrect results dir", new File(mBase, "results").getAbsolutePath(), 164 mHelper.getResultsDir().getAbsolutePath()); 165 } 166 testGetCommandLineArgs()167 public void testGetCommandLineArgs() { 168 assertNull(mHelper.getCommandLineArgs()); 169 mBuild.addBuildAttribute("command_line_args", COMMAND_LINE_ARGS); 170 assertEquals(COMMAND_LINE_ARGS, mHelper.getCommandLineArgs()); 171 172 mBuild.addBuildAttribute("command_line_args", "cts --retry 0"); 173 mHelper.setRetryCommandLineArgs(COMMAND_LINE_ARGS); 174 assertEquals(COMMAND_LINE_ARGS, mHelper.getCommandLineArgs()); 175 } 176 testSetModuleIds()177 public void testSetModuleIds() { 178 mHelper.setModuleIds(new String[] {"module1", "module2"}); 179 180 assertEquals("module1,module2", 181 mBuild.getBuildAttributes().get(CompatibilityBuildHelper.MODULE_IDS)); 182 } 183 184 /** 185 * Test that adding dynamic config to be tracked for reporting is backed by {@link File} 186 * references and not absolute path. When sharding, path are invalidated but Files are copied. 187 */ testAddDynamicFiles()188 public void testAddDynamicFiles() throws Exception { 189 createDirStructure(); 190 File tmpDynamicFile = FileUtil.createTempFile("cts-test-file", ".dynamic"); 191 FileUtil.writeToFile("test string", tmpDynamicFile); 192 try { 193 mHelper.addDynamicConfigFile("CtsModuleName", tmpDynamicFile); 194 File currentDynamicFile = mHelper.getDynamicConfigFiles().get("CtsModuleName"); 195 assertNotNull(currentDynamicFile); 196 assertEquals(tmpDynamicFile, currentDynamicFile); 197 // In case of sharding the underlying build info will be cloned, and old build cleaned. 198 IBuildInfo clone = mBuild.clone(); 199 try { 200 mBuild.cleanUp(); 201 // With cleanup the current dynamic file tracked are cleaned 202 assertFalse(currentDynamicFile.exists()); 203 CompatibilityBuildHelper helperShard = new CompatibilityBuildHelper(clone); 204 File newDynamicFile = helperShard.getDynamicConfigFiles().get("CtsModuleName"); 205 assertNotNull(newDynamicFile); 206 // the cloned build has the infos but backed by other file 207 assertFalse( 208 tmpDynamicFile.getAbsolutePath().equals(newDynamicFile.getAbsolutePath())); 209 // content has also followed. 210 assertEquals("test string", FileUtil.readStringFromFile(newDynamicFile)); 211 } finally { 212 clone.cleanUp(); 213 } 214 } finally { 215 FileUtil.deleteFile(tmpDynamicFile); 216 } 217 } 218 219 /** 220 * Test setting business logic host file. When sharding, the contents are the same. 221 */ testSetBusinessLogicHostFile()222 public void testSetBusinessLogicHostFile() throws Exception { 223 File tmpBLFile = FileUtil.createTempFile("businesslogic-test-file", ".bl"); 224 FileUtil.writeToFile("test string", tmpBLFile); 225 try { 226 mHelper.setBusinessLogicHostFile(tmpBLFile); 227 File currentBLFile = mHelper.getBusinessLogicHostFile(); 228 assertNotNull(currentBLFile); 229 assertEquals(tmpBLFile, currentBLFile); 230 // In case of sharding the underlying build info will be cloned, and old build cleaned. 231 IBuildInfo clone = mBuild.clone(); 232 try { 233 CompatibilityBuildHelper helperShard = new CompatibilityBuildHelper(clone); 234 File newBLFile = helperShard.getBusinessLogicHostFile(); 235 assertNotNull(newBLFile); 236 // content has also followed. 237 assertEquals("test string", FileUtil.readStringFromFile(newBLFile)); 238 } finally { 239 clone.cleanUp(); 240 } 241 } finally { 242 FileUtil.deleteFile(tmpBLFile); 243 } 244 } 245 246 /** 247 * Test setting business logic host file with name. When sharding, the contents are the same. 248 */ testSetBusinessLogicHostFileWithModuleId()249 public void testSetBusinessLogicHostFileWithModuleId() throws Exception { 250 File tmpBLFile = FileUtil.createTempFile("businesslogic-test-file", ".bl"); 251 FileUtil.writeToFile("test string", tmpBLFile); 252 try { 253 String moduleId = "64MODULE1"; 254 mHelper.setBusinessLogicHostFile(tmpBLFile, moduleId); 255 File currentBLFile = mHelper.getBusinessLogicHostFile(moduleId); 256 assertNotNull(currentBLFile); 257 assertEquals(tmpBLFile, currentBLFile); 258 // In case of sharding the underlying build info will be cloned, and old build cleaned. 259 IBuildInfo clone = mBuild.clone(); 260 try { 261 CompatibilityBuildHelper helperShard = new CompatibilityBuildHelper(clone); 262 File newBLFile = helperShard.getBusinessLogicHostFile(moduleId); 263 assertNotNull(newBLFile); 264 // content has also followed. 265 assertEquals("test string", FileUtil.readStringFromFile(newBLFile)); 266 } finally { 267 clone.cleanUp(); 268 } 269 } finally { 270 FileUtil.deleteFile(tmpBLFile); 271 } 272 } 273 274 /** 275 * Test checking business logic host file. When sharding, files still exist. 276 */ testHasBusinessLogicHostFile()277 public void testHasBusinessLogicHostFile() throws Exception { 278 File tmpBLFile = FileUtil.createTempFile("businesslogic-test-file", ".bl"); 279 try { 280 mBuild.setFile(BUSINESS_LOGIC_HOST_FILE, tmpBLFile, tmpBLFile.getName()); 281 assertTrue(mHelper.hasBusinessLogicHostFile()); 282 // In case of sharding the underlying build info will be cloned, and old build cleaned. 283 IBuildInfo clone = mBuild.clone(); 284 try { 285 CompatibilityBuildHelper helperShard = new CompatibilityBuildHelper(clone); 286 assertTrue(helperShard.hasBusinessLogicHostFile()); 287 } finally { 288 clone.cleanUp(); 289 } 290 } finally { 291 FileUtil.deleteFile(tmpBLFile); 292 } 293 } 294 295 /** 296 * Test checking business logic host file with name. When sharding, files still exist. 297 */ testHasBusinessLogicHostFileModuleId()298 public void testHasBusinessLogicHostFileModuleId() throws Exception { 299 File tmpBLFile = FileUtil.createTempFile("businesslogic-test-file", ".bl"); 300 try { 301 String moduleId = "64MODULE1"; 302 mBuild.setFile(BUSINESS_LOGIC_HOST_FILE + moduleId, tmpBLFile, tmpBLFile.getName()); 303 assertTrue(mHelper.hasBusinessLogicHostFile(moduleId)); 304 // In case of sharding the underlying build info will be cloned, and old build cleaned. 305 IBuildInfo clone = mBuild.clone(); 306 try { 307 CompatibilityBuildHelper helperShard = new CompatibilityBuildHelper(clone); 308 assertTrue(helperShard.hasBusinessLogicHostFile(moduleId)); 309 } finally { 310 clone.cleanUp(); 311 } 312 } finally { 313 FileUtil.deleteFile(tmpBLFile); 314 } 315 } 316 317 /** 318 * Test getting business logic host file. When sharding, the contents are the same. 319 */ testGetBusinessLogicHostFile()320 public void testGetBusinessLogicHostFile() throws Exception { 321 File tmpBLFile = FileUtil.createTempFile("businesslogic-test-file", ".bl"); 322 FileUtil.writeToFile("test string", tmpBLFile); 323 try { 324 mBuild.setFile(BUSINESS_LOGIC_HOST_FILE, tmpBLFile, tmpBLFile.getName()); 325 File currentBLFile = mHelper.getBusinessLogicHostFile(); 326 assertNotNull(currentBLFile); 327 assertEquals(tmpBLFile, currentBLFile); 328 // In case of sharding the underlying build info will be cloned, and old build cleaned. 329 IBuildInfo clone = mBuild.clone(); 330 try { 331 CompatibilityBuildHelper helperShard = new CompatibilityBuildHelper(clone); 332 File newBLFile = helperShard.getBusinessLogicHostFile(); 333 assertNotNull(newBLFile); 334 // content has also followed. 335 assertEquals("test string", FileUtil.readStringFromFile(newBLFile)); 336 } finally { 337 clone.cleanUp(); 338 } 339 } finally { 340 FileUtil.deleteFile(tmpBLFile); 341 } 342 } 343 344 /** 345 * Test getting business logic host file with name. When sharding, the contents are the same. 346 */ testGetBusinessLogicHostFileWithModuleId()347 public void testGetBusinessLogicHostFileWithModuleId() throws Exception { 348 File tmpBLFile = FileUtil.createTempFile("businesslogic-test-file", ".bl"); 349 FileUtil.writeToFile("test string", tmpBLFile); 350 try { 351 String moduleId = "64MODULE1"; 352 mBuild.setFile(BUSINESS_LOGIC_HOST_FILE + moduleId, tmpBLFile, tmpBLFile.getName()); 353 File currentBLFile = mHelper.getBusinessLogicHostFile(moduleId); 354 assertNotNull(currentBLFile); 355 assertEquals(tmpBLFile, currentBLFile); 356 // In case of sharding the underlying build info will be cloned, and old build cleaned. 357 IBuildInfo clone = mBuild.clone(); 358 try { 359 CompatibilityBuildHelper helperShard = new CompatibilityBuildHelper(clone); 360 File newBLFile = helperShard.getBusinessLogicHostFile(moduleId); 361 assertNotNull(newBLFile); 362 // content has also followed. 363 assertEquals("test string", FileUtil.readStringFromFile(newBLFile)); 364 } finally { 365 clone.cleanUp(); 366 } 367 } finally { 368 FileUtil.deleteFile(tmpBLFile); 369 } 370 } 371 372 /** 373 * Sets the *_ROOT property of the build's installation location. 374 * 375 * @param value the value to set, or null to clear the property. 376 */ setProperty(String value)377 public static void setProperty(String value) { 378 if (value == null) { 379 System.clearProperty(ROOT_PROPERTY); 380 } else { 381 System.setProperty(ROOT_PROPERTY, value); 382 } 383 } 384 } 385