3ee85c10149df516161edfcd7a43304c358c04ea
[nepi.git] / test / util / serializer.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: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.ec import ExperimentController
21 from nepi.execution.resource import ResourceManager, ResourceState, \
22         clsinit_copy, ResourceAction, ResourceFactory
23
24 import os
25 import tempfile
26 import time
27 import shutil
28 import unittest
29
30 reschedule_delay = "0.5s"
31 deploy_time = 0
32 run_time = 0
33
34 class Link(ResourceManager):
35     _rtype = "dummy::Link"
36     def do_deploy(self):
37         time.sleep(deploy_time)
38         super(Link, self).do_deploy()
39         self.logger.debug(" -------- DEPLOYED ------- ")
40
41 class Interface(ResourceManager):
42     _rtype = "dummy::Interface"
43
44     def do_deploy(self):
45         node = self.get_connected(Node.get_rtype())[0]
46         link = self.get_connected(Link.get_rtype())[0]
47
48         if node.state < ResourceState.READY or \
49                 link.state < ResourceState.READY:
50             self.ec.schedule(reschedule_delay, self.deploy)
51             self.logger.debug(" -------- RESCHEDULING ------- ")
52         else:
53             time.sleep(deploy_time)
54             super(Interface, self).do_deploy()
55             self.logger.debug(" -------- DEPLOYED ------- ")
56
57 class Node(ResourceManager):
58     _rtype = "dummy::Node"
59
60     def do_deploy(self):
61         self.logger.debug(" -------- DO_DEPLOY ------- ")
62         time.sleep(deploy_time)
63         super(Node, self).do_deploy()
64         self.logger.debug(" -------- DEPLOYED ------- ")
65
66 class Application(ResourceManager):
67     _rtype = "dummy::Application"
68
69     def do_deploy(self):
70         node = self.get_connected(Node.get_rtype())[0]
71
72         if node.state < ResourceState.READY: 
73             self.ec.schedule(reschedule_delay, self.deploy)
74             self.logger.debug(" -------- RESCHEDULING ------- ")
75         else:
76             time.sleep(deploy_time)
77             super(Application, self).do_deploy()
78             self.logger.debug(" -------- DEPLOYED ------- ")
79
80     def do_start(self):
81         super(Application, self).do_start()
82         time.sleep(run_time)
83         self.ec.schedule("0s", self.stop)
84
85 ResourceFactory.register_type(Application)
86 ResourceFactory.register_type(Node)
87 ResourceFactory.register_type(Interface)
88 ResourceFactory.register_type(Link)
89
90 class SerializerTestCase(unittest.TestCase):
91     def test_serialize(self):
92         node_count = 4
93         app_count = 2
94
95         dirpath = tempfile.mkdtemp()
96
97         ec = ExperimentController(exp_id = "serialize-test")
98        
99         # Add simulated nodes and applications
100         nodes = list()
101         apps = list()
102         ifaces = list()
103
104         for i in range(node_count):
105             node = ec.register_resource("dummy::Node")
106             nodes.append(node)
107             
108             iface = ec.register_resource("dummy::Interface")
109             ec.register_connection(node, iface)
110             ifaces.append(iface)
111
112             for i in range(app_count):
113                 app = ec.register_resource("dummy::Application")
114                 ec.register_connection(node, app)
115                 apps.append(app)
116
117         link = ec.register_resource("dummy::Link")
118
119         for iface in ifaces:
120             ec.register_connection(link, iface)
121
122         filepath = ec.save(dirpath)
123
124         ec.deploy()
125
126         # Wait until nodes and apps are deployed
127         ec.wait_finished(apps)
128
129         # Do the experiment controller shutdown
130         ec.shutdown()
131
132         # Load serialized experiment
133         ec2 = ExperimentController.load(filepath)
134         apps = ec2.filter_resources("dummy::Application")
135         ec2.deploy()
136         ec2.wait_finished(apps)
137         ec2.shutdown()
138         
139         self.assertEquals(len(ec.resources), len(ec2.resources))
140
141         shutil.rmtree(dirpath)
142                        
143 if __name__ == '__main__':
144     unittest.main()
145