~udienz/ubuntu/natty/checkbox/checkbox.fix.514401

« back to all changes in this revision

Viewing changes to scripts/sleep_test

  • Committer: Bazaar Package Importer
  • Author(s): Marc Tardif
  • Date: 2010-08-13 16:23:16 UTC
  • Revision ID: james.westby@ubuntu.com-20100813162316-vuvxkco2tc8kxfgp
Tags: 0.10.2
New upstream release (LP: #617583):
* Fixed sleep_test to check the connection if using network-manager.
* Fixed reporting bugs against alsa-base and xorg (LP: #607214)
* Fixed apport dialog no longer appearing (LP: #607217)
* Reduced data file size for the desktop image.
* Updated report to be more pretty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
from subprocess import call
78
78
from optparse import OptionParser
79
79
 
 
80
from datetime import datetime, timedelta
 
81
from time import sleep
 
82
 
80
83
class ListDictHandler(logging.StreamHandler):
81
84
    '''
82
85
    Extends logging.StreamHandler to handle list, tuple and dict objects 
272
275
            logging.debug('mode is %s so we\'re skipping success check' % mode)
273
276
            return False
274
277
 
 
278
 
 
279
class NetworkManagerException(Exception):
 
280
 
 
281
    pass
 
282
 
 
283
 
 
284
class NetworkManager(object):
 
285
 
 
286
    NM_SERVICE = "org.freedesktop.NetworkManager"
 
287
    NM_PATH = "/org/freedesktop/NetworkManager"
 
288
    NM_INTERFACE = NM_SERVICE
 
289
 
 
290
    NM_PATH_DEVICES = "/org/freedesktop/NetworkManager/Devices"
 
291
    NM_INTERFACE_DEVICES = "org.freedesktop.NetworkManager.Devices"
 
292
 
 
293
    NMI_SERVICE = "org.freedesktop.NetworkManagerInfo"
 
294
    NMI_PATH = "/org/freedesktop/NetworkManagerInfo"
 
295
    NMI_INTERFACE = NMI_SERVICE
 
296
 
 
297
    HAL_SERVICE = "org.freedesktop.Hal"
 
298
    HAL_PATH = "/org/freedesktop/Hal/Manager"
 
299
    HAL_INTERFACE = "org.freedesktop.Hal.Manager"
 
300
    HAL_INTERFACE_DEVICE = "org.freedesktop.Hal.Device"
 
301
 
 
302
    STATE_UNKNOWN = "unknown"
 
303
    STATE_ASLEEP = "asleep"
 
304
    STATE_CONNECTING = "connecting"
 
305
    STATE_CONNECTED = "connected"
 
306
    STATE_DISCONNECTED = "disconnected"
 
307
 
 
308
    _state_table = [
 
309
        STATE_UNKNOWN,
 
310
        STATE_ASLEEP,
 
311
        STATE_CONNECTING,
 
312
        STATE_CONNECTED,
 
313
        STATE_DISCONNECTED]
 
314
 
 
315
    def __init__(self):
 
316
        try:
 
317
            import dbus
 
318
        except ImportError:
 
319
            raise NetworkManagerException, "Python module not found: dbus"
 
320
 
 
321
        try:
 
322
            self._bus = dbus.SystemBus()
 
323
            self.nm_object  = self._bus.get_object(self.NM_SERVICE, self.NM_PATH)
 
324
            self.nm_service = dbus.Interface(self.nm_object, self.NM_INTERFACE)
 
325
        except dbus.exceptions.DBusException:
 
326
            raise NetworkManagerException, "Failed to connect to dbus service"
 
327
 
 
328
    def get_state(self):
 
329
        state = self.nm_service.state()
 
330
        return self._state_table[state]
 
331
 
 
332
 
 
333
def check_network():
 
334
    try:
 
335
        nm = NetworkManager()
 
336
    except NetworkManagerException:
 
337
        return True
 
338
 
 
339
    start = datetime.now()
 
340
    while True:
 
341
        if nm.get_state() == nm.STATE_CONNECTED:
 
342
            return True
 
343
        # give 60 seconds to NetworkManager to get to a CONNECTED state, then give up
 
344
        if datetime.now() - start > timedelta(60):
 
345
            return False
 
346
        sleep(5)
275
347
            
276
348
def main():
277
349
    usage = 'Usage: %prog [OPTIONS]'
353
425
        else:
354
426
            run_result[run_count] = 'Pass'
355
427
 
 
428
    # Be reasonably sure the network is up before returning, we want
 
429
    # the network to be up for possible following tests.
 
430
    network_is_live = check_network()
 
431
 
356
432
    if 'Fail' in run_result.values():
357
433
        logging.error('One or more suspend tests failed')
358
434
        logging.error(run_result)