Home | History | Annotate | Download | only in testtype
      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 package com.android.cts.tradefed.testtype;
     17 
     18 import com.android.cts.tradefed.build.CtsBuildHelper;
     19 import com.android.tradefed.device.DeviceNotAvailableException;
     20 import com.android.tradefed.device.ITestDevice;
     21 import com.android.tradefed.log.LogUtil.CLog;
     22 import com.android.tradefed.result.ITestInvocationListener;
     23 import com.android.tradefed.util.FileUtil;
     24 
     25 import java.io.File;
     26 import java.io.IOException;
     27 import java.util.zip.ZipFile;
     28 
     29 /**
     30  * A wrapper around {@link JarHostTest} that includes additional device setup and clean up.
     31  *
     32  */
     33 public class VMHostTest extends JarHostTest {
     34 
     35     private static final String VM_TEST_TEMP_DIR = "/data/local/tmp/vm-tests";
     36     private static final String EMULATOR_TEMP_DIR = "/data/local/tmp";
     37 
     38     /**
     39      * {@inheritDoc}
     40      */
     41     @Override
     42     @SuppressWarnings("unchecked")
     43     public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
     44         if (!installVmPrereqs(getDevice(), getBuildHelper())) {
     45             throw new RuntimeException(String.format(
     46                     "Failed to install vm-tests prereqs on device %s",
     47                     getDevice().getSerialNumber()));
     48         }
     49         super.run(listener);
     50         cleanupDeviceFiles(getDevice());
     51     }
     52 
     53     /**
     54      * Install pre-requisite jars for running vm-tests, creates temp directories for test.
     55      *
     56      * @param device the {@link ITestDevice}
     57      * @param ctsBuild the {@link CtsBuildHelper}
     58      * @throws DeviceNotAvailableException
     59      * @return true if test jar files are extracted and pushed to device successfully
     60      */
     61     private boolean installVmPrereqs(ITestDevice device, CtsBuildHelper ctsBuild)
     62             throws DeviceNotAvailableException {
     63         cleanupDeviceFiles(device);
     64         // Creates temp directory recursively. We also need to create the dalvik-cache directory
     65         // which is used by the dalvikvm to optimize things. Without the dalvik-cache, there will be
     66         // a sigsev thrown by the vm.
     67         CLog.d("Creating device temp directory, including dalvik-cache.");
     68         createRemoteDir(device, VM_TEST_TEMP_DIR + "/dalvik-cache" );
     69         try {
     70             File localTmpDir = FileUtil.createTempDir("cts-vm", new File(System.getProperty("java.io.tmpdir")));
     71             CLog.d("Creating host temp dir %s", localTmpDir.getPath());
     72             File jarFile = new File(ctsBuild.getTestCasesDir(), getJarFileName());
     73             if (!jarFile.exists()) {
     74                 CLog.e("Missing jar file %s", jarFile.getPath());
     75                 return false;
     76             }
     77             CLog.d("Extracting jar file %s to host temp directory %s.",
     78                     jarFile.getPath(), localTmpDir.getPath());
     79             ZipFile zipFile = new ZipFile(jarFile);
     80             FileUtil.extractZip(zipFile, localTmpDir);
     81             File localTestTmpDir = new File(localTmpDir, "tests");
     82             CLog.d("Syncing host dir %s to device dir %s",
     83                     localTestTmpDir.getPath(), VM_TEST_TEMP_DIR);
     84             if (!device.pushDir(localTestTmpDir, VM_TEST_TEMP_DIR)) {
     85                 CLog.e("Failed to push vm test files");
     86                 return false;
     87             }
     88             CLog.d("Cleaning up host temp dir %s", localTmpDir.getPath());
     89             FileUtil.recursiveDelete(localTmpDir);
     90         } catch (IOException e) {
     91             CLog.e("Failed to extract jar file %s and sync it to device %s.",
     92                     getJarFileName(), device.getSerialNumber());
     93             return false;
     94         }
     95         return true;
     96     }
     97 
     98     /**
     99      * Removes temporary file directory from device
    100      *
    101      * @param device
    102      * @throws DeviceNotAvailableException
    103      */
    104     private void cleanupDeviceFiles(ITestDevice device) throws DeviceNotAvailableException {
    105         if (device.doesFileExist(VM_TEST_TEMP_DIR)) {
    106             CLog.d("Removing device's temp dir %s from previous runs.", VM_TEST_TEMP_DIR);
    107             device.executeShellCommand(String.format("rm -r %s", VM_TEST_TEMP_DIR));
    108         }
    109     }
    110 
    111     /**
    112      * Creates the file directory recursively in the device.
    113      *
    114      * @param device the {@link ITestDevice}
    115      * @param remoteFilePath the absolute path.
    116      * @throws DeviceNotAvailableException
    117      */
    118     private void createRemoteDir(ITestDevice device, String remoteFilePath)
    119             throws DeviceNotAvailableException {
    120         if (device.doesFileExist(remoteFilePath)) {
    121             return;
    122         }
    123 	 if (!(device.doesFileExist(EMULATOR_TEMP_DIR))) {
    124             CLog.e("Error: Can not found the /data/local/tmp directory!!!");
    125         }
    126         device.executeShellCommand(String.format("mkdir %s", VM_TEST_TEMP_DIR));
    127         device.executeShellCommand(String.format("mkdir %s", remoteFilePath));
    128     }
    129 }
    130