Finish removing "osuosl" names from tests.
[ganeti_webmgr.git] / ganeti_web / tests / forms / cluster.py
blob7fc4eb87ff6547370238c214255f8a6c79fa4cdc
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,
16 # USA.
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
26 Node = models.Node
27 Quota = models.Quota
30 __all__ = ['TestClusterFormNew', 'TestClusterFormEdit']
33 class TestClusterFormBase(TestCase):
35 def setUp(self):
36 models.client.GanetiRapiClient = RapiProxy
38 self.cluster = Cluster(hostname='test.example.test', slug='OSL_TEST')
39 self.cluster.save()
41 self.data = dict(hostname='new-host3.hostname',
42 slug='new-host3',
43 port=5080,
44 description='testing editing clusters',
45 username='tester',
46 password = 'secret',
47 virtual_cpus=1,
48 disk=2,
49 ram=3
52 def tearDown(self):
53 # Tear down the cluster.
54 self.cluster.delete()
57 class TestClusterFormNew(TestClusterFormBase):
59 def test_unbound(self):
60 form = EditClusterForm()
62 def test_bound(self):
63 form = EditClusterForm(self.data)
64 self.assertTrue(form.is_valid())
66 def test_required_fields(self):
67 """
68 Tests adding a new cluster
69 """
70 data = self.data
71 required = ['hostname', 'port']
72 for property in required:
73 data_ = data.copy()
74 del data_[property]
75 form = EditClusterForm(data_)
76 self.assertFalse(form.is_valid())
78 def test_optional_fields(self):
79 data = self.data
80 non_required = ['slug','description','virtual_cpus','disk','ram']
81 for property in non_required:
82 data_ = data.copy()
83 del data_[property]
84 form = EditClusterForm(data_)
85 self.assertTrue(form.is_valid())
86 cluster = form.save()
87 for k, v in data_.items():
88 self.assertEqual(v, getattr(cluster, k))
89 cluster.delete()
91 def test_read_only(self):
92 """ success without username or password """
93 data_ = self.data
94 del data_['username']
95 del data_['password']
96 form = EditClusterForm(data_)
97 self.assertTrue(form.is_valid())
98 cluster = form.save()
99 for k, v in data_.items():
100 self.assertEqual(v, getattr(cluster, k))
101 cluster.delete()
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()
108 del data_[property]
109 form = EditClusterForm(data_)
110 self.assertFalse(form.is_valid())
112 def test_unique_fields(self):
113 # test unique fields
114 form = EditClusterForm(self.data)
115 form.is_valid()
116 form.save()
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
136 data = self.data
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
147 data = self.data
148 del data['username']
149 del data['password']
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'
163 self.cluster.save()
165 data = self.data
166 del data['password']
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'
179 self.cluster.save()
181 data = self.data
182 del data['password']
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'
193 self.cluster.save()
195 data = self.data
196 del data['username']
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
203 password set
205 self.cluster.username = 'foo'
206 self.cluster.password = 'bar'
207 self.cluster.save()
209 data = self.data
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
224 self.cluster.save()
226 data = self.data
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)