When a one-time host is added as a normal host while it's running a job, the status would get reset, messing up the scheduler.  Fix that.

Signed-off-by: Steve Howard <showard@google.com>


git-svn-id: http://test.kernel.org/svn/autotest/trunk@3778 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/frontend/afe/model_logic.py b/frontend/afe/model_logic.py
index 37c43d2..601b9c3 100644
--- a/frontend/afe/model_logic.py
+++ b/frontend/afe/model_logic.py
@@ -812,7 +812,7 @@
             filters = {self.name_field : my_name, 'invalid' : True}
             try:
                 old_object = self.__class__.objects.get(**filters)
-                self.id = old_object.id
+                self.resurrect_object(old_object)
             except self.DoesNotExist:
                 # no existing object
                 pass
@@ -820,11 +820,22 @@
         super(ModelWithInvalid, self).save(*args, **kwargs)
 
 
+    def resurrect_object(self, old_object):
+        """
+        Called when self is about to be saved for the first time and is actually
+        "undeleting" a previously deleted object.  Can be overridden by
+        subclasses to copy data as desired from the deleted entry (but this
+        superclass implementation must normally be called).
+        """
+        self.id = old_object.id
+
+
     def clean_object(self):
         """
         This method is called when an object is marked invalid.
         Subclasses should override this to clean up relationships that
-        should no longer exist if the object were deleted."""
+        should no longer exist if the object were deleted.
+        """
         pass
 
 
diff --git a/frontend/afe/models.py b/frontend/afe/models.py
index b5c5533..2839ea5 100644
--- a/frontend/afe/models.py
+++ b/frontend/afe/models.py
@@ -238,6 +238,13 @@
         return host
 
 
+    def resurrect_object(self, old_object):
+        super(Host, self).resurrect_object(old_object)
+        # invalid hosts can be in use by the scheduler (as one-time hosts), so
+        # don't change the status
+        self.status = old_object.status
+
+
     def clean_object(self):
         self.aclgroup_set.clear()
         self.labels.clear()
diff --git a/frontend/afe/models_test.py b/frontend/afe/models_test.py
index 9f4d99d..47932e0 100755
--- a/frontend/afe/models_test.py
+++ b/frontend/afe/models_test.py
@@ -39,6 +39,32 @@
         self._check_acls(host2, ['my_acl'])
 
 
+class HostTest(unittest.TestCase,
+               frontend_test_utils.FrontendTestMixin):
+    def setUp(self):
+        self._frontend_common_setup()
+
+
+    def tearDown(self):
+        self._frontend_common_teardown()
+
+
+    def test_add_host_previous_one_time_host(self):
+        # ensure that when adding a host which was previously used as a one-time
+        # host, the status isn't reset, since this can interfere with the
+        # scheduler.
+        host = models.Host.create_one_time_host('othost')
+        self.assertEquals(host.invalid, True)
+        self.assertEquals(host.status, models.Host.Status.READY)
+
+        host.status = models.Host.Status.RUNNING
+        host.save()
+
+        host2 = models.Host.add_object(hostname='othost')
+        self.assertEquals(host2.id, host.id)
+        self.assertEquals(host2.status, models.Host.Status.RUNNING)
+
+
 class SpecialTaskUnittest(unittest.TestCase,
                           frontend_test_utils.FrontendTestMixin):
     def setUp(self):