[autotest] merge bug template from test control file with the one from suite template

BUG=chromium:358490
TEST=test with local changes to inject a test with bug template defined.
unittest
trybot build: trybot-falco-paladin/R36-5827.0.0-b121

Change-Id: I77bf5ccdc12bee036ee2f6c6f2cd701ca1561211
Reviewed-on: https://chromium-review.googlesource.com/198538
Tested-by: Dan Shi <dshi@chromium.org>
Reviewed-by: Prashanth B <beeps@chromium.org>
Commit-Queue: Dan Shi <dshi@chromium.org>
diff --git a/server/cros/dynamic_suite/fakes.py b/server/cros/dynamic_suite/fakes.py
index 892bc71..ff4e1f6 100644
--- a/server/cros/dynamic_suite/fakes.py
+++ b/server/cros/dynamic_suite/fakes.py
@@ -24,6 +24,7 @@
         self.retries = 0
         self.sync_count = 1
         self.job_retries = job_retries
+        self.bug_template = {}
 
 
 class FakeJob(object):
diff --git a/server/cros/dynamic_suite/suite.py b/server/cros/dynamic_suite/suite.py
index aef45c0..3c10d6f 100644
--- a/server/cros/dynamic_suite/suite.py
+++ b/server/cros/dynamic_suite/suite.py
@@ -17,6 +17,7 @@
 from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
 from autotest_lib.server.cros.dynamic_suite import job_status
 from autotest_lib.server.cros.dynamic_suite import reporting
+from autotest_lib.server.cros.dynamic_suite import reporting_utils
 from autotest_lib.server.cros.dynamic_suite import tools
 from autotest_lib.server.cros.dynamic_suite.job_status import Status
 
@@ -764,6 +765,7 @@
                 results_generator = job_status.wait_for_results(self._afe,
                                                                 self._tko,
                                                                 self._jobs)
+            template = reporting_utils.BugTemplate(bug_template)
             for result in results_generator:
                 result.record_all(record)
                 if (self._results_dir and
@@ -791,8 +793,22 @@
                             self._tag,
                             result)
 
+                    # Try to merge with bug template in test control file.
+                    try:
+                        test_data = self._jobs_to_tests[result.id]
+                        merged_template = template.finalize_bug_template(
+                                test_data.bug_template)
+                    except AttributeError:
+                        # Test control file does not have bug template defined.
+                        merged_template = bug_template
+                    except reporting_utils.InvalidBugTemplateException as e:
+                        merged_template = {}
+                        logging.error('Merging bug templates failed with '
+                                      'error: %s An empty bug template will '
+                                      'be used.', e)
+
                     bug_id, bug_count = bug_reporter.report(failure,
-                                                            bug_template)
+                                                            merged_template)
 
                     # We use keyvals to communicate bugs filed with run_suite.
                     if bug_id is not None:
diff --git a/server/cros/dynamic_suite/suite_unittest.py b/server/cros/dynamic_suite/suite_unittest.py
index 4368d5f..52b54e8 100644
--- a/server/cros/dynamic_suite/suite_unittest.py
+++ b/server/cros/dynamic_suite/suite_unittest.py
@@ -618,7 +618,6 @@
         Confirm that all the necessary predicates are passed on to the
         bug reporter when a test fails.
         """
-
         test_results = self._createSuiteMockResults()
         self.schedule_and_expect_these_results(
             self.suite,
@@ -629,6 +628,7 @@
         self.mox.ReplayAll()
 
         self.suite.schedule(self.recorder.record_entry, True)
+        self.suite._jobs_to_tests[self._FAKE_JOB_ID] = self.files['seven']
         self.suite.wait(self.recorder.record_entry)
 
 
@@ -650,6 +650,7 @@
         self.mox.ReplayAll()
 
         self.suite.schedule(self.recorder.record_entry, True)
+        self.suite._jobs_to_tests[self._FAKE_JOB_ID] = self.files['seven']
         self.suite.wait(self.recorder.record_entry)