Add fetch code for classpath jars
Fetch all *CLASSPATH jars and dump their data into .pb files in the temp directory shared between the target preparer and the testcases. Test: atest ClasspathFetcherTest Bug: 254647172 Change-Id: I008001975ba5febbec5fec20a908b1dcc02308f6
This commit is contained in:
@@ -16,19 +16,36 @@
|
||||
|
||||
package com.android.modules.targetprep;
|
||||
|
||||
import com.android.tradefed.config.Option;
|
||||
import static android.compat.testing.Classpaths.ClasspathType.BOOTCLASSPATH;
|
||||
import static android.compat.testing.Classpaths.ClasspathType.SYSTEMSERVERCLASSPATH;
|
||||
|
||||
import android.compat.testing.Classpaths;
|
||||
import android.compat.testing.Classpaths.ClasspathType;
|
||||
|
||||
import com.android.modules.proto.ClasspathClasses.Classpath;
|
||||
import com.android.modules.proto.ClasspathClasses.ClasspathClassesDump;
|
||||
import com.android.modules.proto.ClasspathClasses.ClasspathEntry;
|
||||
import com.android.modules.proto.ClasspathClasses.Jar;
|
||||
|
||||
import com.android.tradefed.device.DeviceNotAvailableException;
|
||||
import com.android.tradefed.device.INativeDevice;
|
||||
import com.android.tradefed.device.ITestDevice;
|
||||
import com.android.tradefed.invoker.TestInformation;
|
||||
import com.android.tradefed.log.LogUtil.CLog;
|
||||
import com.android.tradefed.targetprep.BaseTargetPreparer;
|
||||
import com.android.tradefed.targetprep.TargetSetupError;
|
||||
import com.android.tradefed.util.RunUtil;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.jf.dexlib2.iface.ClassDef;
|
||||
|
||||
/*
|
||||
* Target preparer that fetches classpath relevant artifacts for a test in a 'reentrant' manner.
|
||||
@@ -54,20 +71,33 @@ import java.nio.file.Path;
|
||||
public class ClasspathFetcher extends BaseTargetPreparer {
|
||||
|
||||
public static final String DEVICE_JAR_ARTIFACTS_TAG = "device-jar-artifacts";
|
||||
public static final String BCP_CLASSES_FILE = "bcp.pb";
|
||||
public static final String SSCP_CLASSES_FILE = "sscp.pb";
|
||||
|
||||
// TODO(andreionea): also fetch classes for standalone system server jars, apk-in-apex and
|
||||
// shared libraries. They require more mocking on the test side.
|
||||
|
||||
|
||||
private boolean mFetchedArtifacts = false;
|
||||
|
||||
@Override
|
||||
public void setUp(TestInformation testInfo)
|
||||
throws TargetSetupError, DeviceNotAvailableException {
|
||||
Objects.requireNonNull(testInfo.getDevice());
|
||||
// The artifacts have been fetched already, no need to do anything else.
|
||||
if (testInfo.properties().containsKey(DEVICE_JAR_ARTIFACTS_TAG)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final Path tmpDir = Files.createTempDirectory("device_artifacts");
|
||||
testInfo.properties().put(DEVICE_JAR_ARTIFACTS_TAG, tmpDir.toAbsolutePath().toString());
|
||||
// TODO(b/254647172): Fetch data
|
||||
testInfo.properties().put(DEVICE_JAR_ARTIFACTS_TAG,
|
||||
tmpDir.toAbsolutePath().toString());
|
||||
|
||||
getClassesInClasspath(testInfo.getDevice(), BOOTCLASSPATH)
|
||||
.writeTo(new FileOutputStream(new File(tmpDir.toFile(), BCP_CLASSES_FILE)));
|
||||
getClassesInClasspath(testInfo.getDevice(), SYSTEMSERVERCLASSPATH)
|
||||
.writeTo(new FileOutputStream(new File(tmpDir.toFile(), SSCP_CLASSES_FILE)));
|
||||
|
||||
mFetchedArtifacts = true;
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException("Could not create temp artifacts dir!", e);
|
||||
@@ -84,13 +114,70 @@ public class ClasspathFetcher extends BaseTargetPreparer {
|
||||
+ " artifacts, but the DEVICE_JAR_ARTIFACTS_TAG property was removed");
|
||||
}
|
||||
final File jarArtifactsDir = new File(path);
|
||||
if (!jarArtifactsDir.delete()) {
|
||||
throw new RuntimeException("Failed to remove jar artifacts dir!");
|
||||
}
|
||||
deleteDirectory(jarArtifactsDir);
|
||||
} finally {
|
||||
testInfo.properties().remove(DEVICE_JAR_ARTIFACTS_TAG);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Classpath classpathTypeToClasspathEnum(ClasspathType t) {
|
||||
switch(t) {
|
||||
case BOOTCLASSPATH:
|
||||
return Classpath.valueOf(Classpath.BOOTCLASSPATH_VALUE);
|
||||
case SYSTEMSERVERCLASSPATH:
|
||||
return Classpath.valueOf(Classpath.SYSTEMSERVERCLASSPATH_VALUE);
|
||||
default:
|
||||
throw new RuntimeException("Unknown classpath type " + t);
|
||||
}
|
||||
}
|
||||
|
||||
private ImmutableSet<String> getClassesInFile(INativeDevice device, String file)
|
||||
throws DeviceNotAvailableException, IOException {
|
||||
final File jar = device.pullFile(file);
|
||||
if (jar == null) {
|
||||
throw new IllegalStateException("could not pull remote file " + file);
|
||||
}
|
||||
return Classpaths.getClassDefsFromJar(jar)
|
||||
.stream()
|
||||
.map(ClassDef::getType)
|
||||
.collect(ImmutableSet.toImmutableSet());
|
||||
}
|
||||
|
||||
private ClasspathClassesDump getClassesInClasspath(INativeDevice device, ClasspathType type)
|
||||
throws DeviceNotAvailableException, IOException {
|
||||
ClasspathClassesDump.Builder builder = ClasspathClassesDump.newBuilder();
|
||||
final ImmutableList<String> jars = Classpaths.getJarsOnClasspath(device, type);
|
||||
for (String jar : jars) {
|
||||
ClasspathEntry.Builder entryBuilder = ClasspathEntry.newBuilder();
|
||||
Jar.Builder jarBuilder = Jar.newBuilder();
|
||||
jarBuilder.setClasspath(classpathTypeToClasspathEnum(type));
|
||||
jarBuilder.setPath(jar);
|
||||
entryBuilder.setJar(jarBuilder.build());
|
||||
entryBuilder.addAllClasses(getClassesInFile(device, jar));
|
||||
builder.addEntries(entryBuilder.build());
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deletes a directory and its contents recursively
|
||||
*
|
||||
* @param directory to delete
|
||||
*/
|
||||
private static void deleteDirectory(File directory) {
|
||||
File[] files = directory.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (!file.isDirectory()) {
|
||||
file.delete();
|
||||
} else {
|
||||
deleteDirectory(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
directory.delete();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user