Revert "Allow collecting diagnostics from shell commands"

This reverts commit 47b6619fa8.

Reason for revert: DroidMonitor: Potential culprit for http://b/399299587 - verifying through ABTD before revert submission. This is part of the standard investigation process, and does not mean your CL will be reverted.

Change-Id: Id7717258b960ac18a27575fdc2b0852b42ef4e7b
This commit is contained in:
Liana Kazanova (xWF) 2025-02-26 12:20:26 -08:00 • committed by Gerrit Code Review
commit 3eec0163fc
2 changed files with 9 additions and 97 deletions

View file

@ -430,32 +430,19 @@ class ConnectivityDiagnosticsCollector : BaseMetricListener() {
* @param dumpsysCmd The dumpsys command to run (for example "connectivity").
* @param exceptionContext An exception to write a stacktrace to the dump for context.
*/
fun collectDumpsys(dumpsysCmd: String, exceptionContext: Throwable? = null) =
collectCommandOutput("dumpsys $dumpsysCmd", exceptionContext = exceptionContext)
/**
* Add the output of a command to the test data dump.
*
* <p>The output will be collected immediately, and exported to a test artifact file when the
* test ends.
* @param cmd The command to run. Stdout of the command will be collected.
* @param shell The shell to run the command in.
* @param exceptionContext An exception to write a stacktrace to the dump for context.
*/
fun collectCommandOutput(
cmd: String,
shell: String = "sh",
exceptionContext: Throwable? = null
) {
Log.i(TAG, "Collecting '$cmd' for test artifacts")
fun collectDumpsys(dumpsysCmd: String, exceptionContext: Throwable? = null) {
Log.i(TAG, "Collecting dumpsys $dumpsysCmd for test artifacts")
PrintWriter(buffer).let {
it.println("--- $cmd at ${ZonedDateTime.now()} ---")
it.println("--- Dumpsys $dumpsysCmd at ${ZonedDateTime.now()} ---")
maybeWriteExceptionContext(it, exceptionContext)
it.flush()
}
runCommandInShell(cmd, shell) { stdout, _ ->
stdout.copyTo(buffer)
ParcelFileDescriptor.AutoCloseInputStream(
InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand(
"dumpsys $dumpsysCmd"
)
).use {
it.copyTo(buffer)
}
}

View file

@ -1,75 +0,0 @@
/*
* Copyright (C) 2025 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.
*/
@file:JvmName("ShellUtil")
package com.android.testutils
import android.app.UiAutomation
import android.os.ParcelFileDescriptor.AutoCloseInputStream
import android.os.ParcelFileDescriptor.AutoCloseOutputStream
import androidx.test.platform.app.InstrumentationRegistry
import java.io.InputStream
/**
* Run a command in a shell.
*
* Compared to [UiAutomation.executeShellCommand], this allows running commands with pipes and
* redirections. [UiAutomation.executeShellCommand] splits the command on spaces regardless of
* quotes, so it is not able to run commands like `sh -c "echo 123 > some_file"`.
*
* @param cmd Shell command to run.
* @param shell Command used to run the shell.
* @param outputProcessor Function taking stdout, stderr as argument. The streams will be closed
* when this function returns.
* @return Result of [outputProcessor].
*/
fun <T> runCommandInShell(
cmd: String,
shell: String = "sh",
outputProcessor: (InputStream, InputStream) -> T,
): T {
val (stdout, stdin, stderr) = InstrumentationRegistry.getInstrumentation().uiAutomation
.executeShellCommandRwe(shell)
AutoCloseOutputStream(stdin).bufferedWriter().use { it.write(cmd) }
AutoCloseInputStream(stdout).use { outStream ->
AutoCloseInputStream(stderr).use { errStream ->
return outputProcessor(outStream, errStream)
}
}
}
/**
* Run a command in a shell.
*
* Overload of [runCommandInShell] that reads and returns stdout as String.
*/
fun runCommandInShell(
cmd: String,
shell: String = "sh",
) = runCommandInShell(cmd, shell) { stdout, _ ->
stdout.reader().use { it.readText() }
}
/**
* Run a command in a root shell.
*
* This is generally only usable on devices on which [DeviceInfoUtils.isDebuggable] is true.
* @see runCommandInShell
*/
fun runCommandInRootShell(
cmd: String
) = runCommandInShell(cmd, shell = "su root sh")