Merge lp://staging/~soren/vmbuilder/interfacetests into lp://staging/vmbuilder/trunk

Proposed by Soren Hansen
Status: Needs review
Proposed branch: lp://staging/~soren/vmbuilder/interfacetests
Merge into: lp://staging/vmbuilder/trunk
Diff against target: 123 lines
7 files modified
VMBuilder/distro.py (+2/-2)
VMBuilder/hypervisor.py (+1/-1)
VMBuilder/plugins/ubuntu/suite.py (+1/-1)
test/__init__.py (+40/-0)
test/distro_tests.py (+8/-0)
test/hypervisor_tests.py (+7/-0)
test/suite_tests.py (+7/-0)
To merge this branch: bzr merge lp://staging/~soren/vmbuilder/interfacetests
Reviewer Review Type Date Requested Status
Iain Lowe Approve
Review via email: mp+14348@code.staging.launchpad.net
To post a comment you must log in.
Revision history for this message
Soren Hansen (soren) wrote :

This branch adds some interface unit tests. Most of the work was done by Iain, I just fixed a few things up a bit.

Revision history for this message
Iain Lowe (ilowe) wrote :

Looks good to me; that interface checker is much better as a method factory.

review: Approve

Unmerged revisions

358. By Soren Hansen

Merge lp:~ilowe/vmbuilder/lp428548 with a few changes:

  * Kept the disk_tests.py from trunk.
  * Change interface_implements from a decorator to a test factory.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'VMBuilder/distro.py'
2--- VMBuilder/distro.py 2009-06-10 13:40:41 +0000
3+++ VMBuilder/distro.py 2009-11-03 11:00:28 +0000
4@@ -23,11 +23,11 @@
5 class Distro(VMBuilder.plugins.Plugin):
6 def has_xen_support(self):
7 """Install the distro into destdir"""
8- raise NotImplemented('Distro subclasses need to implement the has_xen_support method')
9+ raise NotImplementedError('Distro subclasses need to implement the has_xen_support method')
10
11 def install(self, destdir):
12 """Install the distro into destdir"""
13- raise NotImplemented('Distro subclasses need to implement the install method')
14+ raise NotImplementedError('Distro subclasses need to implement the install method')
15
16 def post_mount(self, fs):
17 """Called each time a filesystem is mounted to let the distro add things to the filesystem"""
18
19=== modified file 'VMBuilder/hypervisor.py'
20--- VMBuilder/hypervisor.py 2009-06-10 13:40:41 +0000
21+++ VMBuilder/hypervisor.py 2009-11-03 11:00:28 +0000
22@@ -25,5 +25,5 @@
23
24 class Hypervisor(VMBuilder.plugins.Plugin):
25 def finalize(self):
26- raise NotImplemented('Hypervisor subclasses need to implement the finalize method')
27+ raise NotImplementedError('Hypervisor subclasses need to implement the finalize method')
28
29
30=== modified file 'VMBuilder/plugins/ubuntu/suite.py'
31--- VMBuilder/plugins/ubuntu/suite.py 2009-06-10 13:40:41 +0000
32+++ VMBuilder/plugins/ubuntu/suite.py 2009-11-03 11:00:28 +0000
33@@ -31,4 +31,4 @@
34
35 def check_arch_validity(self, arch):
36 """Checks whether the given arch is valid for this suite"""
37- raise NotImplemented('Suite subclasses need to implement the check_arch_validity method')
38+ raise NotImplementedError('Suite subclasses need to implement the check_arch_validity method')
39
40=== modified file 'test/__init__.py'
41--- test/__init__.py 2009-10-23 09:27:26 +0000
42+++ test/__init__.py 2009-11-03 11:00:28 +0000
43@@ -15,3 +15,43 @@
44 #
45 # You should have received a copy of the GNU General Public License
46 # along with this program. If not, see <http://www.gnu.org/licenses/>.
47+
48+import unittest
49+
50+def interface_implements(iclass, fname, *args, **kwargs):
51+ """Creates interface validation test methods.
52+
53+ `iclass` - The interface class
54+ `fname` - The name of the method that must be implemented
55+ `args` and `kwargs` - The required parameters of the method to call
56+
57+ >>> import unittest
58+ >>> class A:
59+ ... def test(self, x):
60+ ... raise NotImplemented
61+ ...
62+ >>> class TestA(unittest.TestCase):
63+ ... @interface_implements(A, 'test', None)
64+ ... def test_A_instancesMustImplement_test(self):
65+ ... pass
66+ ...
67+ >>> unittest.main()
68+ """
69+
70+ def inner():
71+ i = iclass(None)
72+ fakeargs = [i]
73+ required_f = getattr(i, fname) if hasattr(i, fname) else None
74+
75+ import inspect
76+ argspec = inspect.formatargspec(*inspect.getargspec(required_f)) if required_f else ''
77+
78+ def _wrapper(self):
79+ if not hasattr(iclass, fname):
80+ self.fail('%s.%s must define a method %s%s that raises NotImplementedError' % (iclass.__module__, iclass.__name__, fname, argspec))
81+
82+ self.assertRaises(NotImplementedError, required_f, *args, **kwargs)
83+
84+ _wrapper.__doc__ = '%s.%s implementations must implement %s%s' % (iclass.__module__, iclass.__name__, fname, argspec)
85+ return _wrapper
86+ return inner()
87
88=== added file 'test/distro_tests.py'
89--- test/distro_tests.py 1970-01-01 00:00:00 +0000
90+++ test/distro_tests.py 2009-11-03 11:00:28 +0000
91@@ -0,0 +1,8 @@
92+import unittest
93+
94+from test import interface_implements
95+from VMBuilder.distro import Distro
96+
97+class TestDistro(unittest.TestCase):
98+ test_distroImplementationsMustImplementHasXenSupport = interface_implements(Distro, 'has_xen_support')
99+ test_distroImplementationsMustImplementInstall = interface_implements(Distro, 'install', None)
100
101=== added file 'test/hypervisor_tests.py'
102--- test/hypervisor_tests.py 1970-01-01 00:00:00 +0000
103+++ test/hypervisor_tests.py 2009-11-03 11:00:28 +0000
104@@ -0,0 +1,7 @@
105+import unittest
106+
107+from test import interface_implements
108+from VMBuilder.hypervisor import Hypervisor
109+
110+class TestDefaultHypervisor(unittest.TestCase):
111+ test_defaultFinalizeRaisesNotImplemented = interface_implements(Hypervisor, 'finalize')
112
113=== added file 'test/suite_tests.py'
114--- test/suite_tests.py 1970-01-01 00:00:00 +0000
115+++ test/suite_tests.py 2009-11-03 11:00:28 +0000
116@@ -0,0 +1,7 @@
117+import unittest
118+
119+from test import interface_implements
120+from VMBuilder.plugins.ubuntu.suite import Suite
121+
122+class TestSuite(unittest.TestCase):
123+ test_suiteImplementationsMustImplementCheckArchValidity = interface_implements(Suite, 'check_arch_validity', None)

Subscribers

People subscribed via source and target branches