• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.atest;
18 
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.invoker.TestInformation;
21 import com.android.tradefed.log.LogUtil.CLog;
22 import com.android.tradefed.targetprep.ITargetPreparer;
23 
24 import java.io.File;
25 import com.android.tradefed.result.FileInputStreamSource;
26 import com.android.tradefed.log.ITestLogger;
27 import com.android.tradefed.result.ITestLoggerReceiver;
28 import com.android.tradefed.result.LogDataType;
29 
30 /** A class to add Atest python artifacts to Tradefed logger. */
31 public final class AtestLogArtifactsUploader implements ITargetPreparer, ITestLoggerReceiver {
32 
33     @Option(name = "log-root-path", description = "root file system path to store log files.")
34     private File mAtestRootReportDir;
35 
36     private ITestLogger mTestLogger;
37 
38     @Override
setTestLogger(ITestLogger testLogger)39     public void setTestLogger(ITestLogger testLogger) {
40         mTestLogger = testLogger;
41     }
42 
43     @Override
setUp(TestInformation testInfo)44     public void setUp(TestInformation testInfo) {
45         // Intentionally left empty.
46     }
47 
48     @Override
tearDown(TestInformation testInfo, Throwable e)49     public void tearDown(TestInformation testInfo, Throwable e) {
50         if (mAtestRootReportDir == null) {
51             CLog.d(
52                     "Atest log root path not specified, skip adding python artifacts to test"
53                             + " logger.");
54             return;
55         }
56 
57         String[] fileNames = new String[] {"atest.log", "test_result"};
58 
59         for (String fileName : fileNames) {
60             File artifactFile = new File(mAtestRootReportDir.getParentFile(), fileName);
61             if (!artifactFile.exists()) {
62                 continue;
63             }
64             mTestLogger.testLog(
65                     fileName, LogDataType.HOST_LOG, new FileInputStreamSource(artifactFile));
66         }
67     }
68 }
69