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