diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2020-07-08 17:35:41 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-07-08 17:35:41 +0000 |
commit | 88166c218c731fcd72294c5e09194a86bed1e619 (patch) | |
tree | 2554e70fd4935c8b49dd3632da064cc8552a01b3 | |
parent | c1dfbbad8cb93b846ad27ca0fa13adc6ede4a0e1 (diff) | |
parent | b6f315359637bf331202aaf47d9cf20d584aa909 (diff) | |
download | tradefederation-88166c218c731fcd72294c5e09194a86bed1e619.tar.gz |
Merge "Start a dependeny provider for the new config use cases" am: b29736f83f am: 81bbb62cc2 am: 24f6631d34 am: 6997f51f29 am: b6f3153596
Original change: https://android-review.googlesource.com/c/platform/tools/tradefederation/+/1351768
Change-Id: Ie93ff13da9856be2a48104cee4b10e9d76bb62dc
5 files changed, 200 insertions, 10 deletions
diff --git a/src/com/android/tradefed/build/DependenciesResolver.java b/src/com/android/tradefed/build/DependenciesResolver.java new file mode 100644 index 000000000..d7245085f --- /dev/null +++ b/src/com/android/tradefed/build/DependenciesResolver.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.tradefed.build; + +import com.android.annotations.VisibleForTesting; +import com.android.tradefed.config.Option; +import com.android.tradefed.dependency.TestDependencyResolver; +import com.android.tradefed.device.DeviceNotAvailableException; +import com.android.tradefed.device.ITestDevice; +import com.android.tradefed.invoker.ExecutionFiles; +import com.android.tradefed.invoker.IInvocationContext; +import com.android.tradefed.invoker.logger.CurrentInvocation; +import com.android.tradefed.invoker.logger.CurrentInvocation.InvocationInfo; +import com.android.tradefed.result.error.InfraErrorIdentifier; +import com.android.tradefed.testtype.IInvocationContextReceiver; +import com.android.tradefed.util.FileUtil; + +import java.io.File; +import java.io.IOException; +import java.util.LinkedHashSet; +import java.util.Set; + +/** A new type of provider that allows to get all the dependencies for a test. */ +public class DependenciesResolver + implements IBuildProvider, IDeviceBuildProvider, IInvocationContextReceiver { + + @Option(name = "build-id", description = "build id to supply.") + private String mBuildId = "0"; + + @Option(name = "branch", description = "build branch name to supply.") + private String mBranch = null; + + @Option(name = "build-flavor", description = "build flavor name to supply.") + private String mBuildFlavor = null; + + @Option(name = "build-os", description = "build os name to supply.") + private String mBuildOs = "linux"; + + @Option(name = "dependency", description = "The set of dependency to provide for the test") + private Set<File> mDependencies = new LinkedHashSet<>(); + + private File mTestsDir; + private IInvocationContext mInvocationContext; + + @Override + public IBuildInfo getBuild(ITestDevice device) + throws BuildRetrievalError, DeviceNotAvailableException { + IDeviceBuildInfo build = + new DeviceBuildInfo( + mBuildId, String.format("%s-%s-%s", mBranch, mBuildOs, mBuildFlavor)); + build.setBuildFlavor(mBuildFlavor); + for (File dependency : mDependencies) { + File f = + TestDependencyResolver.resolveDependencyFromContext( + dependency, build, mInvocationContext); + if (f != null) { + // TODO: Have way to make named-dependencies + getInvocationFiles().put(f.getName(), f); + build.setFile(f.getName(), f, "1"); + } + } + // Create a tests dir if there are none + if (build.getTestsDir() == null) { + try { + mTestsDir = + FileUtil.createTempDir( + "bootstrap-test-dir", + CurrentInvocation.getInfo(InvocationInfo.WORK_FOLDER)); + } catch (IOException e) { + throw new BuildRetrievalError( + e.getMessage(), e, InfraErrorIdentifier.FAIL_TO_CREATE_FILE); + } + build.setTestsDir(mTestsDir, "1"); + } + return build; + } + + @Override + public IBuildInfo getBuild() throws BuildRetrievalError { + throw new IllegalArgumentException("Should not be called"); + } + + @Override + public void cleanUp(IBuildInfo info) { + info.cleanUp(); + } + + @Override + public void setInvocationContext(IInvocationContext invocationContext) { + mInvocationContext = invocationContext; + } + + @VisibleForTesting + public final Set<File> getDependencies() { + return mDependencies; + } + + @VisibleForTesting + ExecutionFiles getInvocationFiles() { + return CurrentInvocation.getInvocationFiles(); + } +} diff --git a/src/com/android/tradefed/config/yaml/OpenObjectLoader.java b/src/com/android/tradefed/config/yaml/OpenObjectLoader.java index 839ee9b0e..f5c5d06cd 100644 --- a/src/com/android/tradefed/config/yaml/OpenObjectLoader.java +++ b/src/com/android/tradefed/config/yaml/OpenObjectLoader.java @@ -15,8 +15,9 @@ */ package com.android.tradefed.config.yaml; -import com.android.tradefed.build.BootstrapBuildProvider; +import com.android.tradefed.build.DependenciesResolver; import com.android.tradefed.config.Configuration; +import com.android.tradefed.config.OptionSetter; import com.android.tradefed.result.suite.SuiteResultReporter; /** Loader for the default objects available in AOSP. */ @@ -29,11 +30,30 @@ public class OpenObjectLoader implements IDefaultObjectLoader { .addConfigObjectDef( Configuration.RESULT_REPORTER_TYPE_NAME, SuiteResultReporter.class.getName()); - // TODO: Replace by a provider that can handle both local and remote - loadConfiguration - .getConfigDef() - .addConfigObjectDef( - Configuration.BUILD_PROVIDER_TYPE_NAME, - BootstrapBuildProvider.class.getName()); + int classCount = + loadConfiguration + .getConfigDef() + .addConfigObjectDef( + Configuration.BUILD_PROVIDER_TYPE_NAME, + DependenciesResolver.class.getName()); + // Set all the dependencies on the provider + for (String depencency : loadConfiguration.getDependencies()) { + String optionName = + String.format( + "%s%c%d%c%s", + DependenciesResolver.class.getName(), + OptionSetter.NAMESPACE_SEPARATOR, + classCount, + OptionSetter.NAMESPACE_SEPARATOR, + "dependency"); + loadConfiguration + .getConfigDef() + .addOptionDef( + optionName, + null, + depencency, + loadConfiguration.getConfigDef().getName(), + Configuration.BUILD_PROVIDER_TYPE_NAME); + } } } diff --git a/src/com/android/tradefed/dependency/TestDependencyResolver.java b/src/com/android/tradefed/dependency/TestDependencyResolver.java new file mode 100644 index 000000000..5034b094d --- /dev/null +++ b/src/com/android/tradefed/dependency/TestDependencyResolver.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.tradefed.dependency; + +import com.android.tradefed.build.BuildRetrievalError; +import com.android.tradefed.build.IBuildInfo; +import com.android.tradefed.invoker.IInvocationContext; + +import java.io.File; + +/** Helper to resolve dependencies if needed. */ +public class TestDependencyResolver { + + /** + * Resolve a single dependency based on some context. + * + * @param dependency The dependency to be resolved. + * @param build The current build information being created. + * @param context The invocation context. + * @return The resolved dependency or null if unresolved. + * @throws BuildRetrievalError + */ + public static File resolveDependencyFromContext( + File dependency, IBuildInfo build, IInvocationContext context) + throws BuildRetrievalError { + if (dependency.exists()) { + return dependency; + } + // TODO: Implement the core logic + return null; + } +} diff --git a/tests/res/testconfigs/yaml/test-config.tf_yaml b/tests/res/testconfigs/yaml/test-config.tf_yaml index d5479532e..eb89137b3 100644 --- a/tests/res/testconfigs/yaml/test-config.tf_yaml +++ b/tests/res/testconfigs/yaml/test-config.tf_yaml @@ -1,5 +1,4 @@ description: "Human friendly description of the test" -infra_run: false dependencies: - apks: ["test.apk", "test2.apk"] diff --git a/tests/src/com/android/tradefed/config/yaml/ConfigurationYamlParserTest.java b/tests/src/com/android/tradefed/config/yaml/ConfigurationYamlParserTest.java index 2fbdc5f19..a001f55e2 100644 --- a/tests/src/com/android/tradefed/config/yaml/ConfigurationYamlParserTest.java +++ b/tests/src/com/android/tradefed/config/yaml/ConfigurationYamlParserTest.java @@ -20,7 +20,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import com.android.tradefed.build.BootstrapBuildProvider; +import com.android.tradefed.build.DependenciesResolver; import com.android.tradefed.config.ConfigurationDef; import com.android.tradefed.config.IConfiguration; import com.android.tradefed.result.ITestInvocationListener; @@ -62,7 +62,18 @@ public class ConfigurationYamlParserTest { IConfiguration config = mConfigDef.createConfiguration(); config.validateOptions(); // build provider - assertTrue(config.getBuildProvider() instanceof BootstrapBuildProvider); + assertTrue(config.getBuildProvider() instanceof DependenciesResolver); + DependenciesResolver resolver = (DependenciesResolver) config.getBuildProvider(); + assertEquals(7, resolver.getDependencies().size()); + assertThat(resolver.getDependencies()) + .containsExactly( + new File("test.apk"), + new File("test2.apk"), + new File("test1.apk"), + new File("tobepushed2.txt"), + new File("tobepushed.txt"), + new File("file1.txt"), + new File("file2.txt")); // Test assertEquals(1, config.getTests().size()); assertTrue(config.getTests().get(0) instanceof AndroidJUnitTest); |