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
.test
import TestCase
20 from ganeti_web
.forms
.cluster
import EditClusterForm
22 from ganeti_web
.util
.proxy
import RapiProxy
23 from ganeti_web
import models
24 Cluster
= models
.Cluster
25 VirtualMachine
= models
.VirtualMachine
30 __all__
= ['TestClusterFormNew', 'TestClusterFormEdit']
33 class TestClusterFormBase(TestCase
):
36 models
.client
.GanetiRapiClient
= RapiProxy
38 self
.cluster
= Cluster(hostname
='test.example.test', slug
='OSL_TEST')
41 self
.data
= dict(hostname
='new-host3.hostname',
44 description
='testing editing clusters',
53 # Tear down the cluster.
57 class TestClusterFormNew(TestClusterFormBase
):
59 def test_unbound(self
):
60 form
= EditClusterForm()
63 form
= EditClusterForm(self
.data
)
64 self
.assertTrue(form
.is_valid())
66 def test_required_fields(self
):
68 Tests adding a new cluster
71 required
= ['hostname', 'port']
72 for property in required
:
75 form
= EditClusterForm(data_
)
76 self
.assertFalse(form
.is_valid())
78 def test_optional_fields(self
):
80 non_required
= ['slug','description','virtual_cpus','disk','ram']
81 for property in non_required
:
84 form
= EditClusterForm(data_
)
85 self
.assertTrue(form
.is_valid())
87 for k
, v
in data_
.items():
88 self
.assertEqual(v
, getattr(cluster
, k
))
91 def test_read_only(self
):
92 """ success without username or password """
96 form
= EditClusterForm(data_
)
97 self
.assertTrue(form
.is_valid())
99 for k
, v
in data_
.items():
100 self
.assertEqual(v
, getattr(cluster
, k
))
103 def test_password_required(self
):
104 """ if either username or password are entered both are required """
105 relation
= ['username', 'password']
106 for property in relation
:
107 data_
= self
.data
.copy()
109 form
= EditClusterForm(data_
)
110 self
.assertFalse(form
.is_valid())
112 def test_unique_fields(self
):
114 form
= EditClusterForm(self
.data
)
117 for property in ['hostname','slug']:
118 data_
= self
.data
.copy()
119 data_
[property] = 'different'
120 form
= EditClusterForm(data_
)
121 self
.assertFalse(form
.is_valid())
124 class TestClusterFormEdit(TestClusterFormBase
):
127 def test_bound_form(self
):
128 """ tests binding form with existing instance """
129 form
= EditClusterForm(self
.data
)
130 self
.assertTrue(form
.is_valid())
132 def test_valid_edit(self
):
134 successfully edit a cluster
137 form
= EditClusterForm(data
, instance
=self
.cluster
)
138 self
.assertTrue(form
.is_valid())
139 cluster
= form
.save()
140 for k
, v
in data
.items():
141 self
.assertEqual(v
, getattr(cluster
, k
))
143 def test_no_username_or_password_for_read_only(self
):
145 tests that username and password are optional for read only cluster
150 form
= EditClusterForm(data
, instance
=self
.cluster
)
151 self
.assertTrue(form
.is_valid())
152 cluster
= form
.save()
153 self
.assertEqual('', cluster
.username
)
154 self
.assertEqual('', cluster
.password
)
156 def test_no_password_for_writeable(self
):
158 Tests that password is not required when the cluster already has the
159 password available. assumes username is the same
161 self
.cluster
.username
= 'tester'
162 self
.cluster
.password
= 'secret'
167 form
= EditClusterForm(data
, instance
=self
.cluster
)
168 self
.assertTrue(form
.is_valid())
169 cluster
= form
.save()
170 self
.assertEqual('tester', cluster
.username
)
171 self
.assertEqual('secret', cluster
.password
)
173 def test_no_password_for_writeable_new_username(self
):
175 tests that the password is required when the username has changed
177 self
.cluster
.username
= 'foo'
178 self
.cluster
.password
= 'bar'
183 data
['username'] = 'different'
184 form
= EditClusterForm(data
, instance
=self
.cluster
)
185 self
.assertFalse(form
.is_valid())
187 def test_username_required_for_writeable_new_password(self
):
189 if password is entered for a cluster, username is required always
191 self
.cluster
.username
= 'foo'
192 self
.cluster
.password
= 'bar'
197 form
= EditClusterForm(data
, instance
=self
.cluster
)
198 self
.assertFalse(form
.is_valid())
200 def test_username_and_password_change(self
):
202 tests changing the password for a cluster that already had username and
205 self
.cluster
.username
= 'foo'
206 self
.cluster
.password
= 'bar'
210 form
= EditClusterForm(data
, instance
=self
.cluster
)
211 self
.assertTrue(form
.is_valid())
212 cluster
= form
.save()
214 self
.assertEqual('tester', cluster
.username
)
215 self
.assertEqual('secret', cluster
.password
)
217 def test_username_and_password_added(self
):
219 tests setting a username and password for a cluster that did not
220 previously have a username and password
222 self
.cluster
.username
= None
223 self
.cluster
.password
= None
227 data
['username'] = 'foo'
228 data
['password'] = 'bar'
229 form
= EditClusterForm(data
, instance
=self
.cluster
)
230 self
.assertTrue(form
.is_valid())
232 cluster
= form
.save()
233 self
.assertEqual('foo', cluster
.username
)
234 self
.assertEqual('bar', cluster
.password
)