b1d83cd462d61e5bd6601b32e4340c58ad969bf6
[nepi.git] / test / util / plotter.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 from nepi.util.plotter import PFormats
24
25 import os
26 import tempfile
27 import time
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 PlotterTestCase(unittest.TestCase):
91     def test_serialize(self):
92         node_count = 4
93         app_count = 2
94
95         ec = ExperimentController(exp_id = "plotter-test")
96        
97         # Add simulated nodes and applications
98         nodes = list()
99         apps = list()
100         ifaces = list()
101
102         for i in xrange(node_count):
103             node = ec.register_resource("dummy::Node")
104             nodes.append(node)
105             
106             iface = ec.register_resource("dummy::Interface")
107             ec.register_connection(node, iface)
108             ifaces.append(iface)
109
110             for i in xrange(app_count):
111                 app = ec.register_resource("dummy::Application")
112                 ec.register_connection(node, app)
113                 apps.append(app)
114
115         link = ec.register_resource("dummy::Link")
116
117         for iface in ifaces:
118             ec.register_connection(link, iface)
119        
120         fpath = ec.plot()
121         statinfo = os.stat(fpath)
122         size = statinfo.st_size
123         self.assertTrue(size > 0)
124         self.assertTrue(fpath.endswith(".png"))
125
126         os.remove(fpath)
127
128         fpath = ec.plot(format = PFormats.DOT)
129         statinfo = os.stat(fpath)
130         size = statinfo.st_size
131         self.assertTrue(size > 0)
132         self.assertTrue(fpath.endswith(".dot"))
133
134         os.remove(fpath)
135
136 if __name__ == '__main__':
137     unittest.main()
138