Add retries to test steps.

This is to make the recipe match the current functionality in the
engine.py recipe.

Bug: https://github.com/flutter/flutter/issues/81855
Change-Id: I34634657cea9fbc8154b3814b44d2bf73b8bd914
Reviewed-on: https://flutter-review.googlesource.com/c/recipes/+/31600
Reviewed-by: Keyong Han <keyonghan@google.com>
Commit-Queue: Godofredo Contreras <godofredoc@google.com>
Reviewed-by: Xilai Zhang <xilaizhang@google.com>
diff --git a/recipes/engine_v2/builder.expected/basic.json b/recipes/engine_v2/builder.expected/basic.json
index 4262dfe..a8661f1 100644
--- a/recipes/engine_v2/builder.expected/basic.json
+++ b/recipes/engine_v2/builder.expected/basic.json
@@ -673,7 +673,7 @@
         "[CACHE]/builder/src/third_party/dart/tools/sdks/dart-sdk/bin"
       ]
     },
-    "name": "mytest"
+    "name": "test: mytest"
   },
   {
     "cmd": [
diff --git a/recipes/engine_v2/builder.expected/basic_gcs.json b/recipes/engine_v2/builder.expected/basic_gcs.json
index 93d1508..44ea53f 100644
--- a/recipes/engine_v2/builder.expected/basic_gcs.json
+++ b/recipes/engine_v2/builder.expected/basic_gcs.json
@@ -673,7 +673,7 @@
         "[CACHE]/builder/src/third_party/dart/tools/sdks/dart-sdk/bin"
       ]
     },
-    "name": "mytest"
+    "name": "test: mytest"
   },
   {
     "cmd": [
diff --git a/recipes/engine_v2/builder.expected/mac.json b/recipes/engine_v2/builder.expected/mac.json
index 2278047..99f40d3 100644
--- a/recipes/engine_v2/builder.expected/mac.json
+++ b/recipes/engine_v2/builder.expected/mac.json
@@ -747,7 +747,7 @@
         "[CACHE]/builder/src/third_party/dart/tools/sdks/dart-sdk/bin"
       ]
     },
-    "name": "mytest"
+    "name": "test: mytest"
   },
   {
     "cmd": [
diff --git a/recipes/engine_v2/builder.expected/monorepo.json b/recipes/engine_v2/builder.expected/monorepo.json
index 6a6505f..83c77cf 100644
--- a/recipes/engine_v2/builder.expected/monorepo.json
+++ b/recipes/engine_v2/builder.expected/monorepo.json
@@ -1169,7 +1169,7 @@
         "hostname": "rdbhost"
       }
     },
-    "name": "mytest"
+    "name": "test: mytest"
   },
   {
     "cmd": [
diff --git a/recipes/engine_v2/builder.py b/recipes/engine_v2/builder.py
index f015eb3..f48873d 100644
--- a/recipes/engine_v2/builder.py
+++ b/recipes/engine_v2/builder.py
@@ -43,6 +43,8 @@
     'flutter/os_utils',
     'flutter/osx_sdk',
     'flutter/repo_util',
+    'flutter/retry',
+    'flutter/test_utils',
     'fuchsia/cas_util',
     'recipe_engine/buildbucket',
     'recipe_engine/context',
@@ -104,7 +106,17 @@
       # TODO(godofredoc): Optimize to run multiple local tests in parallel.
       command.append(checkout.join(test.get('script')))
       command.extend(test.get('parameters', []))
-      api.step(test.get('name'), command)
+      #api.step(test.get('name'), command)
+      step_name = api.test_utils.test_step_name(test.get('name'))
+
+      def run_test():
+        return api.step(step_name, command)
+
+      # Rerun test step 3 times by default if failing.
+      # TODO(keyonghan): notify tree gardener for test failures/flakes:
+      # https://github.com/flutter/flutter/issues/89308
+      api.retry.wrap(run_test, step_name=test.get('name'))
+
     for archive_config in archives:
       outputs[archive_config['name']] = Archive(api, checkout, archive_config)