Home | History | Annotate | Download | only in targetprep
      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 
     17 package com.android.tradefed.targetprep;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNull;
     21 import static org.junit.Assert.fail;
     22 
     23 import com.android.tradefed.build.BuildInfoKey.BuildInfoFileKey;
     24 import com.android.tradefed.build.IBuildInfo;
     25 import com.android.tradefed.build.IDeviceBuildInfo;
     26 import com.android.tradefed.config.OptionSetter;
     27 import com.android.tradefed.device.ITestDevice;
     28 import com.android.tradefed.util.FileUtil;
     29 
     30 import org.easymock.EasyMock;
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 
     34 import java.io.File;
     35 
     36 /** Unit tests for {@link PushFilePreparer} */
     37 public class PushFilePreparerTest {
     38 
     39     private static final String HOST_TESTCASES = "host/testcases";
     40 
     41     private PushFilePreparer mPreparer = null;
     42     private ITestDevice mMockDevice = null;
     43     private OptionSetter mOptionSetter = null;
     44 
     45     @Before
     46     public void setUp() throws Exception {
     47         mMockDevice = EasyMock.createStrictMock(ITestDevice.class);
     48         EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(null);
     49         EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn("SERIAL");
     50         mPreparer = new PushFilePreparer();
     51         mOptionSetter = new OptionSetter(mPreparer);
     52     }
     53 
     54     /** When there's nothing to be done, expect no exception to be thrown */
     55     @Test
     56     public void testNoop() throws Exception {
     57         EasyMock.replay(mMockDevice);
     58         mPreparer.setUp(mMockDevice, null);
     59         EasyMock.verify(mMockDevice);
     60     }
     61 
     62     @Test
     63     public void testLocalNoExist() throws Exception {
     64         mOptionSetter.setOptionValue("push", "/noexist->/data/");
     65         mOptionSetter.setOptionValue("post-push", "ls /");
     66         EasyMock.replay(mMockDevice);
     67         try {
     68             // Should throw TargetSetupError and _not_ run any post-push command
     69             mPreparer.setUp(mMockDevice, null);
     70             fail("TargetSetupError not thrown");
     71         } catch (TargetSetupError e) {
     72             // expected
     73         }
     74         EasyMock.verify(mMockDevice);
     75     }
     76 
     77     @Test
     78     public void testRemoteNoExist() throws Exception {
     79         mOptionSetter.setOptionValue("push", "/bin/sh->/noexist/");
     80         mOptionSetter.setOptionValue("post-push", "ls /");
     81         // expect a pushFile() call and return false (failed)
     82         EasyMock.expect(
     83                 mMockDevice.pushFile((File)EasyMock.anyObject(), EasyMock.eq("/noexist/")))
     84                 .andReturn(Boolean.FALSE);
     85         EasyMock.replay(mMockDevice);
     86         try {
     87             // Should throw TargetSetupError and _not_ run any post-push command
     88             mPreparer.setUp(mMockDevice, null);
     89             fail("TargetSetupError not thrown");
     90         } catch (TargetSetupError e) {
     91             // expected
     92         }
     93         EasyMock.verify(mMockDevice);
     94     }
     95 
     96     @Test
     97     public void testWarnOnFailure() throws Exception {
     98         mOptionSetter.setOptionValue("push", "/bin/sh->/noexist/");
     99         mOptionSetter.setOptionValue("push", "/noexist->/data/");
    100         mOptionSetter.setOptionValue("post-push", "ls /");
    101         mOptionSetter.setOptionValue("abort-on-push-failure", "false");
    102 
    103         // expect a pushFile() call and return false (failed)
    104         EasyMock.expect(
    105                 mMockDevice.pushFile((File)EasyMock.anyObject(), EasyMock.eq("/noexist/")))
    106                 .andReturn(Boolean.FALSE);
    107         // Because we're only warning, the post-push command should be run despite the push failures
    108         EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.eq("ls /"))).andReturn("");
    109         EasyMock.replay(mMockDevice);
    110 
    111         // Don't expect any exceptions to be thrown
    112         mPreparer.setUp(mMockDevice, null);
    113         EasyMock.verify(mMockDevice);
    114     }
    115 
    116     /**
    117      * Test {@link PushFilePreparer#resolveRelativeFilePath(IBuildInfo, String)} do not search
    118      * additional tests directory if the given build if is not of IBuildInfo type.
    119      */
    120     @Test
    121     public void testResolveRelativeFilePath_noDeviceBuildInfo() {
    122         IBuildInfo buildInfo = EasyMock.createStrictMock(IBuildInfo.class);
    123         String fileName = "source_file";
    124         EasyMock.expect(buildInfo.getFile(fileName)).andReturn(null);
    125         EasyMock.replay(buildInfo);
    126 
    127         assertNull(mPreparer.resolveRelativeFilePath(buildInfo, fileName));
    128         EasyMock.verify(buildInfo);
    129     }
    130 
    131     /**
    132      * Test {@link PushFilePreparer#resolveRelativeFilePath(IBuildInfo, String)} can locate a source
    133      * file existed in tests directory of a device build.
    134      */
    135     @Test
    136     public void testResolveRelativeFilePath_withDeviceBuildInfo() throws Exception {
    137         IDeviceBuildInfo buildInfo = EasyMock.createStrictMock(IDeviceBuildInfo.class);
    138         String fileName = "source_file";
    139 
    140         File testsDir = null;
    141         try {
    142             testsDir = FileUtil.createTempDir("tests_dir");
    143             File hostTestCasesDir = FileUtil.getFileForPath(testsDir, HOST_TESTCASES);
    144             FileUtil.mkdirsRWX(hostTestCasesDir);
    145             File sourceFile = FileUtil.createTempFile(fileName, null, hostTestCasesDir);
    146 
    147             fileName = sourceFile.getName();
    148             EasyMock.expect(buildInfo.getFile(fileName)).andReturn(null);
    149             EasyMock.expect(buildInfo.getFile(BuildInfoFileKey.TARGET_LINKED_DIR)).andReturn(null);
    150             EasyMock.expect(buildInfo.getTestsDir()).andReturn(testsDir);
    151             EasyMock.replay(buildInfo);
    152 
    153             assertEquals(
    154                     sourceFile.getAbsolutePath(),
    155                     mPreparer.resolveRelativeFilePath(buildInfo, fileName).getAbsolutePath());
    156             EasyMock.verify(buildInfo);
    157         } finally {
    158             FileUtil.recursiveDelete(testsDir);
    159         }
    160     }
    161 }
    162 
    163