Home | History | Annotate | Download | only in targetprep
      1 /*
      2  * Copyright (C) 2012 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.tradefed.targetprep;
     18 
     19 import com.google.common.io.Files;
     20 
     21 import com.android.tradefed.build.BuildInfo;
     22 import com.android.tradefed.build.DeviceBuildInfo;
     23 import com.android.tradefed.device.DeviceNotAvailableException;
     24 import com.android.tradefed.device.ITestDevice;
     25 import com.android.tradefed.util.FakeTestsZipFolder;
     26 import com.android.tradefed.util.FakeTestsZipFolder.ItemType;
     27 import com.android.tradefed.util.FileUtil;
     28 
     29 import junit.framework.TestCase;
     30 
     31 import org.easymock.EasyMock;
     32 import org.easymock.IAnswer;
     33 
     34 import java.io.File;
     35 import java.util.ArrayList;
     36 import java.util.HashMap;
     37 import java.util.List;
     38 import java.util.Map;
     39 
     40 /**
     41  * A test for {@link TestFilePushSetup}.
     42  */
     43 public class TestFilePushSetupTest extends TestCase {
     44 
     45     private Map<String, ItemType> mFiles;
     46     private List<String> mDeviceLocationList;
     47     private FakeTestsZipFolder mFakeTestsZipFolder;
     48     private File mAltDirFile1, mAltDirFile2;
     49     private static final String ALT_FILENAME1 = "foobar";
     50     private static final String ALT_FILENAME2 = "barfoo";
     51 
     52     private ITestDevice mMockDevice;
     53 
     54     @Override
     55     protected void setUp() throws Exception {
     56         super.setUp();
     57         mFiles = new HashMap<String, ItemType>();
     58         mFiles.put("app/AndroidCommonTests.apk", ItemType.FILE);
     59         mFiles.put("app/GalleryTests.apk", ItemType.FILE);
     60         mFiles.put("testinfo", ItemType.DIRECTORY);
     61         mFiles.put(ALT_FILENAME1, ItemType.FILE);
     62         mFakeTestsZipFolder = new FakeTestsZipFolder(mFiles);
     63         assertTrue(mFakeTestsZipFolder.createItems());
     64         mDeviceLocationList = new ArrayList<String>();
     65         for (String file : mFiles.keySet()) {
     66             mDeviceLocationList.add(TestFilePushSetup.getDevicePathFromUserData(file));
     67         }
     68         File tmpBase = Files.createTempDir();
     69         mAltDirFile1 = new File(tmpBase, ALT_FILENAME1);
     70         assertTrue("failed to create temp file", mAltDirFile1.createNewFile());
     71         mAltDirFile2 = new File(tmpBase, ALT_FILENAME2);
     72         assertTrue("failed to create temp file", mAltDirFile2.createNewFile());
     73         mMockDevice = EasyMock.createMock(ITestDevice.class);
     74         EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn("SERIAL");
     75     }
     76 
     77     @Override
     78     protected void tearDown() throws Exception {
     79         mFakeTestsZipFolder.cleanUp();
     80         File tmpDir = mAltDirFile1.getParentFile();
     81         FileUtil.deleteFile(mAltDirFile1);
     82         FileUtil.deleteFile(mAltDirFile2);
     83         FileUtil.recursiveDelete(tmpDir);
     84         super.tearDown();
     85     }
     86 
     87     public void testSetup() throws TargetSetupError, BuildError, DeviceNotAvailableException {
     88         TestFilePushSetup testFilePushSetup = new TestFilePushSetup();
     89         DeviceBuildInfo stubBuild = new DeviceBuildInfo("0", "stub");
     90         stubBuild.setTestsDir(mFakeTestsZipFolder.getBasePath(), "0");
     91         assertFalse(mFiles.isEmpty());
     92         assertFalse(mDeviceLocationList.isEmpty());
     93         ITestDevice device = EasyMock.createMock(ITestDevice.class);
     94         EasyMock.expect(device.pushDir((File) EasyMock.anyObject(), (String) EasyMock.anyObject()))
     95                 .andAnswer(new IAnswer<Boolean>() {
     96                     @Override
     97                     public Boolean answer() throws Throwable {
     98                         return mDeviceLocationList.remove(EasyMock.getCurrentArguments()[1]);
     99                     }
    100                 });
    101         EasyMock.expect(device.pushFile((File) EasyMock.anyObject(),
    102                 (String) EasyMock.anyObject())).andAnswer(new IAnswer<Boolean>() {
    103                     @Override
    104                     public Boolean answer() throws Throwable {
    105                         return mDeviceLocationList.remove(EasyMock.getCurrentArguments()[1]);
    106                     }
    107                 }).times(3);
    108         EasyMock.expect(device.executeShellCommand((String) EasyMock.anyObject()))
    109                 .andReturn("").times(4);
    110         EasyMock.replay(device);
    111         for (String file : mFiles.keySet()) {
    112             testFilePushSetup.addTestFileName(file);
    113         }
    114         testFilePushSetup.setUp(device, stubBuild);
    115         assertTrue(mDeviceLocationList.isEmpty());
    116         EasyMock.verify(device);
    117     }
    118 
    119     /**
    120      * Test that setup throws an exception if provided with something else than DeviceBuildInfo
    121      */
    122     public void testSetup_notDeviceBuildInfo() throws Exception {
    123         TestFilePushSetup testFilePushSetup = new TestFilePushSetup();
    124         BuildInfo stubBuild = new BuildInfo("stub", "stub");
    125         try {
    126             testFilePushSetup.setUp(EasyMock.createMock(ITestDevice.class), stubBuild);
    127             fail("should have thrown an exception");
    128         } catch (IllegalArgumentException expected) {
    129             assertEquals("Provided buildInfo is not a com.android.tradefed.build.IDeviceBuildInfo",
    130                     expected.getMessage());
    131         }
    132     }
    133 
    134     /**
    135      * Test that the artifact path resolution logic correctly uses alt dir as override
    136      */
    137     public void testAltDirOverride() throws Exception {
    138         TestFilePushSetup setup = new TestFilePushSetup();
    139         setup.setAltDirBehavior(AltDirBehavior.OVERRIDE);
    140         DeviceBuildInfo info = new DeviceBuildInfo();
    141         info.setTestsDir(mFakeTestsZipFolder.getBasePath(), "0");
    142         setup.setAltDir(mAltDirFile1.getParentFile());
    143         File apk = setup.getLocalPathForFilename(info, ALT_FILENAME1, mMockDevice);
    144         assertEquals(mAltDirFile1.getAbsolutePath(), apk.getAbsolutePath());
    145     }
    146 
    147     /**
    148      * Test that the artifact path resolution logic correctly throws if alt dir behavior is invalid
    149      */
    150     public void testAltDirOverride_null() throws Exception {
    151         TestFilePushSetup setup = new TestFilePushSetup();
    152         setup.setAltDirBehavior(null);
    153         DeviceBuildInfo info = new DeviceBuildInfo();
    154         try {
    155             setup.getLocalPathForFilename(info, ALT_FILENAME1, mMockDevice);
    156             fail("Should have thrown an exception");
    157         } catch (TargetSetupError expected) {
    158             assertEquals("Missing handler for alt-dir-behavior: null null", expected.getMessage());
    159         }
    160     }
    161 
    162     /**
    163      * Test that the artifact path resolution logic correctly throws if alt dir is empty.
    164      */
    165     public void testAltDirOverride_empty() throws Exception {
    166         TestFilePushSetup setup = new TestFilePushSetup();
    167         setup.setAltDirBehavior(AltDirBehavior.OVERRIDE);
    168         DeviceBuildInfo info = new DeviceBuildInfo();
    169         try {
    170             setup.getLocalPathForFilename(info, ALT_FILENAME1, mMockDevice);
    171             fail("Should have thrown an exception");
    172         } catch (TargetSetupError expected) {
    173             assertEquals("Provided buildInfo does not contain a valid tests directory and no "
    174                     + "alternative directories were provided null", expected.getMessage());
    175         }
    176     }
    177 
    178     /**
    179      * Test that the artifact path resolution logic correctly favors the one in test dir
    180      */
    181     public void testAltDirNoFallback() throws Exception {
    182         TestFilePushSetup setup = new TestFilePushSetup();
    183         DeviceBuildInfo info = new DeviceBuildInfo();
    184         info.setTestsDir(mFakeTestsZipFolder.getBasePath(), "0");
    185         setup.setAltDir(mAltDirFile1.getParentFile());
    186         File apk = setup.getLocalPathForFilename(info, ALT_FILENAME1, mMockDevice);
    187         File apkInTestDir = new File(
    188                 new File(mFakeTestsZipFolder.getBasePath(), "DATA"), ALT_FILENAME1);
    189         assertEquals(apkInTestDir.getAbsolutePath(), apk.getAbsolutePath());
    190     }
    191 
    192     /**
    193      * Test that the artifact path resolution logic correctly falls back to alt dir
    194      */
    195     public void testAltDirFallback() throws Exception {
    196         TestFilePushSetup setup = new TestFilePushSetup();
    197         DeviceBuildInfo info = new DeviceBuildInfo();
    198         info.setTestsDir(mFakeTestsZipFolder.getBasePath(), "0");
    199         setup.setAltDir(mAltDirFile2.getParentFile());
    200         File apk = setup.getLocalPathForFilename(info, ALT_FILENAME2, mMockDevice);
    201         assertEquals(mAltDirFile2.getAbsolutePath(), apk.getAbsolutePath());
    202     }
    203 
    204     /**
    205      * Test that an exception is thrown if the file doesn't exist in extracted test dir
    206      */
    207     public void testThrowIfNotFound() throws Exception {
    208         TestFilePushSetup setup = new TestFilePushSetup();
    209         setup.setThrowIfNoFile(true);
    210         // Assuming that the "file-not-in-test-zip" file doesn't exist in the test zips folder.
    211         setup.addTestFileName("file-not-in-test-zip");
    212         DeviceBuildInfo stubBuild = new DeviceBuildInfo("0", "stub");
    213         stubBuild.setTestsDir(mFakeTestsZipFolder.getBasePath(), "0");
    214         try {
    215             setup.setUp(mMockDevice, stubBuild);
    216             fail("Should have thrown an exception");
    217         } catch (TargetSetupError expected) {
    218             assertEquals(
    219                     "Could not find test file file-not-in-test-zip "
    220                             + "directory in extracted tests.zip null",
    221                     expected.getMessage());
    222         }
    223     }
    224 
    225     /**
    226      * Test that no exception is thrown if the file doesn't exist in extracted test dir
    227      * given that the option "throw-if-not-found" is set to false.
    228      */
    229     public void testThrowIfNotFound_false() throws Exception {
    230         TestFilePushSetup setup = new TestFilePushSetup();
    231         setup.setThrowIfNoFile(false);
    232         // Assuming that the "file-not-in-test-zip" file doesn't exist in the test zips folder.
    233         setup.addTestFileName("file-not-in-test-zip");
    234         DeviceBuildInfo stubBuild = new DeviceBuildInfo("0", "stub");
    235         stubBuild.setTestsDir(mFakeTestsZipFolder.getBasePath(), "0");
    236         // test that it does not throw
    237         setup.setUp(mMockDevice, stubBuild);
    238     }
    239 }
    240