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_web
.util
.proxy
import RapiProxy
23 from ganeti_web
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
.assertTrue(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
.assertTrue(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
.assertTrue(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
.assertTrue(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
):
123 owner
= user
.get_profile()
124 c1
.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
,
134 cluster
=c1
, status
="running")
135 vm21
= VirtualMachine(hostname
="2one", owner
=owner
,
136 cluster
=c2
, status
="running")
137 vm31
= VirtualMachine(hostname
="3one", owner
=owner
,
138 cluster
=c2
, status
="running")
140 vm12
= VirtualMachine(hostname
="1two", owner
=owner
,
141 cluster
=c1
, status
="running",
142 ram
=1, virtual_cpus
=3, disk_size
=6)
143 vm22
= VirtualMachine(hostname
="2two", owner
=owner
,
144 cluster
=c2
, status
="running",
145 ram
=1, virtual_cpus
=3, disk_size
=6)
146 vm32
= VirtualMachine(hostname
="3two", owner
=owner
,
147 cluster
=c3
, status
="running",
148 ram
=1, virtual_cpus
=3, disk_size
=6)
150 vm13
= VirtualMachine(hostname
="1three", owner
=owner
,
151 cluster
=c1
, status
="stopped",
152 ram
=1, virtual_cpus
=3, disk_size
=6)
153 vm23
= VirtualMachine(hostname
="2three", owner
=owner
,
154 cluster
=c2
, status
="stopped",
155 ram
=1, virtual_cpus
=3, disk_size
=6)
156 vm33
= VirtualMachine(hostname
="3three", owner
=owner
,
157 cluster
=c3
, status
="stopped",
158 ram
=1, virtual_cpus
=3, disk_size
=6)
160 for i
in (vm11
, vm12
, vm13
, vm21
, vm22
, vm23
, vm31
, vm32
, vm33
):
163 # multiple clusters - every VM
164 result
= owner
.used_resources(cluster
=None, only_running
=False)
165 self
.assertTrue(c1
.id in result
.keys())
166 self
.assertTrue(c2
.id in result
.keys())
167 self
.assertTrue(c3
.id in result
.keys())
168 self
.assertEqual(result
[c1
.id]["disk"], 12)
169 self
.assertEqual(result
[c1
.id]["ram"], 2)
170 self
.assertEqual(result
[c1
.id]["virtual_cpus"], 6)
171 self
.assertEqual(result
[c1
.id], result
[c3
.id])
173 # multiple clusters - only running VMs
174 result
= owner
.used_resources(cluster
=None, only_running
=True)
175 self
.assertTrue(c1
.id in result
.keys())
176 self
.assertTrue(c2
.id in result
.keys())
177 self
.assertTrue(c3
.id in result
.keys())
178 self
.assertEqual(result
[c1
.id]["disk"], 12)
179 self
.assertEqual(result
[c1
.id]["ram"], 1)
180 self
.assertEqual(result
[c1
.id]["virtual_cpus"], 3)
181 self
.assertEqual(result
[c1
.id], result
[c3
.id])
183 # single cluster - every VM
184 result
= owner
.used_resources(cluster
=c1
, only_running
=False)
185 self
.assertEqual(result
["disk"], 12)
186 self
.assertEqual(result
["ram"], 2)
187 self
.assertEqual(result
["virtual_cpus"], 6)
189 # single cluster - only running VMs
190 result
= owner
.used_resources(cluster
=c1
, only_running
=True)
191 self
.assertEqual(result
["disk"], 12)
192 self
.assertEqual(result
["ram"], 1)
193 self
.assertEqual(result
["virtual_cpus"], 3)