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 static org.junit.Assert.fail;
     20 import static org.mockito.Mockito.when;
     21 
     22 import com.android.tradefed.build.IBuildInfo;
     23 import com.android.tradefed.device.ITestDevice;
     24 import com.android.tradefed.util.CommandResult;
     25 import com.android.tradefed.util.CommandStatus;
     26 import com.android.tradefed.util.IRunUtil;
     27 
     28 import org.junit.Before;
     29 import org.junit.Test;
     30 import org.junit.runner.RunWith;
     31 import org.junit.runners.JUnit4;
     32 import org.mockito.Mockito;
     33 
     34 import java.io.File;
     35 
     36 /** Unit tests for {@link PushFilePreparer} */
     37 @RunWith(JUnit4.class)
     38 public class PreloadedClassesPreparerTest {
     39     private static final String FAKE_FILE_PATH = "/file/path";
     40     private static final String FAKE_TOOL_PATH = "/tool/path";
     41     private static final String PRELOAD_TOOL_NAME = "preload2.jar";
     42     private static final String WRITE_COMMAND =
     43             "java -cp %s com.android.preload.Main --seq SERIAL write %s";
     44 
     45     private IBuildInfo mMockBuildInfo;
     46     private ITestDevice mMockDevice;
     47     private IRunUtil mMockRunUtil;
     48 
     49     private PreloadedClassesPreparer mRealPreparer;
     50     private PreloadedClassesPreparer mSpyPreparer;
     51 
     52     @Before
     53     public void setUp() throws Exception {
     54         // Setup mocks and spies
     55         mMockDevice = Mockito.mock(ITestDevice.class);
     56         mMockBuildInfo = Mockito.mock(IBuildInfo.class);
     57         mMockRunUtil = Mockito.mock(IRunUtil.class);
     58         mRealPreparer = new PreloadedClassesPreparer();
     59         mSpyPreparer = Mockito.spy(mRealPreparer);
     60         // Setup mock returns
     61         when(mMockDevice.getDeviceDescriptor()).thenReturn(null);
     62         when(mMockDevice.getSerialNumber()).thenReturn("SERIAL");
     63         when(mSpyPreparer.getRunUtil()).thenReturn(mMockRunUtil);
     64     }
     65 
     66     // Using the build info to get the preload tool is specific to remote runs.
     67     @Test
     68     public void testSetUp_RemoteSuccess() throws Exception {
     69         // Create a fully mocked success case
     70         File tool = Mockito.mock(File.class);
     71         when(tool.exists()).thenReturn(true);
     72         when(tool.getAbsolutePath()).thenReturn(FAKE_TOOL_PATH);
     73         when(mMockBuildInfo.getFile(PRELOAD_TOOL_NAME)).thenReturn(tool);
     74         when(mSpyPreparer.getPreloadedClassesPath()).thenReturn(FAKE_FILE_PATH);
     75         CommandResult result = new CommandResult();
     76         result.setStatus(CommandStatus.SUCCESS);
     77         // Expected output command based on the above.
     78         String[] command = String.format(WRITE_COMMAND, FAKE_TOOL_PATH, FAKE_FILE_PATH).split(" ");
     79         when(mMockRunUtil.runTimedCmd(PreloadedClassesPreparer.DEFAULT_TIMEOUT_MS, command))
     80                 .thenReturn(result);
     81         // Run and don't encounter issues
     82         mSpyPreparer.setUp(mMockDevice, mMockBuildInfo);
     83     }
     84 
     85     // Using the build info to get the preload tool is specific to remote runs.
     86     @Test
     87     public void testSetUp_RemoteNoTool() throws Exception {
     88         // Set mocks to fail returning the tool
     89         when(mSpyPreparer.getPreloadedClassesPath()).thenReturn(FAKE_FILE_PATH);
     90         when(mMockBuildInfo.getFile(PRELOAD_TOOL_NAME)).thenReturn(null);
     91         try {
     92             mSpyPreparer.setUp(mMockDevice, mMockBuildInfo);
     93             fail("Did not fail when there was no tool available.");
     94         } catch (TargetSetupError e) {
     95             // Good, this should throw
     96         }
     97     }
     98 
     99     @Test
    100     public void testSetUp_LocalSuccess() throws Exception {
    101         when(mSpyPreparer.getPreloadToolPath()).thenReturn(FAKE_TOOL_PATH);
    102         when(mSpyPreparer.getPreloadedClassesPath()).thenReturn(FAKE_FILE_PATH);
    103         CommandResult result = new CommandResult();
    104         result.setStatus(CommandStatus.SUCCESS);
    105         // Expected output command based on the above.
    106         String[] command = String.format(WRITE_COMMAND, FAKE_TOOL_PATH, FAKE_FILE_PATH).split(" ");
    107         when(mMockRunUtil.runTimedCmd(PreloadedClassesPreparer.DEFAULT_TIMEOUT_MS, command))
    108                 .thenReturn(result);
    109         // Run and don't encounter issues
    110         mSpyPreparer.setUp(mMockDevice, mMockBuildInfo);
    111     }
    112 
    113     @Test
    114     public void testSetUp_NoFile() throws Exception {
    115         // If not skipped, expect this to error out.
    116         mSpyPreparer.setUp(mMockDevice, mMockBuildInfo);
    117     }
    118 
    119     @Test
    120     public void testSetUp_WriteFailure() throws Exception {
    121         when(mSpyPreparer.getPreloadToolPath()).thenReturn(FAKE_TOOL_PATH);
    122         when(mSpyPreparer.getPreloadedClassesPath()).thenReturn(FAKE_FILE_PATH);
    123         CommandResult result = new CommandResult();
    124         result.setStatus(CommandStatus.FAILED);
    125         // Expected output command based on the above.
    126         String[] command = String.format(WRITE_COMMAND, FAKE_TOOL_PATH, FAKE_FILE_PATH).split(" ");
    127         when(mMockRunUtil.runTimedCmd(PreloadedClassesPreparer.DEFAULT_TIMEOUT_MS, command))
    128                 .thenReturn(result);
    129         // Run and encounter a write issue
    130         try {
    131             mSpyPreparer.setUp(mMockDevice, mMockBuildInfo);
    132             fail("Did not fail when writing with the tool failed.");
    133         } catch (TargetSetupError e) {
    134             // Good, this should throw.
    135         }
    136     }
    137 }
    138