1 /*
2  * Copyright (C) 2015 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 
17 package com.android.compatibility.common.tradefed.targetprep;
18 
19 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
20 import com.android.compatibility.common.tradefed.util.CollectorUtil;
21 import com.android.tradefed.build.IBuildInfo;
22 import com.android.tradefed.config.Option;
23 import com.android.tradefed.device.DeviceNotAvailableException;
24 import com.android.tradefed.device.ITestDevice;
25 import com.android.tradefed.device.StubDevice;
26 import com.android.tradefed.invoker.TestInformation;
27 import com.android.tradefed.log.LogUtil.CLog;
28 import com.android.tradefed.targetprep.BaseTargetPreparer;
29 import com.android.tradefed.targetprep.BuildError;
30 import com.android.tradefed.targetprep.ITargetPreparer;
31 import com.android.tradefed.targetprep.TargetSetupError;
32 import com.android.tradefed.util.FileUtil;
33 
34 import java.io.File;
35 import java.io.FileNotFoundException;
36 import java.io.IOException;
37 
38 /** An {@link ITargetPreparer} that prepares and pulls report logs. */
39 public class ReportLogCollector extends BaseTargetPreparer {
40 
41     @Option(name= "src-dir", description = "The directory to copy to the results dir")
42     private String mSrcDir;
43 
44     @Option(name = "dest-dir", description = "The directory under the result to store the files")
45     private String mDestDir;
46 
47     @Option(name = "temp-dir", description = "The temp directory containing host-side report logs")
48     private String mTempReportFolder;
49 
50     @Option(name = "device-dir", description = "Create unique directory for each device")
51     private boolean mDeviceDir;
52 
ReportLogCollector()53     public ReportLogCollector() {
54     }
55 
56     @Override
setUp(TestInformation testInfo)57     public void setUp(TestInformation testInfo)
58             throws TargetSetupError, BuildError, DeviceNotAvailableException {
59         prepareReportLogContainers(testInfo.getBuildInfo());
60     }
61 
prepareReportLogContainers(IBuildInfo buildInfo)62     private void prepareReportLogContainers(IBuildInfo buildInfo) {
63         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(buildInfo);
64         try {
65             File resultDir = buildHelper.getResultDir();
66             if (mDestDir != null) {
67                 resultDir = new File(resultDir, mDestDir);
68             }
69             resultDir.mkdirs();
70             if (!resultDir.isDirectory()) {
71                 CLog.e("%s is not a directory", resultDir.getAbsolutePath());
72                 return;
73             }
74         } catch (FileNotFoundException fnfe) {
75             CLog.e(fnfe);
76         }
77     }
78 
79     @Override
tearDown(TestInformation testInfo, Throwable e)80     public void tearDown(TestInformation testInfo, Throwable e) {
81         if (e instanceof DeviceNotAvailableException) {
82             CLog.e("Invocation finished with DeviceNotAvailable, skipping collecting logs.");
83             return;
84         }
85         ITestDevice device = testInfo.getDevice();
86         IBuildInfo buildInfo = testInfo.getBuildInfo();
87         if (device.getIDevice() instanceof StubDevice) {
88             CLog.d("Skipping ReportLogCollector, it requires a device.");
89             return;
90         }
91         // Pull report log files from device.
92         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(buildInfo);
93         try {
94             File resultDir = buildHelper.getResultDir();
95             if (mDestDir != null) {
96                 resultDir = new File(resultDir, mDestDir);
97             }
98             resultDir.mkdirs();
99             if (!resultDir.isDirectory()) {
100                 CLog.e("%s is not a directory", resultDir.getAbsolutePath());
101                 return;
102             }
103             String tmpDirName = mTempReportFolder;
104             if (mDeviceDir) {
105                 tmpDirName = tmpDirName.replaceAll("/$", "");
106                 tmpDirName += "-" + device.getSerialNumber();
107             }
108             final File hostReportDir = FileUtil.createNamedTempDir(tmpDirName);
109             if (!hostReportDir.isDirectory()) {
110                 CLog.e("%s is not a directory", hostReportDir.getAbsolutePath());
111                 return;
112             }
113             device.pullDir(mSrcDir, resultDir);
114             CollectorUtil.pullFromHost(hostReportDir, resultDir);
115             CollectorUtil.reformatRepeatedStreams(resultDir);
116         } catch (DeviceNotAvailableException | IOException exception) {
117             CLog.e(exception);
118         }
119     }
120 }
121