3 from django
.contrib
.auth
.models
import Group
4 from django
.test
import TestCase
6 from object_permissions
import grant
8 from ganeti_web
import models
, constants
9 from ganeti_web
.tests
.views
.virtual_machine
.base
import TestVirtualMachineViewsBase
10 from ganeti_web
.utilities
import os_prettify
12 __all__
= ['TestVirtualMachineCreateHelpers', 'TestVirtualMachineHelpers']
14 Cluster
= models
.Cluster
20 class TestVirtualMachineCreateHelpers(TestVirtualMachineViewsBase
):
24 def test_view_cluster_choices(self
):
26 Test retrieving list of clusters a user or usergroup has access to
28 url
= '/vm/add/choices/'
29 Cluster
.objects
.all().delete()
30 cluster0
= Cluster(hostname
='user.create_vm', slug
='user_create_vm')
32 cluster1
= Cluster(hostname
='user.admin', slug
='user_admin')
34 cluster2
= Cluster(hostname
='superuser', slug
='superuser')
36 cluster3
= Cluster(hostname
='group.create_vm', slug
='group_create_vm')
38 cluster4
= Cluster(hostname
='group.admin', slug
='group_admin')
40 cluster5
= Cluster(hostname
='no.perms.on.this.group', slug
='no_perms')
42 # cluster ids are 1 through 6
44 group
.user_set
.add(user
)
45 group1
= Group(id=43, name
='testing_group2')
47 group1
.grant('admin',cluster5
)
50 response
= c
.get(url
, follow
=True)
51 self
.assertEqual(200, response
.status_code
)
52 self
.assertTemplateUsed(response
, 'registration/login.html')
54 self
.assert_(c
.login(username
=user
.username
, password
='secret'))
57 response
= c
.get(url
, {'clusteruser_id':-1})
58 self
.assertEqual(404, response
.status_code
)
60 # create_vm permission through a group
61 group
.grant('create_vm', cluster3
)
62 response
= c
.get(url
, {'clusteruser_id': group
.organization
.id})
63 self
.assertEqual(200, response
.status_code
)
64 clusters
= json
.loads(response
.content
)
65 self
.assert_([cluster3
.id,'group.create_vm'] in clusters
)
66 self
.assertEqual(1, len(clusters
))
68 # admin permission through a group
69 group
.grant('admin', cluster4
)
70 response
= c
.get(url
, {'clusteruser_id': group
.organization
.id})
71 self
.assertEqual(200, response
.status_code
)
72 clusters
= json
.loads(response
.content
)
73 self
.assert_([cluster3
.id,'group.create_vm'] in clusters
)
74 self
.assert_([cluster4
.id,'group.admin'] in clusters
)
75 self
.assertEqual(2, len(clusters
))
77 # create_vm permission on the user
78 user
.grant('create_vm', cluster0
)
80 self
.assertEqual(200, response
.status_code
)
81 clusters
= json
.loads(response
.content
)
82 self
.assert_([cluster0
.id,'user.create_vm'] in clusters
)
83 self
.assertEqual(1, len(clusters
), clusters
)
85 # admin permission on the user
86 user
.grant('admin', cluster1
)
88 self
.assertEqual(200, response
.status_code
)
89 clusters
= json
.loads(response
.content
)
90 self
.assert_([cluster0
.id,'user.create_vm'] in clusters
)
91 self
.assert_([cluster1
.id,'user.admin'] in clusters
)
92 self
.assertEqual(2, len(clusters
))
94 # Superusers see everything
95 user
.is_superuser
= True
98 self
.assertEqual(200, response
.status_code
)
99 self
.assertEqual('application/json', response
['content-type'])
100 clusters
= json
.loads(response
.content
)
101 self
.assert_([cluster0
.id,'user.create_vm'] in clusters
)
102 self
.assert_([cluster1
.id,'user.admin'] in clusters
)
103 self
.assert_([cluster2
.id,'superuser'] in clusters
, clusters
)
104 self
.assert_([cluster3
.id,'group.create_vm'] in clusters
)
105 self
.assert_([cluster4
.id,'group.admin'] in clusters
, clusters
)
106 self
.assert_([cluster5
.id,'no.perms.on.this.group'] in clusters
)
107 self
.assertEqual(6, len(clusters
))
109 def test_view_cluster_options(self
):
111 Test retrieving list of options a cluster has for vms
113 url
= '/vm/add/options/?cluster_id=%s'
117 response
= c
.post(url
% args
, follow
=True)
118 self
.assertEqual(200, response
.status_code
)
119 self
.assertTemplateUsed(response
, 'registration/login.html')
122 self
.assert_(c
.login(username
=user
.username
, password
='secret'))
123 response
= c
.get(url
% args
)
124 self
.assertEqual(403, response
.status_code
)
127 response
= c
.get(url
% "-4")
128 self
.assertEqual(404, response
.status_code
)
130 # authorized (create_vm)
131 grant(user
, 'create_vm', cluster
)
132 response
= c
.get(url
% args
)
133 self
.assertEqual(200, response
.status_code
)
134 self
.assertEqual('application/json', response
['content-type'])
135 content
= json
.loads(response
.content
)
136 self
.assertEqual(set([u
'gtest1.osuosl.bak', u
'gtest2.osuosl.bak', u
'gtest3.osuosl.bak']), set(content
['nodes']))
137 self
.assertEqual(content
["os"],
139 [[u
'image+debian-osgeo', u
'Debian Osgeo'],
140 [u
'image+ubuntu-lucid', u
'Ubuntu Lucid']]
143 user
.revoke_all(cluster
)
145 # authorized (cluster admin)
146 grant(user
, 'admin', cluster
)
147 response
= c
.get(url
% args
)
148 self
.assertEqual(200, response
.status_code
)
149 self
.assertEqual('application/json', response
['content-type'])
150 content
= json
.loads(response
.content
)
152 self
.assertEqual(set([u
'gtest1.osuosl.bak', u
'gtest2.osuosl.bak', u
'gtest3.osuosl.bak']), set(content
['nodes']))
153 self
.assertEqual(content
["os"],
155 [[u
'image+debian-osgeo', u
'Debian Osgeo'],
156 [u
'image+ubuntu-lucid', u
'Ubuntu Lucid']]
159 user
.revoke_all(cluster
)
161 # authorized (superuser)
162 user
.is_superuser
= True
164 response
= c
.get(url
% args
)
165 self
.assertEqual(200, response
.status_code
)
166 self
.assertEqual('application/json', response
['content-type'])
167 content
= json
.loads(response
.content
)
168 self
.assertEqual(set([u
'gtest1.osuosl.bak', u
'gtest2.osuosl.bak', u
'gtest3.osuosl.bak']), set(content
['nodes']))
169 self
.assertEqual(content
["os"],
171 [[u
'image+debian-osgeo', u
'Debian Osgeo'],
172 [u
'image+ubuntu-lucid', u
'Ubuntu Lucid']]
176 def test_view_cluster_defaults(self
):
178 Test retrieval of dict of default parameters set on cluster
180 url
= '/vm/add/defaults/?cluster_id=%s'
186 nic_type='paravirtual',
187 root_path='/dev/vda2',
191 disk_type ='paravirtual',
200 nic_type
='paravirtual',
203 vnc_bind_address
='0.0.0.0',
206 hypervisors
=[['kvm', 'kvm']],
207 migration_downtime
=30,
210 ['rtl8139', 'rtl8139'],
211 ['ne2k_isa', 'ne2k_isa'],
212 ['ne2k_pci', 'ne2k_pci'],
213 ['paravirtual', 'paravirtual'],
214 ['i82551', 'i82551'],
215 ['i82557b', 'i82557b'],
216 ['i82559er', 'i82559er'],
226 ['paravirtual', 'paravirtual'],
234 disk_cache
='default',
238 vnc_x509_verify
=False,
242 ['disk', 'Hard Disk'],
244 ['network', 'Network']
249 vnc_password_file
='',
250 migration_bandwidth
=32,
251 disk_type
='paravirtual',
252 security_model
='none',
253 migration_mode
='live',
256 root_path
='/dev/vda2',
263 response
= c
.post(url
% args
, follow
=True)
264 self
.assertEqual(200, response
.status_code
)
265 self
.assertTemplateUsed(response
, 'registration/login.html')
268 self
.assert_(c
.login(username
=user
.username
, password
='secret'))
269 response
= c
.get(url
% args
)
270 self
.assertEqual(403, response
.status_code
)
273 response
= c
.get(url
% "-2")
274 self
.assertEqual(404, response
.status_code
)
276 #authorized (create_vm)
277 grant(user
, 'create_vm', cluster
)
278 response
= c
.get(url
% args
)
279 self
.assertEqual(200, response
.status_code
)
280 self
.assertEqual('application/json', response
['content-type'])
281 content
= json
.loads(response
.content
)
282 self
.assertEqual(expected
, content
, msg
=content
)
283 user
.revoke_all(cluster
)
286 grant(user
, 'admin', cluster
)
287 response
= c
.get(url
% args
)
288 self
.assertEqual(200, response
.status_code
)
289 self
.assertEqual('application/json', response
['content-type'])
290 content
= json
.loads(response
.content
)
291 self
.assertEqual(expected
, content
)
292 user
.revoke_all(cluster
)
294 #authorized (superuser)
295 user
.is_superuser
= True
297 response
= c
.get(url
% args
)
298 self
.assertEqual(200, response
.status_code
)
299 self
.assertEqual('application/json', response
['content-type'])
300 content
= json
.loads(response
.content
)
301 self
.assertEqual(expected
, content
)
302 user
.is_superuser
= False
306 class TestVirtualMachineHelpers(TestCase
):
310 def test_os_prettify(self
):
312 Test the os_prettify() helper function.
315 # Test a single entry.
316 self
.assertEqual(os_prettify(["hurp+durp"]),
319 [("hurp+durp", "Durp")]
323 def test_os_prettify_multiple(self
):
325 Test os_prettify()'s ability to handle multiple entries, including two
326 entries on the same category.
331 "image+obonto-hungry-hydralisk",
333 "dobootstrop+dobion-lotso",
336 ("dobootstrop+dobion-lotso", "Dobion Lotso"),
339 ("image+obonto-hungry-hydralisk",
340 "Obonto Hungry Hydralisk"),
341 ("image+fodoro-core", "Fodoro Core"),
345 def test_os_prettify_2517(self
):
347 Test #2157 compliance.
349 This example should still parse, but in a weird way. Better than
353 self
.assertEqual(os_prettify(["debian-pressed+ia32"]),
354 [('Debian-pressed', [('debian-pressed+ia32', 'Ia32')])])
356 def test_os_prettify_2517_unknown(self
):
358 Test #2157 compliance.
360 This example wasn't part of the bug; it was constructed to show off
364 self
.assertEqual(os_prettify(["deb-ver1", "noop"]),
367 ("deb-ver1", "deb-ver1"),