-
- is8bit = re.compile("[\x80-\xff]").search
- if isinstance(a, str) and is8bit(a):
- a = unicode(a, encoding)
- if isinstance(b, str) and is8bit(b):
- b = unicode(b, encoding)
- return a == b
-
-admin = {'AuthMethod': "capability",
- 'Username': config.PLC_API_MAINTENANCE_USER,
- 'AuthString': config.PLC_API_MAINTENANCE_PASSWORD,
- 'Role': "admin"}
-
-user = {'AuthMethod': "password",
- 'Role': "user"}
-
-pi = {'AuthMethod': "password",
- 'Role': "pi"}
-
-tech = {'AuthMethod': "password",
- 'Role': "tech"}
-
-# Add sites
-site_ids = []
-for i in range(3):
- def random_site():
- return {
- 'name': randstr(254),
- 'abbreviated_name': randstr(50),
- 'login_base': randstr(20, letters).lower(),
- 'latitude': int(randfloat(-90.0, 90.0) * 1000) / 1000.0,
- 'longitude': int(randfloat(-180.0, 180.0) * 1000) / 1000.0,
- }
-
- # Add site
- site_fields = random_site()
- print "AddSite",
- site_id = AddSite(admin, site_fields)
-
- # Should return a unique site_id
- assert site_id not in site_ids
- site_ids.append(site_id)
- print "=>", site_id
-
- # Check site
- print "GetSites(%d)" % site_id,
- site = GetSites(admin, [site_id])[0]
- for key in site_fields:
- assert unicmp(site[key], site_fields[key])
- print "=> OK"
-
- # Update site
- site_fields = random_site()
- # Currently cannot change login_base
- del site_fields['login_base']
- site_fields['max_slices'] = randint(1, 10)
- print "UpdateSite(%d)" % site_id,
- UpdateSite(admin, site_id, site_fields)
- print "=> OK"
-
- # Check site again
- site = GetSites(admin, [site_id])[0]
- for key in site_fields:
- if key != 'login_base':
- assert unicmp(site[key], site_fields[key])
-
-print "GetSites",
-sites = GetSites(admin, site_ids)
-assert set(site_ids) == set([site['site_id'] for site in sites])
-print "=>", site_ids
-
-# Add address types
-address_type_ids = []
-for i in range(3):
- def random_address_type():
- return {
- 'name': randstr(20),
- 'description': randstr(254),
- }
-
- print "AddAddressType",
- address_type_fields = random_address_type()
- address_type_id = AddAddressType(admin, address_type_fields)
-
- # Should return a unique address_type_id
- assert address_type_id not in address_type_ids
- address_type_ids.append(address_type_id)
- print "=>", address_type_id
-
- # Check address type
- print "GetAddressTypes(%d)" % address_type_id,
- address_type = GetAddressTypes(admin, [address_type_id])[0]
- for key in 'name', 'description':
- assert unicmp(address_type[key], address_type_fields[key])
- print "=> OK"
-
- # Update address type
- address_type_fields = random_address_type()
- print "UpdateAddressType(%d)" % address_type_id,
- UpdateAddressType(admin, address_type_id, address_type_fields)
- print "=> OK"
-
- # Check address type again
- address_type = GetAddressTypes(admin, [address_type_id])[0]
- for key in 'name', 'description':
- assert unicmp(address_type[key], address_type_fields[key])
-
-print "GetAddressTypes",
-address_types = GetAddressTypes(admin, address_type_ids)
-assert set(address_type_ids) == set([address_type['address_type_id'] for address_type in address_types])
-print "=>", address_type_ids
-
-# Add site addresses
-address_ids = []
-for site_id in site_ids:
- for i in range(3):
- def random_address():
- return {
- 'line1': randstr(254),
- 'line2': randstr(254),
- 'line3': randstr(254),
- 'city': randstr(254),
- 'state': randstr(254),
- 'postalcode': randstr(64),
- 'country': randstr(128),
- }
-
- print "AddSiteAddress",
- address_fields = random_address()
- address_id = AddSiteAddress(admin, site_id, address_fields)
-
- # Should return a unique address_id
- assert address_id not in address_ids
- address_ids.append(address_id)
- print "=>", address_id
-
- # Check address
- print "GetAddresses(%d)" % address_id,
- address = GetAddresses(admin, [address_id])[0]
- for key in address_fields:
- assert unicmp(address[key], address_fields[key])
- print "=> OK"
-
- # Update address
- address_fields = random_address()
- print "UpdateAddress(%d)" % address_id,
- UpdateAddress(admin, address_id, address_fields)
- print "=> OK"
-
- # Check address again
- address = GetAddresses(admin, [address_id])[0]
- for key in address_fields:
- assert unicmp(address[key], address_fields[key])
-
- # Add address types
- for address_type_id in address_type_ids:
- print "AddAddressTypeToAddress(%d, %d)" % (address_type_id, address_id),
- AddAddressTypeToAddress(admin, address_type_id, address_id)
- print "=> OK"
-
-print "GetAddresses",
-addresses = GetAddresses(admin, address_ids)
-assert set(address_ids) == set([address['address_id'] for address in addresses])
-for address in addresses:
- assert set(address_type_ids) == set(address['address_type_ids'])
-print "=>", address_ids
-
-print "GetRoles",
-roles = GetRoles(admin)
-role_ids = [role['role_id'] for role in roles]
-roles = [role['name'] for role in roles]
-roles = dict(zip(roles, role_ids))
-print "=>", role_ids
-
-# Add users
-person_ids = []
-for auth in user, pi, tech:
- def random_person():
- global auth
-
- person_fields = {
- 'first_name': randstr(128),
- 'last_name': randstr(128),
- # 119 + 1 + 64 + 64 + 6 = 254
- 'email': (randstr(119, letters + digits) + "@" + randhostname()).lower(),
- 'bio': randstr(254),
- # Accounts are disabled by default
- 'enabled': False,
- 'password': randstr(254),
- }
-
- auth['Username'] = person_fields['email']
- auth['AuthString'] = person_fields['password']
-
- return person_fields
-
- # Add account
- person_fields = random_person()
- print "AddPerson",
- person_id = AddPerson(admin, person_fields)
-
- # Should return a unique person_id
- assert person_id not in person_ids
- person_ids.append(person_id)
- print "=>", person_id
-
- # Check account
- print "GetPersons(%d)" % person_id,
- person = GetPersons(admin, [person_id])[0]
- for key in person_fields:
- if key != 'password':
- assert unicmp(person[key], person_fields[key])
- print "=> OK"
-
- # Update account
- person_fields = random_person()
- print "UpdatePerson(%d)" % person_id,
- UpdatePerson(admin, person_id, person_fields)
- print "=> OK"
-
- # Check account again
- person = GetPersons(admin, [person_id])[0]
- for key in person_fields:
- if key != 'password':
- assert unicmp(person[key], person_fields[key])
-
- # Check that account is really disabled
- try:
- assert not AuthCheck(auth)
- except:
- pass
-
- # Add role
- role_id = roles[auth['Role']]
- print "AddRoleToPerson(%d, %d)" % (role_id, person_id),
- AddRoleToPerson(admin, role_id, person_id)
- person = GetPersons(admin, [person_id])[0]
- assert [role_id] == person['role_ids']
- print "=> OK"
-
- # Enable account
- UpdatePerson(admin, person_id, {'enabled': True})
-
- # Check authentication
- print "AuthCheck(%s)" % auth['Username'],
- assert AuthCheck(auth)
- print "=> OK"
-
- # Associate account with each site
- for site_id in site_ids:
- print "AddPersonToSite(%d, %d)" % (person_id, site_id),
- AddPersonToSite(admin, person_id, site_id)
- print "=> OK"
-
- # Make sure it really did it
- person = GetPersons(admin, [person_id])[0]
- person_site_ids = person['site_ids']
- assert set(site_ids) == set(person['site_ids'])
-
- # First site should be the primary site
- print "SetPersonPrimarySite(%d, %d)" % (person_id, person_site_ids[1]),
- SetPersonPrimarySite(auth, person_id, person_site_ids[1])
- person = GetPersons(admin, [person_id])[0]
- assert person['site_ids'][0] == person_site_ids[1]
- print "=> OK"
-
-print "GetPersons",
-persons = GetPersons(admin, person_ids)
-assert set(person_ids) == set([person['person_id'] for person in persons])
-print "=>", person_ids
-
-# Add node groups
-nodegroup_ids = []
-for i in range(3):
- def random_nodegroup():
- return {
- 'name': randstr(50),
- 'description': randstr(200),
- }
-
- # Add node group
- print "AddNodeGroup",
- nodegroup_fields = random_nodegroup()
- nodegroup_id = AddNodeGroup(admin, nodegroup_fields)
-
- # Should return a unique nodegroup_id
- assert nodegroup_id not in nodegroup_ids
- nodegroup_ids.append(nodegroup_id)
- print "=>", nodegroup_id
-
- # Check node group
- print "GetNodeGroups(%d)" % nodegroup_id,
- nodegroup = GetNodeGroups(admin, [nodegroup_id])[0]
- for key in nodegroup_fields:
- assert unicmp(nodegroup[key], nodegroup_fields[key])
- print "=> OK"
-
- # Update node group, with a readable name
- nodegroup_fields = random_nodegroup()
- nodegroup_fields['name'] = randstr(16, letters + ' ' + digits)
- print "UpdateNodeGroup",
- UpdateNodeGroup(admin, nodegroup_id, nodegroup_fields)
- print "=> OK"
-
- # Check node group again
- nodegroup = GetNodeGroups(admin, [nodegroup_id])[0]
- for key in nodegroup_fields:
- assert unicmp(nodegroup[key], nodegroup_fields[key])
-
-print "GetNodeGroups",
-nodegroups = GetNodeGroups(admin, nodegroup_ids)
-assert set(nodegroup_ids) == set([nodegroup['nodegroup_id'] for nodegroup in nodegroups])
-print "=>", nodegroup_ids
-
-print "GetBootStates",
-boot_states = GetBootStates(admin)
-print "=>", boot_states
-
-# Add nodes
-node_ids = []
-for site_id in site_ids:
- for i in range(3):
- def random_node():
- return {
- 'hostname': randhostname(),
- 'boot_state': random.sample(boot_states, 1)[0],
- 'model': randstr(255),
- 'version': randstr(64),
- }
-
- # Add node
- node_fields = random_node()
- print "AddNode",
- node_id = AddNode(admin, site_id, node_fields)
-
- # Should return a unique node_id
- assert node_id not in node_ids
- node_ids.append(node_id)
- print "=>", node_id
-
- # Check node
- print "GetNodes(%d)" % node_id,
- node = GetNodes(admin, [node_id])[0]
- for key in node_fields:
- assert unicmp(node[key], node_fields[key])
- print "=> OK"
-
- # Update node
- node_fields = random_node()
- print "UpdateNode(%d)" % node_id,
- UpdateNode(admin, node_id, node_fields)
- print "=> OK"
-
- # Check node again
- node = GetNodes(admin, [node_id])[0]
- for key in node_fields:
- assert unicmp(node[key], node_fields[key])
-
- # Add to node groups
- for nodegroup_id in nodegroup_ids:
- print "AddNodeToNodeGroup(%d, %d)" % (nodegroup_id, node_id),
- AddNodeToNodeGroup(admin, nodegroup_id, node_id)
- print "=> OK"
-
-print "GetNodes",
-nodes = GetNodes(admin, node_ids)
-assert set(node_ids) == set([node['node_id'] for node in nodes])
-print "=>", node_ids
-
-print "GetNodeGroups",
-nodegroups = GetNodeGroups(admin, nodegroup_ids)
-for nodegroup in nodegroups:
- assert set(nodegroup['node_ids']) == set(node_ids)
-print "=> OK"
-
-print "GetNetworkMethods",
-network_methods = GetNetworkMethods(admin)
-print "=>", network_methods
-
-print "GetNetworkTypes",
-network_types = GetNetworkTypes(admin)
-print "=>", network_types
-
-# Add node networks
-nodenetwork_ids = []
-for node_id in node_ids:
- def random_nodenetwork(method, type):
- nodenetwork_fields = {
- 'method': method,
- 'type': type,
- 'bwlimit': randint(500000, 10000000),
- }
-
- if method != 'dhcp':
- ip = randint(0, 0xffffffff)
- netmask = (0xffffffff << randint(2, 31)) & 0xffffffff
- network = ip & netmask
- broadcast = ((ip & netmask) | ~netmask) & 0xffffffff
- gateway = randint(network + 1, broadcast - 1)
- dns1 = randint(0, 0xffffffff)
-
- for field in 'ip', 'netmask', 'network', 'broadcast', 'gateway', 'dns1':
- nodenetwork_fields[field] = socket.inet_ntoa(struct.pack('>L', locals()[field]))
-
- return nodenetwork_fields
-
- for method in network_methods:
- for type in network_types:
- # Add node network
- print "AddNodeNetwork",
- nodenetwork_fields = random_nodenetwork(method, type)
- nodenetwork_id = AddNodeNetwork(admin, node_id, nodenetwork_fields)
-
- # Should return a unique nodenetwork_id
- assert nodenetwork_id not in nodenetwork_ids
- nodenetwork_ids.append(nodenetwork_id)
- print "=>", nodenetwork_id
-
- # Check node network
- print "GetNodeNetworks(%d)" % nodenetwork_id,
- nodenetwork = GetNodeNetworks(admin, [nodenetwork_id])[0]
- for key in nodenetwork_fields:
- assert unicmp(nodenetwork[key], nodenetwork_fields[key])
- print "=> OK"
-
- # Update node network
- nodenetwork_fields = random_nodenetwork(method, type)
- print "UpdateNodeNetwork(%d)" % nodenetwork_id,
- UpdateNodeNetwork(admin, nodenetwork_id, nodenetwork_fields)
- print "=> OK"
-
- # Check node network again
- nodenetwork = GetNodeNetworks(admin, [nodenetwork_id])[0]
- for key in nodenetwork_fields:
- assert unicmp(nodenetwork[key], nodenetwork_fields[key])
-
-print "GetNodeNetworks",
-nodenetworks = GetNodeNetworks(admin, nodenetwork_ids)
-assert set(nodenetwork_ids) == set([nodenetwork['nodenetwork_id'] for nodenetwork in nodenetworks])
-print "=>", nodenetwork_ids
-
-# Add slice attribute types
-attribute_type_ids = []
-for i in range(3):
- def random_attribute_type():
- return {
- 'name': randstr(100),
- 'description': randstr(254),
- 'min_role_id': random.sample(roles.values(), 1)[0],
- }
-
- # Add slice attribute type
- attribute_type_fields = random_attribute_type()
- print "AddSliceAttributeType",
- attribute_type_id = AddSliceAttributeType(admin, attribute_type_fields)
-
- # Should return a unique attribute_type_id
- assert attribute_type_id not in attribute_type_ids
- attribute_type_ids.append(attribute_type_id)
- print "=>", attribute_type_id
-
- # Check slice attribute type
- print "GetSliceAttributeTypes(%d)" % attribute_type_id,
- attribute_type = GetSliceAttributeTypes(admin, [attribute_type_id])[0]
- for key in attribute_type_fields:
- assert unicmp(attribute_type[key], attribute_type_fields[key])
- print "=> OK"
-
- # Update slice attribute type
- attribute_type_fields = random_attribute_type()
- print "UpdateSliceAttributeType(%d)" % attribute_type_id,
- UpdateSliceAttributeType(admin, attribute_type_id, attribute_type_fields)
- print "=> OK"
-
- # Check slice attribute type again
- attribute_type = GetSliceAttributeTypes(admin, [attribute_type_id])[0]
- for key in attribute_type_fields:
- assert unicmp(attribute_type[key], attribute_type_fields[key])
-
-# Add slices and slice attributes
-slice_ids = []
-slice_attribute_ids = []
-for site in sites:
- for i in range(site['max_slices']):
- def random_slice():
- return {
- 'name': site['login_base'] + "_" + randstr(11, letters).lower(),
- 'url': "http://" + randhostname() + "/",
- 'description': randstr(2048),
- }
-
- # Add slice
- slice_fields = random_slice()
- print "AddSlice",
- slice_id = AddSlice(admin, slice_fields)
-
- # Should return a unique slice_id
- assert slice_id not in slice_ids
- slice_ids.append(slice_id)
- print "=>", slice_id
-
- # Check slice
- print "GetSlices(%d)" % slice_id,
- slice = GetSlices(admin, [slice_id])[0]
- for key in slice_fields:
- assert unicmp(slice[key], slice_fields[key])
- print "=> OK"
-
- # Update slice
- slice_fields = random_slice()
- # Cannot change slice name
- del slice_fields['name']
- print "UpdateSlice(%d)" % slice_id,
- UpdateSlice(admin, slice_id, slice_fields)
- slice = GetSlices(admin, [slice_id])[0]
- for key in slice_fields:
- assert unicmp(slice[key], slice_fields[key])
- print "=> OK"
-
- # Add slice to all nodes
- print "AddSliceToNodes(%d, %s)" % (slice_id, str(node_ids)),
- AddSliceToNodes(admin, slice_id, node_ids)
- slice = GetSlices(admin, [slice_id])[0]
- assert set(node_ids) == set(slice['node_ids'])
- print "=> OK"
-
- # Add users to slice
- for person_id in person_ids:
- print "AddPersonToSlice(%d, %d)" % (person_id, slice_id),
- AddPersonToSlice(admin, person_id, slice_id)
- print "=> OK"
- slice = GetSlices(admin, [slice_id])[0]
- assert set(person_ids) == set(slice['person_ids'])
-
- # Set slice/sliver attributes
- for attribute_type_id in attribute_type_ids:
- value = randstr(16, letters + '_' + digits)
- # Make it a sliver attribute with 50% probability
- node_id = random.sample(node_ids + [None] * len(node_ids), 1)[0]
-
- # Add slice attribute
- print "AddSliceAttribute(%d, %d)" % (slice_id, attribute_type_id),
- if node_id is None:
- slice_attribute_id = AddSliceAttribute(admin, slice_id, attribute_type_id, value)
+
+ for plc in plcs:
+ for peer in plcs:
+ if peer == plc:
+ continue
+
+ print plc.config.PLC_NAME, "refreshing", peer.config.PLC_NAME
+ plc.RefreshPeer(peer.config.PLC_NAME)
+
+ peer_id = plc.GetPeers([peer.config.PLC_NAME])[0]['peer_id']
+
+ peer_sites = todict(plc.GetSites({'peer_id': peer_id}), 'site_id')
+ sites_at_peer = todict(peer.GetSites(), 'site_id')
+
+ peer_keys = todict(plc.GetKeys({'peer_id': peer_id}), 'key_id')
+ keys_at_peer = todict(peer.GetKeys(), 'key_id')
+
+ peer_persons = todict(plc.GetPersons({'peer_id': peer_id}), 'person_id')
+ persons_at_peer = todict(peer.GetPersons(), 'person_id')
+
+ peer_nodes = todict(plc.GetNodes({'peer_id': peer_id}), 'node_id')
+ nodes_at_peer = todict(peer.GetNodes(), 'node_id')
+
+ our_nodes = todict(plc.GetNodes({'peer_id': None}), 'node_id')
+ our_peer_id_at_peer = peer.GetPeers([plc.config.PLC_NAME])[0]['peer_id']
+ our_nodes_at_peer = todict(peer.GetNodes({'peer_id': our_peer_id_at_peer,
+ 'peer_node_id': our_nodes.keys()}), 'peer_node_id')
+
+ peer_slices = todict(plc.GetSlices({'peer_id': peer_id}), 'peer_slice_id')
+ slices_at_peer = todict(peer.GetSlices(), 'slice_id')
+
+ for site_id, site in peer_sites.iteritems():
+ # Verify that this site exists at the peer
+ peer_site_id = site['peer_site_id']
+ assert peer_site_id in sites_at_peer
+ peer_site = sites_at_peer[peer_site_id]
+
+ # And is the same
+ for field in ['name', 'abbreviated_name', 'login_base', 'is_public',
+ 'latitude', 'longitude', 'url',
+ 'max_slices', 'max_slivers',]:
+ assert site[field] == peer_site[field]
+
+ for key_id, key in peer_keys.iteritems():
+ # Verify that this key exists at the peer
+ peer_key_id = key['peer_key_id']
+ assert peer_key_id in keys_at_peer
+ peer_key = keys_at_peer[peer_key_id]
+
+ # And is the same
+ for field in ['key_type', 'key']:
+ assert key[field] == peer_key[field]
+
+ for person_id, person in peer_persons.iteritems():
+ # Verify that this user exists at the peer
+ peer_person_id = person['peer_person_id']
+ assert peer_person_id in persons_at_peer
+ peer_person = persons_at_peer[peer_person_id]
+
+ # And is the same
+ for field in ['first_name', 'last_name', 'title', 'email', 'phone',
+ 'url', 'bio', 'enabled']:
+ assert person[field] == peer_person[field]
+
+ for key_id in person['key_ids']:
+ # Verify that the user is not associated with any local keys
+ assert key_id in peer_keys
+ key = peer_keys[key_id]
+ peer_key_id = key['peer_key_id']
+
+ # Verify that this key exists at the peer
+ assert peer_key_id in keys_at_peer
+ peer_key = keys_at_peer[peer_key_id]
+
+ # And is related to the same user at the peer
+ assert peer_key['key_id'] in peer_person['key_ids']
+
+ for node_id, node in peer_nodes.iteritems():
+ # Verify that this node exists at the peer
+ peer_node_id = node['peer_node_id']
+ assert peer_node_id in nodes_at_peer
+ peer_node = nodes_at_peer[peer_node_id]
+
+ # And is the same
+ for field in ['boot_state', 'ssh_rsa_key', 'hostname',
+ 'version', 'model']:
+ assert node[field] == peer_node[field]
+
+ # Verify that the node is not associated with any local sites
+ assert node['site_id'] in peer_sites
+ site = peer_sites[node['site_id']]
+
+ # Verify that this site exists at the peer
+ peer_site_id = site['peer_site_id']
+ assert peer_site_id in sites_at_peer
+ peer_site = sites_at_peer[peer_site_id]
+
+ # And is related to the same node at the peer
+ assert peer_site['site_id'] == peer_node['site_id']
+
+ for slice_id, slice in peer_slices.iteritems():
+ # Verify that this slice exists at the peer
+ peer_slice_id = slice['peer_slice_id']
+ assert peer_slice_id in slices_at_peer
+ peer_slice = slices_at_peer[peer_slice_id]
+
+ # And is the same
+ for field in ['name', 'instantiation', 'url', 'description',
+ 'max_nodes', 'expires']:
+ assert slice[field] == peer_slice[field]
+
+ for node_id in slice['node_ids']:
+ # Verify that the slice is associated only with
+ # the peer's own nodes, or with our nodes as
+ # last cached by the peer.
+ assert node_id in peer_nodes or node_id in our_nodes_at_peer
+ if node_id in peer_nodes:
+ node = peer_nodes[node_id]
+ peer_node_id = node['peer_node_id']
+ elif node_id in our_nodes_at_peer:
+ peer_node = our_nodes_at_peer[node_id]
+ peer_node_id = peer_node['node_id']
+
+ # Verify that this node exists at the peer
+ assert peer_node_id in nodes_at_peer
+
+ # And is related to the same slice at the peer
+ assert peer_node_id in peer_slice['node_ids']
+
+def TestPeers(plcs, check = True, verbose = True, tiny = False):
+ # Register each peer with each other
+ for plc in plcs:
+ for peer in plcs:
+ if peer == plc:
+ continue
+
+ key = gpg_export(peer.chroot + peer.config.PLC_ROOT_GPG_KEY_PUB)
+ cacert = file(peer.chroot + peer.config.PLC_API_CA_SSL_CRT).read()
+
+ if plc.GetPeers([peer.config.PLC_NAME]):
+ print plc.config.PLC_NAME, "updating peer", peer.config.PLC_NAME
+ plc.UpdatePeer(peer.config.PLC_NAME,
+ {'peer_url': peer.url, 'key': key, 'cacert': cacert})