1 /*
2  * Copyright (C) 2010 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.build;
17 
18 import com.android.tradefed.config.Option;
19 import com.android.tradefed.config.OptionClass;
20 import com.android.tradefed.log.LogUtil.CLog;
21 import com.android.tradefed.result.error.InfraErrorIdentifier;
22 
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28 
29 /**
30  * No-op empty implementation of a {@link IBuildProvider}.
31  * <p/>
32  * Will provide an empty {@link BuildInfo} with the provided values from options.
33  */
34 @OptionClass(alias="stub")
35 public class StubBuildProvider implements IBuildProvider {
36 
37     @Option(name="build-id", description="build id to supply.")
38     private String mBuildId = "0";
39 
40     @Option(name="build-target", description="build target name to supply.")
41     private String mBuildTargetName = "stub";
42 
43     @Option(name="branch", description="build branch name to supply.")
44     private String mBranch = null;
45 
46     @Option(name="build-flavor", description="build flavor name to supply.")
47     private String mBuildFlavor = null;
48 
49     @Option(name = "build-os", description = "build os name to supply.")
50     private String mBuildOs = null;
51 
52     @Option(name="build-attribute", description="build attributes to supply.")
53     private Map<String, String> mBuildAttributes = new HashMap<String,String>();
54 
55     @Option(
56         name = "return-null",
57         description = "force the stub provider to return a null build. Used for testing."
58     )
59     private boolean mReturnNull = false;
60 
61     @Option(
62         name = "throw-build-error",
63         description = "force the stub provider to throw a BuildRetrievalError. Used for testing."
64     )
65     private boolean mThrowError = false;
66 
67     /** Standard platforms */
68     private static final Set<String> STANDARD_PLATFORMS =
69             new HashSet<String>(Arrays.asList("linux", "mac"));
70 
71     /**
72      * {@inheritDoc}
73      */
74     @Override
getBuild()75     public IBuildInfo getBuild() throws BuildRetrievalError {
76         if (mReturnNull) {
77             CLog.d("Returning a null build.");
78             return null;
79         }
80         if (mThrowError) {
81             throw new BuildRetrievalError(
82                     "stub failed to get build.", InfraErrorIdentifier.ARTIFACT_NOT_FOUND);
83         }
84         CLog.d("skipping build provider step");
85         BuildInfo stubBuild = new BuildInfo(mBuildId, mBuildTargetName);
86         stubBuild.setBuildBranch(mBranch);
87         stubBuild.setBuildFlavor(mBuildFlavor);
88 
89         String buildTarget = mBuildFlavor;
90         if (!STANDARD_PLATFORMS.contains(mBuildOs)) {
91             buildTarget += "_" + mBuildOs;
92         }
93         stubBuild.addBuildAttribute("build_target", buildTarget);
94 
95         for (Map.Entry<String, String> attributeEntry : mBuildAttributes.entrySet()) {
96             stubBuild.addBuildAttribute(attributeEntry.getKey(), attributeEntry.getValue());
97         }
98         return stubBuild;
99     }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
cleanUp(IBuildInfo info)105     public void cleanUp(IBuildInfo info) {
106         // ignore
107     }
108 }
109