Пример #1
0
    def test_package_install(self):
        '''package installation/removal

        This will just do some very shallow tests if this is not run as root.
        '''
        # test package; this is most likely not installed yet (test suite will
        # abort if it is)
        test_package = 'lrzsz'

        # use real OSLib here, not test suite's fake implementation
        o = OSLib()

        self.assertRaises(ValueError, o.install_package, 'nonexisting', None)
        # this should not crash, since it is not installed
        o.remove_package('nonexisting', None)

        if os.getuid() != 0:
            return

        self.failIf(o.package_installed(test_package), 
            '%s must not be installed for this test' % test_package)

        # test without progress reporting
        o.install_package(test_package, None)
        self.assert_(o.package_installed(test_package))
        o.remove_package(test_package, None)
        self.failIf(o.package_installed(test_package))
Пример #2
0
    def __init__(self):
        # set up a fake environment
        self.workdir = tempfile.mkdtemp()
        atexit.register(shutil.rmtree, self.workdir)

        OSLib.__init__(self)

        self._make_modinfo()
        self._make_proc_modules()
        self._make_modprobe()
        self._make_modalias()
        self._make_sys()
        self._make_xorg_conf()

        self.reset_packages()
        self.reset_dmi()

        os.mkdir(os.path.join(self.workdir, 'modules.d'))
        self.module_blacklist_file = os.path.join(self.workdir, 'modules.d', 'module-blacklist')

        self.handler_dir = os.path.join(self.workdir, 'handlers')
        os.mkdir(self.handler_dir)
        self.check_cache = os.path.join(self.workdir, 'check_cache')

        self.help_available = False
        self.help_called = False

        self.kernel_header_package = 'linux-dev'
Пример #3
0
    def test_has_repositories(self):
        '''has_repositories()

        This is only a shallow test that the function works. We assume that we
        have repositories in the test environment for now.
        '''
        o = OSLib()
        self.assertEqual(o.has_repositories(), True)
Пример #4
0
    def test_import_gpg_key_multimatch(self):
        '''import_gpg_key() for multiple key ID matches'''

        o = OSLib()

        # there are two 0xDEADBEEF ID keys
        fp = '5425 931B 5B99 C58B 40BD  CE87 7AC1 3FB2 DEAD BEEF'
        o.import_gpg_key(self.tempfile, fp)
        self.assert_(fp in o._gpg_keyring_fingerprints(self.tempfile))
Пример #5
0
    def test_import_gpg_key_no_program(self):
        '''import_gpg_key() for unavailable gpg'''

        o = OSLib()
        orig_path = os.environ.get('PATH', '')
        try:
            os.environ['PATH'] = ''
            fp = '3BDC 0482 4EA8 1277 AE46  EA72 F988 25AC 26B4 7B9F'
            self.assertRaises(SystemError, o.import_gpg_key, self.tempfile, fp)
        finally:
            os.environ['PATH'] = orig_path
        self.assertEqual(o._gpg_keyring_fingerprints(self.tempfile), [])
Пример #6
0
    def test_import_gpg_key_valid(self):
        '''import_gpg_key() for valid fingerprint'''

        o = OSLib()
        o.gpg_key_server = 'localhost'
        self._start_keyserver()
        try:
            o.import_gpg_key(self.tempfile, test_gpg_fp)
        finally:
            self._stop_keyserver()
        self.assertEqual(o._gpg_keyring_fingerprints(self.tempfile),
                [test_gpg_fp])
Пример #7
0
    def test_import_gpg_key_invalid(self):
        '''import_gpg_key() for invalid fingerprint'''

        o = OSLib()
        o.gpg_key_server = 'localhost'
        self._start_keyserver()
        try:
            self.assertRaises(SystemError, o.import_gpg_key, self.tempfile,
                    test_gpg_fp.replace('4', '5')) 
        finally:
            self._stop_keyserver()

        self.assertEqual(o._gpg_keyring_fingerprints(self.tempfile), [])
Пример #8
0
    def __init__(self):
        self.workdir = tempfile.mkdtemp()

        OSLib.__init__(self)
        self.installed_packages = set()
        self.blacklisted_modules = set()

        atexit.register(shutil.rmtree, self.workdir)

        self._make_modinfo()
        self.xorg_conf_path = os.path.join(self.workdir, 'xorg.conf')
        self.module_blacklist_file = os.path.join(self.workdir, 
            'module-blacklist')
Пример #9
0
    def __init__(self):
        '''Initialize system.
        
        This parses command line arguments, detects available hardware,
        and already installed drivers and handlers.
        '''
        gettext.install('jockey', unicode=True)

        (self.argv_options, self.argv_args) = self.parse_argv()
        fix_stdouterr()

        if not OSLib.inst:
            OSLib.inst = OSLib(client_only=not self.argv_options.no_dbus,
                               target_kernel=self.argv_options.kernel)

        if self.argv_options.check:
            time.sleep(self.argv_options.check)

        self.init_strings()

        self._dbus_iface = None
        self.dbus_server_main_loop = None
        self.have_ui = False
        self.search_only = False
        self.current_search = (None, None)  # query, result

        # make Control-C work properly
        signal.signal(signal.SIGINT, signal.SIG_DFL)
Пример #10
0
    def test_package_query(self):
        '''package querying'''

        # use real OSLib here, not test suite's fake implementation
        o = OSLib()

        self.assertEqual(o.package_installed('coreutils'), True)
        self.assertEqual(o.package_installed('nonexisting'), False)

        self.assertEqual(o.is_package_free('coreutils'), True)
        self.assertRaises(ValueError, o.is_package_free, 'nonexisting')

        (short, long) = o.package_description('bash')
        self.assert_('sh' in short.lower())
        self.assert_('shell' in long) 
        self.failIf('size:' in long) 
        self.assertRaises(ValueError, o.package_description, 'nonexisting')

        self.assertRaises(ValueError, o.package_files, 'nonexisting')
        coreutils_files = o.package_files('coreutils')
        self.assert_('/bin/cat' in coreutils_files)
        self.assert_('/bin/tail' in coreutils_files or 
            '/usr/bin/tail' in coreutils_files)
Пример #11
0
class DetectionTest(sandbox.LogTestCase):
    def tearDown(self):
        '''Clean handler dir.'''

        shutil.rmtree(OSLib.inst.handler_dir)
        os.mkdir(OSLib.inst.handler_dir)
        OSLib.inst.reset_dmi()
        # remove DriverDB caches
        for f in glob(os.path.join(OSLib.inst.backup_dir, 'driverdb-*.cache')):
            os.unlink(f)

        OSLib.inst.reset_packages()

    def test_get_hardware(self):
        '''get_hardware() returns correct hardware'''

        hw = jockey.detection.get_hardware()

        #filter out printers, since we cannot predict them
        self.assertEqual(
            '\n'.join(
                sorted([str(h) for h in hw if h.type != 'printer_deviceid'])),
            '''HardwareID('modalias', 'dmi:foo[A-1]bar')
HardwareID('modalias', 'fire:1.2')
HardwareID('modalias', 'pci:v00001001d00003D01sv00001234sd00000001bc03sc00i00')
HardwareID('modalias', 'pci:v0000AAAAd000012AAsv00000000sdDEADBEEFbc02sc80i00')
HardwareID('modalias', 'pci:v0000DEAFd00009999sv00000000sd00000000bc99sc00i00')
HardwareID('modalias', 'pci:v0000FFF0d00000001sv00000000sd00000000bc06sc01i01')
HardwareID('modalias', 'ssb:v4243id0812rev05')''')

    def test_hwid_equality(self):
        '''HardwareID equality implementation
        
        This tests modalias pattern matching in particular, since this is
        nontrivial.'''

        # identity
        x = HardwareID('pci', '1234/5678')
        self.assertEqual(x, x)
        self.failIf(x != x)

        # trivial equality
        x = HardwareID('pci', '1234/5678')
        y = HardwareID('pci', '1234/5678')
        self.assertEqual(x, y)
        self.failIf(x != y)
        self.assertEqual(hash(x), hash(y))

        x = HardwareID(
            'modalias',
            'pci:v00001001d00003D01sv00001234sd00000001bc03sc00i00')
        y = HardwareID(
            'modalias',
            'pci:v00001001d00003D01sv00001234sd00000001bc03sc00i00')
        self.assertEqual(x, y)
        self.failIf(x != y)
        self.assertEqual(hash(x), hash(y))

        # trivial inequality
        x = HardwareID('pci', '1234/5678')
        y = HardwareID('pci', '1234/5679')
        self.assertNotEqual(x, y)
        self.assert_(x != y)
        y = HardwareID('dmi', '42')
        self.assertNotEqual(x, y)
        self.assert_(x != y)
        y = 'IamNotAHardwareID'
        self.assertNotEqual(x, y)
        self.assert_(x != y)

        x = HardwareID(
            'modalias',
            'pci:v00001001d00003D01sv00001234sd00000001bc03sc00i00')
        y = HardwareID(
            'modalias',
            'pci:v00001001d00003D01sv00001235sd00000001bc03sc00i00')
        self.assertNotEqual(x, y)
        self.assert_(x != y)

        # modalias pattern equality
        x = HardwareID('modalias', 'pci:v00001*d00003D*sv*sd*1bc03sc00i*')
        y = HardwareID(
            'modalias',
            'pci:v00001001d00003D01sv00001234sd00000001bc03sc00i00')

        self.assertEqual(x, y)
        self.failIf(x != y)
        self.assertEqual(y, x)
        self.failIf(y != x)
        self.assertEqual(hash(x), hash(y))

        # pattern comparison; this is necessary for usage as dictionary keys,
        # but should just be string comparison
        x = HardwareID('modalias', 'pci:v00001*d00003D*sv*sd*1bc03sc00i*')
        y = HardwareID('modalias', 'pci:v00001*d00003D*sv*sd*1bc03sc00i*')
        self.assertEqual(x, y)
        self.assertEqual(hash(x), hash(y))
        y = HardwareID('modalias', 'pci:v00001*d*sv*sd*1bc03sc00i*')
        self.assertNotEqual(x, y)

        # non-modalias values should not match on patterns
        x = HardwareID('pci', '1234/5678')
        y = HardwareID('pci', '12*/56*')
        self.assertNotEqual(x, y)

    def test_hwid_dictionary(self):
        '''HardwareID can be used as dictionary keys'''

        d = {
            HardwareID('pci', '1234/5678'):
            'pci1',
            HardwareID('pci', '1234/5679'):
            'pci2',
            HardwareID(
                'modalias', 'pci:v0000AAAAd000012AAsv00000000sdDEADBEEFbc02sc80i00'):
            'ma1',
            HardwareID('modalias', 'pci:v00001*d00003D*sv*sd*1bc03sc00i*'):
            'ma_bc03',
            HardwareID('modalias', 'pci:v00001*d00003D*sv*sd*1bc04sc00i*'):
            'ma_bc04',
        }

        self.assertEqual(d[HardwareID('pci', '1234/5678')], 'pci1')
        self.assertRaises(KeyError, d.__getitem__,
                          HardwareID('pci', '1235/1111'))
        self.assertEqual(
            d[HardwareID(
                'modalias',
                'pci:v00001001d00003D01sv00001234sd00000001bc03sc00i00')],
            'ma_bc03')
        self.assertEqual(
            d[HardwareID(
                'modalias',
                'pci:v0000AAAAd000012AAsv00000000sdDEADBEEFbc02sc80i00')],
            'ma1')
        self.assertRaises(
            KeyError, d.__getitem__,
            HardwareID(
                'modalias',
                'pci:v0000FFF0d00000001sv00000000sd00000000bc06sc01i01'))

    def test_get_handlers_no_driverdb(self):
        '''get_handlers() returns applicable handlers without a driver db'''

        open(os.path.join(OSLib.inst.handler_dir, 'kmod_nodetect.py'),
             'w').write(sandbox.h_availability_py)

        h = jockey.detection.get_handlers(jockey.backend.Backend())

        self.assertEqual(len(h), 1,
                         'get_handlers() found one applicable handler')
        for h in h:
            pass  # get single item in set h
        self.assert_(isinstance(h, jockey.handlers.Handler))
        self.assert_(h.available())
        self.assertEqual(str(h.__class__).split('.')[-1], 'AvailMod')
        self.assertEqual(h.module, 'vanilla')
        self.assertEqual(h.name(),
                         'free module with available hardware, graphics card')

        self.assert_(h.enabled())

    def test_get_handlers_invalid(self):
        '''get_handlers() does not fall over when reading invalid handler
        files'''

        open(os.path.join(OSLib.inst.handler_dir, '01_no_python.py'),
             'w').write("Ce n'est ne pas un fichier Python!")
        open(os.path.join(OSLib.inst.handler_dir, '02_valid.py'),
             'w').write('import jockey.handlers\n' + sandbox.h_avail_mod)
        open(
            os.path.join(OSLib.inst.handler_dir, '03_inval_python.py'),
            'w').write('import i.do.not.exist\n' +
                       sandbox.h_avail_mod.replace('AvailMod', 'AlsoAvailMod'))

        h = jockey.detection.get_handlers(jockey.backend.Backend())

        self.assertEqual(len(h), 1,
                         'get_handlers() found one applicable handler')
        for h in h:
            pass  # get single item in set h
        self.assert_(isinstance(h, jockey.handlers.Handler))
        self.assertEqual(h.module, 'vanilla')
        self.assert_('Invalid custom handler' in sandbox.log.getvalue())

    def test_get_handlers_driverdb(self):
        '''get_handlers() returns applicable handlers with querying the driver db'''

        open(os.path.join(OSLib.inst.handler_dir, 'testhandlers.py'),
             'w').write('''
import jockey.handlers

%s

class VanillaGfxHandler(jockey.handlers.KernelModuleHandler):
    def __init__(self, backend):
        jockey.handlers.KernelModuleHandler.__init__(self, backend, 'vanilla3d')

class NonexistingModHandler(jockey.handlers.KernelModuleHandler):
    def __init__(self, backend):
        jockey.handlers.KernelModuleHandler.__init__(self, backend, 'nonexisting')
''' % sandbox.h_avail_mod)

        db = sandbox.TestDriverDB()
        backend = jockey.backend.Backend()
        db.update(backend.hardware)
        result = jockey.detection.get_handlers(backend, db)

        out = '\n'.join(sorted([str(h) for h in result]))
        self.assertEqual(
            out,
            '''firmware:firmwifi([FirmwareHandler, nonfree, disabled] standard module which needs firmware)
kmod:mint([KernelModuleHandler, nonfree, enabled] nonfree module with available hardware, wifi)
kmod:spam([KernelModuleHandler, free, enabled] free mystical module without modaliases)
kmod:vanilla([AvailMod, free, enabled] free module with available hardware, graphics card)
kmod:vanilla3d([VanillaGfxHandler, nonfree, enabled] nonfree, replacement graphics driver for vanilla)'''
        )

        for h in result:
            if h.module == 'firmwifi':
                self.failIf(h.enabled())
                # firmwifi is the only matching driver for this hardware and
                # thus shouldn't be displayed as recommended altough the DB
                # marks it as such
                self.failIf(h.recommended())
            else:
                self.assert_(h.enabled())

    def test_get_handlers_private_class(self):
        '''get_handlers() ignores private base classes starting with _'''

        open(os.path.join(OSLib.inst.handler_dir, 'kmod_baseclass.py'),
             'w').write(sandbox.h_privateclass_py)

        log_offset = sandbox.log.tell()
        h = jockey.detection.get_handlers(jockey.backend.Backend())
        log = sandbox.log.getvalue()[log_offset:]

        self.assertEqual(len(h), 1)
        for h in h:
            pass  # get single item in set h
        self.assert_(isinstance(h, jockey.handlers.Handler))
        self.assert_(h.available())
        self.assertEqual(str(h.__class__).split('.')[-1], 'MyHandler')

        self.failIf('Could not instantiate Handler' in log, log)

    def test_localkernelmod_driverdb(self):
        '''LocalKernelModulesDriverDB creates appropriate handlers
        
        It should prefer custom handlers over autogenerated standard ones and
        create standard ones for detected hardware.'''

        open(os.path.join(OSLib.inst.handler_dir, 'testhandlers.py'),
             'w').write('''
import jockey.handlers

class MintWrapper(jockey.handlers.KernelModuleHandler):
    def __init__(self, backend):
        jockey.handlers.KernelModuleHandler.__init__(self, backend, 'mint',
            'I taste even mintier')
''')

        db = jockey.detection.LocalKernelModulesDriverDB()

        result = jockey.detection.get_handlers(jockey.backend.Backend(), db)

        out = '\n'.join(sorted([str(h) for h in result]))

        self.assertEqual(
            out,
            '''kmod:firmwifi([KernelModuleHandler, free, enabled] standard module which needs firmware)
kmod:foodmi([KernelModuleHandler, free, enabled] foo DMI driver for [A-1] devices)
kmod:mint([MintWrapper, nonfree, enabled] I taste even mintier)
kmod:vanilla([KernelModuleHandler, free, enabled] free module with available hardware, graphics card)
kmod:vanilla3d([KernelModuleHandler, nonfree, enabled] nonfree, replacement graphics driver for vanilla)'''
        )

        for h in result:
            self.assert_(h.enabled())

        # some invalid/unsupported queries, should not cause crashes or
        # problems
        self.assertEqual(db.query(HardwareID('unknownType', '1234')), [])
        self.assertEqual(db.query(HardwareID('modalias', 'nobus1234')), [])

    def test_localkernelmod_multiple_driverdb(self):
        '''get_handlers queries multiple driver DBs'''

        backend = jockey.backend.Backend()
        tddb = sandbox.TestDriverDB()
        tddb.update(backend.hardware)
        result = jockey.detection.get_handlers(
            backend,
            [tddb, jockey.detection.LocalKernelModulesDriverDB()])
        out = '\n'.join([str(h) for h in result])

        # LocalKernelModulesDriverDB delivers vanilla3d, TestDriverDB delivers
        # spam
        self.assert_('kmod:vanilla3d' in out)
        self.assert_('kmod:spam' in out)

    def test_localkernelmod_driverdb_ignored_customhandler(self):
        '''LocalKernelModulesDriverDB creates custom handler for ignored module'''

        open(os.path.join(OSLib.inst.handler_dir, 'testhandlers.py'),
             'w').write(sandbox.h_ignored_custom_mod)

        result = jockey.detection.get_handlers(
            jockey.backend.Backend(),
            jockey.detection.LocalKernelModulesDriverDB())

        out = '\n'.join(sorted([str(h) for h in result]))

        self.assertEqual(
            out,
            '''kmod:dullstd([LessDullMod, free, enabled] standard module which should be ignored for detection)
kmod:firmwifi([KernelModuleHandler, free, enabled] standard module which needs firmware)
kmod:foodmi([KernelModuleHandler, free, enabled] foo DMI driver for [A-1] devices)
kmod:mint([KernelModuleHandler, nonfree, enabled] nonfree module with available hardware, wifi)
kmod:vanilla([KernelModuleHandler, free, enabled] free module with available hardware, graphics card)
kmod:vanilla3d([KernelModuleHandler, nonfree, enabled] nonfree, replacement graphics driver for vanilla)'''
        )

        for h in result:
            self.assert_(h.enabled())

    def test_localkernelmod_driverdb_disabled_customhandler(self):
        '''LocalKernelModulesDriverDB properly treats disabled custom handlers
        
        It should prefer custom handlers over autogenerated standard ones even
        if the custom handler is not available, instead of falling back to
        creating a standard handler.
        '''
        open(os.path.join(OSLib.inst.handler_dir, 'testhandlers.py'),
             'w').write(sandbox.h_notavail_mint_mod)
        db = jockey.detection.LocalKernelModulesDriverDB()

        result = jockey.detection.get_handlers(jockey.backend.Backend(), db)

        out = '\n'.join(sorted([str(h) for h in result]))

        self.failIf('kmod:mint' in out, out)

    def test_localkernelmod_driverdb_overrides(self):
        '''LocalKernelModulesDriverDB respects modalias overrides'''

        try:
            f = open(os.path.join(OSLib.inst.modaliases[1], 'ham.alias'), 'w')
            f.write('''# some bogus modaliases
alias abcd foobar
alias usb:v057Cp3500d*dc*dsc*dp*ic*isc*ip* fcdslslusb
alias usb:v0582p0044d*dc*dsc*dp*ic*isc*ip* snd_usb_audio
''')
            f.close()

            # assign spam module to the unknown piece of hardware
            f = open(os.path.join(OSLib.inst.modaliases[1], 'spam'), 'w')
            f.write('''# let's put an use to spam
alias pci:v0000FFF0d*1sv*sd*bc06sc*i* spam
''')
            f.close()

            result = jockey.detection.get_handlers(
                jockey.backend.Backend(),
                jockey.detection.LocalKernelModulesDriverDB())

            out = '\n'.join(sorted([str(h) for h in result]))

            self.assertEqual(
                out,
                '''kmod:firmwifi([KernelModuleHandler, free, enabled] standard module which needs firmware)
kmod:foodmi([KernelModuleHandler, free, enabled] foo DMI driver for [A-1] devices)
kmod:mint([KernelModuleHandler, nonfree, enabled] nonfree module with available hardware, wifi)
kmod:spam([KernelModuleHandler, free, enabled] free mystical module without modaliases)
kmod:vanilla([KernelModuleHandler, free, enabled] free module with available hardware, graphics card)
kmod:vanilla3d([KernelModuleHandler, nonfree, enabled] nonfree, replacement graphics driver for vanilla)'''
            )
        finally:
            for f in os.listdir(OSLib.inst.modaliases[1]):
                os.unlink(os.path.join(OSLib.inst.modaliases[1], f))

    def test_localkernelmod_driverdb_modalias_reset(self):
        '''LocalKernelModulesDriverDB resetting of modaliases'''

        try:
            f = open(os.path.join(OSLib.inst.modaliases[1], '01_bad'), 'w')
            f.write('alias pci:v0000FFF0d*sv*sd*bc*sc*i* cherry\n')
            f.close()
            # resets both the original and above bogus cherry entry; resetting
            # vanilla uncovers vanilla3d (which is the second candidate)
            f = open(os.path.join(OSLib.inst.modaliases[1], '02_reset'), 'w')
            f.write('reset cherry\nreset vanilla\n')
            f.close()
            f = open(os.path.join(OSLib.inst.modaliases[1], '03_good'), 'w')
            f.write('alias pci:v0000FFF0d*1sv*sd*bc06sc*i* spam\n')
            f.close()

            result = jockey.detection.get_handlers(
                jockey.backend.Backend(),
                jockey.detection.LocalKernelModulesDriverDB())

            out = '\n'.join(sorted([str(h) for h in result]))

            self.assertEqual(
                out,
                '''kmod:firmwifi([KernelModuleHandler, free, enabled] standard module which needs firmware)
kmod:foodmi([KernelModuleHandler, free, enabled] foo DMI driver for [A-1] devices)
kmod:mint([KernelModuleHandler, nonfree, enabled] nonfree module with available hardware, wifi)
kmod:spam([KernelModuleHandler, free, enabled] free mystical module without modaliases)
kmod:vanilla3d([KernelModuleHandler, nonfree, enabled] nonfree, replacement graphics driver for vanilla)'''
            )
        finally:
            for f in os.listdir(OSLib.inst.modaliases[1]):
                os.unlink(os.path.join(OSLib.inst.modaliases[1], f))

    def test_localkernelmod_driverdb_package_headers(self):
        '''LocalKernelModulesDriverDB reads modaliases from package headers'''

        orig_phm = OSLib.inst.package_header_modaliases
        try:
            # assign spam module to the unknown piece of hardware
            OSLib.inst.package_header_modaliases = lambda: {
                'pretzel': {
                    'spam': [
                        'foo:v1234dDEADBEEFx*',
                        'pci:v0000FFF0d*1sv*sd*bc06sc*i*'
                    ]
                }
            }

            result = jockey.detection.get_handlers(
                jockey.backend.Backend(),
                jockey.detection.LocalKernelModulesDriverDB())
        finally:
            OSLib.inst.package_header_modaliases = orig_phm

        for h in result:
            if h.module == 'spam':
                self.assertEqual(h.package, 'pretzel')
                break
        else:
            self.fail('no handler for spam module created')

    def test_localkernelmod_driverdb_packages(self):
        '''LocalKernelModulesDriverDB properly handles package field'''

        # test handler which is unavailable for mesa-vanilla
        open(os.path.join(OSLib.inst.handler_dir, 'testhandlers.py'),
             'w').write('''
import jockey.handlers

class PickyHandler(jockey.handlers.KernelModuleHandler):
    def __init__(self, backend):
        jockey.handlers.KernelModuleHandler.__init__(self, backend, 'chocolate')

    def available(self):
        if self.package == 'mesa-vanilla':
            return False
        return jockey.handlers.KernelModuleHandler.available(self)
''')

        try:
            f = open(os.path.join(OSLib.inst.modaliases[1], 'pretzel.alias'),
                     'w')
            f.write('alias pci:v0000FFF0d*1sv*sd*bc06sc*i* spam pretzel\n')
            f.close()

            f = open(os.path.join(OSLib.inst.modaliases[1], 'picky.alias'),
                     'w')
            f.write(
                'alias pci:v0000FFF0d*1sv*sd*bc06sc*i* chocolate mesa-vanilla\n'
            )
            f.close()

            result = jockey.detection.get_handlers(
                jockey.backend.Backend(),
                jockey.detection.LocalKernelModulesDriverDB())

            h_pretzel = None
            for h in result:
                if h.module == 'spam':
                    h_pretzel = h
                    break
                if h.module == 'chocolate':
                    self.fail(
                        'PickyHandler was instantiated, although it is unavailable for mesa-vanilla'
                    )
            self.assert_(h_pretzel, 'delivered spam kmod handler')

            self.failIf(h_pretzel.free())  # pretzel packge is nonfree
            self.assertEqual(h_pretzel.package, 'pretzel')
            self.failIf(h_pretzel.enabled())  # pretzel packge is not installed
        finally:
            for f in os.listdir(OSLib.inst.modaliases[1]):
                os.unlink(os.path.join(OSLib.inst.modaliases[1], f))

    def test_localkernelmod_driverdb_alternative_handler(self):
        '''LocalKernelModulesDriverDB offers alternative handlers for the same module'''

        # test handler which is unavailable for mesa-vanilla
        open(os.path.join(OSLib.inst.handler_dir, 'testhandlers.py'),
             'w').write('''
import jockey.handlers

class VanillaHandler(jockey.handlers.KernelModuleHandler):
    def __init__(self, backend, package=None):
        jockey.handlers.KernelModuleHandler.__init__(self, backend, 'chocolate')
        self.package = package or 'mesa-vanilla'

    def id(self):
        return 'kmod:%s:%s' % (self.module, self.package)

class VanillaUpdatesHandler(VanillaHandler):
    def __init__(self, backend):
        VanillaHandler.__init__(self, backend, 'mesa-vanilla-updates')
''')

        try:
            f = open(os.path.join(OSLib.inst.modaliases[1], 'vanilla.alias'),
                     'w')
            f.write(
                '''alias pci:v0000FFF0d*1sv*sd*bc06sc*i* chocolate mesa-vanilla
alias pci:v0000FFF0d*1sv*sd*bc06sc*i* chocolate mesa-vanilla-updates
''')
            f.close()

            result = jockey.detection.get_handlers(
                jockey.backend.Backend(),
                jockey.detection.LocalKernelModulesDriverDB())

            out = '\n'.join(
                sorted([str(h) for h in result if h.module == 'chocolate']))

            self.assertEqual(
                out,
                '''kmod:chocolate:mesa-vanilla([VanillaHandler, nonfree, disabled] free module with nonavailable hardware)
kmod:chocolate:mesa-vanilla-updates([VanillaUpdatesHandler, nonfree, disabled] free module with nonavailable hardware)'''
            )

        finally:
            for f in os.listdir(OSLib.inst.modaliases[1]):
                os.unlink(os.path.join(OSLib.inst.modaliases[1], f))

    def test_localkernelmod_driverdb_alternative_handler_xorg(self):
        '''LocalKernelModulesDriverDB offers alternative X.org handlers for the same module'''

        # test handler which is unavailable for mesa-vanilla
        open(os.path.join(OSLib.inst.handler_dir, 'testhandlers.py'),
             'w').write('''
import jockey.handlers

class VanillaHandler(jockey.xorg_driver.XorgDriverHandler):
    def __init__(self, backend, package=None):
        if not package:
            package = 'mesa-vanilla'
        if package and 'update' in package:
            name = 'Vanilla 3D driver (post-release updates)'
        else:
            name = 'Vanilla 3D driver'
        jockey.xorg_driver.XorgDriverHandler.__init__(self, backend,
          'chocolate', package, 'vanilla3d', 'vanilla', 
          name=name)

class VanillaUpdatesHandler(VanillaHandler):
    def __init__(self, backend):
        VanillaHandler.__init__(self, backend, 'mesa-vanilla-updates')

    def id(self):
        return VanillaHandler.id(self) + '-updates'

''')

        try:
            f = open(os.path.join(OSLib.inst.modaliases[1], 'vanilla.alias'),
                     'w')
            f.write(
                '''alias pci:v0000FFF0d*1sv*sd*bc06sc*i* chocolate mesa-vanilla
alias pci:v0000FFF0d*1sv*sd*bc06sc*i* chocolate mesa-vanilla-updates
''')
            f.close()

            result = jockey.detection.get_handlers(
                jockey.backend.Backend(),
                jockey.detection.LocalKernelModulesDriverDB())

            out = '\n'.join(
                sorted([str(h) for h in result if h.module == 'chocolate']))

            self.assertEqual(
                out,
                '''xorg:chocolate([VanillaHandler, nonfree, disabled] Vanilla 3D driver)
xorg:chocolate-updates([VanillaUpdatesHandler, nonfree, disabled] Vanilla 3D driver (post-release updates))'''
            )

        finally:
            for f in os.listdir(OSLib.inst.modaliases[1]):
                os.unlink(os.path.join(OSLib.inst.modaliases[1], f))

    def test_xmlrpc_driverdb_no_systemid(self):
        '''XMLRPCDriverDB with a working (local) server and no system ID'''

        backend = jockey.backend.Backend()
        # should *not* contact server, do that before starting server
        OSLib.inst.set_dmi('', '')
        db = jockey.detection.XMLRPCDriverDB('http://*****:*****@unittest.skipUnless(OSLib.has_defaultroute(), 'online test')
    def test_openprinting_unknownprinter(self):
        '''OpenPrintingDriverDB with an unknown printer'''

        backend = jockey.backend.Backend()
        backend.hardware = set(
            [HardwareID('printer_deviceid', 'MFG:FooTech;MDL:X-12;CMD:GDI')])
        backend.driver_dbs.append(jockey.detection.OpenPrintingDriverDB())
        backend.update_driverdb()
        self.assertEqual(backend.available(), [])

    @unittest.skipUnless(OSLib.has_defaultroute(), 'online test')
    def test_openprinting_apt_ppd(self):
        '''OpenPrintingDriverDB, known printer, apt package system, PPD only
        
        Note that this test case does assumptions about the data returned by
        openprinting.org, thus it needs to be adapted over time.
        '''
        prn = HardwareID('printer_deviceid',
                         'MFG:Hewlett-Packard;MDL:HP DesignJet 3500CP')
        db = jockey.detection.OpenPrintingDriverDB()
        orig_pkgsys = OSLib.inst.packaging_system
        try:
            OSLib.inst.packaging_system = lambda: 'apt'
            db.update(set([prn]))
        finally:
            OSLib.inst.packaging_system = orig_pkgsys

        drv = db.query(prn)

        self.assertEqual(len(drv), 1)
        drv = drv[0].properties

        self.assertEqual(drv['driver_type'], 'printer_driver')
        self.assert_('HP' in drv['description']['C'])
        self.assert_('lineart' in drv['long_description']['C'])
        self.assertEqual(drv['package'], 'openprinting-ppds-postscript-hp')
        self.assertEqual(drv['driver_vendor'], 'HP')
        self.assertEqual(drv['free'], True)
        self.assert_(drv['repository'].startswith('deb '))
        self.assert_('license' in drv)

    @unittest.skipUnless(OSLib.has_defaultroute(), 'online test')
    def test_openprinting_apt_binary(self):
        '''OpenPrintingDriverDB, known printer, apt package system, binary

        Note that this test case does assumptions about the data returned by
        openprinting.org, thus it needs to be adapted over time.

        This includes fetching the GPG fingerprint URL and verifying the SSL
        certificate validity and trust.
        '''
        prn = HardwareID('printer_deviceid', 'MFG:EPSON;MDL:Epson ME 320')
        db = jockey.detection.OpenPrintingDriverDB()
        orig_pkgsys = OSLib.inst.packaging_system
        try:
            OSLib.inst.packaging_system = lambda: 'apt'
            db.update(set([prn]))
        finally:
            OSLib.inst.packaging_system = orig_pkgsys

        drv = db.query(prn)

        self.assertEqual(len(drv), 1)
        drv = drv[0].properties

        self.assertEqual(drv['driver_type'], 'printer_driver')
        self.assert_('Epson' in drv['description']['C'])
        self.assert_('lineart' in drv['long_description']['C'])
        self.assert_(drv['package'].startswith('epson-inkjet-'))
        self.assert_('Epson' in drv['driver_vendor'])
        self.assert_(drv['repository'].startswith('deb '))
        self.assertEqual(drv['fingerprint'],
                         'E522 0FB7 014D 0FBD A50D  FC2B E5E8 6C00 8AA6 5D56')
        self.assert_('license' in drv)

    @unittest.skipUnless(OSLib.has_defaultroute(), 'online test')
    def test_openprinting_handler_ppd(self):
        '''OpenPrintingDriverDB: PrinterDriverHandler for PPD package'''

        backend = jockey.backend.Backend()
        backend.hardware = set([
            HardwareID('printer_deviceid',
                       'MFG:Hewlett-Packard;MDL:HP DesignJet 3500CP')
        ])
        backend.driver_dbs.append(jockey.detection.OpenPrintingDriverDB())
        orig_pkgsys = OSLib.inst.packaging_system
        try:
            OSLib.inst.packaging_system = lambda: 'apt'
            backend.update_driverdb()
        finally:
            OSLib.inst.packaging_system = orig_pkgsys
        handlers = backend.available()
        self.assertEqual(handlers,
                         ['printer:openprinting-ppds-postscript-hp:20091009'])

        hi = backend.handler_info(handlers[0])
        self.assertEqual(hi['package'], 'openprinting-ppds-postscript-hp')
        self.assertEqual(hi['free'], 'True')
        self.assert_('repository' in hi)
        self.assert_('driver_vendor' in hi)

    @unittest.skipUnless(OSLib.has_defaultroute(), 'online test')
    def test_openprinting_handler_binary(self):
        '''OpenPrintingDriverDB: PrinterDriverHandler for binary package'''

        backend = jockey.backend.Backend()
        backend.hardware = set(
            [HardwareID('printer_deviceid', 'MFG:EPSON;MDL:Epson ME 320')])
        backend.driver_dbs.append(jockey.detection.OpenPrintingDriverDB())
        orig_pkgsys = OSLib.inst.packaging_system
        try:
            OSLib.inst.packaging_system = lambda: 'apt'
            backend.update_driverdb()
        finally:
            OSLib.inst.packaging_system = orig_pkgsys
        handlers = backend.available()
        self.assertEqual(len(handlers), 1)
        self.assertTrue(
            handlers[0].startswith('printer:epson-inkjet-printer-n10-nx127:1'))

        hi = backend.handler_info(handlers[0])
        self.assert_(hi['package'].startswith('epson-inkjet-'))
        self.assert_(hi['repository'].startswith('deb '))
        self.assert_('repository_sign_fp' in hi)
        self.assert_('driver_vendor' in hi)

    @unittest.skipUnless(OSLib.has_defaultroute(), 'online test')
    def test_openprinting_yum(self):
        '''OpenPrintingDriverDB, known printer, yum package system
        
        Note that this test case does assumptions about the data returned by
        openprinting.org, thus it needs to be adapted over time.
        '''
        prn = HardwareID('printer_deviceid',
                         'MFG:Hewlett-Packard;MDL:HP DesignJet 3500CP')
        db = jockey.detection.OpenPrintingDriverDB()

        orig_pkgsys = OSLib.inst.packaging_system
        try:
            OSLib.inst.packaging_system = lambda: 'yum'
            db.update(set([prn]))
        finally:
            OSLib.inst.packaging_system = orig_pkgsys

        drv = db.query(prn)

        self.assertEqual(len(drv), 1)
        drv = drv[0].properties

        self.assertEqual(drv['driver_type'], 'printer_driver')
        self.assert_('HP' in drv['description']['C'])
        self.assert_('lineart' in drv['long_description']['C'])
        self.assertEqual(drv['package'], 'openprinting-ppds-postscript-hp')
        self.assertEqual(drv['driver_vendor'], 'HP')
        self.assertEqual(drv['free'], True)
        self.assert_('rpms' in drv['repository'].lower())
        self.assert_('license' in drv)

    @unittest.skipUnless(OSLib.has_defaultroute(), 'online test')
    def test_openprinting_unknownpkg(self):
        '''OpenPrintingDriverDB, known printer, unknown package system'''

        prn = HardwareID('printer_deviceid',
                         'MFG:Hewlett-Packard;MDL:HP DesignJet 3500CP')
        db = jockey.detection.OpenPrintingDriverDB()
        orig_pkgsys = OSLib.inst.packaging_system
        try:
            OSLib.inst.packaging_system = lambda: 'foo'
            db.update(set([prn]))
        finally:
            OSLib.inst.packaging_system = orig_pkgsys

        drv = db.query(prn)
        self.assertEqual(len(drv), 0)
Пример #12
0
inherit and properly implement jockey.ui.AbstractUI.'''

import sys

import jockey.backend
from jockey.oslib import OSLib

if len(sys.argv) not in (1, 3):
    print >> sys.stderr, '''Usage:
  %(prog)  - list available drivers
  %(prog) enable <driver>  - enable driver
  %(prog) disable <driver> - disable driver
''' % {
        'prog': sys.argv[0]
    }

OSLib.inst = OSLib()
backend = jockey.backend.Backend()

if len(sys.argv) == 1:
    print backend.available()
    sys.exit(0)

if sys.argv[1] == 'enable':
    backend.set_enabled(sys.argv[2], True)
elif sys.argv[1] == 'disable':
    backend.set_enabled(sys.argv[2], False)
else:
    print >> sys.stderr, 'Invalid mode', sys.argv[1]
    sys.exit(1)