Retry getImageVersion command on invalid output.

Change-Id: Ib9869515a27e305fe7cd86cb4f768d14167fce5d
diff --git a/src/com/android/tradefed/targetprep/FastbootDeviceFlasher.java b/src/com/android/tradefed/targetprep/FastbootDeviceFlasher.java
index 6371a91..cdab40e 100644
--- a/src/com/android/tradefed/targetprep/FastbootDeviceFlasher.java
+++ b/src/com/android/tradefed/targetprep/FastbootDeviceFlasher.java
@@ -35,6 +35,8 @@
 public class FastbootDeviceFlasher implements IDeviceFlasher  {
     public static final String BASEBAND_IMAGE_NAME = "radio";
 
+    private static final int MAX_RETRY_ATTEMPTS = 3;
+
     private UserDataFlashOption mUserDataFlashOption = UserDataFlashOption.FLASH;
 
     private IFlashingResourcesRetriever mResourceRetriever;
@@ -500,16 +502,25 @@
      */
     protected String getImageVersion(ITestDevice device, String imageName)
             throws DeviceNotAvailableException, TargetSetupError {
+        int attempts = 0;
         String versionQuery = String.format("version-%s", imageName);
-        String queryOutput = executeFastbootCmd(device, "getvar", versionQuery);
         String patternString = String.format("%s:\\s(.*)\\s", versionQuery);
         Pattern versionOutputPattern = Pattern.compile(patternString);
-        Matcher matcher = versionOutputPattern.matcher(queryOutput);
-        if (matcher.find()) {
-            return matcher.group(1);
+
+        while (attempts < MAX_RETRY_ATTEMPTS) {
+            String queryOutput = executeFastbootCmd(device, "getvar", versionQuery);
+            Matcher matcher = versionOutputPattern.matcher(queryOutput);
+            if (matcher.find()) {
+                return matcher.group(1);
+            } else {
+                attempts++;
+                CLog.w("Could not find version for '%s'. Output '%s', retrying.",
+                            imageName, queryOutput);
+                continue;
+            }
         }
-        throw new TargetSetupError(String.format("Could not find version for '%s'. Output '%s'",
-                imageName, queryOutput));
+        throw new TargetSetupError(String.format(
+                "Could not find version for '%s' after %d retry attempts", imageName, attempts));
     }
 
     /**
diff --git a/tests/src/com/android/tradefed/targetprep/FastbootDeviceFlasherTest.java b/tests/src/com/android/tradefed/targetprep/FastbootDeviceFlasherTest.java
index 1c0f27d..c60eb82 100644
--- a/tests/src/com/android/tradefed/targetprep/FastbootDeviceFlasherTest.java
+++ b/tests/src/com/android/tradefed/targetprep/FastbootDeviceFlasherTest.java
@@ -126,6 +126,33 @@
     }
 
     /**
+     * Test that a fastboot command is retried if it does not output anything.
+     */
+    public void testRetryGetVersionCommand() throws DeviceNotAvailableException, TargetSetupError {
+        // The first time command is tried, make it return an empty string.
+        CommandResult fastbootInValidResult = new CommandResult();
+        fastbootInValidResult.setStatus(CommandStatus.SUCCESS);
+        // output of getvar is on stderr for some unknown reason
+        fastbootInValidResult.setStderr("");
+        fastbootInValidResult.setStdout("");
+
+        // Return the correct value on second attempt.
+        CommandResult fastbootValidResult = new CommandResult();
+        fastbootValidResult.setStatus(CommandStatus.SUCCESS);
+        fastbootValidResult.setStderr("version-baseband: 1.0.1\nfinished. total time: 0.001s");
+        fastbootValidResult.setStdout("");
+
+        EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "version-baseband")).
+                andReturn(fastbootInValidResult);
+        EasyMock.expect(mMockDevice.executeFastbootCommand("getvar", "version-baseband")).
+                andReturn(fastbootValidResult);
+
+        EasyMock.replay(mMockDevice);
+        String actualVersion = mFlasher.getImageVersion(mMockDevice, "baseband");
+        assertEquals("1.0.1", actualVersion);
+    }
+
+    /**
      * Test that baseband can be flashed when current baseband version is empty
      */
     public void testFlashBaseband_noVersion()