- RefreshPeer & AddSliceToNodes had bugs
[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 import xmlrpclib
18
19 plc1={ 'name':'plc1 in federation',
20        'url':'https://lurch.cs.princeton.edu:443/',
21        'builtin_admin_id':'root@localhost.localdomain',
22        'builtin_admin_password':'root',
23        'peer_admin_name':'plc1@planet-lab.org',
24        'peer_admin_password':'peer',
25        'nodename':'n11.plc1.org'
26        }
27 plc2={ 'name':'plc2 in federation',
28        'url':'https://planetlab-devbox.inria.fr:443/',
29        'builtin_admin_id':'root@localhost.localdomain',
30        'builtin_admin_password':'root',
31        'peer_admin_name':'plc2@planet-lab.org',
32        'peer_admin_password':'peer',
33        'nodename':'n21.plc2.org'
34        }
35
36 ## we use indexes 1 and 2 
37 plc=[None,None,None]
38 # the server objects
39 s=[None,None,None]
40 # the authentication objects
41 a=[None,None,None]
42 aa=[None,None,None]
43
44 def peer_index(i):
45     return 3-i
46
47 ### cant use digits in slice login name
48 def plain_name (i):
49     if i==1:
50         return 'one'
51     elif i==2:
52         return 'two'
53     else:
54         raise Exception,"Unexpected input in plain_name"
55
56 def test00_init (args=[1,2]):
57     global plc,s,a,aa
58     ## have you loaded this file already (support for reload)
59     if plc[1]:
60         pass
61     else:
62         plc=[None,plc1,plc2]
63         for i in args:
64             url=plc[i]['url']+'/PLCAPI/'
65             s[i]=xmlrpclib.Server(url)
66             print 'initializing s[%d]'%i,url
67             aa[i]={'Username':plc[i]['builtin_admin_id'],
68                    'AuthMethod':'password',
69                    'AuthString':plc[i]['builtin_admin_password'],
70                    'Role':'admin'}
71             print 'initialized aa[%d]'%i, aa[i]
72             a[i]={'Username':plc[i]['peer_admin_name'],
73                   'AuthMethod':'password',
74                   'AuthString':plc[i]['peer_admin_password'],
75                   'Role':'admin'}
76             print 'initialized a[%d]'%i, a[i]
77
78 def test00_print (args=[1,2]):
79     global plc,s,a,aa
80     for i in args:
81         print 's[%d]'%i,s[i]
82         print 'aa[%d]'%i, aa[i]
83         print 'a[%d]'%i, a[i]
84
85 def test00_admin (args=[1,2]):
86     global plc,s,a
87     for i in args:
88         peer=peer_index(i)
89         person_id=s[i].AddPerson(aa[i],{'first_name':'Local', 'last_name':'PeerPoint', 'role_ids':[10],
90                                               'email':plc[i]['peer_admin_name'],'password':plc[i]['peer_admin_password']})
91         print '%02d: created peer admin account %d, %s - %s'%(i,person_id,plc[i]['peer_admin_name'],plc[i]['peer_admin_password'])
92         plc[i]['peer_admin_id']=person_id
93
94 def test00_enable (args=[1,2]):
95     global plc,s,a
96     for i in args:
97         peer=peer_index(i)
98         s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer_admin_id'],True)
99         s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer_admin_id'])
100         print '%02d: enabled+admin on account %d:%s'%(i,plc[i]['peer_admin_id'],plc[i]['peer_admin_name'])
101
102 def test01_check (args=[1,2]):
103     global plc,s,a
104     for i in args:
105         n=len(s[i].GetNodes(aa[i]))
106         f=len(s[i].GetForeignNodes(a[i]))
107         print '%02d: Checking connection: got %d local nodes & %d foreign nodes'%(i,n,f)
108
109 def test01_node (args=[1,2]):
110     global plc,s,a
111     for i in args:
112         n=s[i].AddNode(a[i],1,{'hostname': plc[i]['nodename']})
113         print '%02d: Added node %d %s'%(i,n,plc[i]['nodename'])
114
115 def test01_peer_person (args=[1,2]):
116     global plc,s,a
117     for i in args:
118         peer=peer_index(i)
119         person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc[peer]['name'], 'role_ids':[3000],
120                                            'email':plc[peer]['peer_admin_name'],'password':plc[peer]['peer_admin_password']})
121         print '02%d:Created person %d as the peer person'%(i,person_id)
122         plc[i]['peer_person_id']=person_id
123
124 def test01_peer (args=[1,2]):
125     global plc,s,a
126     for i in args:
127         peer=peer_index(i)
128         peer_id=s[i].AddPeer (a[i], {'peername':plc[peer]['name'],'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
129         # NOTE : need to manually reset the encrypted password through SQL at this point
130         print '%02d:Created peer %d'%(i,peer_id)
131         plc[i]['peer_id']=peer_id
132         print "Please MANUALLY set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
133     
134 def test02_refresh (args=[1,2]):
135     global plc,s,a
136     for i in args:
137         print '%02d: Refreshing peer'%(i)
138         s[i].RefreshPeer(a[i],plc[i]['peer_id'])
139
140 def test03_site (args=[1,2]):
141     global plc,s,a
142     for i in args:
143         peer=peer_index(i)
144         ### create a site (required for creating a slice)
145         sitename="site"+str(i)
146         abbrev_name="abbr"+str(i)
147         plain=plain_name(i)
148         site_id=s[i].AddSite (a[i], {'name':plc[i]['name'],
149                                      'abbreviated_name': abbrev_name,
150                                      'login_base': plain,
151                                      'is_public': True,
152                                      'url': 'http://%s.com/'%abbrev_name,
153                                      'max_slices':10})
154         ### max_slices does not seem taken into account at that stage
155         s[i].UpdateSite(a[i],plc[i]['site_id'],{'max_slices':10})
156         print '%02d: Created site %d with max_slices=10'%(i,site_id)
157         plc[i]['site_id']=site_id
158
159 def test03_slice (args=[1,2]):
160     global plc,s,a
161     for i in args:
162         peer=peer_index(i)
163         plain=plain_name(i)
164         ### create a slice
165         slice_name="slic"+str(i)
166         slice_id=s[i].AddSlice (a[i],{'name':'%s_%s'%(plain,slice_name),
167                                       'description':'slice %s_%s on plc %s'%(plain,slice_name,plc[i]['name']),
168                                       'url':'http://planet-lab.org/%s'%slice_name,
169                                       'max_nodes':100,
170                                       'instanciation':'plc-instantiated',
171                                       })
172         print '%02d: created slice %d'%(i,slice_id)
173         plc[i]['slice_id']=slice_id
174         
175
176 def test04_lnode (args=[1,2]):
177     global plc,s,a
178     for i in args:
179         ### add node to it
180         hostname=plc[i]['nodename']
181         s[i].AddSliceToNodes (a[i], plc[i]['slice_id'],hostname)
182         print '%02d: added local node %s'%(i,hostname)
183
184 def test04_fnode (args=[1,2]):
185     global plc,s,a
186     for i in args:
187         peer=peer_index(i)
188         ### add node to it
189         hostname=plc[peer]['nodename']
190         s[i].AddSliceToNodes (a[i], plc[i]['slice_id'],hostname)
191         print '%02d: added local node %s'%(i,hostname)
192
193 def catch_up (args=[1,2]):
194     for i in args:
195         plc[i]['peer_admin_id']=3
196         plc[i]['peer_person_id']=4
197         plc[i]['peer_id']=1
198         plc[i]['slice_id']=1
199