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.8 2006/10/03 19:33:16 mlhuang Exp $
11 from pprint import pprint
12 from string import letters, digits, punctuation
17 from random import Random
20 def randfloat(min = 0.0, max = 1.0):
21 return float(min) + (random.random() * (float(max) - float(min)))
23 def randint(min = 0, max = 1):
24 return int(randfloat(min, max + 1))
26 # See "2.2 Characters" in the XML specification:
28 # #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
30 # [#x7F-#x84], [#x86-#x9F], [#xFDD0-#xFDDF]
32 low_xml_chars = map(unichr, [0x9, 0xA, 0xD])
33 low_xml_chars += map(unichr, xrange(0x20, 0x7F - 1))
34 low_xml_chars += map(unichr, xrange(0x84 + 1, 0x86 - 1))
35 low_xml_chars += map(unichr, xrange(0x9F + 1, 0xFF))
36 valid_xml_chars = list(low_xml_chars)
37 valid_xml_chars += map(unichr, xrange(0xFF + 1, 0xD7FF))
38 valid_xml_chars += map(unichr, xrange(0xE000, 0xFDD0 - 1))
39 valid_xml_chars += map(unichr, xrange(0xFDDF + 1, 0xFFFD))
41 def randstr(length, pool = valid_xml_chars, encoding = "utf-8"):
42 sample = random.sample(pool, min(length, len(pool)))
45 bytes = len(s.encode(encoding))
49 sample += random.sample(pool, min(length - bytes, len(pool)))
50 random.shuffle(sample)
56 # 1. Each part begins and ends with a letter or number.
57 # 2. Each part except the last can contain letters, numbers, or hyphens.
58 # 3. Each part is between 1 and 64 characters, including the trailing dot.
59 # 4. At least two parts.
60 # 5. Last part can only contain between 2 and 6 letters.
61 hostname = 'a' + randstr(61, letters + digits + '-') + '1.' + \
62 'b' + randstr(61, letters + digits + '-') + '2.' + \
63 'c' + randstr(5, letters)
66 def unicmp(a, b, encoding = "utf-8"):
68 When connected directly to the DB, values are returned as raw
69 8-bit strings that may need to be decoded (as UTF-8 by default) in
70 order to compare them against expected Python Unicode strings.
73 is8bit = re.compile("[\x80-\xff]").search
74 if isinstance(a, str) and is8bit(a):
75 a = unicode(a, encoding)
76 if isinstance(b, str) and is8bit(b):
77 b = unicode(b, encoding)
80 admin = {'AuthMethod': "capability",
81 'Username': config.PLC_API_MAINTENANCE_USER,
82 'AuthString': config.PLC_API_MAINTENANCE_PASSWORD,
85 user = {'AuthMethod': "password",
88 pi = {'AuthMethod': "password",
91 tech = {'AuthMethod': "password",
98 abbreviated_name = randstr(50)
99 login_base = randstr(20, letters).lower()
100 latitude = int(randfloat(-90.0, 90.0) * 1000) / 1000.0
101 longitude = int(randfloat(-180.0, 180.0) * 1000) / 1000.0
104 print "AdmAddSite(%s)" % login_base,
105 site_id = AdmAddSite(admin, name, abbreviated_name, login_base,
106 {'latitude': latitude, 'longitude': longitude})
108 # Should return a unique site_id
109 assert site_id not in site_ids
110 site_ids.append(site_id)
114 print "AdmGetSites(%d)" % site_id,
115 site = AdmGetSites(admin, [site_id])[0]
116 for key in 'name', 'abbreviated_name', 'login_base', 'latitude', 'longitude', 'site_id':
117 assert unicmp(site[key], locals()[key])
122 abbreviated_name = randstr(50)
123 latitude = int(randfloat(-90.0, 90.0) * 1000) / 1000.0
124 longitude = int(randfloat(-180.0, 180.0) * 1000) / 1000.0
126 print "AdmUpdateSite(%s)" % login_base,
127 AdmUpdateSite(admin, site_id, {'name': name, 'abbreviated_name': abbreviated_name,
128 'latitude': latitude, 'longitude': longitude,
129 'max_slices': max_slices})
130 site = AdmGetSites(admin, [site_id])[0]
131 for key in 'name', 'abbreviated_name', 'latitude', 'longitude', 'max_slices':
132 assert unicmp(site[key], locals()[key])
136 sites = AdmGetSites(admin, site_ids)
137 assert set(site_ids) == set([site['site_id'] for site in sites])
140 print "AdmGetAllRoles",
141 role_ids = AdmGetAllRoles(admin)
142 roles = dict(zip(role_ids.values(), map(int, role_ids.keys())))
147 for auth in user, pi, tech:
148 first_name = randstr(128)
149 last_name = randstr(128)
150 # 119 + 1 + 64 + 64 + 6 = 254
151 email = (randstr(119, letters + digits) + "@" + randhostname()).lower()
153 # Accounts are disabled by default
156 auth['Username'] = email
157 auth['AuthString'] = randstr(254)
159 print "AdmAddPerson(%s)" % email,
160 person_id = AdmAddPerson(admin, first_name, last_name,
161 {'email': email, 'bio': bio,
162 'password': auth['AuthString']})
164 # Should return a unique person_id
165 assert person_id not in person_ids
166 person_ids.append(person_id)
167 print "=>", person_id
170 print "AdmGetPersons(%d)" % person_id,
171 person = AdmGetPersons(admin, [person_id])[0]
172 for key in 'first_name', 'last_name', 'email', 'bio', 'person_id', 'enabled':
173 assert unicmp(person[key], locals()[key])
177 first_name = randstr(128)
178 last_name = randstr(128)
180 print "AdmUpdatePerson(%d)" % person_id,
181 AdmUpdatePerson(admin, person_id, {'first_name': first_name,
182 'last_name': last_name,
184 person = AdmGetPersons(admin, [person_id])[0]
185 for key in 'first_name', 'last_name', 'email', 'bio':
186 assert unicmp(person[key], locals()[key])
190 print "AdmSetPersonEnabled(%d, True)" % person_id,
191 AdmSetPersonEnabled(admin, person_id, True)
192 person = AdmGetPersons(admin, [person_id])[0]
193 assert person['enabled']
197 role_id = roles[auth['Role']]
198 print "AdmGrantRoleToPerson(%d, %d)" % (person_id, role_id),
199 AdmGrantRoleToPerson(admin, person_id, role_id)
202 print "AdmGetPersonRoles(%d)" % person_id,
203 person_roles = AdmGetPersonRoles(admin, person_id)
204 person_role_ids = map(int, person_roles.keys())
205 assert [role_id] == person_role_ids
206 person = AdmGetPersons(admin, [person_id])[0]
207 assert [role_id] == person['role_ids']
208 print "=>", person_role_ids
210 # Associate account with each site
211 for site_id in site_ids:
212 print "AdmAddPersonToSite(%d, %d)" % (person_id, site_id),
213 AdmAddPersonToSite(admin, person_id, site_id)
216 print "AdmGetSitePersons(%d)" % site_id,
217 site_person_ids = AdmGetSitePersons(admin, site_id)
218 assert person_id in site_person_ids
219 print "=>", site_person_ids
221 # Make sure it really did it
222 print "AdmGetPersonSites(%d)" % person_id,
223 person_site_ids = AdmGetPersonSites(auth, person_id)
224 assert set(site_ids) == set(person_site_ids)
225 person = AdmGetPersons(admin, [person_id])[0]
226 assert set(site_ids) == set(person['site_ids'])
227 print "=>", person_site_ids
229 # First site should be the primary site
230 print "AdmSetPersonPrimarySite(%d, %d)" % (person_id, person_site_ids[1]),
231 AdmSetPersonPrimarySite(auth, person_id, person_site_ids[1])
232 assert AdmGetPersonSites(auth, person_id)[0] == person_site_ids[1]
233 person = AdmGetPersons(admin, [person_id])[0]
234 assert person['site_ids'][0] == person_site_ids[1]
237 # Check authentication
238 print "AdmAuthCheck(%s)" % auth['Username'],
239 assert AdmAuthCheck(auth)
242 print "AdmGetPersons",
243 persons = AdmGetPersons(admin, person_ids)
244 assert set(person_ids) == set([person['person_id'] for person in persons])
245 print "=>", person_ids
248 for person in persons:
249 if 'pi' in person['roles']:
250 assert AdmIsPersonInRole(admin, pi['Username'], roles['pi'])
256 description = randstr(200)
257 print "AdmAddNodeGroup",
258 nodegroup_id = AdmAddNodeGroup(admin, name, description)
260 # Should return a unique nodegroup_id
261 assert nodegroup_id not in nodegroup_ids
262 nodegroup_ids.append(nodegroup_id)
263 print "=>", nodegroup_id
266 print "AdmGetNodeGroups(%d)" % nodegroup_id,
267 nodegroup = AdmGetNodeGroups(admin, [nodegroup_id])[0]
268 for key in 'name', 'description', 'nodegroup_id':
269 assert unicmp(nodegroup[key], locals()[key])
274 description = randstr(200)
275 print "AdmUpdateNodeGroup",
276 AdmUpdateNodeGroup(admin, nodegroup_id, name, description)
277 nodegroup = AdmGetNodeGroups(admin, [nodegroup_id])[0]
278 for key in 'name', 'description', 'nodegroup_id':
279 assert unicmp(nodegroup[key], locals()[key])
282 print "AdmGetNodeGroups",
283 nodegroups = AdmGetNodeGroups(admin, nodegroup_ids)
284 assert set(nodegroup_ids) == set([nodegroup['nodegroup_id'] for nodegroup in nodegroups])
285 print "=>", nodegroup_ids
289 for site_id in site_ids:
291 hostname = randhostname()
296 print "AdmAddNode(%s)" % hostname,
297 node_id = AdmAddNode(admin, site_id, hostname, boot_state,
300 # Should return a unique node_id
301 assert node_id not in node_ids
302 node_ids.append(node_id)
306 print "AdmGetNodes(%d)" % node_id,
307 node = AdmGetNodes(admin, [node_id])[0]
308 for key in 'hostname', 'boot_state', 'model', 'node_id':
309 assert unicmp(node[key], locals()[key])
313 hostname = randhostname()
315 print "AdmUpdateNode(%s)" % hostname,
316 AdmUpdateNode(admin, node_id, {'hostname': hostname, 'model': model})
317 node = AdmGetNodes(admin, [node_id])[0]
318 for key in 'hostname', 'boot_state', 'model', 'node_id':
319 assert unicmp(node[key], locals()[key])
323 for nodegroup_id in nodegroup_ids:
324 print "AdmAddNodeToNodeGroup(%d, %d)" % (nodegroup_id, node_id),
325 AdmAddNodeToNodeGroup(admin, nodegroup_id, node_id)
329 nodes = AdmGetNodes(admin, node_ids)
330 assert set(node_ids) == set([node['node_id'] for node in nodes])
333 print "AdmGetSiteNodes" % site_ids,
334 site_node_ids = AdmGetSiteNodes(admin, site_ids)
335 assert set(node_ids) == set(reduce(lambda a, b: a + b, site_node_ids.values()))
336 print "=>", site_node_ids
338 for nodegroup_id in nodegroup_ids:
339 print "AdmGetNodeGroupNodes(%d)" % nodegroup_id,
340 nodegroup_node_ids = AdmGetNodeGroupNodes(admin, nodegroup_id)
341 assert set(nodegroup_node_ids) == set(node_ids)
342 print "=>", nodegroup_node_ids
344 print "AdmGetAllNodeNetworkBandwidthLimits",
345 bwlimits = AdmGetAllNodeNetworkBandwidthLimits(admin)
350 for node_id in node_ids:
351 ip = randint(0, 0xffffffff)
352 netmask = (0xffffffff << randint(2, 31)) & 0xffffffff
353 network = ip & netmask
354 broadcast = ((ip & netmask) | ~netmask) & 0xffffffff
355 gateway = randint(network + 1, broadcast - 1)
356 dns1 = randint(0, 0xffffffff)
358 for method in 'static', 'dhcp':
360 if method == 'static':
361 for key in 'ip', 'netmask', 'network', 'broadcast', 'gateway', 'dns1':
362 optional[key] = socket.inet_ntoa(struct.pack('>L', locals()[key]))
364 print "AdmAddNodeNetwork(%s)" % method,
365 nodenetwork_id = AdmAddNodeNetwork(admin, node_id, method, 'ipv4', optional)
367 # Should return a unique nodenetwork_id
368 assert nodenetwork_id not in nodenetwork_ids
369 nodenetwork_ids.append(nodenetwork_id)
370 print "=>", nodenetwork_id
372 # Check node networks
373 print "AdmGetAllNodeNetworks(%d)" % node_id,
374 nodenetworks = AdmGetAllNodeNetworks(admin, node_id)
375 for nodenetwork in nodenetworks:
376 if nodenetwork['method'] == 'static':
377 for key in 'ip', 'netmask', 'network', 'broadcast', 'gateway', 'dns1':
378 address = struct.unpack('>L', socket.inet_aton(nodenetwork[key]))[0]
379 assert address == locals()[key]
380 print "=>", [nodenetwork['nodenetwork_id'] for nodenetwork in nodenetworks]
382 # Update node networks
383 for node_id in node_ids:
384 ip = randint(0, 0xffffffff)
385 netmask = (0xffffffff << randint(2, 31)) & 0xffffffff
386 network = ip & netmask
387 broadcast = ((ip & netmask) | ~netmask) & 0xffffffff
388 gateway = randint(network + 1, broadcast - 1)
389 dns1 = randint(0, 0xffffffff)
391 nodenetworks = AdmGetAllNodeNetworks(admin, node_id)
392 for nodenetwork in nodenetworks:
393 # Update node network
395 if nodenetwork['method'] == 'static':
396 for key in 'ip', 'netmask', 'network', 'broadcast', 'gateway', 'dns1':
397 optional[key] = socket.inet_ntoa(struct.pack('>L', locals()[key]))
399 print "AdmUpdateNodeNetwork(%s)" % nodenetwork['method'],
400 AdmUpdateNodeNetwork(admin, nodenetwork['nodenetwork_id'], optional)
403 # Check node network again
404 print "AdmGetAllNodeNetworks(%d)" % node_id,
405 nodenetworks = AdmGetAllNodeNetworks(admin, node_id)
406 for nodenetwork in nodenetworks:
407 if nodenetwork['method'] == 'static':
408 for key in 'ip', 'netmask', 'network', 'broadcast', 'gateway', 'dns1':
409 address = struct.unpack('>L', socket.inet_aton(nodenetwork[key]))[0]
410 assert address == locals()[key]
411 print "=>", [nodenetwork['nodenetwork_id'] for nodenetwork in nodenetworks]
413 # Add attribute types
417 description = randstr(254)
418 min_role_id = random.sample(roles.values(), 1)[0]
421 print "AddAttribute",
422 attribute_id = AddAttribute(admin, name,
423 {'description': description,
424 'min_role_id': min_role_id})
426 # Should return a unique attribute_id
427 assert attribute_id not in attribute_ids
428 attribute_ids.append(attribute_id)
429 print "=>", attribute_id
432 print "GetAttributes(%d)" % attribute_id,
433 attribute = GetAttributes(admin, [attribute_id])[0]
434 for key in 'min_role_id', 'description':
435 assert unicmp(attribute[key], locals()[key])
439 description = randstr(254)
440 min_role_id = random.sample(roles.values(), 1)[0]
441 print "UpdateAttribute(%d)" % attribute_id,
442 UpdateAttribute(admin, attribute_id,
443 {'description': description,
444 'min_role_id': min_role_id})
445 attribute = GetAttributes(admin, [attribute_id])[0]
446 for key in 'min_role_id', 'description':
447 assert unicmp(attribute[key], locals()[key])
450 # Add slices and slice attributes
452 slice_attribute_ids = []
453 sites = AdmGetSites(admin, site_ids)
456 name = site['login_base'] + "_" + randstr(11, letters).lower()
457 url = "http://" + randhostname() + "/"
458 description = randstr(2048)
461 print "AddSlice(%s)" % name,
462 slice_id = AddSlice(admin, name, {'url': url, 'description': description})
464 # Should return a unique slice_id
465 assert slice_id not in slice_ids
466 slice_ids.append(slice_id)
470 print "GetSlices(%d)" % slice_id,
471 slice = GetSlices(admin, [slice_id])[0]
472 for key in 'name', 'url', 'description', 'slice_id':
473 assert unicmp(slice[key], locals()[key])
477 url = "http://" + randhostname() + "/"
478 description = randstr(2048)
479 print "UpdateSlice(%s)" % name,
480 UpdateSlice(admin, slice_id, {'url': url, 'description': description})
481 slice = GetSlices(admin, [slice_id])[0]
482 for key in 'name', 'url', 'description', 'slice_id':
483 assert unicmp(slice[key], locals()[key])
486 # XXX Add nodes to slice
488 # XXX Add people to slice
490 # Set slice/sliver attributes
491 for attribute_id in attribute_ids:
493 # Make it a sliver attribute with 50% probability
494 # node_id = random.sample(node_ids + [None] * len(node_ids), 1)[0]
497 # Add slice attribute
498 print "AddSliceAttribute(%s, %d)" % (name, attribute_id),
500 slice_attribute_id = AddSliceAttribute(admin, slice_id, attribute_id, value)
502 slice_attribute_id = AddSliceAttribute(admin, slice_id, attribute_id, value, node_id)
504 # Should return a unique slice_attribute_id
505 assert slice_attribute_id not in slice_attribute_ids
506 slice_attribute_ids.append(slice_attribute_id)
507 print "=>", slice_attribute_id
509 # Check slice attribute
510 print "GetSliceAttributes(%d)" % slice_attribute_id,
511 slice_attribute = GetSliceAttributes(admin, slice_id, [slice_attribute_id])[0]
512 for key in 'attribute_id', 'slice_id', 'node_id', 'slice_attribute_id', 'value':
513 assert unicmp(slice_attribute[key], locals()[key])
516 # Update slice attribute
517 url = "http://" + randhostname() + "/"
518 description = randstr(2048)
519 print "UpdateSliceAttribute(%s)" % name,
520 UpdateSliceAttribute(admin, slice_attribute_id, value)
521 slice_attribute = GetSliceAttributes(admin, slice_id, [slice_attribute_id])[0]
522 for key in 'attribute_id', 'slice_id', 'node_id', 'slice_attribute_id', 'value':
523 assert unicmp(slice_attribute[key], locals()[key])
527 for slice_id in slice_ids:
528 # Delete slice attributes
529 slice = GetSlices(admin, [slice_id])[0]
530 for slice_attribute_id in slice['slice_attribute_ids']:
531 print "DeleteSliceAttribute(%s, %d)" % (slice['name'], slice_attribute_id),
532 DeleteSliceAttribute(admin, slice_attribute_id)
534 slice = GetSlices(admin, [slice_id])[0]
535 assert not slice['slice_attribute_ids']
538 print "DeleteSlice(%d)" % slice_id,
539 DeleteSlice(admin, slice_id)
540 assert not GetSlices(admin, [slice_id])
542 # Make sure it really deleted it
543 slices = GetSlices(admin, slice_ids)
544 assert slice_id not in [slice['slice_id'] for slice in slices]
548 assert not GetSlices(admin, slice_ids)
552 for attribute_id in attribute_ids:
554 print "DeleteAttribute(%d)" % attribute_id,
555 DeleteAttribute(admin, attribute_id)
556 assert not GetAttributes(admin, [attribute_id])
558 # Make sure it really deleted it
559 attributes = GetAttributes(admin, attribute_ids)
560 assert attribute_id not in [attribute['attribute_id'] for attribute in attributes]
563 print "GetAttributes",
564 assert not GetAttributes(admin, attribute_ids)
567 # Delete node networks
568 for node_id in node_ids:
569 nodenetworks = AdmGetAllNodeNetworks(admin, node_id)
570 for nodenetwork in nodenetworks:
571 # Delete node network
572 print "AdmDeleteNodeNetwork(%d, %d)" % (node_id, nodenetwork['nodenetwork_id']),
573 AdmDeleteNodeNetwork(admin, node_id, nodenetwork['nodenetwork_id'])
575 assert not AdmGetAllNodeNetworks(admin, node_id)
578 for node_id in node_ids:
579 # Remove from node groups
580 for nodegroup_id in nodegroup_ids:
581 print "AdmRemoveNodeFromNodeGroup(%d, %d)" % (nodegroup_id, node_id),
582 AdmRemoveNodeFromNodeGroup(admin, nodegroup_id, node_id)
583 # Make sure it really deleted it
584 assert node_id not in AdmGetNodeGroupNodes(nodegroup_id)
588 print "AdmDeleteNode(%d)" % node_id,
589 AdmDeleteNode(admin, node_id)
590 assert not AdmGetNodes(admin, [node_id])
592 # Make sure it really deleted it
593 nodes = AdmGetNodes(admin, node_ids)
594 assert node_id not in [node['node_id'] for node in nodes]
598 assert not AdmGetNodes(admin, node_ids)
601 for nodegroup_id in nodegroup_ids:
602 print "AdmGetNodeGroupNodes(%d)" % nodegroup_id,
603 assert not AdmGetNodeGroupNodes(nodegroup_id)
607 for person_id in person_ids:
608 # Remove from each site
609 for site_id in site_ids:
610 print "AdmRemovePersonFromSite(%d, %d)" % (person_id, site_id),
611 AdmRemovePersonFromSite(admin, person_id, site_id)
612 assert site_id not in AdmGetPersonSites(admin, person_id)
613 person = AdmGetPersons(admin, [person_id])[0]
614 assert site_id not in person['site_ids']
617 assert not AdmGetPersonSites(admin, person_id)
620 person_roles = AdmGetPersonRoles(admin, person_id)
621 role_id = int(person_roles.keys()[0])
622 print "AdmRevokeRoleFromPerson(%d, %d)" % (person_id, role_id),
623 AdmRevokeRoleFromPerson(admin, person_id, role_id)
624 assert not AdmGetPersonRoles(admin, person_id)
625 person = AdmGetPersons(admin, [person_id])[0]
626 assert not person['role_ids']
630 print "AdmSetPersonEnabled(%d, False)" % person_id,
631 AdmSetPersonEnabled(admin, person_id, False)
632 person = AdmGetPersons(admin, [person_id])[0]
633 assert not person['enabled']
637 print "AdmDeletePerson(%d)" % person_id,
638 AdmDeletePerson(admin, person_id)
639 assert not AdmGetPersons(admin, [person_id])
642 print "AdmGetPersons",
643 assert not AdmGetPersons(admin, person_ids)
647 for nodegroup_id in nodegroup_ids:
648 print "AdmDeleteNodeGroup(%d)" % nodegroup_id,
649 AdmDeleteNodeGroup(admin, nodegroup_id)
650 assert not AdmGetNodeGroups(admin, [nodegroup_id])
653 print "AdmGetNodeGroups",
654 assert not AdmGetNodeGroups(admin, nodegroup_ids)
658 for site_id in site_ids:
659 print "AdmDeleteSite(%d)" % site_id,
660 AdmDeleteSite(admin, site_id)
661 assert not AdmGetSites(admin, [site_id])
665 assert not AdmGetSites(admin, site_ids)