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