# Copyright (c) 2015, Facebook, Inc.  All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
#
from sparts.tests.base import MultiTaskTestCase, Skip
try:
    import dbus
    from sparts.tasks.dbus import DBusServiceTask, DBusMainLoopTask
except ImportError:
    raise Skip("dbus support is required to run this test")

from sparts.sparts import option
from random import getrandbits


class TestDBusTask(DBusServiceTask):
    BUS_NAME = 'com.github.facebook.test-{}'.format(getrandbits(32))


class TestDBusSystemTask(DBusServiceTask):
    BUS_NAME = 'com.github.facebook.systemtest'
    USE_SYSTEM_BUS = True

    def start(self):
        try:
            super(TestDBusSystemTask, self).start()
        except dbus.DBusException as err:
            self.acquire_name_error = str(err)


class TestDBus(MultiTaskTestCase):
    TASKS = [TestDBusTask, DBusMainLoopTask]

    def test_session_bus(self):
        bus = dbus.SessionBus(private=True)
        self.assertTrue(bus.name_has_owner(TestDBusTask.BUS_NAME))


class TestSystemDBus(MultiTaskTestCase):
    TASKS = [TestDBusSystemTask, DBusMainLoopTask]

    def test_system_bus(self):
        # expecting system task to fail due to missing DBus policy
        t = self.service.getTask(TestDBusSystemTask)
        err = getattr(t, 'acquire_name_error', None)
        self.assertNotNone(err)

        self.assertTrue(err.startswith('org.freedesktop.DBus.Error.AccessDenied'))

@@ -344,24 +344,8 @@
 ry:%0A
-    import dbus%0A
     
@@ -408,16 +408,32 @@
 oopTask%0A
+    import dbus%0A
 except I
@@ -569,24 +569,35 @@
 andbits%0A
+import time
 %0A%0Aclass 
 TestDBus
@@ -580,32 +580,36 @@
 ort time%0A%0Aclass 
+Base
 TestDBusTask(DBu
@@ -615,32 +615,377 @@
 usServiceTask):%0A
+    def start(self):%0A        try:%0A            self.logger.debug('call start()')%0A            super(BaseTestDBusTask, self).start()%0A        except dbus.DBusException as err:%0A            self.logger.debug('got exception')%0A            self.acquire_name_error = str(err)%0A%0A%0Aclass TestDBusSessionTask(BaseTestDBusTask):%0A    OPT_PREFIX = 'dbus-session'%0A
     BUS_NAME = '
@@ -1064,33 +1064,65 @@
 temTask(
-DBusServiceTask):
+BaseTestDBusTask):%0A    OPT_PREFIX = 'dbus-system'
 %0A    BUS
@@ -1193,184 +1193,8 @@
 ue%0A%0A
-    def start(self):%0A        try:%0A            super(TestDBusSystemTask, self).start()%0A        except dbus.DBusException as err:%0A            self.acquire_name_error = str(err)%0A%0A
 %0Acla
@@ -1242,24 +1242,31 @@
  = %5BTestDBus
+Session
 Task, DBusMa
@@ -1322,51 +1322,75 @@
     
-bus = dbus.SessionBus(private=True)
+# expecting session task to have been started without error
 %0A       
  sel
@@ -1389,66 +1389,142 @@
     
+ t =
  self.
-as
 ser
-tTrue(bus.name_has_owner(TestDBusTask.BUS_NAME)
+vice.getTask(TestDBusSessionTask)%0A        err = getattr(t, 'acquire_name_error', None)%0A        self.assertEqual(err, None
 )%0A%0A%0A
@@ -1852,16 +1852,57 @@
 ne(err)%0A
+        self.logger.debug('err: %25s', err)
 %0A       

refactor tests to make passive checks