Add --pidfile option to the devserver for easy daemon maintenance.

BUG=chromium:251309
TEST=Ran the unittests + manual with/without option.

Change-Id: Iaf08cc342119082582e791d37905009fc8843ba6
Reviewed-on: https://gerrit.chromium.org/gerrit/66548
Commit-Queue: Chris Sosa <sosa@chromium.org>
Reviewed-by: Chris Sosa <sosa@chromium.org>
Tested-by: Chris Sosa <sosa@chromium.org>
diff --git a/devserver_unittest.py b/devserver_unittest.py
index 51a4058..44db7ed 100755
--- a/devserver_unittest.py
+++ b/devserver_unittest.py
@@ -9,6 +9,7 @@
 import json
 from xml.dom import minidom
 import os
+import psutil
 import shutil
 import signal
 import subprocess
@@ -80,7 +81,8 @@
                                              API_SET_UPDATE_REQUEST,
                                              TEST_IMAGE_NAME))
 
-    self.devserver_process = self._StartServer()
+    self.pidfile = tempfile.mktemp('devserver_unittest')
+    self.devserver_process = self._StartServer(self.pidfile)
 
   def tearDown(self):
     """Removes testing files."""
@@ -89,15 +91,17 @@
 
   # Helper methods begin here.
 
-  def _StartServer(self):
+  def _StartServer(self, pidfile):
     """Starts devserver, returns process."""
     cmd = [
         'python',
         os.path.join(self.src_dir, 'devserver.py'),
         'devserver.py',
-        '--static_dir',
-        self.test_data_path,
+        '--static_dir', self.test_data_path,
+        '--pidfile', pidfile
         ]
+    if pidfile:
+      cmd.extend(['--pidfile', pidfile])
 
     process = subprocess.Popen(cmd,
                                stderr=subprocess.PIPE)
@@ -211,6 +215,17 @@
     self.assertEqual(
         json.loads(response)['forced_update_label'], API_SET_UPDATE_REQUEST)
 
+  def testPidFile(self):
+    """Test that using a pidfile works correctly."""
+    with open(self.pidfile, 'r') as f:
+      pid = f.read()
+
+    # Let's assert some process information about the devserver.
+    self.assertTrue(pid.isdigit())
+    process = psutil.Process(int(pid))
+    self.assertTrue(process.is_running())
+    self.assertTrue('devserver.py' in process.cmdline)
+
 
 if __name__ == '__main__':
   unittest.main()