Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / Test.py
1 #!/usr/bin/python
2 #
3 # Test script for peer caching
4 #
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
7 #
8 # $Id: Test.py,v 1.23 2007/09/14 15:34:01 mef Exp $
9 #
10
11 """
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:
15
16 # Shut down MyPLC
17 service plc stop
18
19 # Copy to /plc2
20 cp -ra /plc /plc2
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
24
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:
27 #
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)
38
39 # Start up both MyPLC instances
40 service plc start
41 service plc2 start
42
43 # Run test
44 ./Test.py -f /etc/planetlab/plc_config -f /plc2/data/etc/planetlab/plc_config
45
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.
48 service plc stop
49 rm -rf /plc/data/var/lib/pgsql/data
50 service plc start
51
52 service plc2 stop
53 rm -rf /plc2/data/var/lib/pgsql/data
54 service plc2 start
55 """
56
57 import re
58 from optparse import OptionParser
59
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
64
65 def todict(list_of_dicts, key):
66     """
67     Turn a list of dicts into a dict keyed on key.
68     """
69
70     return dict([(d[key], d) for d in list_of_dicts])
71
72 def RefreshPeers(plcs):
73     """
74     Refresh each peer with each other.
75     """
76
77     for plc in plcs:
78         for peer in plcs:
79             if peer == plc:
80                 continue
81
82             print plc.config.PLC_NAME, "refreshing", peer.config.PLC_NAME
83             plc.RefreshPeer(peer.config.PLC_NAME)
84
85             peer_id = plc.GetPeers([peer.config.PLC_NAME])[0]['peer_id']
86
87             peer_sites = todict(plc.GetSites({'peer_id': peer_id}), 'site_id')
88             sites_at_peer = todict(peer.GetSites(), 'site_id')
89
90             peer_keys = todict(plc.GetKeys({'peer_id': peer_id}), 'key_id')
91             keys_at_peer = todict(peer.GetKeys(), 'key_id')
92
93             peer_persons = todict(plc.GetPersons({'peer_id': peer_id}), 'person_id')
94             persons_at_peer = todict(peer.GetPersons(), 'person_id')
95
96             peer_nodes = todict(plc.GetNodes({'peer_id': peer_id}), 'node_id')
97             nodes_at_peer = todict(peer.GetNodes(), 'node_id')
98
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')
103
104             peer_slices = todict(plc.GetSlices({'peer_id': peer_id}), 'peer_slice_id')
105             slices_at_peer = todict(peer.GetSlices(), 'slice_id')
106  
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]
112
113                 # And is the same
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]
118
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]
124
125                 # And is the same
126                 for field in ['key_type', 'key']:
127                     assert key[field] == peer_key[field]
128
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]
134
135                 # And is the same
136                 for field in ['first_name', 'last_name', 'title', 'email', 'phone',
137                               'url', 'bio', 'enabled']:
138                     assert person[field] == peer_person[field]
139
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']
145
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]
149
150                     # And is related to the same user at the peer
151                     assert peer_key['key_id'] in peer_person['key_ids']
152
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]
158
159                 # And is the same
160                 for field in ['boot_state', 'ssh_rsa_key', 'hostname',
161                               'version', 'model']:
162                     assert node[field] == peer_node[field]
163
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']]
167
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]
172
173                 # And is related to the same node at the peer
174                 assert peer_site['site_id'] == peer_node['site_id']
175
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]
181
182                 # And is the same
183                 for field in ['name', 'instantiation', 'url', 'description',
184                               'max_nodes', 'expires']:
185                     assert slice[field] == peer_slice[field]
186
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']
198
199                     # Verify that this node exists at the peer
200                     assert peer_node_id in nodes_at_peer
201
202                     # And is related to the same slice at the peer
203                     assert peer_node_id in peer_slice['node_ids']
204
205 def TestPeers(plcs, check = True, verbose = True, tiny = False):
206     # Register each peer with each other
207     for plc in plcs:
208         for peer in plcs:
209             if peer == plc:
210                 continue
211
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()
214
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})
219             else:
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})
223
224         # Populate the DB
225         plc.test = Test(api = plc, check = check, verbose = verbose)
226
227         if tiny:
228             params = Test.tiny
229         else:
230             params = Test.default
231
232         print "Populating", plc.config.PLC_NAME
233         plc.test.Add(**params)
234
235     # Refresh each other
236     RefreshPeers(plcs)
237
238     # Change some things
239     for plc in plcs:
240         print "Updating", plc.config.PLC_NAME
241         plc.test.Update()
242
243     # Refresh each other again
244     RefreshPeers(plcs)
245
246 def main():
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()
253
254     # Test single peer by default
255     if not options.configs:
256         options.configs = ["/etc/planetlab/plc_config"]
257
258     plcs = []
259     for path in options.configs:
260         # Load configuration file
261         config = Config(path)
262
263         # Determine path to chroot
264         m = re.match(r'(.*)/etc/planetlab', path)
265         if m is not None:
266             chroot = m.group(1)
267         else:
268             chroot = ""
269
270         # Fix up path to SSL certificate
271         cacert = chroot + config.PLC_API_CA_SSL_CRT
272
273         # Always connect with XML-RPC
274         plc = Shell(config = path, cacert = cacert, xmlrpc = True)
275         plc.chroot = chroot
276         plcs.append(plc)
277
278     TestPeers(plcs, check = options.check, verbose = not options.quiet, tiny = options.tiny)
279
280 if __name__ == "__main__":
281     main()