platform_CUPSDaemon: add CUPS daemon sanity tests

Add some basic sanity tests for CUPS and its socket-based activation.
They're by no means thorough, but at least it's a start.

BUG=chromium:616778
TEST=`test_that <IP> platform_CUPSDaemon`
CQ-DEPEND=CL:343273

Change-Id: Iefa125de474ce87954219674265ad16d06568ef4
Signed-off-by: Brian Norris <briannorris@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/349679
Reviewed-by: Sean Kau <skau@chromium.org>
diff --git a/client/site_tests/platform_CUPSDaemon/control b/client/site_tests/platform_CUPSDaemon/control
new file mode 100644
index 0000000..0177326
--- /dev/null
+++ b/client/site_tests/platform_CUPSDaemon/control
@@ -0,0 +1,23 @@
+# Copyright (c) 2016 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+TIME="SHORT"
+AUTHOR = "The Chromium OS Authors"
+DOC = """
+Tests some basic functionality of the CUPS daemon and the Upstart jobs that it
+relies on for socket-activation.
+"""
+NAME = "platform_CUPSDaemon"
+PURPOSE = "Test that CUPS daemon will launch on demand."
+CRITERIA = """
+Fail if Upstart won't launch CUPS properly or doesn't clean up its sockets
+correctly.
+"""
+ATTRIBUTES = "suite:bvt-inline, suite:smoke"
+TEST_CLASS = "platform"
+TEST_CATEGORY = "Functional"
+TEST_TYPE = "client"
+JOB_RETRIES = 2
+
+job.run_test("platform_CUPSDaemon")
diff --git a/client/site_tests/platform_CUPSDaemon/platform_CUPSDaemon.py b/client/site_tests/platform_CUPSDaemon/platform_CUPSDaemon.py
new file mode 100644
index 0000000..6beea80
--- /dev/null
+++ b/client/site_tests/platform_CUPSDaemon/platform_CUPSDaemon.py
@@ -0,0 +1,67 @@
+# Copyright 2016 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import os.path
+
+from autotest_lib.client.bin import test, utils
+from autotest_lib.client.common_lib import error
+from autotest_lib.client.cros import upstart
+
+
+class platform_CUPSDaemon(test.test):
+    """
+    Runs some sanity tests for cupsd and the upstart-socket-bridge
+    socket-activation.
+    """
+    version = 1
+
+    _CUPS_SOCK_PATH = '/run/cups/cups.sock'
+
+
+    def check_cups_is_responding(self):
+        """
+        Run a basic sanity test to be sure CUPS is operating.
+        """
+
+        # Try a simple CUPS command; timeout/fail if it takes too long (i.e.,
+        # socket may exist, but it may not get passed off to cupsd propertly).
+        utils.system_output('lpstat -W all', timeout=10)
+
+
+    def run_once(self):
+        """
+        Run some sanity tests for cupsd and the upstart-socket-bridge
+        socket-activation.
+        """
+        if not upstart.has_service('cupsd'):
+            raise error.TestNAError('No cupsd service found')
+
+        upstart.ensure_running('upstart-socket-bridge')
+
+        if not os.path.exists(self._CUPS_SOCK_PATH):
+            raise error.TestFail('Missing CUPS socket: %s', self._CUPS_SOCK_PATH)
+
+        # Make sure CUPS is stopped, so we can test on-demand launch.
+        if upstart.is_running('cupsd'):
+            upstart.stop_job('cupsd')
+
+        self.check_cups_is_responding()
+
+        # Now try stopping socket bridge, to see it clean up its files.
+        upstart.stop_job('upstart-socket-bridge')
+        upstart.stop_job('cupsd')
+
+        if os.path.exists(self._CUPS_SOCK_PATH):
+            raise error.TestFail('CUPS socket was not cleaned up: %s', self._CUPS_SOCK_PATH)
+
+        # Create dummy file, to see if upstart-socket-bridge will clear it out
+        # properly.
+        utils.system('touch %s' % self._CUPS_SOCK_PATH)
+
+        upstart.restart_job('upstart-socket-bridge')
+
+        if not os.path.exists(self._CUPS_SOCK_PATH):
+            raise error.TestFail('Missing CUPS socket: %s', self._CUPS_SOCK_PATH)
+
+        self.check_cups_is_responding()