1 /*
2  * Copyright (C) 2018 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.compatibility.common.tradefed.result.suite;
17 
18 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.config.OptionClass;
21 import com.android.tradefed.invoker.IInvocationContext;
22 import com.android.tradefed.log.LogUtil.CLog;
23 import com.android.tradefed.result.proto.FileProtoResultReporter;
24 import com.android.tradefed.result.proto.TestRecordProto.TestRecord;
25 import com.android.tradefed.util.IDisableable;
26 
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 
30 /** Proto reporter that will drop a {@link TestRecord} protobuf in the result directory. */
31 @OptionClass(alias = "result-reporter")
32 public class CompatibilityProtoResultReporter extends FileProtoResultReporter
33         implements IDisableable {
34 
35     public static final String PROTO_FILE_NAME = "test-record.pb";
36     public static final String PROTO_DIR = "proto";
37 
38     @Option(name = "disable", description = "Whether or not to disable this reporter.")
39     private boolean mDisable = false;
40 
41     private CompatibilityBuildHelper mBuildHelper;
42 
43     /** The directory containing the proto results */
44     private File mResultDir = null;
45 
46     private File mBaseProtoFile = null;
47 
48     @Override
processStartInvocation( TestRecord invocationStartRecord, IInvocationContext invocationContext)49     public void processStartInvocation(
50             TestRecord invocationStartRecord, IInvocationContext invocationContext) {
51         if (mBuildHelper == null) {
52             mBuildHelper = new CompatibilityBuildHelper(invocationContext.getBuildInfos().get(0));
53             mResultDir = getProtoResultDirectory(mBuildHelper);
54             mBaseProtoFile = new File(mResultDir, PROTO_FILE_NAME);
55             setFileOutput(mBaseProtoFile);
56         }
57         super.processStartInvocation(invocationStartRecord, invocationContext);
58     }
59 
getProtoResultDirectory(CompatibilityBuildHelper buildHelper)60     public static File getProtoResultDirectory(CompatibilityBuildHelper buildHelper) {
61         File protoDir = null;
62         try {
63             File resultDir = buildHelper.getResultDir();
64             if (resultDir != null) {
65                 resultDir.mkdirs();
66             }
67             protoDir = new File(resultDir, PROTO_DIR);
68             protoDir.mkdir();
69         } catch (FileNotFoundException e) {
70             throw new RuntimeException(e);
71         }
72         if (!protoDir.exists()) {
73             throw new RuntimeException(
74                     "Result Directory was not created: " + protoDir.getAbsolutePath());
75         }
76         CLog.d("Proto Results Directory: %s", protoDir.getAbsolutePath());
77         return protoDir;
78     }
79 
80     @Override
isDisabled()81     public boolean isDisabled() {
82         return mDisable;
83     }
84 }