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.android.tradefed.build.BuildInfo;
     20 import com.android.tradefed.build.IBuildInfo;
     21 import com.android.tradefed.build.IDeviceBuildInfo;
     22 import com.android.tradefed.build.IKernelBuildInfo;
     23 import com.android.tradefed.build.KernelDeviceBuildInfo;
     24 import com.android.tradefed.command.remote.DeviceDescriptor;
     25 import com.android.tradefed.device.DeviceAllocationState;
     26 import com.android.tradefed.device.DeviceUnresponsiveException;
     27 import com.android.tradefed.device.ITestDevice;
     28 import com.android.tradefed.util.CommandResult;
     29 import com.android.tradefed.util.CommandStatus;
     30 import com.android.tradefed.util.FileUtil;
     31 import com.android.tradefed.util.IRunUtil;
     32 
     33 import junit.framework.TestCase;
     34 
     35 import org.easymock.Capture;
     36 import org.easymock.EasyMock;
     37 
     38 import java.io.File;
     39 import java.io.IOException;
     40 
     41 /**
     42  * Unit tests for {@link KernelFlashPreparer}.
     43  */
     44 public class KernelFlashPreparerTest extends TestCase {
     45 
     46     private ITestDevice mMockDevice;
     47     private IDeviceBuildInfo mMockDeviceBuildInfo;
     48     private IKernelBuildInfo mMockKernelBuildInfo;
     49     private IRunUtil mMockRunUtil;
     50     private KernelDeviceBuildInfo mBuildInfo;
     51     private KernelFlashPreparer mFlashPreparer;
     52     private File mKernel = null;
     53     private File mMkbootimg = null;
     54     private File mRamdisk = null;
     55     private File mBootImg = null;
     56 
     57     /**
     58      * {@inheritDoc}
     59      */
     60     @Override
     61     protected void setUp() throws Exception {
     62         super.setUp();
     63 
     64         mMkbootimg = File.createTempFile("mkbootimg", "");
     65         mRamdisk = File.createTempFile("ramdisk", ".img");
     66         mKernel = File.createTempFile("kernel", "");
     67         mBootImg = File.createTempFile("boot", ".img");
     68 
     69         mMockDevice = EasyMock.createMock(ITestDevice.class);
     70         EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn("serial");
     71         EasyMock.expect(mMockDevice.getBuildId()).andStubReturn("0");
     72         EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(null);
     73 
     74         mMockDeviceBuildInfo = EasyMock.createMock(IDeviceBuildInfo.class);
     75         EasyMock.expect(mMockDeviceBuildInfo.getRamdiskFile()).andStubReturn(mRamdisk);
     76         EasyMock.expect(mMockDeviceBuildInfo.getMkbootimgFile()).andStubReturn(mMkbootimg);
     77 
     78         mMockKernelBuildInfo = EasyMock.createMock(IKernelBuildInfo.class);
     79         EasyMock.expect(mMockKernelBuildInfo.getKernelFile()).andStubReturn(mKernel);
     80         EasyMock.expect(mMockKernelBuildInfo.getSha1()).andStubReturn("sha1");
     81 
     82         mBuildInfo = new KernelDeviceBuildInfo("0", "build");
     83         mBuildInfo.setDeviceBuild(mMockDeviceBuildInfo);
     84         mBuildInfo.setKernelBuild(mMockKernelBuildInfo);
     85 
     86         mMockRunUtil = EasyMock.createMock(IRunUtil.class);
     87 
     88         mFlashPreparer = new KernelFlashPreparer() {
     89             @Override
     90             IRunUtil getRunUtil() {
     91                 return mMockRunUtil;
     92             }
     93 
     94             @Override
     95             String getBootImgPath() {
     96                 return mBootImg.getAbsolutePath();
     97             }
     98         };
     99     }
    100 
    101     /**
    102      * {@inheritDoc}
    103      */
    104     @Override
    105     protected void tearDown() throws Exception {
    106         super.tearDown();
    107 
    108         FileUtil.deleteFile(mKernel);
    109         FileUtil.deleteFile(mMkbootimg);
    110         FileUtil.deleteFile(mRamdisk);
    111         FileUtil.deleteFile(mBootImg);
    112     }
    113 
    114     /**
    115      * Test {@link KernelFlashPreparer#setUp(ITestDevice, com.android.tradefed.build.IBuildInfo)} in
    116      * the normal execution path.
    117      * <p>
    118      * Expect for the boot image to be created, the device to be booted into the bootloader,
    119      * flashed, rebooted, and the boot image to be deleted.
    120      * </p>
    121      */
    122     public void testSetUp() throws Exception {
    123         mFlashPreparer = getStubBootPreparer();
    124 
    125         mMockDevice.rebootIntoBootloader();
    126         EasyMock.expect(mMockDevice.executeFastbootCommand("flash", "boot",
    127                 mBootImg.getAbsolutePath())).andReturn(new CommandResult());
    128         mMockDevice.reboot();
    129         mMockDevice.postBootSetup();
    130 
    131         EasyMock.replay(mMockDevice, mMockDeviceBuildInfo, mMockKernelBuildInfo);
    132         mFlashPreparer.setUp(mMockDevice, mBuildInfo);
    133         EasyMock.verify(mMockDevice, mMockDeviceBuildInfo, mMockKernelBuildInfo);
    134 
    135         assertFalse(mBootImg.exists());
    136     }
    137 
    138     /**
    139      * Test {@link KernelFlashPreparer#setUp(ITestDevice, com.android.tradefed.build.IBuildInfo)} if
    140      * the boot image cannot be created
    141      * <p>
    142      * Expect a {@link TargetSetupError} to be thrown.
    143      * </p>
    144      */
    145     public void testSetup_targetsetuperror() throws Exception {
    146         mFlashPreparer = new KernelFlashPreparer() {
    147             @Override
    148             File createBootImage(File mkbootimg, File kernel, File ramdisk) throws IOException {
    149                 throw new IOException();
    150             }
    151         };
    152 
    153         EasyMock.replay(mMockDevice, mMockDeviceBuildInfo, mMockKernelBuildInfo);
    154         try {
    155             mFlashPreparer.setUp(mMockDevice, mBuildInfo);
    156             fail("Expected TargetSetupError");
    157         } catch (TargetSetupError e) {
    158             // Expected
    159         }
    160         EasyMock.verify(mMockDevice, mMockDeviceBuildInfo, mMockKernelBuildInfo);
    161     }
    162 
    163     /**
    164      * Test {@link KernelFlashPreparer#setUp(ITestDevice, com.android.tradefed.build.IBuildInfo)} if
    165      * the device is unresponsive in fastboot.
    166      * <p>
    167      * Expect for the boot image to be created, a {@link DeviceUnresponsiveException} to be thrown,
    168      * and the boot image to be deleted.
    169      * </p>
    170      */
    171     public void testSetup_fastbooterror() throws Exception {
    172         mFlashPreparer = getStubBootPreparer();
    173 
    174         mMockDevice.rebootIntoBootloader();
    175         EasyMock.expect(mMockDevice.executeFastbootCommand("flash", "boot",
    176                 mBootImg.getAbsolutePath())).andThrow(new DeviceUnresponsiveException());
    177 
    178         EasyMock.replay(mMockDevice, mMockDeviceBuildInfo, mMockKernelBuildInfo);
    179         try {
    180             mFlashPreparer.setUp(mMockDevice, mBuildInfo);
    181             fail("Expected DeviceUnresponsiveException");
    182         } catch (DeviceUnresponsiveException e) {
    183             // Expected
    184         }
    185         EasyMock.verify(mMockDevice, mMockDeviceBuildInfo, mMockKernelBuildInfo);
    186 
    187         assertFalse(mBootImg.exists());
    188     }
    189 
    190     /**
    191      * Test {@link KernelFlashPreparer#setUp(ITestDevice, com.android.tradefed.build.IBuildInfo)} if
    192      * the device is unresponsive at boot.
    193      * <p>
    194      * Expect for the boot image to be created, the device to be booted into the bootloader,
    195      * flashed, a {@link BuildError} to be thrown, and the boot image to be deleted.
    196      * </p>
    197      */
    198     public void testSetup_builderror() throws Exception {
    199         mFlashPreparer = getStubBootPreparer();
    200 
    201         mMockDevice.rebootIntoBootloader();
    202         EasyMock.expect(mMockDevice.executeFastbootCommand("flash", "boot",
    203                 mBootImg.getAbsolutePath())).andReturn(new CommandResult());
    204         mMockDevice.reboot();
    205         EasyMock.expectLastCall().andThrow(new DeviceUnresponsiveException());
    206         EasyMock.expect(mMockDevice.getDeviceDescriptor()).andReturn(new DeviceDescriptor("SERIAL",
    207                 false, DeviceAllocationState.Available, "unknown", "unknown", "unknown", "unknown",
    208                 "unknown"));
    209         EasyMock.replay(mMockDevice, mMockDeviceBuildInfo, mMockKernelBuildInfo);
    210         try {
    211             mFlashPreparer.setUp(mMockDevice, mBuildInfo);
    212             fail("Expected BuildError");
    213         } catch (BuildError e) {
    214             // Expected
    215         }
    216         EasyMock.verify(mMockDevice, mMockDeviceBuildInfo, mMockKernelBuildInfo);
    217 
    218         assertFalse(mBootImg.exists());
    219     }
    220 
    221     /**
    222      * Test {@link KernelFlashPreparer#setUp(ITestDevice, com.android.tradefed.build.IBuildInfo)} if
    223      * the device boots successfully but is unresponsive post boot.
    224      * <p>
    225      * Expect for the boot image to be created, the device to be booted into the bootloader,
    226      * flashed, and rebooted, a {@link DeviceUnresponsiveException} to be thrown, and the boot image
    227      * to be deleted.
    228      * </p>
    229      */
    230     public void testSetup_unresponsive() throws Exception {
    231         mFlashPreparer = getStubBootPreparer();
    232 
    233         mMockDevice.rebootIntoBootloader();
    234         EasyMock.expect(mMockDevice.executeFastbootCommand("flash", "boot",
    235                 mBootImg.getAbsolutePath())).andReturn(new CommandResult());
    236         mMockDevice.reboot();
    237         mMockDevice.postBootSetup();
    238         EasyMock.expectLastCall().andThrow(new DeviceUnresponsiveException());
    239 
    240         EasyMock.replay(mMockDevice, mMockDeviceBuildInfo, mMockKernelBuildInfo);
    241         try {
    242             mFlashPreparer.setUp(mMockDevice, mBuildInfo);
    243             fail("Expected DeviceUnresponsiveException");
    244         } catch (DeviceUnresponsiveException e) {
    245             // Expected
    246         }
    247         EasyMock.verify(mMockDevice, mMockDeviceBuildInfo, mMockKernelBuildInfo);
    248 
    249         assertFalse(mBootImg.exists());
    250     }
    251 
    252     /**
    253      * Test {@link KernelFlashPreparer#setUp(ITestDevice, com.android.tradefed.build.IBuildInfo)} if
    254      * passed an {@link IBuildInfo} object that is not a {@link KernelDeviceBuildInfo}.
    255      */
    256     public void testSetup_invalidbuildinfo() throws Exception {
    257         mFlashPreparer = new KernelFlashPreparer();
    258         try {
    259             mFlashPreparer.setUp(null, new BuildInfo());
    260             fail("Expected IllegalArgumentException");
    261         } catch (IllegalArgumentException e) {
    262             // Expected
    263         }
    264     }
    265 
    266     /**
    267      * Get a {@link KernelFlashPreparer} which returns {@code mBootImg} when
    268      * {@code createBootImage()} is called.
    269      */
    270     private KernelFlashPreparer getStubBootPreparer() {
    271         return new KernelFlashPreparer() {
    272             @Override
    273             File createBootImage(File mkbootimg, File kernel, File ramdisk) {
    274                 return mBootImg;
    275             }
    276         };
    277     }
    278 
    279     /**
    280      * Test {@link KernelFlashPreparer#createBootImage(File, File, File)} in the normal execution
    281      * path.
    282      */
    283     public void testCreateBootImage() throws IOException {
    284         Capture<String> bootimgPath = new Capture<String>();
    285         EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyInt(),
    286                 EasyMock.eq(mMkbootimg.getAbsolutePath()), EasyMock.eq("--kernel"),
    287                 EasyMock.eq(mKernel.getAbsolutePath()), EasyMock.eq("--ramdisk"),
    288                 EasyMock.eq(mRamdisk.getAbsolutePath()), EasyMock.eq("-o"),
    289                 EasyMock.capture(bootimgPath))).andReturn(new CommandResult(CommandStatus.SUCCESS));
    290 
    291         EasyMock.replay(mMockRunUtil);
    292 
    293         mBootImg = mFlashPreparer.createBootImage(mMkbootimg, mKernel, mRamdisk);
    294         assertEquals(mBootImg.getAbsolutePath(), bootimgPath.getValue());
    295         assertTrue(mBootImg.exists());
    296         assertTrue(mBootImg.isFile());
    297         EasyMock.verify(mMockRunUtil);
    298     }
    299 
    300     /**
    301      * Test {@link KernelFlashPreparer#createBootImage(File, File, File)} if mkbootimg fails.
    302      */
    303     public void testCreateBootImage_failed() {
    304         Capture<String> bootimgPath = new Capture<String>();
    305         EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyInt(),
    306                 EasyMock.eq(mMkbootimg.getAbsolutePath()), EasyMock.eq("--kernel"),
    307                 EasyMock.eq(mKernel.getAbsolutePath()), EasyMock.eq("--ramdisk"),
    308                 EasyMock.eq(mRamdisk.getAbsolutePath()), EasyMock.eq("-o"),
    309                 EasyMock.capture(bootimgPath))).andReturn(new CommandResult(CommandStatus.FAILED));
    310 
    311 
    312         EasyMock.replay(mMockRunUtil);
    313 
    314         try {
    315             mBootImg = mFlashPreparer.createBootImage(mMkbootimg, mKernel, mRamdisk);
    316             fail("Expected IOException");
    317         } catch (IOException e) {
    318             // Expected.
    319         }
    320         assertEquals(mBootImg.getAbsolutePath(), bootimgPath.getValue());
    321         assertFalse(mBootImg.exists());
    322 
    323         EasyMock.verify(mMockRunUtil);
    324     }
    325 
    326     /**
    327      * Test {@link KernelFlashPreparer#createBootImage(File, File, File)} if there was a
    328      * Runtime.
    329      * <p>
    330      * Expect that the boot image is deleted.
    331      * </p>
    332      */
    333     public void testCreateBootImage_runtimeexception() throws IOException {
    334         Capture<String> bootimgPath = new Capture<String>();
    335         EasyMock.expect(mMockRunUtil.runTimedCmd(EasyMock.anyInt(),
    336                 EasyMock.eq(mMkbootimg.getAbsolutePath()), EasyMock.eq("--kernel"),
    337                 EasyMock.eq(mKernel.getAbsolutePath()), EasyMock.eq("--ramdisk"),
    338                 EasyMock.eq(mRamdisk.getAbsolutePath()), EasyMock.eq("-o"),
    339                 EasyMock.capture(bootimgPath))).andThrow(new RuntimeException());
    340 
    341         EasyMock.replay(mMockRunUtil);
    342 
    343         try {
    344             mBootImg = mFlashPreparer.createBootImage(mMkbootimg, mKernel, mRamdisk);
    345             fail("Expected RuntimeException");
    346         } catch (RuntimeException e) {
    347             // Expected.
    348         }
    349         assertEquals(mBootImg.getAbsolutePath(), bootimgPath.getValue());
    350         assertFalse(mBootImg.exists());
    351 
    352         EasyMock.verify(mMockRunUtil);
    353     }
    354 }
    355