Di Qian | 38c02a7 | 2019-11-18 19:14:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.tradefed.cluster; |
| 17 | |
| 18 | import com.android.annotations.VisibleForTesting; |
| 19 | import com.android.tradefed.build.BuildRetrievalError; |
| 20 | import com.android.tradefed.build.IBuildInfo; |
| 21 | import com.android.tradefed.build.IBuildProvider; |
| 22 | import com.android.tradefed.config.Option; |
| 23 | import com.android.tradefed.config.OptionClass; |
| 24 | import com.android.tradefed.util.ZipUtil2; |
| 25 | |
| 26 | import org.apache.commons.compress.archivers.zip.ZipFile; |
| 27 | import java.io.File; |
| 28 | import java.io.IOException; |
| 29 | import java.util.Map; |
| 30 | import java.util.Map.Entry; |
| 31 | import java.util.TreeMap; |
| 32 | |
| 33 | /** A {@link IBuildProvider} to download TFC test resources. */ |
| 34 | @OptionClass(alias = "cluster", global_namespace = false) |
| 35 | public 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 Peykov | 3fbee1a | 2020-02-14 12:35:47 -0800 | [diff] [blame] | 45 | @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 Qian | 38c02a7 | 2019-11-18 19:14:07 -0800 | [diff] [blame] | 51 | @Override |
| 52 | public IBuildInfo getBuild() throws BuildRetrievalError { |
| 53 | try { |
| 54 | mRootDir.mkdirs(); |
Daniel Peykov | 3fbee1a | 2020-02-14 12:35:47 -0800 | [diff] [blame] | 55 | final IBuildInfo buildInfo = new ClusterBuildInfo(mRootDir, mBuildId, mBuildTarget); |
Di Qian | 38c02a7 | 2019-11-18 19:14:07 -0800 | [diff] [blame] | 56 | 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 | } |