[autotest] Don't automatically create shards in heartbeats

Shards are added and assigned to labels using atest. If they
already exist this can not be done. Therefore they must not
automatically be created on their first heartbeat. Also it makes
more sense to fail instead of silently accepting unknown heartbeats.

BUG=None
DEPLOY=apache
TEST=Ran suites.

Change-Id: Id485ad956c83c16dc96916ba58e25c6f91028dd8
Reviewed-on: https://chromium-review.googlesource.com/219423
Reviewed-by: Alex Miller <milleral@chromium.org>
Commit-Queue: Jakob Jülich <jakobjuelich@chromium.org>
Tested-by: Jakob Jülich <jakobjuelich@chromium.org>
diff --git a/frontend/afe/rpc_utils.py b/frontend/afe/rpc_utils.py
index f1f6ec5..80756e3 100644
--- a/frontend/afe/rpc_utils.py
+++ b/frontend/afe/rpc_utils.py
@@ -931,17 +931,15 @@
 
 def retrieve_shard(shard_hostname):
     """
-    Retrieves the shard with the given hostname from the database or creates it.
+    Retrieves the shard with the given hostname from the database.
 
     @param shard_hostname: Hostname of the shard to retrieve
 
+    @raises models.Shard.DoesNotExist, if no shard with this hostname was found.
+
     @returns: Shard object
     """
-    try:
-        shard = models.Shard.smart_get(shard_hostname)
-    except models.Shard.DoesNotExist:
-        shard = models.Shard.objects.create(hostname=shard_hostname)
-    return shard
+    return models.Shard.smart_get(shard_hostname)
 
 
 def find_records_for_shard(shard):
diff --git a/frontend/afe/site_rpc_interface_unittest.py b/frontend/afe/site_rpc_interface_unittest.py
index c720d57..3f651cd 100644
--- a/frontend/afe/site_rpc_interface_unittest.py
+++ b/frontend/afe/site_rpc_interface_unittest.py
@@ -396,8 +396,8 @@
         retval_hosts, retval_jobs = retval['hosts'], retval['jobs']
 
         expected_jobs = [
-            (job.id, job.name, int(shard_hostname)) for job in jobs]
-        returned_jobs = [(job['id'], job['name'], job['shard']['id'])
+            (job.id, job.name, shard_hostname) for job in jobs]
+        returned_jobs = [(job['id'], job['name'], job['shard']['hostname'])
                          for job in retval_jobs]
         self.assertEqual(returned_jobs, expected_jobs)
 
@@ -419,8 +419,8 @@
         models.Label.objects.create(name='board:lumpy', platform=True)
         label2 = models.Label.objects.create(name='bluetooth', platform=False)
 
-        shard_hostname = self._do_heartbeat_and_assert_response()
-        shard = models.Shard.smart_get(shard_hostname)
+        shard_hostname = 'host1'
+        shard = models.Shard.objects.create(hostname=shard_hostname)
         shard.labels.add(models.Label.smart_get('board:lumpy'))
 
         job1 = self._create_job(hostless=True)
@@ -448,11 +448,11 @@
         leased_host.labels.add(lumpy_label)
         host2.labels.add(grumpy_label)
 
-        shard_hostname1 = self._do_heartbeat_and_assert_response()
-        shard_hostname2 = self._do_heartbeat_and_assert_response()
+        shard_hostname1 = 'host1'
+        shard_hostname2 = 'host2'
 
-        shard1 = models.Shard.smart_get(shard_hostname1)
-        shard2 = models.Shard.smart_get(shard_hostname2)
+        shard1 = models.Shard.objects.create(hostname=shard_hostname1)
+        shard2 = models.Shard.objects.create(hostname=shard_hostname2)
 
         shard1.labels.add(lumpy_label)
         shard2.labels.add(grumpy_label)