3 # Test script for peer caching
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
12 Test script for peer caching. Intended for testing multiple PLCs
13 running on the same machine in different chroots. Here is how I set
14 things up after installing and configuring MyPLC:
21 ln -sf plc /etc/init.d/plc2
22 echo 'PLC_ROOT=/plc2/root' > /etc/sysconfig/plc2
23 echo 'PLC_DATA=/plc2/data' >> /etc/sysconfig/plc2
25 # Edit /plc2/data/etc/planetlab/plc_config.xml and change at least the
26 # following so that they do not conflict with the defaults:
28 # PLC_NAME (e.g., PlanetLab Two)
29 # PLC_SLICE_PREFIX (e.g., two)
30 # PLC_ROOT_USER (e.g., root@planetlab.two)
31 # PLC_API_MAINTENANCE_USER (e.g., maint@planetlab.two)
32 # PLC_DB_PORT (e.g., 5433)
33 # PLC_WWW_PORT (e.g., 81)
34 # PLC_WWW_SSL_PORT (e.g., 444)
35 # PLC_API_PORT (must be the same as PLC_WWW_SSL_PORT, e.g., 444)
36 # PLC_BOOT_SSL_PORT (must be the same as PLC_WWW_SSL_PORT, e.g., 444)
37 # PLC_BOOT_PORT (may be the same as PLC_WWW_PORT, e.g., 81)
39 # Start up both MyPLC instances
44 ./Test.py -f /etc/planetlab/plc_config -f /plc2/data/etc/planetlab/plc_config
46 # If the test fails and your databases are corrupt and/or you want to
47 # start over, you can always just blow the databases away.
49 rm -rf /plc/data/var/lib/pgsql/data
53 rm -rf /plc2/data/var/lib/pgsql/data
58 from optparse import OptionParser
60 from PLC.Config import Config
61 from PLC.GPG import gpg_export
62 from PLC.Shell import Shell
63 from PLC.Test import Test
65 def todict(list_of_dicts, key):
67 Turn a list of dicts into a dict keyed on key.
70 return dict([(d[key], d) for d in list_of_dicts])
72 def RefreshPeers(plcs):
74 Refresh each peer with each other.
82 print plc.config.PLC_NAME, "refreshing", peer.config.PLC_NAME
83 plc.RefreshPeer(peer.config.PLC_NAME)
85 peer_id = plc.GetPeers([peer.config.PLC_NAME])[0]['peer_id']
87 peer_sites = todict(plc.GetSites({'peer_id': peer_id}), 'site_id')
88 sites_at_peer = todict(peer.GetSites(), 'site_id')
90 peer_keys = todict(plc.GetKeys({'peer_id': peer_id}), 'key_id')
91 keys_at_peer = todict(peer.GetKeys(), 'key_id')
93 peer_persons = todict(plc.GetPersons({'peer_id': peer_id}), 'person_id')
94 persons_at_peer = todict(peer.GetPersons(), 'person_id')
96 peer_nodes = todict(plc.GetNodes({'peer_id': peer_id}), 'node_id')
97 nodes_at_peer = todict(peer.GetNodes(), 'node_id')
99 our_nodes = todict(plc.GetNodes({'peer_id': None}), 'node_id')
100 our_peer_id_at_peer = peer.GetPeers([plc.config.PLC_NAME])[0]['peer_id']
101 our_nodes_at_peer = todict(peer.GetNodes({'peer_id': our_peer_id_at_peer,
102 'peer_node_id': our_nodes.keys()}), 'peer_node_id')
104 peer_slices = todict(plc.GetSlices({'peer_id': peer_id}), 'peer_slice_id')
105 slices_at_peer = todict(peer.GetSlices(), 'slice_id')
107 for site_id, site in peer_sites.iteritems():
108 # Verify that this site exists at the peer
109 peer_site_id = site['peer_site_id']
110 assert peer_site_id in sites_at_peer
111 peer_site = sites_at_peer[peer_site_id]
114 for field in ['name', 'abbreviated_name', 'login_base', 'is_public',
115 'latitude', 'longitude', 'url',
116 'max_slices', 'max_slivers',]:
117 assert site[field] == peer_site[field]
119 for key_id, key in peer_keys.iteritems():
120 # Verify that this key exists at the peer
121 peer_key_id = key['peer_key_id']
122 assert peer_key_id in keys_at_peer
123 peer_key = keys_at_peer[peer_key_id]
126 for field in ['key_type', 'key']:
127 assert key[field] == peer_key[field]
129 for person_id, person in peer_persons.iteritems():
130 # Verify that this user exists at the peer
131 peer_person_id = person['peer_person_id']
132 assert peer_person_id in persons_at_peer
133 peer_person = persons_at_peer[peer_person_id]
136 for field in ['first_name', 'last_name', 'title', 'email', 'phone',
137 'url', 'bio', 'enabled']:
138 assert person[field] == peer_person[field]
140 for key_id in person['key_ids']:
141 # Verify that the user is not associated with any local keys
142 assert key_id in peer_keys
143 key = peer_keys[key_id]
144 peer_key_id = key['peer_key_id']
146 # Verify that this key exists at the peer
147 assert peer_key_id in keys_at_peer
148 peer_key = keys_at_peer[peer_key_id]
150 # And is related to the same user at the peer
151 assert peer_key['key_id'] in peer_person['key_ids']
153 for node_id, node in peer_nodes.iteritems():
154 # Verify that this node exists at the peer
155 peer_node_id = node['peer_node_id']
156 assert peer_node_id in nodes_at_peer
157 peer_node = nodes_at_peer[peer_node_id]
160 for field in ['boot_state', 'ssh_rsa_key', 'hostname',
162 assert node[field] == peer_node[field]
164 # Verify that the node is not associated with any local sites
165 assert node['site_id'] in peer_sites
166 site = peer_sites[node['site_id']]
168 # Verify that this site exists at the peer
169 peer_site_id = site['peer_site_id']
170 assert peer_site_id in sites_at_peer
171 peer_site = sites_at_peer[peer_site_id]
173 # And is related to the same node at the peer
174 assert peer_site['site_id'] == peer_node['site_id']
176 for slice_id, slice in peer_slices.iteritems():
177 # Verify that this slice exists at the peer
178 peer_slice_id = slice['peer_slice_id']
179 assert peer_slice_id in slices_at_peer
180 peer_slice = slices_at_peer[peer_slice_id]
183 for field in ['name', 'instantiation', 'url', 'description',
184 'max_nodes', 'expires']:
185 assert slice[field] == peer_slice[field]
187 for node_id in slice['node_ids']:
188 # Verify that the slice is associated only with
189 # the peer's own nodes, or with our nodes as
190 # last cached by the peer.
191 assert node_id in peer_nodes or node_id in our_nodes_at_peer
192 if node_id in peer_nodes:
193 node = peer_nodes[node_id]
194 peer_node_id = node['peer_node_id']
195 elif node_id in our_nodes_at_peer:
196 peer_node = our_nodes_at_peer[node_id]
197 peer_node_id = peer_node['node_id']
199 # Verify that this node exists at the peer
200 assert peer_node_id in nodes_at_peer
202 # And is related to the same slice at the peer
203 assert peer_node_id in peer_slice['node_ids']
205 def TestPeers(plcs, check = True, verbose = True, tiny = False):
206 # Register each peer with each other
212 key = gpg_export(peer.chroot + peer.config.PLC_ROOT_GPG_KEY_PUB)
213 cacert = file(peer.chroot + peer.config.PLC_API_CA_SSL_CRT).read()
215 if plc.GetPeers([peer.config.PLC_NAME]):
216 print plc.config.PLC_NAME, "updating peer", peer.config.PLC_NAME
217 plc.UpdatePeer(peer.config.PLC_NAME,
218 {'peer_url': peer.url, 'key': key, 'cacert': cacert})
220 print plc.config.PLC_NAME, "adding peer", peer.config.PLC_NAME
221 plc.AddPeer({'peername': peer.config.PLC_NAME,
222 'peer_url': peer.url, 'key': key, 'cacert': cacert})
225 plc.test = Test(api = plc, check = check, verbose = verbose)
230 params = Test.default
232 print "Populating", plc.config.PLC_NAME
233 plc.test.Add(**params)
240 print "Updating", plc.config.PLC_NAME
243 # Refresh each other again
247 parser = OptionParser()
248 parser.add_option("-f", "--config", dest = "configs", action = "append", default = [], help = "Configuration file (default: %default)")
249 parser.add_option("-c", "--check", action = "store_true", default = False, help = "Verify actions (default: %default)")
250 parser.add_option("-q", "--quiet", action = "store_true", default = False, help = "Be quiet (default: %default)")
251 parser.add_option("-t", "--tiny", action = "store_true", default = False, help = "Run a tiny test (default: %default)")
252 (options, args) = parser.parse_args()
254 # Test single peer by default
255 if not options.configs:
256 options.configs = ["/etc/planetlab/plc_config"]
259 for path in options.configs:
260 # Load configuration file
261 config = Config(path)
263 # Determine path to chroot
264 m = re.match(r'(.*)/etc/planetlab', path)
270 # Fix up path to SSL certificate
271 cacert = chroot + config.PLC_API_CA_SSL_CRT
273 # Always connect with XML-RPC
274 plc = Shell(config = path, cacert = cacert, xmlrpc = True)
278 TestPeers(plcs, check = options.check, verbose = not options.quiet, tiny = options.tiny)
280 if __name__ == "__main__":