blob: ef89dc7312840dbf555a0ee7800d3447c0137e9b [file] [log] [blame]
Di Qian38c02a72019-11-18 19:14:07 -08001/*
2 * Copyright (C) 2019 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 */
16package com.android.tradefed.cluster;
17
18import com.android.annotations.VisibleForTesting;
19import com.android.tradefed.build.BuildRetrievalError;
20import com.android.tradefed.build.IBuildInfo;
21import com.android.tradefed.build.IBuildProvider;
22import com.android.tradefed.config.Option;
23import com.android.tradefed.config.OptionClass;
24import com.android.tradefed.util.ZipUtil2;
25
26import org.apache.commons.compress.archivers.zip.ZipFile;
27import java.io.File;
28import java.io.IOException;
29import java.util.Map;
30import java.util.Map.Entry;
31import java.util.TreeMap;
32
33/** A {@link IBuildProvider} to download TFC test resources. */
34@OptionClass(alias = "cluster", global_namespace = false)
35public class ClusterBuildProvider implements IBuildProvider {
36
37 private static final String DEFAULT_FILE_VERSION = "0";
38
39 @Option(name = "root-dir", description = "A root directory", mandatory = true)
40 private File mRootDir;
41
42 @Option(name = "test-resource", description = "Test resources", mandatory = true)
43 private Map<String, String> mTestResources = new TreeMap<>();
44
Daniel Peykov3fbee1a2020-02-14 12:35:47 -080045 @Option(name = "build-id", description = "Build ID")
46 private String mBuildId = IBuildInfo.UNKNOWN_BUILD_ID;
47
48 @Option(name = "build-target", description = "Build target name")
49 private String mBuildTarget = "stub";
50
Di Qian38c02a72019-11-18 19:14:07 -080051 @Override
52 public IBuildInfo getBuild() throws BuildRetrievalError {
53 try {
54 mRootDir.mkdirs();
Daniel Peykov3fbee1a2020-02-14 12:35:47 -080055 final IBuildInfo buildInfo = new ClusterBuildInfo(mRootDir, mBuildId, mBuildTarget);
Di Qian38c02a72019-11-18 19:14:07 -080056 final TestResourceDownloader downloader = new TestResourceDownloader(mRootDir);
57 for (final Entry<String, String> entry : mTestResources.entrySet()) {
58 final TestResource resource = new TestResource(entry.getKey(), entry.getValue());
59 final File file = downloader.download(resource);
60 buildInfo.setFile(resource.getName(), file, DEFAULT_FILE_VERSION);
61 if (file.getName().endsWith(".zip")) {
62 // If a zip file is downloaded to a subfolder, unzip there.
63 extractZip(file, file.getParentFile());
64 }
65 }
66 return buildInfo;
67 } catch (IOException e) {
68 throw new BuildRetrievalError("failed to get test resources", e);
69 }
70 }
71
72 /** Extracts the zip to a root dir. */
73 private void extractZip(File zip, File destDir) throws IOException {
74 try (ZipFile zipFile = new ZipFile(zip)) {
75 ZipUtil2.extractZip(zipFile, destDir);
76 } catch (IOException e) {
77 throw e;
78 }
79 }
80
81 @Override
82 public void buildNotTested(IBuildInfo info) {}
83
84 @Override
85 public void cleanUp(IBuildInfo info) {
86 if (!(info instanceof ClusterBuildInfo)) {
87 throw new IllegalArgumentException("info is not an instance of ClusterBuildInfo");
88 }
89 }
90
91 @VisibleForTesting
92 File getRootDir() {
93 return mRootDir;
94 }
95
96 @VisibleForTesting
97 Map<String, String> getTestResources() {
98 return mTestResources;
99 }
100}