replacing assertEquals into assertEqual
[nepi.git] / test / execution / runner.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.execution.runner import ExperimentRunner
24
25 import functools
26 import os
27 import shutil
28 import tempfile
29 import time
30 import unittest
31
32 reschedule_delay = "0.5s"
33 deploy_time = 0
34 run_time = 0
35
36 class Link(ResourceManager):
37     _rtype = "dummy::Link"
38     def do_deploy(self):
39         time.sleep(deploy_time)
40         super(Link, self).do_deploy()
41         self.logger.debug(" -------- DEPLOYED ------- ")
42
43 class Interface(ResourceManager):
44     _rtype = "dummy::Interface"
45
46     def do_deploy(self):
47         node = self.get_connected(Node.get_rtype())[0]
48         link = self.get_connected(Link.get_rtype())[0]
49
50         if node.state < ResourceState.READY or \
51                 link.state < ResourceState.READY:
52             self.ec.schedule(reschedule_delay, self.deploy)
53             self.logger.debug(" -------- RESCHEDULING ------- ")
54         else:
55             time.sleep(deploy_time)
56             super(Interface, self).do_deploy()
57             self.logger.debug(" -------- DEPLOYED ------- ")
58
59 class Node(ResourceManager):
60     _rtype = "dummy::Node"
61
62     def do_deploy(self):
63         self.logger.debug(" -------- DO_DEPLOY ------- ")
64         time.sleep(deploy_time)
65         super(Node, self).do_deploy()
66         self.logger.debug(" -------- DEPLOYED ------- ")
67
68 class Application(ResourceManager):
69     _rtype = "dummy::Application"
70
71     def do_deploy(self):
72         node = self.get_connected(Node.get_rtype())[0]
73
74         if node.state < ResourceState.READY: 
75             self.ec.schedule(reschedule_delay, self.deploy)
76             self.logger.debug(" -------- RESCHEDULING ------- ")
77         else:
78             time.sleep(deploy_time)
79             super(Application, self).do_deploy()
80             self.logger.debug(" -------- DEPLOYED ------- ")
81
82     def do_start(self):
83         super(Application, self).do_start()
84         time.sleep(run_time)
85         self.ec.schedule("0s", self.stop)
86
87 ResourceFactory.register_type(Application)
88 ResourceFactory.register_type(Node)
89 ResourceFactory.register_type(Interface)
90 ResourceFactory.register_type(Link)
91
92 class RunnerTestCase(unittest.TestCase):
93     def test_runner_max_runs(self):
94         node_count = 4
95         app_count = 2
96
97         ec = ExperimentController(exp_id = "max-runs-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         rnr = ExperimentRunner()
123         runs = rnr.run(ec, min_runs = 5, max_runs = 10, wait_guids = apps, 
124                 wait_time = 0)
125
126         self.assertEqual(runs, 10)
127
128     def test_runner_convergence(self):
129         node_count = 4
130         app_count = 2
131
132         ec = ExperimentController(exp_id = "convergence-test")
133        
134         # Add simulated nodes and applications
135         nodes = list()
136         apps = list()
137         ifaces = list()
138
139         for i in range(node_count):
140             node = ec.register_resource("dummy::Node")
141             nodes.append(node)
142             
143             iface = ec.register_resource("dummy::Interface")
144             ec.register_connection(node, iface)
145             ifaces.append(iface)
146
147             for i in range(app_count):
148                 app = ec.register_resource("dummy::Application")
149                 ec.register_connection(node, app)
150                 apps.append(app)
151
152         link = ec.register_resource("dummy::Link")
153
154         for iface in ifaces:
155             ec.register_connection(link, iface)
156
157         samples = [10, 10, 10, 10, 12, 10, 12, 10, 10, 11]
158         
159         def compute_metric_callback(samples, ec, run):
160             return samples[run-1]
161
162         metric_callback = functools.partial(compute_metric_callback, samples)
163
164         rnr = ExperimentRunner()
165         runs = rnr.run(ec, min_runs = 5, 
166                 compute_metric_callback = metric_callback,
167                 wait_guids = apps, 
168                 wait_time = 0)
169
170         self.assertEqual(runs, 10)
171                        
172 if __name__ == '__main__':
173     unittest.main()
174