Expose testing swarming task IDs as an output property.

The results uploader previously could get these from a Swarming RPC API,
but that API no longer exposes this, so we will instead set an output
property and use the BuildBucket RPC API to get the same information.

Bug: 43388
Change-Id: Iaa4c46e2397e9c3c99bb9740b94b755f51c806c2
diff --git a/recipe_modules/testing/api.py b/recipe_modules/testing/api.py
index b12c77b..7efe0bf 100644
--- a/recipe_modules/testing/api.py
+++ b/recipe_modules/testing/api.py
@@ -22,6 +22,7 @@
 KERNEL_LOG = 'kernel_log.txt'
 COVARGS_LOG_LEVEL = 'debug'
 COVARGS_OUTPUT_JSON = 'covargs-output.json'
+SWARMING_TASK_IDS_PROPERTY = 'testing-swarming-task-ids'
 
 
 @attr.s
@@ -37,8 +38,8 @@
       uploaded to GCS when upload_results() is called.
     outputs (dict[str]str): A mapping from of relative paths to files
       containing stdout+stderr data to strings containing those contents.
-    env_name (str): The name of the task that ran these tests.
     swarming_task_id (str): The ID of the task that ran these tests.
+    env_name (str): The name of the task that ran these tests.
     tests (seq(testsharder.Test)): The tests that this task was instructed
       to run (as opposed to the results of the tests that this task *did*
       run, which are enumerated in `summary`).
@@ -58,8 +59,8 @@
   results_dir = attr.ib(type=Path)
   output_dir = attr.ib(type=Path)
   outputs = attr.ib(type=dict)
+  swarming_task_id = attr.ib(type=str)
   _env_name = attr.ib(type=str)
-  _swarming_task_id = attr.ib(type=str)
   _tests = attr.ib(type=testsharder_api.Test)
   _legacy_qemu = attr.ib(type=bool)
   _api = attr.ib(type=recipe_api.RecipeApi)
@@ -165,8 +166,7 @@
   def upload_results(self, gcs_bucket, upload_to_catapult):
     """Upload select test results (e.g., coverage data) to a given GCS bucket."""
     assert gcs_bucket
-    with self._api.step.nest('upload %s test results' %
-                             self._env_name) as presentation:
+    with self._api.step.nest('upload %s test results' % self._env_name):
       if self.summary:
         # Save the summary JSON to the test shard output dir so it gets
         # uploaded to GCS for easy access by e.g. Dachsiaboard.
@@ -186,7 +186,7 @@
         source=self.output_dir,
         bucket=gcs_bucket,
         # Namespace to avoid collision across shards and attempts.
-        subpath='%s/%s' % (self._env_name, self._swarming_task_id),
+        subpath='%s/%s' % (self._env_name, self.swarming_task_id),
     )
 
   def raise_failures(self):
@@ -360,8 +360,8 @@
           from_fuchsia=self._targets_fuchsia,
           results_dir=results_dir,
           outputs=test_results_map,
-          env_name=result.name,
           swarming_task_id=attempt.task_id,
+          env_name=result.name,
           tests=self._tests,
           legacy_qemu=self._uses_legacy_qemu,
           api=api,
@@ -620,6 +620,12 @@
       for test, output in test_results.passed_test_outputs.iteritems():
         self._report_test_result(test, output, passed=True)
 
+    # Consumed by the google3 results uploader.
+    swarming_task_ids = presentation.properties.get(SWARMING_TASK_IDS_PROPERTY,
+                                                    [])
+    swarming_task_ids.append(test_results.swarming_task_id)
+    presentation.properties[SWARMING_TASK_IDS_PROPERTY] = swarming_task_ids
+
   def _report_test_result(self, test, output, passed):
     name = test
     if not passed:
@@ -897,8 +903,8 @@
             from_fuchsia=True,
             results_dir=test_results_dir,
             outputs=test_results_map,
-            env_name=task_result.name,
             swarming_task_id=task_result.id,
+            env_name=task_result.name,
             tests=tests,
             legacy_qemu=self.m.emu.is_emulator_type(device_type),
             api=self.m,
diff --git a/recipe_modules/testing/examples/full.expected/asan_tests.json b/recipe_modules/testing/examples/full.expected/asan_tests.json
index 4a955fa..b73b234 100644
--- a/recipe_modules/testing/examples/full.expected/asan_tests.json
+++ b/recipe_modules/testing/examples/full.expected/asan_tests.json
@@ -1319,7 +1319,8 @@
       "@@@STEP_LOG_END@summary.json@@@",
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
       "@@@STEP_LOG_END@goodbye-txt@@@",
-      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@"
+      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {
diff --git a/recipe_modules/testing/examples/full.expected/async.json b/recipe_modules/testing/examples/full.expected/async.json
index fe59ba4..c84cf18 100644
--- a/recipe_modules/testing/examples/full.expected/async.json
+++ b/recipe_modules/testing/examples/full.expected/async.json
@@ -1491,7 +1491,8 @@
       "@@@STEP_LOG_LINE@summary.json@}@@@",
       "@@@STEP_LOG_END@summary.json@@@",
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
-      "@@@STEP_LOG_END@goodbye-txt@@@"
+      "@@@STEP_LOG_END@goodbye-txt@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {
diff --git a/recipe_modules/testing/examples/full.expected/isolated_test_device_no_pave.json b/recipe_modules/testing/examples/full.expected/isolated_test_device_no_pave.json
index f7a862a..9db0309 100644
--- a/recipe_modules/testing/examples/full.expected/isolated_test_device_no_pave.json
+++ b/recipe_modules/testing/examples/full.expected/isolated_test_device_no_pave.json
@@ -1306,7 +1306,8 @@
       "@@@STEP_LOG_END@summary.json@@@",
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
       "@@@STEP_LOG_END@goodbye-txt@@@",
-      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@"
+      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {
diff --git a/recipe_modules/testing/examples/full.expected/isolated_tests_test_failure.json b/recipe_modules/testing/examples/full.expected/isolated_tests_test_failure.json
index a2363b9..f8328f5 100644
--- a/recipe_modules/testing/examples/full.expected/isolated_tests_test_failure.json
+++ b/recipe_modules/testing/examples/full.expected/isolated_tests_test_failure.json
@@ -1323,6 +1323,7 @@
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
       "@@@STEP_LOG_END@goodbye-txt@@@",
       "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@",
       "@@@STEP_FAILURE@@@"
     ]
   },
@@ -1583,6 +1584,7 @@
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
       "@@@STEP_LOG_END@goodbye-txt@@@",
       "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"2\"]@@@",
       "@@@STEP_FAILURE@@@"
     ]
   },
diff --git a/recipe_modules/testing/examples/full.expected/upload_test_coverage.json b/recipe_modules/testing/examples/full.expected/upload_test_coverage.json
index 4a955fa..b73b234 100644
--- a/recipe_modules/testing/examples/full.expected/upload_test_coverage.json
+++ b/recipe_modules/testing/examples/full.expected/upload_test_coverage.json
@@ -1319,7 +1319,8 @@
       "@@@STEP_LOG_END@summary.json@@@",
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
       "@@@STEP_LOG_END@goodbye-txt@@@",
-      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@"
+      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {
diff --git a/recipes/fuchsia/fuchsia.expected/successful_build_and_test_not_in_shards.json b/recipes/fuchsia/fuchsia.expected/successful_build_and_test_not_in_shards.json
index 023e22a..f9887f2 100644
--- a/recipes/fuchsia/fuchsia.expected/successful_build_and_test_not_in_shards.json
+++ b/recipes/fuchsia/fuchsia.expected/successful_build_and_test_not_in_shards.json
@@ -507,7 +507,8 @@
       "@@@STEP_LOG_END@summary.json@@@",
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
       "@@@STEP_LOG_END@goodbye-txt@@@",
-      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@"
+      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"610\"]@@@"
     ]
   },
   {
diff --git a/recipes/fuchsia_perf.expected/device_tests.json b/recipes/fuchsia_perf.expected/device_tests.json
index 289dbc6..5501ce7 100644
--- a/recipes/fuchsia_perf.expected/device_tests.json
+++ b/recipes/fuchsia_perf.expected/device_tests.json
@@ -1955,7 +1955,8 @@
       "@@@STEP_LOG_END@summary.json@@@",
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
       "@@@STEP_LOG_END@goodbye-txt@@@",
-      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@"
+      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {
diff --git a/recipes/fuchsia_perf.expected/failed_run.json b/recipes/fuchsia_perf.expected/failed_run.json
index 5ca984c..6392773 100644
--- a/recipes/fuchsia_perf.expected/failed_run.json
+++ b/recipes/fuchsia_perf.expected/failed_run.json
@@ -1970,6 +1970,7 @@
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
       "@@@STEP_LOG_END@goodbye-txt@@@",
       "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@",
       "@@@STEP_FAILURE@@@"
     ]
   },
diff --git a/recipes/fuchsia_perf.expected/successful_run.json b/recipes/fuchsia_perf.expected/successful_run.json
index 339e2c2..cfd4ba2 100644
--- a/recipes/fuchsia_perf.expected/successful_run.json
+++ b/recipes/fuchsia_perf.expected/successful_run.json
@@ -1966,7 +1966,8 @@
       "@@@STEP_LOG_END@summary.json@@@",
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
       "@@@STEP_LOG_END@goodbye-txt@@@",
-      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@"
+      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {
diff --git a/recipes/fuchsia_perf.expected/with_patch.json b/recipes/fuchsia_perf.expected/with_patch.json
index 61d4aa9..c501f31 100644
--- a/recipes/fuchsia_perf.expected/with_patch.json
+++ b/recipes/fuchsia_perf.expected/with_patch.json
@@ -2055,7 +2055,8 @@
       "@@@STEP_LOG_END@summary.json@@@",
       "@@@STEP_LOG_LINE@goodbye-txt@goodbye@@@",
       "@@@STEP_LOG_END@goodbye-txt@@@",
-      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@"
+      "@@@STEP_LINK@test outputs@https://isolateserver.appspot.com/browse?namespace=default-gzip&hash=abc123@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {
diff --git a/recipes/fuchsia_perfcompare.expected/successful_run.json b/recipes/fuchsia_perfcompare.expected/successful_run.json
index 9ed085b..dce704b 100644
--- a/recipes/fuchsia_perfcompare.expected/successful_run.json
+++ b/recipes/fuchsia_perfcompare.expected/successful_run.json
@@ -4019,7 +4019,8 @@
       "@@@STEP_LOG_LINE@summary.json@    }@@@",
       "@@@STEP_LOG_LINE@summary.json@  ]@@@",
       "@@@STEP_LOG_LINE@summary.json@}@@@",
-      "@@@STEP_LOG_END@summary.json@@@"
+      "@@@STEP_LOG_END@summary.json@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {
@@ -4209,7 +4210,8 @@
       "@@@STEP_LOG_LINE@summary.json@    }@@@",
       "@@@STEP_LOG_LINE@summary.json@  ]@@@",
       "@@@STEP_LOG_LINE@summary.json@}@@@",
-      "@@@STEP_LOG_END@summary.json@@@"
+      "@@@STEP_LOG_END@summary.json@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {
@@ -4414,7 +4416,8 @@
       "@@@STEP_LOG_LINE@summary.json@    }@@@",
       "@@@STEP_LOG_LINE@summary.json@  ]@@@",
       "@@@STEP_LOG_LINE@summary.json@}@@@",
-      "@@@STEP_LOG_END@summary.json@@@"
+      "@@@STEP_LOG_END@summary.json@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {
@@ -4604,7 +4607,8 @@
       "@@@STEP_LOG_LINE@summary.json@    }@@@",
       "@@@STEP_LOG_LINE@summary.json@  ]@@@",
       "@@@STEP_LOG_LINE@summary.json@}@@@",
-      "@@@STEP_LOG_END@summary.json@@@"
+      "@@@STEP_LOG_END@summary.json@@@",
+      "@@@SET_BUILD_PROPERTY@testing-swarming-task-ids@[\"1\"]@@@"
     ]
   },
   {