e8a6ddcd0edf65206a60ec9ba05c5e7b0a620711
[nepi.git] / test / util / manifoldapi.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Lucia Guevgeozian <lucia.guevgeozian_odizzio@inria.fr>
20
21 # NOTE: Manifold API can not be tested yet with OMF nodes because there
22 # no OMF platforms connected currently.
23
24 from nepi.util.manifoldapi import MANIFOLDAPIFactory
25 from nepi.util.manifoldapi import MANIFOLDAPI 
26
27 import unittest
28 from multiprocessing import Process
29
30 class MFAPIFactoryTestCase(unittest.TestCase):
31     """
32     Test for the Manifold API Factory. Check that the same instance is used
33     when credentials match.
34     """
35
36     def test_factory(self):
37         """
38         Check that the same API instance is used when the credentials match.
39         """
40         username="lucia.guevgeozian_odizzio@inria.fr"
41         password="demo"
42         api1 = MANIFOLDAPIFactory.get_api(username, password)
43         api2 = MANIFOLDAPIFactory.get_api(username, password)
44
45         self.assertIsInstance(api1, MANIFOLDAPI)
46         self.assertIsInstance(api2, MANIFOLDAPI)
47         self.assertEquals(api1, api2)
48         
49
50 class MANIFOLDAPITestCase(unittest.TestCase):
51
52     def setUp(self):
53         self.username="lucia.guevgeozian_odizzio@inria.fr"
54         self.password="demo"
55         self.slicename = "ple.inria.sfatest"
56         self.api = MANIFOLDAPIFactory.get_api(self.username, self.password)
57
58     def test_get_resource_info(self):
59         """
60         Check that the API retrieves the right set of info acoridng to the
61         filters and fields defined.
62         """
63
64         filters = dict()
65         filters['hostname'] = 'planetlab2.tlm.unavarra.es'
66
67         r_info = self.api.get_resource_info(filters=filters)
68         hostname = r_info[0]['hostname']
69         self.assertEquals(hostname, 'planetlab2.tlm.unavarra.es')
70
71         # query with 2 filters
72         filters['network'] = 'ple'
73                 
74         r_info = self.api.get_resource_info(filters=filters)
75         hostname = r_info[0]['hostname']
76         self.assertEquals(hostname, 'planetlab2.tlm.unavarra.es')
77
78         # query with fields only, without filters
79         fields = ['latitude','longitude']
80
81         r_info = self.api.get_resource_info(fields=fields)
82         value = r_info[10]
83         self.assertEquals(value.keys(), fields)
84
85         # query with 2 filters and 2 fields
86         r_info = self.api.get_resource_info(filters, fields)
87         value = r_info[0]
88         result = {'latitude': '42.7993', 'longitude': '-1.63544'}
89         self.assertEquals(value, result)
90
91         # query with filters where the AND should be zero resources
92         filters['network'] = 'omf'
93         
94         r_info = self.api.get_resource_info(filters, fields)
95         self.assertEquals(r_info, [])
96
97     def test_fail_if_invalid_field(self):
98         """
99         Check that the invalid field is not used for the query
100         """
101         filters = dict()
102         filters['network'] = 'omf'
103         filters['component_id'] = 'urn:publicid:IDN+omf:nitos+node+node001'
104         fields = ['arch']
105
106         r_info = self.api.get_resource_info(filters, fields)
107         value = r_info[0]
108         self.assertNotEqual(len(value), 1)
109     
110     def test_get_slice_resources(self):
111         """
112         Test that the API retrives the resources in the user's slice.
113         The slice used is ple.inria.sfatest, change slice and nodes in order 
114         for the test not to fail.
115         """
116         resources = self.api.get_slice_resources(self.slicename)
117         
118         result = ['urn:publicid:IDN+ple:lilleple+node+node2pl.planet-lab.telecom-lille1.eu', 
119         'urn:publicid:IDN+ple:uttple+node+planetlab2.utt.fr', 
120         'urn:publicid:IDN+ple:lilleple+node+node1pl.planet-lab.telecom-lille1.eu']
121        
122         self.assertEquals(resources, result)
123     
124     def test_update_resources_from_slice(self):
125         """
126         Test that the nodes are correctly added to the user's slice.
127         Test that the nodes are correctly removed from the user's slice.
128         """
129         new_resource = 'urn:publicid:IDN+ple:unavarraple+node+planetlab2.tlm.unavarra.es'
130         self.api.add_resource_to_slice(self.slicename, new_resource)
131
132         resources = self.api.get_slice_resources(self.slicename)
133         self.assertIn(new_resource, resources)
134
135         resource_to_remove = 'urn:publicid:IDN+ple:unavarraple+node+planetlab2.tlm.unavarra.es'
136         self.api.remove_resource_from_slice(self.slicename, resource_to_remove)
137
138         resources = self.api.get_slice_resources(self.slicename)
139         self.assertNotIn(resource_to_remove, resources)
140
141     def test_concurrence(self):
142         resources = ['urn:publicid:IDN+ple:itaveirople+node+planet2.servers.ua.pt',
143             'urn:publicid:IDN+ple:quantavisple+node+marie.iet.unipi.it',
144             'urn:publicid:IDN+ple:elteple+node+planet1.elte.hu',
145             'urn:publicid:IDN+ple:inria+node+wlab39.pl.sophia.inria.fr',
146             'urn:publicid:IDN+ple:poznanple+node+planetlab-2.man.poznan.pl',
147             'urn:publicid:IDN+ple:tmsp+node+planetlab-node3.it-sudparis.eu',
148             'urn:publicid:IDN+ple:colbudple+node+evghu6.colbud.hu',
149             'urn:publicid:IDN+ple:erlangenple+node+planetlab1.informatik.uni-erlangen.de',
150             'urn:publicid:IDN+ple:kitple+node+iraplab1.iralab.uni-karlsruhe.de',
151             'urn:publicid:IDN+ple:polimiple+node+planetlab1.elet.polimi.it',
152             'urn:publicid:IDN+ple:colbudple+node+evghu12.colbud.hu',
153             'urn:publicid:IDN+ple:ccsr+node+pl2.ccsrfi.net',
154             'urn:publicid:IDN+ple:modenaple+node+planetlab-1.ing.unimo.it',
155             'urn:publicid:IDN+ple:cambridgeple+node+planetlab1.xeno.cl.cam.ac.uk',
156             'urn:publicid:IDN+ple:lig+node+planetlab-1.imag.fr',
157             'urn:publicid:IDN+ple:polslple+node+plab3.ple.silweb.pl',
158             'urn:publicid:IDN+ple:uc3mple+node+planetlab1.uc3m.es',
159             'urn:publicid:IDN+ple:colbudple+node+evghu4.colbud.hu',
160             'urn:publicid:IDN+ple:hiitple+node+planetlab3.hiit.fi',
161             'urn:publicid:IDN+ple:l3sple+node+planet1.l3s.uni-hannover.de',
162             'urn:publicid:IDN+ple:colbudple+node+evghu5.colbud.hu',
163             'urn:publicid:IDN+ple:dbislab+node+planetlab2.ionio.gr',
164             'urn:publicid:IDN+ple:forthple+node+planetlab2.ics.forth.gr',
165             'urn:publicid:IDN+ple:netmodeple+node+vicky.planetlab.ntua.gr',
166             'urn:publicid:IDN+ple:colbudple+node+evghu1.colbud.hu',
167             'urn:publicid:IDN+ple:cyprusple+node+planetlab-3.cs.ucy.ac.cy',
168             'urn:publicid:IDN+ple:darmstadtple+node+host1.planetlab.informatik.tu-darmstadt.de',
169             'urn:publicid:IDN+ple:cnetple+node+plab2.create-net.org',
170             'urn:publicid:IDN+ple:unictple+node+gschembra3.diit.unict.it',
171             'urn:publicid:IDN+ple:sevillaple+node+ait05.us.es']
172
173         def add_resource(new_resource):
174             self.api.add_resource_to_slice(self.slicename, new_resource)
175
176         def runInParallel(fns):
177             for fn in fns:
178                 p = Process(target=fn)
179                 p.start()
180                 p.join()
181
182         funcs = list()
183         for r in resources:
184             funcs.append(add_resource(r))  
185
186         runInParallel(funcs)
187
188         slice_res = self.api.get_slice_resources(self.slicename)
189         for r in resources:
190             self.assertIn(r, slice_res)
191
192         for r in resources:
193             self.api.remove_resource_from_slice(self.slicename, r)
194         
195 if __name__ == '__main__':
196     unittest.main()
197
198
199