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