1 #!/usr/bin/env ./Shell.py
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
8 # $Id: Test.py,v 1.5 2006/09/14 15:47:27 tmack Exp $
11 from pprint import pprint
12 from string import letters, digits, punctuation
14 from random import Random
19 def randfloat(min = 0.0, max = 1.0):
20 return float(min) + (random.random() * (float(max) - float(min)))
22 def randint(min = 0, max = 1):
23 return int(randfloat(min, max + 1))
25 # See "2.2 Characters" in the XML specification:
27 # #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
29 # [#x7F-#x84], [#x86-#x9F], [#xFDD0-#xFDDF]
31 low_xml_chars = map(unichr, [0x9, 0xA, 0xD])
32 low_xml_chars += map(unichr, xrange(0x20, 0x7F - 1))
33 low_xml_chars += map(unichr, xrange(0x84 + 1, 0x86 - 1))
34 low_xml_chars += map(unichr, xrange(0x9F + 1, 0xFF))
35 valid_xml_chars = list(low_xml_chars)
36 valid_xml_chars += map(unichr, xrange(0xFF + 1, 0xD7FF))
37 valid_xml_chars += map(unichr, xrange(0xE000, 0xFDD0 - 1))
38 valid_xml_chars += map(unichr, xrange(0xFDDF + 1, 0xFFFD))
40 def randstr(length, pool = valid_xml_chars, encoding = "utf-8"):
41 sample = random.sample(pool, min(length, len(pool)))
44 bytes = len(s.encode(encoding))
48 sample += random.sample(pool, min(length - bytes, len(pool)))
49 random.shuffle(sample)
55 # 1. Each part begins and ends with a letter or number.
56 # 2. Each part except the last can contain letters, numbers, or hyphens.
57 # 3. Each part is between 1 and 64 characters, including the trailing dot.
58 # 4. At least two parts.
59 # 5. Last part can only contain between 2 and 6 letters.
60 hostname = 'a' + randstr(61, letters + digits + '-') + '1.' + \
61 'b' + randstr(61, letters + digits + '-') + '2.' + \
62 'c' + randstr(5, letters)
65 def unicmp(a, b, encoding = "utf-8"):
67 When connected directly to the DB, values are returned as raw
68 8-bit strings that may need to be decoded (as UTF-8 by default) in
69 order to compare them against expected Python Unicode strings.
72 is8bit = re.compile("[\x80-\xff]").search
73 if isinstance(a, str) and is8bit(a):
74 a = unicode(a, encoding)
75 if isinstance(b, str) and is8bit(b):
76 b = unicode(b, encoding)
79 admin = {'AuthMethod': "capability",
80 'Username': config.PLC_API_MAINTENANCE_USER,
81 'AuthString': config.PLC_API_MAINTENANCE_PASSWORD,
84 user = {'AuthMethod': "password",
87 pi = {'AuthMethod': "password",
90 tech = {'AuthMethod': "password",
97 abbreviated_name = randstr(50)
98 login_base = randstr(20, letters).lower()
99 latitude = int(randfloat(-90.0, 90.0) * 1000) / 1000.0
100 longitude = int(randfloat(-180.0, 180.0) * 1000) / 1000.0
103 print "AdmAddSite(%s)" % login_base,
104 site_id = AdmAddSite(admin, name, abbreviated_name, login_base,
105 {'latitude': latitude, 'longitude': longitude})
107 # Should return a unique site_id
108 assert site_id not in site_ids
109 site_ids.append(site_id)
112 print "AdmGetSites(%d)" % site_id,
113 site = AdmGetSites(admin, [site_id])[0]
114 for key in 'name', 'abbreviated_name', 'login_base', 'latitude', 'longitude', 'site_id':
115 assert unicmp(site[key], locals()[key])
119 sites = AdmGetSites(admin, site_ids)
120 assert set(site_ids) == set([site['site_id'] for site in sites])
123 print "AdmGetAllRoles",
124 role_ids = AdmGetAllRoles(admin)
125 roles = dict(zip(role_ids.values(), map(int, role_ids.keys())))
130 for auth in user, pi, tech:
131 first_name = randstr(128)
132 last_name = randstr(128)
133 # 119 + 1 + 64 + 64 + 6 = 254
134 email = randstr(119, letters + digits) + "@" + randhostname()
136 # Accounts are disabled by default
139 auth['Username'] = email
140 auth['AuthString'] = randstr(254)
142 print "AdmAddPerson(%s)" % email,
143 person_id = AdmAddPerson(admin, first_name, last_name,
144 {'email': email, 'bio': bio,
145 'password': auth['AuthString']})
147 # Should return a unique person_id
148 assert person_id not in person_ids
149 person_ids.append(person_id)
150 print "=>", person_id
153 print "AdmGetPersons(%d)" % person_id,
154 person = AdmGetPersons(admin, [person_id])[0]
155 for key in 'first_name', 'last_name', 'bio', 'person_id', 'enabled':
156 assert unicmp(person[key], locals()[key])
160 print "AdmSetPersonEnabled(%d, True)" % person_id,
161 AdmSetPersonEnabled(admin, person_id, True)
162 person = AdmGetPersons(admin, [person_id])[0]
163 assert person['enabled']
167 role_id = roles[auth['Role']]
168 print "AdmGrantRoleToPerson(%d, %d)" % (person_id, role_id),
169 AdmGrantRoleToPerson(admin, person_id, role_id)
172 print "AdmGetPersonRoles(%d)" % person_id,
173 person_roles = AdmGetPersonRoles(admin, person_id)
174 person_role_ids = map(int, person_roles.keys())
175 assert [role_id] == person_role_ids
176 person = AdmGetPersons(admin, [person_id])[0]
177 assert [role_id] == person['role_ids']
178 print "=>", person_role_ids
180 # Associate account with each site
181 for site_id in site_ids:
182 print "AdmAddPersonToSite(%d, %d)" % (person_id, site_id),
183 AdmAddPersonToSite(admin, person_id, site_id)
186 # Make sure it really did it
187 print "AdmGetPersonSites(%d)" % person_id,
188 person_site_ids = AdmGetPersonSites(auth, person_id)
189 assert set(site_ids) == set(person_site_ids)
190 person = AdmGetPersons(admin, [person_id])[0]
191 assert set(site_ids) == set(person['site_ids'])
192 print "=>", person_site_ids
194 # First site should be the primary site
195 print "AdmSetPersonPrimarySite(%d, %d)" % (person_id, person_site_ids[1]),
196 AdmSetPersonPrimarySite(auth, person_id, person_site_ids[1])
197 assert AdmGetPersonSites(auth, person_id)[0] == person_site_ids[1]
198 person = AdmGetPersons(admin, [person_id])[0]
199 assert person['site_ids'][0] == person_site_ids[1]
202 print "AdmGetPersons",
203 persons = AdmGetPersons(admin, person_ids)
204 assert set(person_ids) == set([person['person_id'] for person in persons])
205 print "=>", person_ids
211 description = randstr(200)
212 print "AdmAddNodeGroup(%s)" % name,
213 nodegroup_id = AdmAddNodeGroup(admin, name, description)
215 # Should return a unique nodegroup_id
216 assert nodegroup_id not in nodegroup_ids
217 nodegroup_ids.append(nodegroup_id)
218 print "=>", nodegroup_id
221 print "AdmGetNodeGroups(%d)" % nodegroup_id,
222 nodegroup = AdmGetNodeGroups(admin, [nodegroup_id])[0]
223 for key in 'name', 'description', 'nodegroup_id':
224 assert unicmp(nodegroup[key], locals()[key])
229 description = randstr(200)
230 print "AdmUpdateNodeGroup(%s)" % name,
231 AdmUpdateNodeGroup(admin, nodegroup_id, name, description)
232 nodegroup = AdmGetNodeGroups(admin, [nodegroup_id])[0]
233 for key in 'name', 'description', 'nodegroup_id':
234 assert unicmp(nodegroup[key], locals()[key])
237 print "AdmGetNodeGroups",
238 nodegroups = AdmGetNodeGroups(admin, nodegroup_ids)
239 assert set(nodegroup_ids) == set([nodegroup['nodegroup_id'] for nodegroup in nodegroups])
240 print "=>", nodegroup_ids
244 for site_id in site_ids:
246 hostname = randhostname()
251 print "AdmAddNode(%s)" % hostname,
252 node_id = AdmAddNode(admin, site_id, hostname, boot_state,
255 # Should return a unique node_id
256 assert node_id not in node_ids
257 node_ids.append(node_id)
261 print "AdmGetNodes(%d)" % node_id,
262 node = AdmGetNodes(admin, [node_id])[0]
263 for key in 'hostname', 'boot_state', 'model', 'node_id':
264 assert unicmp(node[key], locals()[key])
268 hostname = randhostname()
270 print "AdmUpdateNode(%s)" % hostname,
271 AdmUpdateNode(admin, node_id, {'hostname': hostname, 'model': model})
272 node = AdmGetNodes(admin, [node_id])[0]
273 for key in 'hostname', 'boot_state', 'model', 'node_id':
274 assert unicmp(node[key], locals()[key])
278 for nodegroup_id in nodegroup_ids:
279 print "AdmAddNodeToNodeGroup(%d, %d)" % (nodegroup_id, node_id),
280 AdmAddNodeToNodeGroup(admin, nodegroup_id, node_id)
284 nodes = AdmGetNodes(admin, node_ids)
285 assert set(node_ids) == set([node['node_id'] for node in nodes])
288 print "AdmGetSiteNodes" % site_ids,
289 site_node_ids = AdmGetSiteNodes(admin, site_ids)
290 assert set(node_ids) == set(reduce(lambda a, b: a + b, site_node_ids.values()))
291 print "=>", site_node_ids
293 for nodegroup_id in nodegroup_ids:
294 print "AdmGetNodeGroupNodes(%d)" % nodegroup_id,
295 nodegroup_node_ids = AdmGetNodeGroupNodes(admin, nodegroup_id)
296 assert set(nodegroup_node_ids) == set(node_ids)
297 print "=>", nodegroup_node_ids
300 for node_id in node_ids:
301 # Remove from node groups
302 for nodegroup_id in nodegroup_ids:
303 print "AdmRemoveNodeFromNodeGroup(%d, %d)" % (nodegroup_id, node_id),
304 AdmRemoveNodeFromNodeGroup(admin, nodegroup_id, node_id)
305 # Make sure it really deleted it
306 assert node_id not in AdmGetNodeGroupNodes(nodegroup_id)
310 print "AdmDeleteNode(%d)" % node_id,
311 AdmDeleteNode(admin, node_id)
312 assert not AdmGetNodes(admin, [node_id])
314 # Make sure it really deleted it
315 nodes = AdmGetNodes(admin, node_ids)
316 assert node_id not in [node['node_id'] for node in nodes]
320 assert not AdmGetNodes(admin, node_ids)
323 for nodegroup_id in nodegroup_ids:
324 print "AdmGetNodeGroupNodes(%d)" % nodegroup_id,
325 assert not AdmGetNodeGroupNodes(nodegroup_id)
329 for person_id in person_ids:
330 # Remove from each site
331 for site_id in site_ids:
332 print "AdmRemovePersonFromSite(%d, %d)" % (person_id, site_id),
333 AdmRemovePersonFromSite(admin, person_id, site_id)
334 assert site_id not in AdmGetPersonSites(admin, person_id)
335 person = AdmGetPersons(admin, [person_id])[0]
336 assert site_id not in person['site_ids']
339 assert not AdmGetPersonSites(admin, person_id)
342 person_roles = AdmGetPersonRoles(admin, person_id)
343 role_id = int(person_roles.keys()[0])
344 print "AdmRevokeRoleFromPerson(%d, %d)" % (person_id, role_id),
345 AdmRevokeRoleFromPerson(admin, person_id, role_id)
346 assert not AdmGetPersonRoles(admin, person_id)
347 person = AdmGetPersons(admin, [person_id])[0]
348 assert not person['role_ids']
352 print "AdmSetPersonEnabled(%d, False)" % person_id,
353 AdmSetPersonEnabled(admin, person_id, False)
354 person = AdmGetPersons(admin, [person_id])[0]
355 assert not person['enabled']
359 print "AdmDeletePerson(%d)" % person_id,
360 AdmDeletePerson(admin, person_id)
361 assert not AdmGetPersons(admin, [person_id])
364 print "AdmGetPersons",
365 assert not AdmGetPersons(admin, person_ids)
369 for nodegroup_id in nodegroup_ids:
370 print "AdmDeleteNodeGroup(%d)" % nodegroup_id,
371 AdmDeleteNodeGroup(admin, nodegroup_id)
372 assert not AdmGetNodeGroups(admin, [nodegroup_id])
375 print "AdmGetNodeGroups",
376 assert not AdmGetNodeGroups(admin, nodegroup_ids)
380 for site_id in site_ids:
381 print "AdmDeleteSite(%d)" % site_id,
382 AdmDeleteSite(admin, site_id)
383 assert not AdmGetSites(admin, [site_id])
387 assert not AdmGetSites(admin, site_ids)