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