persons get connected to slices. GetSlivers reports the right keys
[plcapi.git] / TestPeers.py
1 #!/usr/bin/env python
2 ###
3 ##############################
4 ###
5 ### preparation / requirements
6 ###
7 ### two separate instances of myplc
8 ### for now they are located on the same box on lurch
9 ###
10 ### expectations :
11 ### your myplcs should more or less come out of the box, 
12 ### I prefer not to alter the default PLC_ROOT_USER value,
13 ### instead we create a PI account on the site_id=1
14 ###
15 ##############################
16
17 # support reloading without wiping everything off
18 # dunno how to do (defvar plc)
19
20 import getopt
21 import sys
22 import time
23
24 ## we use indexes 1 and 2 
25 try:
26     dir(plc)
27 except:
28     plc=[None,None,None]
29 ## the server objects
30 try:
31     dir(s)
32 except:
33     s=[None,None,None]
34 ## the authentication objects
35 ## our user
36 try:
37     dir(a)
38 except:
39     a=[None,None,None]
40 ## the builtin root user for bootstrapping
41 try:
42     dir(aa)
43 except:
44     aa=[None,None,None]
45
46 ####################
47 import xmlrpclib
48 import os
49
50 ####################
51 plc1={ 'plcname':'plc1 in federation',
52        'hostname':'lurch.cs.princeton.edu',
53        'url-format':'https://%s:443/PLCAPI/',
54        'builtin-admin-id':'root@plc1.org',
55        'builtin-admin-password':'root',
56        'peer-admin-name':'plc1@planet-lab.org',
57        'peer-admin-password':'peer',
58        'node-format':'n1%02d.plc1.org',
59        'plainname' : 'one',
60        'slice-format' : 's1%02d',
61        'person-format' : 'user1-%d@plc1.org',
62        'key-format':'ssh-rsa 1111111111111111 user%d-key%d',
63        'person-password' : 'password1',
64        }
65 plc2={ 'plcname':'plc2 in federation',
66        'hostname':'planetlab-devbox.inria.fr',
67        'url-format':'https://%s:443/PLCAPI/',
68        'builtin-admin-id':'root@plc2.org',
69        'builtin-admin-password':'root',
70        'peer-admin-name':'plc2@planet-lab.org',
71        'peer-admin-password':'peer',
72        'node-format':'n2%02d.plc2.org',
73        'plainname' : 'two',
74        'slice-format' : 's2%02d',
75        'person-format' : 'user2-%d@plc2.org',
76        'key-format':'ssh-rsa 2222222222222222 user%d-key%d',
77        'person-password' : 'password2',
78        }
79
80 ####################
81 # set initial conditions
82 def define_test (keys,persons,nodes,slices,fast_mode):
83     global number_keys, number_persons, number_nodes, number_slices, fast_flag
84     number_keys=keys
85     number_persons=persons
86     number_nodes=nodes
87     number_slices=slices
88     fast_flag=fast_mode
89
90 def fast():
91     define_test(1,1,1,1,True)
92     
93 define_test (keys=4,persons=2,nodes=5,slices=3,fast_mode=False)
94
95 # predefined stuff
96 # number of 'system' persons
97 # builtin maint, local root, 2 persons for the peering
98 system_persons = 4
99 # among that, 1 gets refreshed - other ones have conflicting names
100 system_persons_cross = 1
101
102 system_slices_ids = (1,2)
103 def system_slices ():
104     return len(system_slices_ids)
105 def total_slices ():
106     return number_slices+system_slices()
107
108 # temporary - the myplc I use doesnt know about 'system' yet
109 def system_slivers ():
110 #    return len(system_slices_ids)
111     return 0
112 def total_slivers ():
113     return number_slices+system_slivers()
114
115 ####################
116 def peer_index(i):
117     return 3-i
118
119 def plc_name (i):
120     return plc[i]['plcname']
121
122 def site_name (i):
123     return 'site'+str(i)
124
125 def node_name (i,n):
126     return plc[i]['node-format']%n
127
128 def slice_name (i,n):
129     return plc[i]['plainname']+'_'+plc[i]['slice-format']%n
130
131 def person_name (i,n):
132     return plc[i]['person-format']%n
133
134 def key_name (i,n,k):
135     return plc[i]['key-format']%(n,k)
136
137 # to have indexes start at 1
138 def myrange (n):
139     return range (1,n+1,1)
140
141 def message (*args):
142     print "====================",
143     print args
144     
145 ##########
146 def timer_start ():
147     global epoch
148     epoch = time.time()
149     print '+++ timer start'
150
151 def timer_show ():
152     print '+++ %d seconds ellapsed'%(time.time()-epoch)
153
154 ####################
155 def test00_init (args=[1,2]):
156     global plc,s,a,aa
157     ## have you loaded this file already (support for reload)
158     if plc[1]:
159         pass
160     else:
161         plc=[None,plc1,plc2]
162         for i in args:
163             url=plc[i]['url-format']%plc[i]['hostname']
164             plc[i]['url']=url
165             s[i]=xmlrpclib.ServerProxy(url,allow_none=True)
166             print 'initializing s[%d]'%i,url
167             aa[i]={'Username':plc[i]['builtin-admin-id'],
168                    'AuthMethod':'password',
169                    'AuthString':plc[i]['builtin-admin-password'],
170                    'Role':'admin'}
171             print 'initialized aa[%d]'%i, aa[i]
172             a[i]={'Username':plc[i]['peer-admin-name'],
173                   'AuthMethod':'password',
174                   'AuthString':plc[i]['peer-admin-password'],
175                   'Role':'admin'}
176             print 'initialized a[%d]'%i, a[i]
177
178 def test00_print (args=[1,2]):
179     for i in args:
180         print 's[%d]'%i,s[i]
181         print 'aa[%d]'%i, aa[i]
182         print 'a[%d]'%i, a[i]
183
184 def check_nodes (el,ef,args=[1,2]):
185     for i in args:
186         # use a single request and sort afterwards for efficiency
187         # could have used GetNodes's scope as well
188         all_nodes = s[i].GetNodes(a[i])
189         n = len ([ x for x in all_nodes if x['peer_id'] is None])
190         f = len ([ x for x in all_nodes if x['peer_id'] is not None])
191         print '%02d: Checking nodes: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
192         assert n==el
193         assert f==ef
194
195 def check_keys (el,ef,args=[1,2]):
196     for i in args:
197         # use a single request and sort afterwards for efficiency
198         # could have used GetKeys's scope as well
199         all_keys = s[i].GetKeys(a[i])
200         n = len ([ x for x in all_keys if x['peer_id'] is None])
201         f = len ([ x for x in all_keys if x['peer_id'] is not None])
202         print '%02d: Checking keys: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
203         assert n==el
204         assert f==ef
205
206 def check_persons (el,ef,args=[1,2]):
207     for i in args:
208         # use a single request and sort afterwards for efficiency
209         # could have used GetPersons's scope as well
210         all_persons = s[i].GetPersons(a[i])
211         n = len ([ x for x in all_persons if x['peer_id'] is None])
212         f = len ([ x for x in all_persons if x['peer_id'] is not None])
213         print '%02d: Checking persons: got %d local (e=%d) & %d foreign (e=%d)'%(i,n,el,f,ef)
214         assert n==el
215         assert f==ef
216
217 # expected : local slices, foreign slices
218 def check_slices (els,efs,args=[1,2]):
219     for i in args:
220         ls=len(s[i].GetSlices(a[i],{'peer_id':None}))
221         fs=len(s[i].GetSlices(a[i],{'~peer_id':None}))
222         print '%02d: Checking slices: got %d local (e=%d) & %d foreign (e=%d)'%(i,ls,els,fs,efs)
223         assert els==ls
224         assert efs==fs
225
226 def show_nodes (i,node_ids):
227     # same as above
228     all_nodes = s[i].GetNodes(a[i],node_ids)
229     loc_nodes = filter (lambda n: n['peer_id'] is None, all_nodes)
230     for_nodes = filter (lambda n: n['peer_id'] is not None, all_nodes)
231
232     for message,nodes in [ ['LOC',loc_nodes], ['FOR',for_nodes] ] :
233         if nodes:
234             print '[%s:%d] : '%(message,len(nodes)),
235             for node in nodes:
236                 print node['hostname']+' ',
237             print ''
238
239 def check_slice_nodes (expected_nodes, is_local_slice, args=[1,2]):
240     for ns in myrange(number_slices):
241         check_slice_nodes_n (ns,expected_nodes, is_local_slice, args)
242
243 def check_slice_nodes_n (ns,expected_nodes, is_local_slice, args=[1,2]):
244     for i in args:
245         peer=peer_index(i)
246         if is_local_slice:
247             sname=slice_name(i,ns)
248             slice=s[i].GetSlices(a[i],{'name':[sname],'peer_id':None})[0]
249             message='local'
250         else:
251             sname=slice_name(peer,ns)
252             slice=s[i].GetSlices(a[i],{'name':[sname],'~peer_id':None})[0]
253             message='foreign'
254         print '%02d: %s slice %s (e=%d) '%(i,message,sname,expected_nodes),
255         slice_node_ids=slice['node_ids']
256         print 'on nodes ',slice_node_ids
257         show_nodes (i,slice_node_ids)
258         assert len(slice_node_ids)>=expected_nodes
259         if len(slice_node_ids) != expected_nodes:
260             print 'TEMPORARY'
261
262 # expected : nodes on local slice
263 def check_local_slice_nodes (expected, args=[1,2]):
264     check_slice_nodes(expected,True,args)
265
266 # expected : nodes on foreign slice
267 def check_foreign_slice_nodes (expected, args=[1,2]):
268     check_slice_nodes(expected,False,args)
269
270 def check_conf_files (args=[1,2]):
271     for nn in myrange(number_nodes):
272         check_conf_files_n (nn,args)
273
274 def check_conf_files_n (nn,args=[1,2]):
275     for i in args:
276         nodename=node_name(i,nn)
277         ndict= s[i].GetSlivers(a[i],[nodename])[0]
278         assert ndict['hostname'] == nodename
279         conf_files = ndict['conf_files']
280         print '%02d: %d conf_files in GetSlivers for node %s'%(i,len(conf_files),nodename)
281         for conf_file in conf_files:
282             print 'source=',conf_file['source'],'|',
283             print 'dest=',conf_file['dest'],'|',
284             print 'enabled=',conf_file['enabled'],'|',
285             print ''
286
287 import pprint
288 pp = pprint.PrettyPrinter(indent=3)
289
290 def check_slivers (esn,args=[1,2]):
291     for nn in myrange(number_nodes):
292         check_slivers_n (nn,esn,args)
293
294 # too verbose to check all nodes, let's check only the first one
295 def check_slivers_1 (esn,args=[1,2]):
296     check_slivers_n (1,esn,args)
297
298 def check_slivers_n (nn,esn,args=[1,2]):
299     for i in args:
300         nodename=node_name(i,nn)
301         ndict= s[i].GetSlivers(a[i],[nodename])[0]
302         assert ndict['hostname'] == nodename
303         slivers = ndict['slivers']
304         print '%02d: %d slivers (exp. %d) in GetSlivers for node %s'\
305               %(i,len(slivers),esn,nodename)
306         for sliver in slivers:
307             print '>>slivername = ',sliver['name']
308             pp.pprint(sliver)
309         assert len(slivers) == esn
310                 
311
312 ####################
313 def test00_admin_person (args=[1,2]):
314     global plc
315     for i in args:
316         email = plc[i]['peer-admin-name']
317         try:
318             p=s[i].GetPersons(a[i],[email])[0]
319             plc[i]['peer-admin-id']=p['person_id']
320         except:
321             person_id=s[i].AddPerson(aa[i],{'first_name':'Local', 
322                                             'last_name':'PeerPoint', 
323                                             'role_ids':[10],
324                                             'email':email,
325                                             'password':plc[i]['peer-admin-password']})
326             print '%02d:== created peer admin account %d, %s - %s'%(i,
327                                                                   person_id,plc[i]['peer-admin-name'],
328                                                                   plc[i]['peer-admin-password'])
329             plc[i]['peer-admin-id']=person_id
330
331 def test00_admin_enable (args=[1,2]):
332     for i in args:
333         s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer-admin-id'],True)
334         s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer-admin-id'])
335         print '%02d:== enabled+admin on account %d:%s'%(i,plc[i]['peer-admin-id'],plc[i]['peer-admin-name'])
336
337 def test01_peer_person (args=[1,2]):
338     global plc
339     for i in args:
340         peer=peer_index(i)
341         email=plc[peer]['peer-admin-name']
342         try:
343             p=s[i].GetPersons(a[i],[email])[0]
344             plc[i]['peer_person_id']=p['person_id']
345         except:
346             person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc_name(peer), 'role_ids':[3000],
347                                                'email':email,'password':plc[peer]['peer-admin-password']})
348             print '%02d:Created person %d as the peer person'%(i,person_id)
349             plc[i]['peer_person_id']=person_id
350
351 ####################
352 def test01_peer (args=[1,2]):
353     global plc
354     for i in args:
355         peer=peer_index(i)
356         peername = plc_name(peer)
357         try:
358             p=s[i].GetPeers (a[i], [peername])[0]
359             plc[i]['peer_id']=p['peer_id']
360         except:
361             peer_id=s[i].AddPeer (a[i], {'peername':peername,'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
362             # NOTE : need to manually reset the encrypted password through SQL at this point
363             print '%02d:Created peer %d'%(i,peer_id)
364             plc[i]['peer_id']=peer_id
365             print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
366
367 def test01_peer_passwd (args=[1,2]):
368     for i in args:
369         # using an ad-hoc local command for now - never could get quotes to reach sql....
370         print "Attempting to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i),
371         retcod=os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
372         print '-> system returns',retcod
373     
374 def test01_site (args=[1,2]):
375     global plc
376     for i in args:
377         peer=peer_index(i)
378         ### create a site (required for creating a slice)
379         sitename=site_name(i)
380         abbrev_name="abbr"+str(i)
381         login_base=plc[i]['plainname']
382         ### should be enough - needs to check we can add/del slices
383         max_slices = number_slices 
384         try:
385             s[i].GetSites(a[i],{'login_base':login_base})[0]
386         except:
387             site_id=s[i].AddSite (a[i], {'name':plc_name(i),
388                                          'abbreviated_name': abbrev_name,
389                                          'login_base': login_base,
390                                          'is_public': True,
391                                          'url': 'http://%s.com/'%abbrev_name,
392                                          'max_slices':max_slices})
393         ### max_slices does not seem taken into account at that stage
394             s[i].UpdateSite(a[i],site_id,{'max_slices':max_slices})
395             print '%02d:== Created site %d with max_slices=%d'%(i,site_id,max_slices)
396             plc[i]['site_id']=site_id
397
398 ##############################
399 # this one gets cached 
400 def get_peer_id (i):
401     try:
402         return plc[i]['peer_id']
403     except:
404         peername = plc_name (peer_index(i))
405         peer_id = s[i].GetPeers(a[i],[peername])[0]['peer_id']
406         plc[i]['peer_id'] = peer_id
407         return peer_id
408
409 def test01_refresh (message,args=[1,2]):
410     print '=== refresh',message
411     timer_show()
412     for i in args:
413         print '%02d:== Refreshing peer'%(i),
414         retcod=s[i].RefreshPeer(a[i],get_peer_id(i))
415         print 'got ',retcod
416         timer_show()
417
418 ####################
419 # retrieves node_id from hostname - checks for local nodes only
420 def get_local_node_id(i,nodename):
421     return s[i].GetNodes(a[i],[nodename],None,'local')[0]['node_id']
422
423 # clean all local nodes - foreign nodes are not supposed to be cleaned up manually
424 def clean_all_nodes (args=[1,2]):
425     for i in args:
426         print '%02d:== Cleaning all nodes'%i
427         loc_nodes = s[i].GetNodes(a[i],None,None,'local')
428         for node in loc_nodes:
429             print '%02d:==== Cleaning node %d'%(i,node['node_id'])
430             s[i].DeleteNode(a[i],node['node_id'])
431
432 def test02_node (args=[1,2]):
433     for nn in myrange(number_nodes):
434         test02_node_n (nn,args)
435
436 def test02_node_n (nn,args=[1,2]):
437     for i in args:
438         nodename = node_name(i,nn)
439         try:
440             get_local_node_id(i,nodename)
441         except:
442             n=s[i].AddNode(a[i],1,{'hostname': nodename})
443             print '%02d:== Added node %d %s'%(i,n,node_name(i,i))
444
445 def test02_delnode (args=[1,2]):
446     for nn in myrange(number_nodes):
447         test02_delnode_n (nn,args)
448
449 def test02_delnode_n (nn,args=[1,2]):
450     for i in args:
451         nodename = node_name(i,nn)
452         node_id = get_local_node_id (i,nodename)
453         retcod=s[i].DeleteNode(a[i],nodename)
454         print '%02d:== Deleted node %d, returns %s'%(i,node_id,retcod)
455
456 ####################
457 def test05_person (args=[1,2]):
458     for np in myrange(number_persons):
459         test05_person_n (np,True,args)
460
461 def test05_del_person (args=[1,2]):
462     for np in myrange(number_persons):
463         test05_person_n (np,False,args)
464
465 def test05_person_n (np,add_if_true,args=[1,2]):
466     test05_person_n_ks (np, myrange(number_keys),add_if_true,args)
467
468 def test05_person_n_ks (np,nks,add_if_true,args=[1,2]):
469     for i in args:
470         email = person_name(i,np)
471         try:
472             person_id=s[i].GetPersons(a[i],[email])[0]['person_id']
473             if not add_if_true:
474                 s[i].DeletePerson(a[i],person_id)
475                 print "%02d:== deleted person_id %d"%(i,person_id)
476         except:
477             if add_if_true:
478                 password = plc[i]['person-password']
479                 person_id=s[i].AddPerson(a[i],{'first_name':'Your average', 
480                                                'last_name':'User%d'%np, 
481                                                'role_ids':[30],
482                                                'email':email,
483                                                'password': password })
484                 print '%02d:== created user account %d, %s - %s'%(i, person_id,email,password)
485                 for nk in nks:
486                     key=key_name(i,np,nk)
487                     s[i].AddPersonKey(aa[i],email,{'key_type':'ssh', 'key':key})
488                     print '%02d:== added key %s to person %s'%(i,key,email)
489
490 ####################
491 def clean_all_slices (args=[1,2]):
492     for i in args:
493         print '%02d:== Cleaning all slices'%i
494         for slice in s[i].GetSlices(a[i],{'peer_id':None}):
495             slice_id = slice['slice_id']
496             if slice_id not in system_slices_ids:
497                 print '%02d:==== Cleaning slice %d'%(i,slice_id)
498                 s[i].DeleteSlice(a[i],slice_id)
499
500 def get_local_slice_id (i,name):
501     return s[i].GetSlices(a[i],{'name':[name],'peer_id':None})[0]['slice_id']
502
503 def test03_slice (args=[1,2]):
504     for n in myrange(number_slices):
505         test03_slice_n (n,args)
506
507 def test03_slice_n (ns,args=[1,2]):
508     for i in args:
509         peer=peer_index(i)
510         plcname=plc_name(i)
511         slicename=slice_name(i,ns)
512         max_nodes=number_nodes
513         try:
514             s[i].GetSlices(a[i],[slicename])[0]
515         except:
516             slice_id=s[i].AddSlice (a[i],{'name':slicename,
517                                           'description':'slice %s on %s'%(slicename,plcname),
518                                           'url':'http://planet-lab.org/%s'%slicename,
519                                           'max_nodes':max_nodes,
520                                           'instanciation':'plc-instantiated',
521                                           })
522             print '%02d:== created slice %d - max nodes=%d'%(i,slice_id,max_nodes)
523             for np in myrange(number_persons):
524                 email = person_name (i,np)
525                 retcod = s[i].AddPersonToSlice (a[i], email, slicename)
526                 print '%02d:== Attached person %s to slice %s'%(i,email,slicename)
527         
528
529 def test04_node_slice (is_local, add_if_true, args=[1,2]):
530     for ns in myrange(number_slices):
531         test04_node_slice_ns (ns,is_local, add_if_true, args)
532
533 def test04_node_slice_ns (ns,is_local, add_if_true, args=[1,2]):
534     test04_node_slice_nl_n (myrange(number_nodes),ns,is_local, add_if_true, args)
535
536 def test04_node_slice_nl_n (nnl,ns,is_local, add_if_true, args=[1,2]):
537     for i in args:
538         peer=peer_index(i)
539         slice_id = get_local_slice_id (i,slice_name (i,ns))
540         
541         if is_local:
542             hostnames=[node_name(i,nn) for nn in nnl]
543             nodetype='local'
544         else:
545             hostnames=[node_name(peer,nn) for nn in nnl]
546             nodetype='foreign'
547         if add_if_true:
548             s[i].AddSliceToNodes (a[i], slice_id,hostnames)
549             message="added"
550         else:
551             s[i].DeleteSliceFromNodes (a[i], slice_id,hostnames)
552             message="deleted"
553         print '%02d:== %s in slice %d %s '%(i,message,slice_id,nodetype),
554         print hostnames
555
556 def test04_slice_add_lnode (args=[1,2]):
557     test04_node_slice (True,True,args)
558
559 def test04_slice_add_fnode (args=[1,2]):
560     test04_node_slice (False,True,args)
561
562 def test04_slice_del_lnode (args=[1,2]):
563     test04_node_slice (True,False,args)
564
565 def test04_slice_del_fnode (args=[1,2]):
566     test04_node_slice (False,False,args)
567
568 ####################
569 def test_all_init ():
570     message ("INIT")
571     test00_init ()
572     test00_print ()
573     test00_admin_person ()
574     test00_admin_enable ()
575     test01_peer_person ()
576     test01_peer ()
577     test01_peer_passwd ()
578
579     test01_site ()
580
581 def test_all_nodes ():
582
583     message ("RESETTING NODES")
584     clean_all_nodes ()
585     test01_refresh ('cleaned nodes')
586     check_nodes(0,0)
587
588     # create one node on each site
589     message ("CREATING NODES")
590     test02_node ()
591     check_nodes(number_nodes,0)
592     test01_refresh ('after node creation')
593     check_nodes(number_nodes,number_nodes)
594     test02_delnode([2])
595     if not fast_flag:
596         message ("2 extra del/add cycles on plc2 for different indexes")
597         test02_node ([2])
598         test02_delnode([2])
599         test02_node ([2])
600         test02_delnode([2])
601     check_nodes(0,number_nodes,[2])
602     test01_refresh('after deletion on plc2')
603     check_nodes(number_nodes,0,[1])
604     check_nodes(0,number_nodes,[2])
605     message ("ADD on plc2 for different indexes")
606     test02_node ([2])
607     check_nodes (number_nodes,0,[1])
608     check_nodes (number_nodes,number_nodes,[2])
609     test01_refresh('after re-creation on plc2')
610     check_nodes (number_nodes,number_nodes,)
611
612 def test_all_addslices ():
613
614     # reset
615     message ("RESETTING SLICES TEST")
616     clean_all_nodes ()
617     test02_node ()
618     clean_all_slices ()
619     test01_refresh ("After slices init")
620
621     # create slices on plc1
622     message ("CREATING SLICES on plc1")
623     test03_slice ([1])
624     # each site has 3 local slices and 0 foreign slice
625     check_slices (total_slices(),0,[1])
626     check_slices (system_slices(),0,[2])
627     test01_refresh ("after slice created on plc1")
628     check_slices (total_slices(),0,[1])
629     check_slices (system_slices(),number_slices,[2])
630     # no slice has any node yet
631     check_local_slice_nodes(0,[1])
632     check_foreign_slice_nodes(0,[2])
633
634     # insert local nodes in local slice on plc1
635     message ("ADDING LOCAL NODES IN SLICES")
636     test04_slice_add_lnode ([1])
637     # of course the change is only local
638     check_local_slice_nodes (number_nodes,[1])
639     check_foreign_slice_nodes(0,[2])
640
641     # refreshing
642     test01_refresh ("After local nodes were added on plc1")
643     check_local_slice_nodes (number_nodes,[1])
644     check_foreign_slice_nodes (number_nodes,[2])
645
646     # now we add foreign nodes into local slice
647     message ("ADDING FOREIGN NODES IN SLICES")
648     test04_slice_add_fnode ([1])
649     check_local_slice_nodes (2*number_nodes,[1])
650     check_foreign_slice_nodes (number_nodes,[2])
651
652     # refreshing
653     test01_refresh ("After foreign nodes were added in plc1")
654     # remember that foreign slices only know about LOCAL nodes
655     # so this does not do anything
656     check_local_slice_nodes (2*number_nodes,[1])
657     check_foreign_slice_nodes (2*number_nodes,[2])
658
659     check_slivers_1(total_slivers())
660
661 def test_all_delslices ():
662
663     message ("DELETING FOREIGN NODES FROM SLICES")
664     test04_slice_del_fnode([1])
665     check_local_slice_nodes (number_nodes,[1])
666     check_foreign_slice_nodes (2*number_nodes,[2])
667     # mmh?
668     check_slivers_1(total_slivers(),[1])
669
670     test01_refresh ("After foreign nodes were removed on plc1")
671     check_local_slice_nodes (number_nodes,[1])
672     check_foreign_slice_nodes (number_nodes,[2])
673     
674     message ("DELETING LOCAL NODES FROM SLICES")
675     test04_slice_del_lnode([1])
676     check_local_slice_nodes (0,[1])
677     check_foreign_slice_nodes (number_nodes,[2])
678
679     test01_refresh ("After local nodes were removed on plc1")
680     check_local_slice_nodes (0,[1])
681     check_foreign_slice_nodes (0,[2])
682
683     message ("CHECKING SLICES CLEAN UP")
684     clean_all_slices([1])
685     check_slices (system_slices(),0,[1])
686     check_slices (system_slices(),number_slices,[2])
687     test01_refresh ("After slices clenaup")
688     check_slices(system_slices(),0)
689
690 def test_all_slices ():
691     test_all_addslices ()
692     test_all_delslices ()
693     
694 def test_all_persons ():
695     test05_del_person()
696     test01_refresh ('before persons&keys creation')
697     check_keys(0,0)
698     check_persons(system_persons,system_persons_cross)
699     message ("Creating persons&keys")
700     test05_person ()
701     if not fast_flag:
702         message ("1 extra del/add cycle for unique indexes")
703         test05_del_person([2])
704         test05_person([2])
705     check_keys(number_persons*number_keys,0)
706     check_persons(system_persons+number_persons,system_persons_cross)
707     test01_refresh ('after persons&keys creation')
708     check_keys(number_persons*number_keys,number_persons*number_keys)
709     check_persons(system_persons+number_persons,system_persons_cross+number_persons)
710
711 def test_all ():
712     test_all_init ()
713     timer_show()
714     test_all_persons ()
715     timer_show()
716     test_all_nodes ()
717     timer_show()
718     test_all_slices ()
719     timer_show()
720
721 ### ad hoc test sequences
722 def populate ():
723     test05_person()
724     test02_node()
725     test03_slice([1])
726     test01_refresh ("populate: refreshing peer 1",[1])
727     test04_slice_add_lnode([1])
728     test04_slice_add_fnode([1])
729     test01_refresh("populate: refresh all")
730
731 def test_now ():
732     test_all_init()
733     test_all_persons ()
734 #    clean_all_nodes()
735 #    clean_all_slices()
736 #    populate()
737
738 #####
739 def usage ():
740     print "Usage: %s [-n] [-f]"%sys.argv[0]
741     print " -f runs faster (1 node - 1 slice)"
742     print " -n runs test_now instead of test_all"
743     print " -p runs populate instead of test_all"
744     
745     sys.exit(1)
746
747 def main ():
748     try:
749         (o,a) = getopt.getopt(sys.argv[1:], "fnp")
750     except:
751         usage()
752     func = test_all
753     for (opt,val) in o:
754         if opt=='-f':
755             fast()
756         elif opt=='-n':
757             print 'Running test_now'
758             func = test_now
759         elif opt=='-p':
760             print 'Running populate'
761             func = populate
762         else:
763             usage()
764     if a:
765         usage()
766     print '%d nodes & %d slices'%(number_nodes,number_slices)
767     timer_start()
768     func()   
769  
770 if __name__ == '__main__':
771     main()
772