aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tradefed/testtype/TfTestLauncher.java
blob: 87aae1f17daf3cea1207e3f3073398dcbede1a99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tradefed.testtype;

import com.android.ddmlib.testrunner.TestIdentifier;
import com.android.tradefed.build.IFolderBuildInfo;
import com.android.tradefed.config.Option;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.result.FileInputStreamSource;
import com.android.tradefed.result.ITestInvocationListener;
import com.android.tradefed.result.InputStreamSource;
import com.android.tradefed.result.LogDataType;
import com.android.tradefed.util.CommandResult;
import com.android.tradefed.util.CommandStatus;
import com.android.tradefed.util.FileUtil;
import com.android.tradefed.util.HprofAllocSiteParser;
import com.android.tradefed.util.RunUtil;
import com.android.tradefed.util.StreamUtil;

import com.google.common.annotations.VisibleForTesting;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * A {@link IRemoteTest} for running unit or functional tests against a separate TF installation.
 * <p/>
 * Launches an external java process to run the tests. Used for running the TF unit or
 * functional tests continuously.
 */
public class TfTestLauncher extends SubprocessTfLauncher {

    private static final long COVERAGE_REPORT_TIMEOUT_MS = 2 * 60 * 1000;

    @Option(name = "jacoco-code-coverage", description = "Enable jacoco code coverage on the java "
            + "sub process. Run will be slightly slower because of the overhead.")
    private boolean mEnableCoverage = false;

    @Option(
        name = "hprof-heap-memory",
        description =
                "Enable hprof agent while running the java"
                        + "sub process. Run will be slightly slower because of the overhead."
    )
    private boolean mEnableHprof = false;

    @Option(name = "ant-config-res", description = "The name of the ant resource configuration to "
            + "transform the results in readable format.")
    private String mAntConfigResource = "/jacoco/ant-tf-coverage.xml";

    @Option(name = "sub-branch", description = "The branch to be provided to the sub invocation, "
            + "if null, the branch in build info will be used.")
    private String mSubBranch = null;

    @Option(name = "sub-build-flavor", description = "The build flavor to be provided to the "
            + "sub invocation, if null, the build flavor in build info will be used.")
    private String mSubBuildFlavor = null;

    @Option(name = "sub-build-id", description = "The build id that the sub invocation will try "
            + "to use in case where it needs its own device.")
    private String mSubBuildId = null;

    @Option(name = "use-virtual-device", description =
            "Flag if the subprocess is going to need to instantiate a virtual device to run.")
    private boolean mUseVirtualDevice = false;

    @Option(name = "sub-apk-path", description = "The name of all the Apks that needs to be "
            + "installed by the subprocess invocation. Apk need to be inside the downloaded zip. "
            + "Can be repeated.")
    private List<String> mSubApkPath = new ArrayList<String>();

    // The regex pattern of temp files to be found in the temporary dir of the subprocess.
    // Any file not matching the patterns, or multiple files in the temporary dir match the same
    // pattern, is considered as test failure.
    private static final String[] EXPECTED_TMP_FILE_PATTERNS = {
        "inv_.*", "tradefed_global_log_.*", "lc_cache", "stage-android-build-api",
    };

    // A destination file where the report will be put.
    private File mDestCoverageFile = null;
    // A destination file where the hprof report will be put.
    private File mHprofFile = null;
    // A {@link File} pointing to the jacoco args jar file extracted from the resources
    private File mAgent = null;

    /** {@inheritDoc} */
    @Override
    protected void addJavaArguments(List<String> args) {
        super.addJavaArguments(args);
        try {
            if (mEnableCoverage) {
                mDestCoverageFile = FileUtil.createTempFile("coverage", ".exec");
                mAgent = extractJacocoAgent();
                addCoverageArgs(mAgent, args, mDestCoverageFile);
            }
            if (mEnableHprof) {
                mHprofFile = FileUtil.createTempFile("java.hprof", ".txt");
                // verbose=n to avoid dump in stderr
                // cutoff the min value we look at.
                String hprofAgent =
                        String.format(
                                "-agentlib:hprof=heap=sites,cutoff=0.01,depth=12,verbose=n,file=%s",
                                mHprofFile.getAbsolutePath());
                args.add(hprofAgent);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /** {@inheritDoc} */
    @Override
    protected void preRun() {
        super.preRun();

        if (!mUseVirtualDevice) {
            mCmdArgs.add("-n");
        } else {
            // if it needs a device we also enable more logs
            mCmdArgs.add("--log-level");
            mCmdArgs.add("VERBOSE");
            mCmdArgs.add("--log-level-display");
            mCmdArgs.add("VERBOSE");
        }
        mCmdArgs.add("--test-tag");
        mCmdArgs.add(mBuildInfo.getTestTag());
        mCmdArgs.add("--build-id");
        if (mSubBuildId != null) {
            mCmdArgs.add(mSubBuildId);
        } else {
            mCmdArgs.add(mBuildInfo.getBuildId());
        }
        mCmdArgs.add("--branch");
        if (mSubBranch != null) {
            mCmdArgs.add(mSubBranch);
        } else if (mBuildInfo.getBuildBranch() != null) {
            mCmdArgs.add(mBuildInfo.getBuildBranch());
        } else {
            throw new RuntimeException("Branch option is required for the sub invocation.");
        }
        mCmdArgs.add("--build-flavor");
        if (mSubBuildFlavor != null) {
            mCmdArgs.add(mSubBuildFlavor);
        } else if (mBuildInfo.getBuildFlavor() != null) {
            mCmdArgs.add(mBuildInfo.getBuildFlavor());
        } else {
            throw new RuntimeException("Build flavor option is required for the sub invocation.");
        }

        for (String apk : mSubApkPath) {
            mCmdArgs.add("--apk-path");
            String apkPath =
                    String.format(
                            "%s%s%s",
                            ((IFolderBuildInfo) mBuildInfo).getRootDir().getAbsolutePath(),
                            File.separator,
                            apk);
            mCmdArgs.add(apkPath);
        }
    }

    /** {@inheritDoc} */
    @Override
    protected void postRun(ITestInvocationListener listener, boolean exception, long elapsedTime) {
        super.postRun(listener, exception, elapsedTime);
        reportMetrics(elapsedTime, listener);
        FileUtil.deleteFile(mAgent);

        // Evaluate coverage from the subprocess
        if (mEnableCoverage) {
            InputStreamSource coverage = null;
            File xmlResult = null;
            try {
                xmlResult = processExecData(mDestCoverageFile, mRootDir);
                coverage = new FileInputStreamSource(xmlResult);
                listener.testLog("coverage_xml", LogDataType.JACOCO_XML, coverage);
            } catch (IOException e) {
                if (exception) {
                    // If exception was thrown above, we only log this one since it's most
                    // likely related to it.
                    CLog.e(e);
                } else {
                    throw new RuntimeException(e);
                }
            } finally {
                FileUtil.deleteFile(mDestCoverageFile);
                StreamUtil.cancel(coverage);
                FileUtil.deleteFile(xmlResult);
            }
        }
        if (mEnableHprof) {
            logHprofResults(mHprofFile, listener);
        }

        if (mTmpDir != null) {
            testTmpDirClean(mTmpDir, listener);
        }
        cleanTmpFile();
    }

    @VisibleForTesting
    void cleanTmpFile() {
        FileUtil.deleteFile(mHprofFile);
        FileUtil.deleteFile(mDestCoverageFile);
        FileUtil.deleteFile(mAgent);
    }

    /**
     * Helper to add arguments required for code coverage collection.
     *
     * @param jacocoAgent the jacoco args file to run the coverage.
     * @param args list of arguments that will be run in the subprocess.
     * @param destfile destination file where the report will be put.
     */
    private void addCoverageArgs(File jacocoAgent, List<String> args, File destfile) {
        String javaagent = String.format("-javaagent:%s=destfile=%s,"
                + "includes=com.android.tradefed*:com.google.android.tradefed*",
                jacocoAgent.getAbsolutePath(),
                destfile.getAbsolutePath());
        args.add(javaagent);
    }

    /**
     * Returns a {@link File} pointing to the jacoco args jar file extracted from the resources.
     */
    private File extractJacocoAgent() throws IOException {
        String jacocoAgentRes = "/jacoco/jacocoagent.jar";
        InputStream jacocoAgentStream = getClass().getResourceAsStream(jacocoAgentRes);
        if (jacocoAgentStream == null) {
            throw new IOException("Could not find " + jacocoAgentRes);
        }
        File jacocoAgent = FileUtil.createTempFile("jacocoagent", ".jar");
        FileUtil.writeToFile(jacocoAgentStream, jacocoAgent);
        return jacocoAgent;
    }

    /**
     * Helper to process the execution data into user readable format (xml) that can easily be
     * parsed.
     *
     * @param executionData output files of the java args jacoco.
     * @param rootDir base directory of downloaded TF
     * @return a {@link File} pointing to the human readable xml result file.
     */
    private File processExecData(File executionData, String rootDir) throws IOException {
        File xmlReport = FileUtil.createTempFile("coverage_xml", ".xml");
        InputStream template = getClass().getResourceAsStream(mAntConfigResource);
        if (template == null) {
            throw new IOException("Could not find " + mAntConfigResource);
        }
        String jacocoAntRes = "/jacoco/jacocoant.jar";
        InputStream jacocoAntStream = getClass().getResourceAsStream(jacocoAntRes);
        if (jacocoAntStream == null) {
            throw new IOException("Could not find " + jacocoAntRes);
        }
        File antConfig = FileUtil.createTempFile("ant-merge_", ".xml");
        File jacocoAnt = FileUtil.createTempFile("jacocoant", ".jar");
        try {
            FileUtil.writeToFile(template, antConfig);
            FileUtil.writeToFile(jacocoAntStream, jacocoAnt);
            String[] cmd = {"ant", "-f", antConfig.getPath(),
                    "-Djacocoant.path=" + jacocoAnt.getAbsolutePath(),
                    "-Dexecution.files=" + executionData.getAbsolutePath(),
                    "-Droot.dir=" + rootDir,
                    "-Ddest.file=" + xmlReport.getAbsolutePath()};
            CommandResult result = RunUtil.getDefault().runTimedCmd(COVERAGE_REPORT_TIMEOUT_MS,
                    cmd);
            CLog.d(result.getStdout());
            if (!CommandStatus.SUCCESS.equals(result.getStatus())) {
                throw new IOException(result.getStderr());
            }
            return xmlReport;
        } finally {
            FileUtil.deleteFile(antConfig);
            FileUtil.deleteFile(jacocoAnt);
        }
    }

    /**
     * Report an elapsed-time metric to keep track of it.
     *
     * @param elapsedTime time it took the subprocess to run.
     * @param listener the {@link ITestInvocationListener} where to report the metric.
     */
    private void reportMetrics(long elapsedTime, ITestInvocationListener listener) {
        if (elapsedTime == -1l) {
            return;
        }
        listener.testRunStarted("elapsed-time", 1);
        TestIdentifier tid = new TestIdentifier("elapsed-time", "run-elapsed-time");
        listener.testStarted(tid);
        Map<String, String> runMetrics = new HashMap<>();
        runMetrics.put("elapsed-time", Long.toString(elapsedTime));
        listener.testEnded(tid, runMetrics);
        listener.testRunEnded(elapsedTime, runMetrics);
    }

    /**
     * Extra test to ensure no files are created by the unit tests in the subprocess and not
     * cleaned.
     *
     * @param tmpDir the temporary dir of the subprocess.
     * @param listener the {@link ITestInvocationListener} where to report the test.
     */
    @VisibleForTesting
    protected void testTmpDirClean(File tmpDir, ITestInvocationListener listener) {
        listener.testRunStarted("temporaryFiles", 1);
        TestIdentifier tid = new TestIdentifier("temporary-files", "testIfClean");
        listener.testStarted(tid);
        String[] listFiles = tmpDir.list();
        List<String> unmatchedFiles = new ArrayList<String>();
        List<String> patterns = new ArrayList<String>(Arrays.asList(EXPECTED_TMP_FILE_PATTERNS));
        for (String file : Arrays.asList(listFiles)) {
            Boolean matchFound = false;
            for (String pattern : patterns) {
                if (Pattern.matches(pattern, file)) {
                    patterns.remove(pattern);
                    matchFound = true;
                    break;
                }
            }
            if (!matchFound) {
                unmatchedFiles.add(file);
            }
        }
        if (unmatchedFiles.size() > 0) {
            String trace = String.format("Found '%d' unexpected temporary files: %s.\nOnly "
                    + "expected files are: %s. And each should appears only once.",
                    unmatchedFiles.size(), unmatchedFiles,
                    Arrays.asList(EXPECTED_TMP_FILE_PATTERNS));
            listener.testFailed(tid, trace);
        }
        listener.testEnded(tid, Collections.emptyMap());
        listener.testRunEnded(0, Collections.emptyMap());
    }

    /**
     * Helper to log and report as metric the hprof data.
     *
     * @param hprofFile file containing the Hprof report
     * @param listener the {@link ITestInvocationListener} where to report the test.
     */
    private void logHprofResults(File hprofFile, ITestInvocationListener listener) {
        if (hprofFile == null) {
            CLog.w("Hprof file was null. Skipping parsing.");
            return;
        }
        if (!hprofFile.exists()) {
            CLog.w("Hprof file %s was not found. Skipping parsing.", hprofFile.getAbsolutePath());
            return;
        }
        InputStreamSource memory = null;
        try {
            memory = new FileInputStreamSource(hprofFile);
            listener.testLog("hprof", LogDataType.TEXT, memory);
        } finally {
            StreamUtil.cancel(memory);
        }
        HprofAllocSiteParser parser = new HprofAllocSiteParser();
        try {
            Map<String, String> results = parser.parse(hprofFile);
            if (results.isEmpty()) {
                CLog.d("No allocation site found from hprof file");
                return;
            }
            listener.testRunStarted("hprofAllocSites", 1);
            TestIdentifier tid = new TestIdentifier("hprof", "allocationSites");
            listener.testStarted(tid);
            listener.testEnded(tid, results);
            listener.testRunEnded(0, Collections.emptyMap());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}