Ensure the adb path is consistent on remote VM

Test: unit tests
Bug: 119135210
Change-Id: I9dcc19d37c9c252b26af18935c17955a5e06bd38
diff --git a/src/com/android/tradefed/invoker/RemoteInvocationExecution.java b/src/com/android/tradefed/invoker/RemoteInvocationExecution.java
index c705aba..cacb226 100644
--- a/src/com/android/tradefed/invoker/RemoteInvocationExecution.java
+++ b/src/com/android/tradefed/invoker/RemoteInvocationExecution.java
@@ -71,6 +71,7 @@
 
     private String mRemoteTradefedDir = null;
     private String mRemoteFinalResult = null;
+    private String mRemoteAdbPath = null;
 
     @Override
     public boolean fetchBuild(
@@ -102,11 +103,13 @@
             throws Throwable {
         ManagedRemoteDevice device = (ManagedRemoteDevice) context.getDevices().get(0);
         GceAvdInfo info = device.getRemoteAvdInfo();
+
         // Run remote TF (new tests?)
         IRunUtil runUtil = new RunUtil();
 
         TestDeviceOptions options = device.getOptions();
         String mainRemoteDir = getRemoteMainDir(options);
+        mRemoteAdbPath = String.format("/home/%s/bin/adb", options.getInstanceUser());
 
         String tfPath = System.getProperty("TF_JAR_DIR");
         if (tfPath == null) {
@@ -258,20 +261,17 @@
         List<String> remoteTfCommand = new ArrayList<>();
         remoteTfCommand.add("pushd");
         remoteTfCommand.add(mRemoteTradefedDir + ";");
+        remoteTfCommand.add(String.format("PATH=%s:$PATH", new File(mRemoteAdbPath).getParent()));
+        remoteTfCommand.add("screen -dmSU tradefed sh -c");
 
-        remoteTfCommand.add("TF_GLOBAL_CONFIG=" + globalConfig.getName());
-        remoteTfCommand.add("nohup");
-        remoteTfCommand.add("./tradefed.sh");
-        remoteTfCommand.add("run");
-        remoteTfCommand.add("commandAndExit");
-        remoteTfCommand.add(mRemoteTradefedDir + configFile.getName());
+        String tfCommand = ("TF_GLOBAL_CONFIG=" + globalConfig.getName());
+        tfCommand +=
+                (" ./tradefed.sh run commandAndExit " + mRemoteTradefedDir + configFile.getName());
         if (config.getCommandOptions().shouldUseRemoteSandboxMode()) {
-            remoteTfCommand.add("--" + CommandOptions.USE_SANDBOX);
+            tfCommand += (" --" + CommandOptions.USE_SANDBOX);
         }
-        remoteTfCommand.add("> " + STDOUT_FILE);
-        remoteTfCommand.add("2> " + STDERR_FILE);
-        remoteTfCommand.add("&");
-
+        tfCommand += (" > " + STDOUT_FILE + " 2> " + STDERR_FILE);
+        remoteTfCommand.add("\"" + tfCommand + "\"");
         // Kick off the actual remote run
         CommandResult resultRemoteExecution =
                 GceManager.remoteSshCommandExecution(
@@ -315,7 +315,9 @@
                                         TimeUtil.formatElapsedTime(maxTimeout))));
                 break;
             }
-            RunUtil.getDefault().sleep(15000L);
+            if (stillRunning) {
+                RunUtil.getDefault().sleep(15000L);
+            }
         }
         File resultFile = null;
         if (!stillRunning) {
@@ -380,7 +382,14 @@
     private void resetAdb(GceAvdInfo info, TestDeviceOptions options, IRunUtil runUtil) {
         CommandResult probAdb =
                 GceManager.remoteSshCommandExecution(
-                        info, options, runUtil, 120000L, "adb", "devices");
+                        info, options, runUtil, 120000L, mRemoteAdbPath, "devices");
         CLog.d("remote adb prob: %s", probAdb.getStdout());
+        CLog.d("%s", probAdb.getStderr());
+
+        CommandResult versionAdb =
+                GceManager.remoteSshCommandExecution(
+                        info, options, runUtil, 120000L, mRemoteAdbPath, "version");
+        CLog.d("version adb: %s", versionAdb.getStdout());
+        CLog.d("%s", versionAdb.getStderr());
     }
 }
diff --git a/src/com/android/tradefed/sandbox/TradefedSandbox.java b/src/com/android/tradefed/sandbox/TradefedSandbox.java
index f1156c8..719d48b 100644
--- a/src/com/android/tradefed/sandbox/TradefedSandbox.java
+++ b/src/com/android/tradefed/sandbox/TradefedSandbox.java
@@ -277,6 +277,9 @@
             }
             String[] args = QuotationAwareTokenizer.tokenizeLine(commandLine);
             mGlobalConfig = dumpGlobalConfig(config, new HashSet<>());
+            try (InputStreamSource source = new FileInputStreamSource(mGlobalConfig)) {
+                listener.testLog("sandbox-global-config", LogDataType.XML, source);
+            }
             DumpCmd mode = DumpCmd.RUN_CONFIG;
             if (config.getCommandOptions().shouldUseSandboxTestMode()) {
                 mode = DumpCmd.TEST_MODE;
diff --git a/tests/src/com/android/tradefed/sandbox/TradefedSandboxTest.java b/tests/src/com/android/tradefed/sandbox/TradefedSandboxTest.java
index d0d7520..b6f09df 100644
--- a/tests/src/com/android/tradefed/sandbox/TradefedSandboxTest.java
+++ b/tests/src/com/android/tradefed/sandbox/TradefedSandboxTest.java
@@ -31,6 +31,7 @@
 import com.android.tradefed.invoker.IInvocationContext;
 import com.android.tradefed.invoker.InvocationContext;
 import com.android.tradefed.result.ITestInvocationListener;
+import com.android.tradefed.result.LogDataType;
 import com.android.tradefed.util.CommandResult;
 import com.android.tradefed.util.CommandStatus;
 import com.android.tradefed.util.FileUtil;
@@ -112,6 +113,10 @@
         mMockRunUtil.setEnvVariable(
                 EasyMock.eq(GlobalConfiguration.GLOBAL_CONFIG_VARIABLE), EasyMock.anyObject());
         mMockRunUtil.setEnvVariablePriority(EnvPriority.SET);
+        mMockListener.testLog(
+                EasyMock.eq("sandbox-global-config"),
+                EasyMock.eq(LogDataType.XML),
+                EasyMock.anyObject());
         CommandResult result = new CommandResult();
         result.setStatus(CommandStatus.SUCCESS);
         EasyMock.expect(
@@ -150,6 +155,10 @@
         mMockRunUtil.setEnvVariable(
                 EasyMock.eq(GlobalConfiguration.GLOBAL_CONFIG_VARIABLE), EasyMock.anyObject());
         mMockRunUtil.setEnvVariablePriority(EnvPriority.SET);
+        mMockListener.testLog(
+                EasyMock.eq("sandbox-global-config"),
+                EasyMock.eq(LogDataType.XML),
+                EasyMock.anyObject());
         CommandResult result = new CommandResult();
         result.setStatus(CommandStatus.FAILED);
         result.setStderr("Ouch I failed.");