~ubuntu-branches/ubuntu/quantal/virtinst/quantal-proposed

« back to all changes in this revision

Viewing changes to tests/validation.py

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2007-11-20 13:40:28 UTC
  • Revision ID: james.westby@ubuntu.com-20071120134028-rg0pjby0jc4mycks
Tags: upstream-0.300.1+hg20071120
ImportĀ upstreamĀ versionĀ 0.300.1+hg20071120

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
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.
 
6
#
 
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.
 
11
#
 
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,
 
15
# MA 02110-1301 USA.
 
16
 
 
17
import virtinst
 
18
import unittest
 
19
import traceback
 
20
import os
 
21
 
 
22
# Template for adding arguments to test
 
23
#      { 'label'    : { 'VAR'       : { 'invalid' : [param],\
 
24
#                                       'valid'   : [param]},\
 
25
#                       '__init__'  : { 'invalid' : [{'initparam':val}],\
 
26
#                                       'valid'   : [{'initparam':val}]}\
 
27
 
 
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'),\
 
41
                                                    {}], \
 
42
                                        'valid'   : [False, 'sdl', 'vnc', \
 
43
                                                    (True, 'sdl', '',\
 
44
                                                    'key_map-2'),\
 
45
                                                    {'enabled' : True, \
 
46
                                                     'type':'vnc', 'opts':5900}\
 
47
                                                    ]},\
 
48
                        'type'      : { 'invalid' : [], \
 
49
                                        'valid'   : ['sometype'] }, \
 
50
                        'cdrom'     : { 'invalid' : ['', 0, '/somepath'],\
 
51
                                        'valid'   : ['/dev/loop0']}\
 
52
                      },\
 
53
         'fvguest'  : { \
 
54
                        'os_type'   : { 'invalid' : ['notpresent',0,''],\
 
55
                                        'valid'   : ['other', 'windows',\
 
56
                                                     'unix', 'linux']}, \
 
57
                        'os_variant': { 'invalid' : ['', 0, 'invalid'], \
 
58
                                        'valid'   : ['rhel5', \
 
59
                                                     'sles10']}, \
 
60
                      },\
 
61
         'disk'     : { \
 
62
                        '__init__'  : { 'invalid' : [{ 'path' : 0},\
 
63
                                                     { 'path' : '/root' },\
 
64
                                                     { 'path' : 'valid',
 
65
                                                       'size' : None },\
 
66
                                                     { 'path' : "valid", \
 
67
                                                       'size' : 'invalid'},\
 
68
                                                     { 'path' : 'valid', \
 
69
                                                       'size' : -1},\
 
70
                                                     { 'path' : 'notblock',\
 
71
                                                       'type' : virtinst.VirtualDisk.TYPE_BLOCK},\
 
72
                                                     { 'path' :'/dev/null',\
 
73
                                                       'type' : virtinst.VirtualDisk.TYPE_BLOCK},
 
74
                                                     { 'path' : None}],\
 
75
                                        'valid'   : [{ 'path' : '/dev/loop0'},\
 
76
                                                     { 'path' : 'nonexist', \
 
77
                                                       'size' : 10}, \
 
78
                                                     { 'path' :'/dev/null'},
 
79
                                                     { 'path' : None,
 
80
                                                       'device' : virtinst.VirtualDisk.DEVICE_CDROM},
 
81
                                                     { 'path' : None,
 
82
                                                       'device' : virtinst.VirtualDisk.DEVICE_FLOPPY}]}\
 
83
                      },\
 
84
         'installer' : { \
 
85
                        'boot'      : { 'invalid' : ['', 0, ('1element'),\
 
86
                                                     ['1el', '2el', '3el'],\
 
87
                                                     {'1element': '1val'},\
 
88
                                                     {'kernel' : 'a',\
 
89
                                                      'wronglabel' : 'b'}],\
 
90
                                        'valid'   : [('kern', 'init'),\
 
91
                                                     ['kern', 'init'],\
 
92
                                                     { 'kernel' : 'a',\
 
93
                                                       'initrd' : 'b'}]}, \
 
94
                        'extraargs' : { 'invalid' : [], \
 
95
                                        'valid'   : ['someargs']}, \
 
96
                             },\
 
97
         'distroinstaller' : { \
 
98
                        'location'  : { 'invalid' : ['nogood', \
 
99
                                                     'http:/nogood'],\
 
100
                                        'valid'   : ['/file',\
 
101
                                                     'http://web',\
 
102
                                                     'ftp://ftp',\
 
103
                                                     'nfs:nfsserv']}\
 
104
                             },\
 
105
         'network'   : { \
 
106
                        '__init__'  : { 'invalid' : [{'macaddr':0}, \
 
107
                                                     {'macaddr':''},\
 
108
                                                     {'macaddr':'$%XD'}, \
 
109
                                                     {'type':'network'}, \
 
110
                                                     {'type':'network', \
 
111
                                                      'bridge':'somebridge'},\
 
112
                                                     {'network':'somenet'}, \
 
113
                                                     {'type':'user',\
 
114
                                                      'network':'somenet'},\
 
115
                                                     {'type':'user',\
 
116
                                                      'bridge':'somebridge'},\
 
117
                                                     {'type':'unknowntype'}],\
 
118
                                        'valid'   : []}, \
 
119
                     },\
 
120
         'clonedesign' : {\
 
121
                        'original_guest' :{\
 
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],
 
129
                                        'valid'   :
 
130
                                        ['12345678123456781234567812345678']},\
 
131
                        'clone_mac' : { 'invalid' : ['badformat'],
 
132
                                        'valid'   : ['AA:BB:CC:DD:EE:FF']},\
 
133
                        'clone_bs'  : { 'invalid' : [],
 
134
                                        'valid'   : ['valid']}}\
 
135
       }
 
136
 
 
137
class TestValidation(unittest.TestCase):
 
138
 
 
139
 
 
140
    guest = virtinst.Guest(hypervisorURI="test:///default", type="xen")
 
141
 
 
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']:
 
148
 
 
149
                try:
 
150
                    if paramname is '__init__':
 
151
                        testclass(*(), **val)                    
 
152
                    else:
 
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:
 
159
                    raise e
 
160
                except ValueError:
 
161
                    pass
 
162
                except Exception, 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
 
169
                
 
170
            for val in args[name][paramname]['valid']:
 
171
                try:
 
172
                    # Skip NFS test as non-root
 
173
                    if name == "distroinstaller" and paramname == "location" and val[0:3] == "nfs" and os.geteuid() != 0:
 
174
                        continue
 
175
                    if paramname is '__init__':
 
176
                        testclass(*(), **val)                    
 
177
                    else:
 
178
                        setattr(object, paramname, val)
 
179
                except Exception, e:
 
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
 
187
 
 
188
    # Actual Tests
 
189
 
 
190
    def testGuestValidation(self):
 
191
        PVGuest = virtinst.ParaVirtGuest(hypervisorURI="test:///default",\
 
192
                                         type="xen")
 
193
        self._testArgs(PVGuest, virtinst.Guest, 'guest')
 
194
 
 
195
    def testDiskValidation(self):
 
196
        disk = virtinst.VirtualDisk("/dev/loop0")
 
197
        self._testArgs(disk, virtinst.VirtualDisk, 'disk')
 
198
 
 
199
    def testFVGuestValidation(self):
 
200
        FVGuest = virtinst.FullVirtGuest(hypervisorURI="test:///default",\
 
201
                                         type="xen")
 
202
        self._testArgs(FVGuest, virtinst.FullVirtGuest, 'fvguest')
 
203
 
 
204
    def testNetworkValidation(self):
 
205
        network = virtinst.VirtualNetworkInterface()
 
206
        self._testArgs(network, virtinst.VirtualNetworkInterface, 'network')
 
207
 
 
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]
 
212
 
 
213
        for params in ({'macaddr' : hostmac},):
 
214
            network = virtinst.VirtualNetworkInterface(*(), **params)
 
215
            self.assertRaises(RuntimeError, network.setup, \
 
216
                              self.guest.conn)
 
217
        
 
218
        # Test dynamic MAC/Bridge success
 
219
        try:
 
220
            network = virtinst.VirtualNetworkInterface()
 
221
            network.setup(self.guest.conn)
 
222
        except Exception, e:
 
223
            raise AssertionError, \
 
224
                "Network setup with no params failed, expected success."
 
225
 
 
226
    def testDistroInstaller(self):
 
227
       dinstall = virtinst.DistroInstaller()
 
228
       self._testArgs(dinstall, virtinst.DistroInstaller, 'installer')
 
229
       self._testArgs(dinstall, virtinst.DistroInstaller, 'distroinstaller')
 
230
 
 
231
    def testCloneManager(self):
 
232
        cman = virtinst.CloneManager.CloneDesign(self.guest.conn)
 
233
        self._testArgs(cman, virtinst.CloneManager.CloneDesign, 'clonedesign')
 
234
    
 
235
 
 
236
if __name__ == "__main__":
 
237
    unittest.main()