1 # Copyright (C) 2010 Oregon State University et al.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 from django
.contrib
.auth
.models
import User
, Group
20 from django
.test
import TestCase
22 from ganeti
.tests
.rapi_proxy
import RapiProxy
23 from ganeti
import models
24 VirtualMachine
= models
.VirtualMachine
25 Cluster
= models
.Cluster
26 ClusterUser
= models
.ClusterUser
27 Profile
= models
.Profile
28 Organization
= models
.Organization
31 __all__
= ('TestClusterUser',)
34 class TestClusterUser(TestCase
):
38 models
.client
.GanetiRapiClient
= RapiProxy
41 Quota
.objects
.all().delete()
42 Profile
.objects
.all().delete()
43 ClusterUser
.objects
.all().delete()
44 User
.objects
.all().delete()
45 Organization
.objects
.all().delete()
46 Group
.objects
.all().delete()
47 VirtualMachine
.objects
.all().delete()
48 Cluster
.objects
.all().delete()
50 def test_user_signal(self
):
52 Test signals related to User:
55 * profile is created/deleted with user
57 user
= User(username
='tester')
61 profile
= user
.get_profile()
62 self
.assert_(profile
, 'profile was not created')
66 self
.assertFalse(Profile
.objects
.filter(id=profile
.id).exists())
68 def test_group_signal(self
):
70 Test signals related to User:
73 * organization is created/deleted with Group
75 group
= Group(name
='tester')
79 org
= group
.organization
80 self
.assert_(group
.organization
, 'profile was not created')
84 self
.assertFalse(Organization
.objects
.filter(id=org
.id).exists())
86 def test_casting_profile(self
):
88 Tests casting ClusterUser into Profile
90 user
= User(username
='tester')
93 cluster_user
= ClusterUser
.objects
.all()[0]
94 profile
= cluster_user
.cast()
96 self
.assert_(isinstance(profile
, (Profile
,)))
98 def test_casting_organization(self
):
100 Tests casting ClusterUser into an Organization
102 group
= Group(name
='tester')
105 cluster_user
= ClusterUser
.objects
.all()[0]
106 organization
= cluster_user
.cast()
108 self
.assert_(isinstance(organization
, (Organization
,)))
110 def test_used_resources(self
):
112 Tests retrieving dictionary of resources used by a cluster user
114 c1
= Cluster(hostname
="testing1", slug
="test1")
115 c2
= Cluster(hostname
="testing2", slug
="test2")
116 c3
= Cluster(hostname
="testing3", slug
="test3")
117 user
= User(username
="owner")
118 quota
= {"disk": 26, "ram":6, "virtual_cpus":14}
120 for i
in (c1
, c2
, c3
, user
): i
.save()
122 owner
= user
.get_profile()
123 c1
.set_quota(owner
, quota
)
124 #c2.set_quota(owner, quota)
125 c3
.set_quota(owner
, quota
)
127 # test used_resources returns zeros for no values
128 result
= owner
.used_resources(cluster
=c1
)
129 self
.assertEqual(0, result
['ram'])
130 self
.assertEqual(0, result
['disk'])
131 self
.assertEqual(0, result
['virtual_cpus'])
133 vm11
= VirtualMachine(hostname
="1one", owner
=owner
, cluster
=c1
, status
="running")
134 vm21
= VirtualMachine(hostname
="2one", owner
=owner
, cluster
=c2
, status
="running")
135 vm31
= VirtualMachine(hostname
="3one", owner
=owner
, cluster
=c2
, status
="running")
137 vm12
= VirtualMachine(hostname
="1two", owner
=owner
, cluster
=c1
, status
="running",
138 ram
=1, virtual_cpus
=3, disk_size
=6)
139 vm22
= VirtualMachine(hostname
="2two", owner
=owner
, cluster
=c2
, status
="running",
140 ram
=1, virtual_cpus
=3, disk_size
=6)
141 vm32
= VirtualMachine(hostname
="3two", owner
=owner
, cluster
=c3
, status
="running",
142 ram
=1, virtual_cpus
=3, disk_size
=6)
144 vm13
= VirtualMachine(hostname
="1three", owner
=owner
, cluster
=c1
, status
="stopped",
145 ram
=1, virtual_cpus
=3, disk_size
=6)
146 vm23
= VirtualMachine(hostname
="2three", owner
=owner
, cluster
=c2
, status
="stopped",
147 ram
=1, virtual_cpus
=3, disk_size
=6)
148 vm33
= VirtualMachine(hostname
="3three", owner
=owner
, cluster
=c3
, status
="stopped",
149 ram
=1, virtual_cpus
=3, disk_size
=6)
151 for i
in (vm11
, vm12
, vm13
, vm21
, vm22
, vm23
, vm31
, vm32
, vm33
):
154 # multiple clusters - every VM
155 result
= owner
.used_resources(cluster
=None, only_running
=False)
156 self
.assert_(c1
.id in result
.keys())
157 self
.assert_(c2
.id in result
.keys())
158 self
.assert_(c3
.id in result
.keys())
159 self
.assertEqual(result
[c1
.id]["disk"], 12)
160 self
.assertEqual(result
[c1
.id]["ram"], 2)
161 self
.assertEqual(result
[c1
.id]["virtual_cpus"], 6)
162 self
.assertEqual(result
[c1
.id], result
[c3
.id])
164 # multiple clusters - only running VMs
165 result
= owner
.used_resources(cluster
=None, only_running
=True)
166 self
.assert_(c1
.id in result
.keys())
167 self
.assert_(c2
.id in result
.keys())
168 self
.assert_(c3
.id in result
.keys())
169 self
.assertEqual(result
[c1
.id]["disk"], 12)
170 self
.assertEqual(result
[c1
.id]["ram"], 1)
171 self
.assertEqual(result
[c1
.id]["virtual_cpus"], 3)
172 self
.assertEqual(result
[c1
.id], result
[c3
.id])
174 # single cluster - every VM
175 result
= owner
.used_resources(cluster
=c1
, only_running
=False)
176 self
.assertEqual(result
["disk"], 12)
177 self
.assertEqual(result
["ram"], 2)
178 self
.assertEqual(result
["virtual_cpus"], 6)
180 # single cluster - only running VMs
181 result
= owner
.used_resources(cluster
=c1
, only_running
=True)
182 self
.assertEqual(result
["disk"], 12)
183 self
.assertEqual(result
["ram"], 1)
184 self
.assertEqual(result
["virtual_cpus"], 3)