[autotest] Read tko tests from database after it's saved.

This is to confirm the CloudSQL instance has tko data saved.

BUG=chromium:737157
TEST=local run tests

Change-Id: Ibe3547363f2bd2ae9d590c95949e3f296827bdca
Reviewed-on: https://chromium-review.googlesource.com/575436
Commit-Ready: Dan Shi <dshi@google.com>
Tested-by: Dan Shi <dshi@google.com>
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
diff --git a/tko/parse.py b/tko/parse.py
index 4d8d4f1..b8a2728 100755
--- a/tko/parse.py
+++ b/tko/parse.py
@@ -27,6 +27,12 @@
 from autotest_lib.tko import models, parser_lib
 from autotest_lib.tko.perf_upload import perf_uploader
 
+try:
+    from chromite.lib import metrics
+except ImportError:
+    metrics = utils.metrics_mock
+
+
 _ParseOptions = collections.namedtuple(
     'ParseOptions', ['reparse', 'mail_on_failure', 'dry_run', 'suite_report',
                      'datastore_creds', 'export_to_gcloud_path'])
@@ -366,6 +372,21 @@
                 jobname, job,
                 parent_job_id=job_keyval.get(constants.PARENT_JOB_ID, None))
 
+            # Verify the job data is written to the database.
+            if job.tests:
+                tests_in_db = db.find_tests(job_data['job_idx'])
+                tests_in_db_count = len(tests_in_db) if tests_in_db else 0
+                if tests_in_db_count != len(job.tests):
+                    tko_utils.dprint(
+                            'Failed to find enough tests for job_idx: %d. The '
+                            'job should have %d tests, only found %d tests.' %
+                            (job_data['job_idx'], len(job.tests),
+                             tests_in_db_count))
+                    metrics.Counter(
+                            'chromeos/autotest/result/db_save_failure',
+                            description='The number of times parse failed to '
+                            'save job to TKO database.').increment()
+
             # Upload perf values to the perf dashboard, if applicable.
             for test in job.tests:
                 perf_uploader.upload_test(job, test, jobname)