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