Home | History | Annotate | Download | only in targetprep
      1 /*
      2  * Copyright (C) 2017 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.ddmlib.AdbCommandRejectedException;
     20 import com.android.ddmlib.IDevice;
     21 import com.android.ddmlib.IShellOutputReceiver;
     22 import com.android.ddmlib.ShellCommandUnresponsiveException;
     23 import com.android.ddmlib.TimeoutException;
     24 import com.android.tradefed.build.IBuildInfo;
     25 import com.android.tradefed.config.ConfigurationException;
     26 import com.android.tradefed.config.OptionSetter;
     27 import com.android.tradefed.device.DeviceNotAvailableException;
     28 import com.android.tradefed.device.ITestDevice;
     29 import com.android.tradefed.device.TestDeviceState;
     30 import com.android.tradefed.util.CommandResult;
     31 import com.android.tradefed.util.CommandStatus;
     32 import com.android.tradefed.util.RunUtil;
     33 
     34 import org.easymock.EasyMock;
     35 import org.junit.Before;
     36 import org.junit.Test;
     37 
     38 import java.io.IOException;
     39 import java.util.concurrent.TimeUnit;
     40 
     41 
     42 /** Unit Tests for {@link RunCommandTargetPreparer} */
     43 public class RunCommandTargetPreparerTest {
     44 
     45     private static final int LONG_WAIT_TIME_MS = 200;
     46     private static final TestDeviceState ONLINE_STATE = TestDeviceState.ONLINE;
     47 
     48     private RunCommandTargetPreparer mPreparer = null;
     49     private ITestDevice mMockDevice = null;
     50     private IBuildInfo mMockBuildInfo = null;
     51 
     52     @Before
     53     public void setUp() throws Exception {
     54         mPreparer = new RunCommandTargetPreparer();
     55         OptionSetter setter = new OptionSetter(mPreparer);
     56         setter.setOptionValue("use-shell-v2", "true");
     57         mMockDevice = EasyMock.createMock(ITestDevice.class);
     58         mMockBuildInfo = EasyMock.createMock(IBuildInfo.class);
     59     }
     60 
     61     /**
     62      * Test that {@link RunCommandTargetPreparer#setUp(ITestDevice, IBuildInfo)} is properly going
     63      * through without exception when running a command.
     64      */
     65     @Test
     66     public void testSetUp() throws Exception {
     67         final String command = "mkdir test";
     68         OptionSetter setter = new OptionSetter(mPreparer);
     69         setter.setOptionValue("run-command", command);
     70         EasyMock.expect(mMockDevice.getSerialNumber()).andReturn("SERIAL").times(2);
     71         CommandResult res = new CommandResult();
     72         res.setStatus(CommandStatus.SUCCESS);
     73         res.setStdout("");
     74         EasyMock.expect(mMockDevice.executeShellV2Command(EasyMock.eq(command))).andReturn(res);
     75         EasyMock.replay(mMockDevice, mMockBuildInfo);
     76         mPreparer.setUp(mMockDevice, mMockBuildInfo);
     77         EasyMock.verify(mMockDevice, mMockBuildInfo);
     78     }
     79 
     80     /**
     81      * Test that {@link RunCommandTargetPreparer#setUp(ITestDevice, IBuildInfo)} is properly going
     82      * through without exception when running a command with timeout.
     83      */
     84     @Test
     85     public void testSetUp_withTimeout() throws Exception {
     86         final String command = "mkdir test";
     87         OptionSetter setter = new OptionSetter(mPreparer);
     88         setter.setOptionValue("run-command", command);
     89         setter.setOptionValue("run-command-timeout", "100");
     90         EasyMock.expect(mMockDevice.getSerialNumber()).andReturn("SERIAL").times(2);
     91         CommandResult res = new CommandResult();
     92         res.setStatus(CommandStatus.SUCCESS);
     93         res.setStdout("");
     94         EasyMock.expect(
     95                         mMockDevice.executeShellV2Command(
     96                                 EasyMock.eq(command),
     97                                 EasyMock.eq(100l),
     98                                 EasyMock.eq(TimeUnit.MILLISECONDS),
     99                                 EasyMock.eq(0)))
    100                 .andReturn(res);
    101         EasyMock.replay(mMockDevice, mMockBuildInfo);
    102         mPreparer.setUp(mMockDevice, mMockBuildInfo);
    103         EasyMock.verify(mMockDevice, mMockBuildInfo);
    104     }
    105 
    106     /**
    107      * Test that {@link RunCommandTargetPreparer#setUp(ITestDevice, IBuildInfo)} and {@link
    108      * RunCommandTargetPreparer#tearDown(ITestDevice, IBuildInfo, Throwable)} are properly skipped
    109      * when disabled and no command is ran.
    110      */
    111     @Test
    112     public void testDisabled() throws Exception {
    113         final String command = "mkdir test";
    114         OptionSetter setter = new OptionSetter(mPreparer);
    115         setter.setOptionValue("run-command", command);
    116         setter.setOptionValue("disable", "true");
    117         EasyMock.replay(mMockDevice, mMockBuildInfo);
    118         mPreparer.setUp(mMockDevice, mMockBuildInfo);
    119         mPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
    120         EasyMock.verify(mMockDevice, mMockBuildInfo);
    121     }
    122 
    123     /**
    124      * Test that {@link RunCommandTargetPreparer#tearDown(ITestDevice, IBuildInfo, Throwable)} is
    125      * properly going through without exception when running a command.
    126      */
    127     @Test
    128     public void testTearDown() throws Exception {
    129         final String command = "mkdir test";
    130         OptionSetter setter = new OptionSetter(mPreparer);
    131         setter.setOptionValue("teardown-command", command);
    132         EasyMock.expect(mMockDevice.getSerialNumber()).andReturn("SERIAL").times(1);
    133         EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.eq(command))).andReturn("");
    134         EasyMock.replay(mMockDevice, mMockBuildInfo);
    135         mPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
    136         EasyMock.verify(mMockDevice, mMockBuildInfo);
    137     }
    138 
    139     /**
    140      * Test that {@link RunCommandTargetPreparer#setUp(ITestDevice, IBuildInfo)} and
    141      * {@link RunCommandTargetPreparer#tearDown(ITestDevice, IBuildInfo, Throwable)} is properly
    142      * going through without exception when running a background command.
    143      */
    144     @Test
    145     public void testBgCmd() throws AdbCommandRejectedException, ConfigurationException,
    146         DeviceNotAvailableException, IOException, ShellCommandUnresponsiveException,
    147         TargetSetupError, TimeoutException {
    148         final String command = "mkdir test";
    149         OptionSetter setter = new OptionSetter(mPreparer);
    150         setter.setOptionValue("run-bg-command", command);
    151         IDevice mMockIDevice = EasyMock.createMock(IDevice.class);
    152         EasyMock.expect(mMockIDevice.getSerialNumber()).andReturn("SERIAL").anyTimes();
    153         IShellOutputReceiver mMockReceiver = EasyMock.createMock(IShellOutputReceiver.class);
    154         mMockReceiver.addOutput((byte[]) EasyMock.anyObject(), EasyMock.anyInt(),
    155                 EasyMock.anyInt());
    156         EasyMock.expect(mMockDevice.getIDevice()).andReturn(mMockIDevice).atLeastOnce();
    157         EasyMock.expect(mMockDevice.getSerialNumber()).andReturn("SERIAL").atLeastOnce();
    158         mMockIDevice.executeShellCommand(EasyMock.eq(command), EasyMock.same(mMockReceiver),
    159                 EasyMock.anyLong(), EasyMock.eq(TimeUnit.MILLISECONDS));
    160         EasyMock.expect(mMockDevice.getDeviceState()).andReturn(ONLINE_STATE).atLeastOnce();
    161         EasyMock.replay(mMockDevice, mMockBuildInfo);
    162         mPreparer.setUp(mMockDevice, mMockBuildInfo);
    163         RunUtil.getDefault().sleep(LONG_WAIT_TIME_MS);
    164         mPreparer.tearDown(mMockDevice, mMockBuildInfo, null);
    165         EasyMock.verify(mMockDevice, mMockBuildInfo);
    166     }
    167 }
    168