File: test_controllers.py

package info (click to toggle)
xen-3.0 3.0.3-0-2
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 31,772 kB
  • ctags: 70,362
  • sloc: ansic: 417,153; python: 28,855; asm: 23,892; sh: 5,157; makefile: 4,830; objc: 613; perl: 372; xml: 351
file content (81 lines) | stat: -rw-r--r-- 2,071 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import re
import unittest

import xen.xend.XendRoot

xen.xend.XendRoot.XendRoot.config_default = '/dev/null'

from xen.xend.server import netif


FAKE_DOMID = 42
FAKE_DEVID = 63


xroot = xen.xend.XendRoot.instance()


class test_controllers(unittest.TestCase):

    def testNetif(self):
        controller = self.controllerInstance(netif.NetifController)

        self.assertNetif(controller.getDeviceDetails(['vif']), None)
        self.assertNetif(
            controller.getDeviceDetails(
            ['vif', ['mac', 'aa:bb:cc:dd:ee:ff']]),
            'aa:bb:cc:dd:ee:ff')


    def assertNetif(self, results, expectedMac):

        (devid, backdets, frontdets) = results

        self.assertEqual(devid, FAKE_DEVID)

        self.assertEqual(backdets['handle'], str(FAKE_DEVID))
        self.assertEqual(backdets['script'],
                         os.path.join(xroot.network_script_dir,
                                      xroot.get_vif_script()))
        self.assertValidMac(backdets['mac'], expectedMac)

        self.assertEqual(frontdets['handle'], str(FAKE_DEVID))
        self.assertValidMac(frontdets['mac'], expectedMac)


    MAC_REGEXP = re.compile('^' +
                            ':'.join([r'[0-9a-f][0-9a-f]'
                                      for i in range(0, 6)]) +
                            '$')

    def assertValidMac(self, mac, expected):
        if expected:
            self.assertEqual(mac, expected)
        else:
            self.assert_(self.MAC_REGEXP.match(mac))
            

    def controllerInstance(self, cls):
        """Allocate an instance of the given controller class, and override
        methods as appropriate so that we can run tests without needing
        Xenstored."""
        
        result = cls(FakeXendDomainInfo())

        result.allocateDeviceID = fakeID

        return result


class FakeXendDomainInfo:
    def getDomainPath(self):
        return "/test/fake/domain/%d/" % FAKE_DOMID


def fakeID():
    return FAKE_DEVID


def test_suite():
    return unittest.makeSuite(test_controllers)