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