1 from itertools
import chain
4 from django
.contrib
.auth
.models
import User
, Group
5 from django
.test
import TestCase
7 from ganeti
import models
8 from ganeti
import constants
9 from ganeti
.forms
.virtual_machine
import NewVirtualMachineForm
, \
10 HvmModifyVirtualMachineForm
, KvmModifyVirtualMachineForm
, \
11 PvmModifyVirtualMachineForm
12 from ganeti
.tests
.rapi_proxy
import RapiProxy
, XenRapiProxy
, INFO
, XEN_INFO
13 from ganeti
.tests
.views
.virtual_machine
.base
import VirtualMachineTestCaseMixin
15 __all__
= ['TestNewVirtualMachineForm',
16 'TestKvmModifyVirtualMachineForm',
17 'TestHvmModifyVirtualMachineForm',
18 'TestPvmModifyVirtualMachineForm']
20 VirtualMachine
= models
.VirtualMachine
21 Cluster
= models
.Cluster
23 global user
, user1
, group
24 global cluster0
, cluster1
, cluster2
, cluster3
25 global kvm_cluster
, hvm_cluster
, pvm_cluster
26 global kvm_vm
, hvm_vm
, pvm_vm
28 class TestNewVirtualMachineForm(TestCase
, VirtualMachineTestCaseMixin
):
31 global user
, user1
, group
, cluster0
, cluster1
, cluster2
, cluster3
33 models
.client
.GanetiRapiClient
= RapiProxy
34 cluster0
= Cluster
.objects
.create(hostname
='test0', slug
='test0')
35 cluster1
= Cluster
.objects
.create(hostname
='test1', slug
='test1')
36 cluster2
= Cluster
.objects
.create(hostname
='test2', slug
='test2')
37 cluster3
= Cluster
.objects
.create(hostname
='test3', slug
='test3')
41 user
= User(id=67, username
='tester0')
42 user
.set_password('secret')
44 user1
= User(id=70, username
='tester1')
45 user1
.set_password('secret')
47 group
= Group(id=45, name
='testing_group')
51 User
.objects
.all().delete()
52 Group
.objects
.all().delete()
53 VirtualMachine
.objects
.all().delete()
54 Cluster
.objects
.all().delete()
56 def test_default_choices(self
):
58 Test that ChoiceFields have the correct default options
60 form
= NewVirtualMachineForm(user
)
61 self
.assertEqual([(u
'', u
'---------')],
62 form
.fields
['nic_type'].choices
)
63 self
.assertEqual([(u
'', u
'---------'),
64 (u
'routed', u
'routed'),
65 (u
'bridged', u
'bridged')],
66 form
.fields
['nic_mode'].choices
)
67 self
.assertEqual([(u
'', u
'---------')],
68 form
.fields
['boot_order'].choices
)
69 self
.assertEqual([(u
'', u
'---------'),
73 (u
'diskless', u
'diskless')],
74 form
.fields
['disk_template'].choices
)
76 def test_cluster_init(self
):
78 Tests initializing a form with a Cluster
81 * cluster choices are set correctly
82 * node choices are set correctly
86 form
= NewVirtualMachineForm(user
)
87 self
.assertEqual([(u
'', u
'---------')], form
.fields
['pnode'].choices
)
88 self
.assertEqual([(u
'', u
'---------')], form
.fields
['snode'].choices
)
89 self
.assertEqual([(u
'', u
'---------')], form
.fields
['os'].choices
)
91 # cluster from initial data
92 form
= NewVirtualMachineForm(user
, {'cluster':cluster0
.id})
93 self
.assertEqual(set([(u
'', u
'---------'), (u
'gtest1.osuosl.bak', u
'gtest1.osuosl.bak'), (u
'gtest2.osuosl.bak', u
'gtest2.osuosl.bak'), (u
'gtest3.osuosl.bak', u
'gtest3.osuosl.bak')]), set(form
.fields
['pnode'].choices
))
94 self
.assertEqual(set([(u
'', u
'---------'), (u
'gtest1.osuosl.bak', u
'gtest1.osuosl.bak'), (u
'gtest2.osuosl.bak', u
'gtest2.osuosl.bak'), (u
'gtest3.osuosl.bak', u
'gtest3.osuosl.bak')]), set(form
.fields
['snode'].choices
))
95 self
.assertEqual(form
.fields
['os'].choices
,
99 [('image+debian-osgeo', 'Debian Osgeo'),
100 ('image+ubuntu-lucid', 'Ubuntu Lucid')]
105 # cluster from initial data
106 form
= NewVirtualMachineForm(user
, {'cluster':cluster0
.id})
107 self
.assertEqual(set([(u
'', u
'---------'), (u
'gtest1.osuosl.bak', u
'gtest1.osuosl.bak'), (u
'gtest2.osuosl.bak', u
'gtest2.osuosl.bak'), (u
'gtest3.osuosl.bak', u
'gtest3.osuosl.bak')]), set(form
.fields
['pnode'].choices
))
108 self
.assertEqual(set([(u
'', u
'---------'), (u
'gtest1.osuosl.bak', u
'gtest1.osuosl.bak'), (u
'gtest2.osuosl.bak', u
'gtest2.osuosl.bak'), (u
'gtest3.osuosl.bak', u
'gtest3.osuosl.bak')]), set(form
.fields
['snode'].choices
))
109 self
.assertEqual(form
.fields
['os'].choices
,
113 [('image+debian-osgeo', 'Debian Osgeo'),
114 ('image+ubuntu-lucid', 'Ubuntu Lucid')]
119 def test_cluster_choices_init(self
):
121 Tests that cluster choices are based on User permissions
124 * superusers have all Clusters as choices
125 * if owner is set, only display clusters the owner has permissions
126 directly on. This includes both users and groups
127 * if no owner is set, choices include clusters that the user has
128 permission directly on, or through a group
132 # no owner, no permissions
133 form
= NewVirtualMachineForm(user
)
134 self
.assertEqual(set([(u
'', u
'---------')]), set(form
.fields
['cluster'].choices
))
136 # no owner, group and direct permissions
137 user
.grant('admin', cluster0
)
138 user
.grant('create_vm', cluster1
)
139 group
.grant('admin', cluster2
)
140 group
.user_set
.add(user
)
141 self
.assertEqual(set([(u
'', u
'---------'), (cluster0
.pk
, u
'test0'), (cluster1
.pk
, u
'test1'), (cluster2
.pk
, u
'test2')]), set(form
.fields
['cluster'].choices
))
142 user
.revoke_all(cluster0
)
143 user
.revoke_all(cluster1
)
144 group
.revoke_all(cluster2
)
146 # owner, user with no choices
147 form
= NewVirtualMachineForm(user
, initial
={'owner':user
.get_profile().id})
148 self
.assertEqual(set([(u
'', u
'---------')]), set(form
.fields
['cluster'].choices
))
150 # owner, user with choices
151 user
.grant('admin', cluster0
)
152 user
.grant('create_vm', cluster1
)
153 form
= NewVirtualMachineForm(user
, initial
={'owner':user
.get_profile().id})
154 self
.assertEqual(set([(u
'', u
'---------'), (cluster0
.pk
, u
'test0'), (cluster1
.pk
, u
'test1')]), set(form
.fields
['cluster'].choices
))
156 # owner, group with no choices
157 form
= NewVirtualMachineForm(user
, initial
={'owner':group
.organization
.id})
158 self
.assertEqual(set([(u
'', u
'---------')]), set(form
.fields
['cluster'].choices
))
160 # owner, group with choices
161 group
.grant('admin', cluster2
)
162 group
.grant('create_vm', cluster3
)
163 form
= NewVirtualMachineForm(user
, initial
={'owner':group
.organization
.id})
164 self
.assertEqual(set([(u
'', u
'---------'), (cluster2
.pk
, u
'test2'), (cluster3
.pk
, u
'test3')]), set(form
.fields
['cluster'].choices
))
167 user
.is_superuser
= True
169 form
= NewVirtualMachineForm(user
, initial
={'owner':user
.get_profile().id})
170 self
.assertEqual(set([(u
'', u
'---------'), (cluster0
.pk
, u
'test0'), (cluster1
.pk
, u
'test1'), (cluster2
.pk
, u
'test2'), (cluster3
.pk
, u
'test3')]), set(form
.fields
['cluster'].choices
))
173 form
= NewVirtualMachineForm(user
, initial
={'owner':group
.organization
.id})
174 self
.assertEqual(set([(u
'', u
'---------'), (cluster0
.pk
, u
'test0'), (cluster1
.pk
, u
'test1'), (cluster2
.pk
, u
'test2'), (cluster3
.pk
, u
'test3')]), set(form
.fields
['cluster'].choices
))
176 def test_owner_choices_init(self
):
178 Tests that owner choices are set based on User permissions
181 * superusers have all clusterusers as choices
182 * user receives themselves as a choice if they have perms
183 * user receives all groups they are a member of
186 # user with no choices
187 form
= NewVirtualMachineForm(user
)
188 self
.assertEqual([(u
'', u
'---------')], form
.fields
['owner'].choices
)
190 # user with perms on self, no groups
191 user
.grant('admin', cluster0
)
192 form
= NewVirtualMachineForm(user
)
196 (user
.profile
.id, u
'tester0'),
197 ], form
.fields
['owner'].choices
)
198 user
.set_perms(['create_vm'], cluster0
)
199 form
= NewVirtualMachineForm(user
)
203 (user
.profile
.id, u
'tester0'),
204 ], form
.fields
['owner'].choices
)
206 # user with perms on self and groups
207 group
.user_set
.add(user
)
208 group
.grant('admin', cluster0
)
209 form
= NewVirtualMachineForm(user
)
213 (group
.organization
.id, u
'testing_group'),
214 (user
.profile
.id, u
'tester0'),
215 ], form
.fields
['owner'].choices
)
216 user
.revoke_all(cluster0
)
218 # user with no perms on self, but groups
219 form
= NewVirtualMachineForm(user
)
223 (group
.organization
.id, u
'testing_group'),
224 ], form
.fields
['owner'].choices
)
225 group
.set_perms(['create_vm'], cluster0
)
226 form
= NewVirtualMachineForm(user
)
230 (group
.organization
.id, u
'testing_group'),
231 ], form
.fields
['owner'].choices
)
232 group
.revoke_all(cluster0
)
235 user
.is_superuser
= True
237 form
= NewVirtualMachineForm(user
)
241 (user
.profile
.id, u
'tester0'),
242 (user1
.profile
.id, u
'tester1'),
243 (group
.organization
.id, u
'testing_group'),
244 ], list(form
.fields
['owner'].choices
))
247 class TestKvmModifyVirtualMachineForm(TestCase
, VirtualMachineTestCaseMixin
):
249 global kvm_cluster
, kvm_vm
251 models
.client
.GanetiRapiClient
= RapiProxy
252 kvm_cluster
= Cluster
.objects
.create(hostname
='test0.kvm_cluster',
254 kvm_vm
= VirtualMachine
.objects
.create(hostname
='kvm.osuosl',
258 VirtualMachine
.objects
.all().delete()
259 Cluster
.objects
.all().delete()
261 def test_meta_rapiproxy_set(self
):
262 self
.assertEqual(models
.client
.GanetiRapiClient
, RapiProxy
)
264 def test_form_defaults(self
):
266 Test the default choices for ChoiceFields on the form.
268 choices
= constants
.KVM_CHOICES
269 disk_type
= choices
['disk_type']
270 nic_type
= choices
['nic_type']
271 boot_order
= choices
['boot_order']
272 disk_caches
= constants
.HV_DISK_CACHES
273 kvm_flags
= constants
.KVM_FLAGS
274 security_models
= constants
.HV_SECURITY_MODELS
275 usb_mice
= constants
.HV_USB_MICE
277 form
= KvmModifyVirtualMachineForm(kvm_vm
)
279 self
.assertEqual(set(disk_type
),
280 set(fields
['disk_type'].choices
))
281 self
.assertEqual(set(nic_type
),
282 set(fields
['nic_type'].choices
))
283 self
.assertEqual(set(boot_order
),
284 set(fields
['boot_order'].choices
))
285 self
.assertEqual(set(disk_caches
),
286 set(fields
['disk_cache'].choices
))
287 self
.assertEqual(set(kvm_flags
),
288 set(fields
['kvm_flag'].choices
))
289 self
.assertEqual(set(security_models
),
290 set(fields
['security_model'].choices
))
291 self
.assertEqual(set(usb_mice
),
292 set(fields
['usb_mouse'].choices
))
294 def test_initial_form_fields(self
):
296 Test that the form does not contain any extra fields.
298 modify_fields
= ('vcpus', 'memory', 'nic_link', 'nic_mac', 'os_name')
299 hv_fields
= KvmModifyVirtualMachineForm
.hvparam_fields
300 form
= KvmModifyVirtualMachineForm(kvm_vm
)
302 for field
in chain(modify_fields
, hv_fields
):
303 self
.assertTrue(field
in form
.fields
)
305 for field
in form
.Meta
.exclude
:
306 self
.assertFalse(field
in form
.fields
)
309 class TestHvmModifyVirtualMachineForm(TestCase
, VirtualMachineTestCaseMixin
):
311 from ganeti
.tests
.rapi_proxy
import XEN_HVM_INSTANCE
312 global hvm_cluster
, hvm_vm
314 models
.client
.GanetiRapiClient
= XenRapiProxy
315 hvm_cluster
= Cluster
.objects
.create(hostname
='test2.hvm_cluster',
317 hvm_cluster
.info
= XEN_INFO
.copy()
318 hvm_cluster
.info
['default_hypervisor'] = 'xen-hvm'
319 hvm_vm
= VirtualMachine
.objects
.create(hostname
='hvm.osuosl',
321 hvm_vm
.info
= XEN_HVM_INSTANCE
324 User
.objects
.all().delete()
325 Group
.objects
.all().delete()
326 VirtualMachine
.objects
.all().delete()
327 Cluster
.objects
.all().delete()
329 def test_meta_xenrapiproxy_set(self
):
330 self
.assertEqual(models
.client
.GanetiRapiClient
, XenRapiProxy
)
332 def test_meta_default_hypervisor(self
):
333 self
.assertEqual(hvm_cluster
.info
['default_hypervisor'], 'xen-hvm')
335 def test_form_defaults(self
):
336 choices
= constants
.HVM_CHOICES
337 disk_type
= choices
['disk_type']
338 nic_type
= choices
['nic_type']
339 boot_order
= choices
['boot_order']
341 form
= HvmModifyVirtualMachineForm(hvm_vm
)
342 self
.assertEqual(set(disk_type
),
343 set(form
.fields
['disk_type'].choices
))
344 self
.assertEqual(set(nic_type
),
345 set(form
.fields
['nic_type'].choices
))
346 self
.assertEqual(set(boot_order
),
347 set(form
.fields
['boot_order'].choices
))
349 def test_initial_form_fields(self
):
351 Test that the form does not contain any extra fields.
353 modify_fields
= ('vcpus', 'memory', 'nic_link', 'nic_mac', 'os_name')
354 hv_fields
= HvmModifyVirtualMachineForm
.hvparam_fields
355 form
= HvmModifyVirtualMachineForm(hvm_vm
)
357 for field
in chain(modify_fields
, hv_fields
):
358 self
.assertTrue(field
in form
.fields
)
360 for field
in form
.Meta
.exclude
:
361 self
.assertFalse(field
in form
.fields
)
363 # Uncomment to see if modify contains all available hvparam fields
364 hvparam_fields = HvmModifyVirtualMachineForm.hvparam_fields
365 hvparams = hvm_cluster.info['hvparams']['xen-hvm']
366 self.assertEqual(set(hvparams), set(hvparam_fields))
369 def test_field_initial_values(self
):
371 Test that fields contain the correct initial values taken from a vm.
373 hvparam_fields
= HvmModifyVirtualMachineForm
.hvparam_fields
374 hvparams
= hvm_vm
.info
['hvparams']
375 form
= HvmModifyVirtualMachineForm(hvm_vm
)
377 for field
in hvparam_fields
:
378 self
.assertEqual(form
.fields
[field
].initial
, hvparams
[field
])
380 class TestPvmModifyVirtualMachineForm(TestCase
, VirtualMachineTestCaseMixin
):
382 from ganeti
.tests
.rapi_proxy
import XEN_PVM_INSTANCE
383 global pvm_cluster
, pvm_vm
385 models
.client
.GanetiRapiClient
= XenRapiProxy
386 pvm_cluster
= Cluster
.objects
.create(hostname
='test1.pvm_cluster',
388 pvm_cluster
.info
= XEN_INFO
389 pvm_vm
= VirtualMachine
.objects
.create(hostname
='pvm.osuosl',
391 pvm_vm
.info
= XEN_PVM_INSTANCE
394 User
.objects
.all().delete()
395 Group
.objects
.all().delete()
396 VirtualMachine
.objects
.all().delete()
397 Cluster
.objects
.all().delete()
399 def test_meta_default_hypervisor(self
):
400 self
.assertEqual(pvm_cluster
.info
['default_hypervisor'], 'xen-pvm')
402 def test_meta_xenrapiproxy_set(self
):
403 self
.assertEqual(models
.client
.GanetiRapiClient
, XenRapiProxy
)
405 def test_form_defaults(self
):
406 form
= PvmModifyVirtualMachineForm(pvm_vm
)
408 def test_initial_form_fields(self
):
410 Test that the form does not contain any extra fields.
412 modify_fields
= ('vcpus', 'memory', 'nic_link', 'nic_mac', 'os_name')
413 hv_fields
= PvmModifyVirtualMachineForm
.hvparam_fields
414 form
= PvmModifyVirtualMachineForm(pvm_vm
)
416 for field
in chain(modify_fields
, hv_fields
):
417 self
.assertTrue(field
in form
.fields
)
419 for field
in form
.Meta
.exclude
:
420 self
.assertFalse(field
in form
.fields
)
422 def test_bound_form(self
):
424 os_name
= 'image+default',
428 form
= PvmModifyVirtualMachineForm(pvm_vm
, data
)
429 self
.assertTrue(form
.is_bound
)
430 self
.assertTrue(form
.is_valid(), msg
=form
.errors
)
432 def test_field_initial_values(self
):
434 Test that fields contain the correct initial values taken from a vm.
436 hvparam_fields
= PvmModifyVirtualMachineForm
.hvparam_fields
437 hvparams
= pvm_vm
.info
['hvparams']
438 form
= PvmModifyVirtualMachineForm(pvm_vm
)
440 for field
in hvparam_fields
:
441 self
.assertEqual(form
.fields
[field
].initial
, hvparams
[field
])