2
# This program is free software; you can redistribute it and/or modify
3
# it under the terms of the GNU General Public License as published by
4
# the Free Software Foundation; either version 2 of the License, or
5
# (at your option) any later version.
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
# Template for adding arguments to test
23
# { 'label' : { 'VAR' : { 'invalid' : [param],\
24
# 'valid' : [param]},\
25
# '__init__' : { 'invalid' : [{'initparam':val}],\
26
# 'valid' : [{'initparam':val}]}\
28
args = { 'guest' : { \
29
'name' : { 'invalid' : ['123456789', 'im_invalid!', '', 0, 'verylongnameverylongnameverylongnameverylongnameveryvery'], \
30
'valid' : ['Valid_name.01'] }, \
31
'memory' : { 'invalid' : [-1, 0, ''], \
32
'valid' : [200, 2000] }, \
33
'maxmemory' : { 'invalid' : [-1, 0, ''], \
34
'valid' : [200, 2000], }, \
35
'uuid' : { 'invalid' : [ '', 0, '1234567812345678123456781234567x'], \
36
'valid' : ['12345678123456781234567812345678','12345678-1234-1234-ABCD-ABCDEF123456']}, \
37
'vcpus' : { 'invalid' : [-1, 0, 1000, ''], \
38
'valid' : [ 1, 32 ] }, \
39
'graphics' : { 'invalid' : ['', True, 'unknown', {},\
40
('', '', '', 0), ('','','', 'longerthan16chars'), ('','','','invalid!!ch@r'),\
42
'valid' : [False, 'sdl', 'vnc', \
46
'type':'vnc', 'opts':5900}\
48
'type' : { 'invalid' : [], \
49
'valid' : ['sometype'] }, \
50
'cdrom' : { 'invalid' : ['', 0, '/somepath'],\
51
'valid' : ['/dev/loop0']}\
54
'os_type' : { 'invalid' : ['notpresent',0,''],\
55
'valid' : ['other', 'windows',\
57
'os_variant': { 'invalid' : ['', 0, 'invalid'], \
62
'__init__' : { 'invalid' : [{ 'path' : 0},\
63
{ 'path' : '/root' },\
70
{ 'path' : 'notblock',\
71
'type' : virtinst.VirtualDisk.TYPE_BLOCK},\
72
{ 'path' :'/dev/null',\
73
'type' : virtinst.VirtualDisk.TYPE_BLOCK},
75
'valid' : [{ 'path' : '/dev/loop0'},\
76
{ 'path' : 'nonexist', \
78
{ 'path' :'/dev/null'},
80
'device' : virtinst.VirtualDisk.DEVICE_CDROM},
82
'device' : virtinst.VirtualDisk.DEVICE_FLOPPY}]}\
85
'boot' : { 'invalid' : ['', 0, ('1element'),\
86
['1el', '2el', '3el'],\
87
{'1element': '1val'},\
89
'wronglabel' : 'b'}],\
90
'valid' : [('kern', 'init'),\
94
'extraargs' : { 'invalid' : [], \
95
'valid' : ['someargs']}, \
97
'distroinstaller' : { \
98
'location' : { 'invalid' : ['nogood', \
106
'__init__' : { 'invalid' : [{'macaddr':0}, \
108
{'macaddr':'$%XD'}, \
109
{'type':'network'}, \
111
'bridge':'somebridge'},\
112
{'network':'somenet'}, \
114
'network':'somenet'},\
116
'bridge':'somebridge'},\
117
{'type':'unknowntype'}],\
122
'invalid' : ['', 0, 'invalid_name&',\
123
'123456781234567812345678123456789'],\
124
'valid' : ['some.valid-name_9', \
125
'12345678123456781234567812345678']},\
126
'clone_name': { 'invalid' : [0],
127
'valid' : ['some.valid-name_9']},
128
'clone_uuid': { 'invalid' : [0],
130
['12345678123456781234567812345678']},\
131
'clone_mac' : { 'invalid' : ['badformat'],
132
'valid' : ['AA:BB:CC:DD:EE:FF']},\
133
'clone_bs' : { 'invalid' : [],
134
'valid' : ['valid']}}\
137
class TestValidation(unittest.TestCase):
140
guest = virtinst.Guest(hypervisorURI="test:///default", type="xen")
142
def _testArgs(self, object, testclass, name):
143
"""@object Object to test parameters against
144
@testclass Full class to test initialization against
145
@name String name indexing args"""
146
for paramname in args[name]:
147
for val in args[name][paramname]['invalid']:
150
if paramname is '__init__':
151
testclass(*(), **val)
153
setattr(object, paramname, val)
154
msg = "Expected TypeError or ValueError: None raised.\n"
155
msg += "For '%s' object, paramname '%s', val '%s':" % \
156
(name, paramname, val)
157
raise AssertionError, msg
158
except AssertionError, e:
163
msg = "Unexpected exception raised: %s\n" % e
164
msg += "Original traceback was: \n%s\n" % \
165
traceback.format_exc()
166
msg += "For '%s' object, paramname '%s', val '%s':" % \
167
(name, paramname, val)
168
raise AssertionError, msg
170
for val in args[name][paramname]['valid']:
172
# Skip NFS test as non-root
173
if name == "distroinstaller" and paramname == "location" and val[0:3] == "nfs" and os.geteuid() != 0:
175
if paramname is '__init__':
176
testclass(*(), **val)
178
setattr(object, paramname, val)
180
msg = "Validation case failed, expected success.\n"
181
msg +="Exception received was: %s\n" % e
182
msg += "Original traceback was: \n%s\n" % \
183
traceback.format_exc()
184
msg += "For '%s' object, paramname '%s', val '%s':" % \
185
(name, paramname, val)
186
raise AssertionError, msg
190
def testGuestValidation(self):
191
PVGuest = virtinst.ParaVirtGuest(hypervisorURI="test:///default",\
193
self._testArgs(PVGuest, virtinst.Guest, 'guest')
195
def testDiskValidation(self):
196
disk = virtinst.VirtualDisk("/dev/loop0")
197
self._testArgs(disk, virtinst.VirtualDisk, 'disk')
199
def testFVGuestValidation(self):
200
FVGuest = virtinst.FullVirtGuest(hypervisorURI="test:///default",\
202
self._testArgs(FVGuest, virtinst.FullVirtGuest, 'fvguest')
204
def testNetworkValidation(self):
205
network = virtinst.VirtualNetworkInterface()
206
self._testArgs(network, virtinst.VirtualNetworkInterface, 'network')
208
# Test MAC Address collision
209
hostmac = virtinst.util.get_host_network_devices()
210
if len(hostmac) is not 0:
211
hostmac = hostmac[0][4]
213
for params in ({'macaddr' : hostmac},):
214
network = virtinst.VirtualNetworkInterface(*(), **params)
215
self.assertRaises(RuntimeError, network.setup, \
218
# Test dynamic MAC/Bridge success
220
network = virtinst.VirtualNetworkInterface()
221
network.setup(self.guest.conn)
223
raise AssertionError, \
224
"Network setup with no params failed, expected success."
226
def testDistroInstaller(self):
227
dinstall = virtinst.DistroInstaller()
228
self._testArgs(dinstall, virtinst.DistroInstaller, 'installer')
229
self._testArgs(dinstall, virtinst.DistroInstaller, 'distroinstaller')
231
def testCloneManager(self):
232
cman = virtinst.CloneManager.CloneDesign(self.guest.conn)
233
self._testArgs(cman, virtinst.CloneManager.CloneDesign, 'clonedesign')
236
if __name__ == "__main__":