ia64/xen-unstable

changeset 7941:5b0a7cac1087

Added unit test for the DevController subclasses.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Mon Nov 21 12:41:39 2005 +0100 (2005-11-21)
parents 2cfded7de82f
children d7b91e64bd4c
files tools/python/setup.py tools/python/xen/xend/server/tests/__init__.py tools/python/xen/xend/server/tests/test_controllers.py
line diff
     1.1 --- a/tools/python/setup.py	Mon Nov 21 12:40:02 2005 +0100
     1.2 +++ b/tools/python/setup.py	Mon Nov 21 12:41:39 2005 +0100
     1.3 @@ -45,6 +45,7 @@ setup(name            = 'xen',
     1.4                           'xen.sv',
     1.5  
     1.6                           'xen.xend.tests',
     1.7 +                         'xen.xend.server.tests',
     1.8                           'xen.xm.tests'
     1.9                           ],
    1.10        ext_package = "xen.lowlevel",
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/tools/python/xen/xend/server/tests/__init__.py	Mon Nov 21 12:41:39 2005 +0100
     2.3 @@ -0,0 +1,1 @@
     2.4 + 
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tools/python/xen/xend/server/tests/test_controllers.py	Mon Nov 21 12:41:39 2005 +0100
     3.3 @@ -0,0 +1,81 @@
     3.4 +import os
     3.5 +import re
     3.6 +import unittest
     3.7 +
     3.8 +import xen.xend.XendRoot
     3.9 +
    3.10 +xen.xend.XendRoot.XendRoot.config_default = '/dev/null'
    3.11 +
    3.12 +from xen.xend.server import netif
    3.13 +
    3.14 +
    3.15 +FAKE_DOMID = 42
    3.16 +FAKE_DEVID = 63
    3.17 +
    3.18 +
    3.19 +xroot = xen.xend.XendRoot.instance()
    3.20 +
    3.21 +
    3.22 +class test_controllers(unittest.TestCase):
    3.23 +
    3.24 +    def testNetif(self):
    3.25 +        controller = self.controllerInstance(netif.NetifController)
    3.26 +
    3.27 +        self.assertNetif(controller.getDeviceDetails(['vif']), None)
    3.28 +        self.assertNetif(
    3.29 +            controller.getDeviceDetails(
    3.30 +            ['vif', ['mac', 'aa:bb:cc:dd:ee:ff']]),
    3.31 +            'aa:bb:cc:dd:ee:ff')
    3.32 +
    3.33 +
    3.34 +    def assertNetif(self, results, expectedMac):
    3.35 +
    3.36 +        (devid, backdets, frontdets) = results
    3.37 +
    3.38 +        self.assertEqual(devid, FAKE_DEVID)
    3.39 +
    3.40 +        self.assertEqual(backdets['handle'], str(FAKE_DEVID))
    3.41 +        self.assertEqual(backdets['script'],
    3.42 +                         os.path.join(xroot.network_script_dir,
    3.43 +                                      xroot.get_vif_script()))
    3.44 +        self.assertValidMac(backdets['mac'], expectedMac)
    3.45 +
    3.46 +        self.assertEqual(frontdets['handle'], str(FAKE_DEVID))
    3.47 +        self.assertValidMac(frontdets['mac'], expectedMac)
    3.48 +
    3.49 +
    3.50 +    MAC_REGEXP = re.compile('^' +
    3.51 +                            ':'.join([r'[0-9a-f][0-9a-f]'
    3.52 +                                      for i in range(0, 6)]) +
    3.53 +                            '$')
    3.54 +
    3.55 +    def assertValidMac(self, mac, expected):
    3.56 +        if expected:
    3.57 +            self.assertEqual(mac, expected)
    3.58 +        else:
    3.59 +            self.assert_(self.MAC_REGEXP.match(mac))
    3.60 +            
    3.61 +
    3.62 +    def controllerInstance(self, cls):
    3.63 +        """Allocate an instance of the given controller class, and override
    3.64 +        methods as appropriate so that we can run tests without needing
    3.65 +        Xenstored."""
    3.66 +        
    3.67 +        result = cls(FakeXendDomainInfo())
    3.68 +
    3.69 +        result.allocateDeviceID = fakeID
    3.70 +
    3.71 +        return result
    3.72 +
    3.73 +
    3.74 +class FakeXendDomainInfo:
    3.75 +    def getDomainPath(self):
    3.76 +        return "/test/fake/domain/%d/" % FAKE_DOMID
    3.77 +
    3.78 +
    3.79 +def fakeID():
    3.80 +    return FAKE_DEVID
    3.81 +
    3.82 +
    3.83 +def test_suite():
    3.84 +    return unittest.makeSuite(test_controllers)