[autotest] Throw on sigterm when locking machines.

If the python process receives a sigterm while machines are locked, the
machines will never be unlocked. If we translate the signal into an
exception, then the standard python cleanup mechanisms can apply, and
all will be well.

BUG=chromium-os:33094
TEST=pylint+unit

Change-Id: Ifdd96df1fef809da9618e47d7361c290e29cdd76
Reviewed-on: https://gerrit.chromium.org/gerrit/29321
Tested-by: Alex Miller <milleral@chromium.org>
Reviewed-by: Chris Masone <cmasone@chromium.org>
Commit-Ready: Alex Miller <milleral@chromium.org>
diff --git a/server/cros/dynamic_suite_unittest.py b/server/cros/dynamic_suite_unittest.py
index 6ed1aa3..1de8e08 100755
--- a/server/cros/dynamic_suite_unittest.py
+++ b/server/cros/dynamic_suite_unittest.py
@@ -8,8 +8,10 @@
 
 import logging
 import mox
+import os
 import random
 import shutil
+import signal
 import tempfile
 import time
 import unittest
@@ -156,6 +158,31 @@
         self.assertEquals(spec.add_experimental, True)
 
 
+    def testReimageAndSIGTERM(self):
+        """Should reimage_and_run that causes a SIGTERM and fails cleanly."""
+        def suicide():
+            os.kill(os.getpid(), signal.SIGTERM)
+
+        # mox does not play nicely with receiving a bare SIGTERM, but it does
+        # play nicely with unhandled exceptions...
+        class UnhandledSIGTERM(Exception):
+            pass
+
+        self.mox.StubOutWithMock(dev_server.DevServer, 'create')
+        dev_server.DevServer.create().WithSideEffects(suicide)
+        manager = self.mox.CreateMock(host_lock_manager.HostLockManager)
+        manager.unlock()
+        spec = self.mox.CreateMock(dynamic_suite.SuiteSpec)
+        spec.skip_reimage = True
+
+        self.mox.ReplayAll()
+
+        with dynamic_suite.SignalsAsExceptions(UnhandledSIGTERM):
+            self.assertRaises(error.SignalException,
+                              dynamic_suite._perform_reimage_and_run,
+                              spec, None, None, None, manager)
+
+
 class ReimagerTest(mox.MoxTestBase):
     """Unit tests for dynamic_suite.Reimager.